def testEnum(self): try: clrs = Enum('red', 'green', 'blue') self.assertTrue(clrs.red in clrs) self.assertTrue("red" in clrs) self.assertTrue(clrs.green in clrs) self.assertTrue("green" in clrs) self.assertTrue(clrs.blue in clrs) self.assertTrue("blue" in clrs) self.assertTrue("yellow" not in clrs) self.assertEqual(clrs.red.index, 0) self.assertEqual(clrs.green.index, 1) self.assertEqual(clrs.blue.index, 2) self.assertEqual(clrs[0], clrs.red) self.assertEqual(clrs['red'], clrs.red) self.assertEqual(clrs[1], clrs.green) self.assertEqual(clrs['green'], clrs.green) self.assertEqual(clrs[2], clrs.blue) self.assertEqual(clrs['blue'], clrs.blue) self.assertEqual(str(clrs.red), "red") self.assertEqual(str(clrs[0]), "red") self.assertEqual(str(clrs['red']), "red") self.assertEqual(str(clrs.green), "green") self.assertEqual(str(clrs[1]), "green") self.assertEqual(str(clrs['green']), "green") self.assertEqual(str(clrs.blue), "blue") self.assertEqual(str(clrs[2]), "blue") self.assertEqual(str(clrs['blue']), "blue") self.assertTrue(clrs.green > clrs.red) self.assertTrue(clrs.blue > clrs.green) green_cpy = deepcopy(clrs.green) self.assertTrue(clrs.green in clrs) self.assertTrue("green" in clrs) self.assertEqual(clrs.green.index, 1) self.assertEqual(str(clrs.green), "green") self.assertEqual(green_cpy, clrs.green) same_clrs = Enum('red', 'green', 'blue') self.assertEqual(same_clrs, clrs) self.assertEqual(same_clrs.red, clrs.red) self.assertEqual(same_clrs.green, clrs.green) self.assertEqual(same_clrs.blue, clrs.blue) self.assertTrue(same_clrs.green > clrs.red) self.assertTrue(same_clrs.blue > clrs.green) another_clrs = Enum('red', 'blue', 'green') self.assertNotEqual(another_clrs, clrs) self.assertNotEqual(another_clrs.red, clrs.red) self.assertNotEqual(another_clrs.green, clrs.green) self.assertNotEqual(another_clrs.blue, clrs.blue) except BaseException as e: print(("Error. %s. %s. %s" % (type(e), str(e), util.formatTb()))) self.assertTrue(False)
def testGoodConfig2(self): try: conf = ccpydconfparser.parse("ccpyd.conf.good.2") self.assertEqual(len(conf), 2) self.assertEqual(conf['ccpyConfig'], '/etc/ccpy.conf') self.assertEqual(conf['logging'], False) except BaseException as e: print(("Error. %s. %s. %s" % (type(e), str(e), util.formatTb()))) self.assertTrue(False)
def testPidExist(self): try: import os self.assertTrue(util.isPidExist(os.getpid())) self.assertTrue(util.isPidExist(1)) self.assertTrue(not util.isPidExist(-123)) except BaseException as e: print(("Error. %s. %s. %s" % (type(e), str(e), util.formatTb()))) self.assertTrue(False)
def main(argv): if sys.version_info[0] < 2 or (sys.version_info[0] == 2 and sys.version_info[1] < 5): sys.stderr.write("Python 2.5 or higher is required for the program to run.") return -1 try: if run_in_fg(argv): print("Starting in foreground") else: print("Starting in background") util.daemonize() myCcPydConf = ccpydconfparser.parse() if not myCcPydConf['logging']: myCcPydConf['logFile'] = '/dev/null' Logger = util.initLogger( common.LoggerName, myCcPydConf['logFile'], common.ProductName + ' v.' + common.ProductVersion, myCcPydConf['logLevel']) except Exception as e: sys.stderr.write("%s. %s. %s" % (type(e), str(e), util.formatTb())) return -1 try: mySysSingleton = util.SysSingleton(common.DaemonName) myCcPyConf = myCcPydConf['ccpyConfig'] execTasks(myCcPyConf) util.closeLogger(Logger) return 0 except BaseException as e: Logger.error("%s: %s. %s" % (type(e), str(e), util.formatTb())) util.closeLogger(Logger) return -1 except: Logger.error("Unexpected error") util.closeLogger(Logger) return -2 Logger.error("We should never get here.") return -3
def testGoodConfig1(self): try: conf = ccpydconfparser.parse("ccpyd.conf.good.1") self.assertEqual(len(conf), 4) self.assertEqual(conf['ccpyConfig'], '/etc/ccpy.conf.1') self.assertEqual(conf['logging'], True) self.assertEqual(conf['logFile'], '/var/log/ccpyd.log') self.assertEqual(conf['logLevel'], 'DEBUG') except BaseException as e: print(("Error. %s. %s. %s" % (type(e), str(e), util.formatTb()))) self.assertTrue(False)
def testSysSingleton(self): try: mySingleton1 = util.SysSingleton('testapp1') mySingleton2 = util.SysSingleton('testapp2') self.assertRaises(util.SysSingletonCreateError, util.SysSingleton, 'testapp1', False) mySingleton1 = util.SysSingleton('testapp1') mySingleton1 = util.SysSingleton('testapp1', True) self.assertRaises(util.SysSingletonCreateError, util.SysSingleton, 'testapp2', False) mySingleton2 = util.SysSingleton('testapp2') mySingleton2 = util.SysSingleton('testapp2', True) except BaseException as e: print(("Error. %s. %s. %s" % (type(e), str(e), util.formatTb()))) self.assertTrue(False)
def testCcPyStateGetSet(self): try: myCcPyState = CcPyState('ccpy.state.tmp') self.assertEqual(myCcPyState.getPrjState('Product V2'), PrjStates.OK) self.assertEqual(myCcPyState.getPrjState('Product V3'), PrjStates.UNKNOWN) myCcPyState.setPrjState('Product V3', PrjStates.FAILED) self.assertEqual(myCcPyState.getPrjState('Product V2'), PrjStates.OK) self.assertEqual(myCcPyState.getPrjState('Product V3'), PrjStates.FAILED) myCcPyState.setPrjState('Product V2', PrjStates.FAILED) myCcPyState.setPrjState('Product V3', PrjStates.OK) self.assertEqual(myCcPyState.getPrjState('Product V2'), PrjStates.FAILED) self.assertEqual(myCcPyState.getPrjState('Product V3'), PrjStates.OK) os.remove('ccpy.state.tmp') myCcPyState = CcPyState('ccpy.state.tmp') self.assertEqual(myCcPyState.getPrjState('Product V2'), PrjStates.UNKNOWN) self.assertEqual(myCcPyState.getPrjState('Product V3'), PrjStates.UNKNOWN) myCcPyState.setPrjState('Product V2', PrjStates.OK) self.assertEqual(myCcPyState.getPrjState('Product V2'), PrjStates.OK) self.assertEqual(myCcPyState.getPrjState('Product V3'), PrjStates.UNKNOWN) except BaseException as e: print(("Error. %s. %s. %s" % (type(e), str(e), util.formatTb()))) self.assertTrue(False)
def testGoodConfig1Contents(self): try: myProjects = ccpyconfparser.parse("ccpy.conf.good.1") self.assertEqual(len(myProjects), 5) # Product2 project myProjName = 'Product2' myTasks = myProjects[myProjName]['tasks'] self.assertEqual(len(myTasks), 6) self.assertEqual( len([task for task in myTasks if isinstance(task, svntask.SvnTask)]), 1) self.assertEqual( len([task for task in myTasks if isinstance(task, gittask.GitTask)]), 1) self.assertEqual( len([task for task in myTasks if isinstance(task, maketask.MakeTask)]), 2) self.assertEqual( len([task for task in myTasks if isinstance(task, exectask.ExecTask)]), 2) self.assertEqual(myProjects[myProjName]['emailFrom'], '*****@*****.**') self.assertEqual( myProjects[myProjName]['emailTo'], [ '*****@*****.**', '*****@*****.**']) self.assertEqual(myProjects[myProjName]['emailFormat'], util.EmailFormat.attachment) self.assertEqual(myProjects[myProjName]['emailServerHost'], 'localhost') self.assertEqual(myProjects[myProjName]['emailServerPort'], 25) self.assertEqual(myProjects[myProjName]['emailServerUsername'], None) self.assertEqual(myProjects[myProjName]['emailServerPassword'], None) self.assertEqual(myProjects[myProjName]['failOnError'], True) myTask = myTasks[0] self.assertTrue(isinstance(myTask, svntask.SvnTask)) self.assertEqual(myTask.url, "https://company.com/repos/product2/mk") self.assertEqual(myTask.workingDir, "/ProductBuilds/mk") self.assertTrue(myTask.preCleanWorkingDir) myTask = myTasks[1] self.assertTrue(isinstance(myTask, maketask.MakeTask)) self.assertEqual(myTask.workingDir, "/ProductBuilds/SysInfra/Projects/common") self.assertEqual(myTask.args, "clean release") self.assertEqual(myTask.timeout, 120) myTask = myTasks[2] self.assertTrue(isinstance(myTask, maketask.MakeTask)) self.assertEqual(myTask.workingDir, "/ProductBuilds/SysInfra/Projects/logging") self.assertEqual(myTask.args, "") self.assertEqual(myTask.timeout, 600) myTask = myTasks[3] self.assertTrue(isinstance(myTask, exectask.ExecTask)) self.assertEqual(myTask.executable, "commontests") self.assertEqual(myTask.args, "--xmlout") self.assertEqual(myTask.workingDir, "/ProductBuilds/SysInfra/TestProjects/commontests") self.assertEqual(myTask.timeout, 30) self.assertEqual(myTask.warningExitCode, 2) myTask = myTasks[4] self.assertTrue(isinstance(myTask, exectask.ExecTask)) self.assertEqual(myTask.executable, "loggingTests") self.assertEqual(myTask.args, "") self.assertEqual(myTask.workingDir, "") self.assertEqual(myTask.timeout, 600) myTask = myTasks[5] self.assertTrue(isinstance(myTask, gittask.GitTask)) self.assertEqual(myTask.url, "https://company.com/repos/product2/Common") self.assertEqual(myTask.workingDir, "/ProductBuilds/Common") self.assertFalse(myTask.preCleanWorkingDir) # Product3 project myProjName = "Product3" myTasks = myProjects[myProjName]['tasks'] self.assertEqual(len(myTasks), 1) self.assertEqual( len([task for task in myTasks if isinstance(task, svntask.SvnTask)]), 1) self.assertEqual(myProjects[myProjName]['emailFrom'], '*****@*****.**') self.assertEqual(myProjects[myProjName]['emailTo'], ['*****@*****.**']) self.assertEqual(myProjects[myProjName]['emailFormat'], util.EmailFormat.attachment) self.assertEqual(myProjects[myProjName]['emailAttachments'], []) self.assertEqual(myProjects[myProjName]['failOnError'], False) myTask = myTasks[0] self.assertTrue(isinstance(myTask, svntask.SvnTask)) self.assertEqual(myTask.url, "https://company.com/repos/product3/server") self.assertEqual(myTask.workingDir, "/ProductBuilds/server") self.assertFalse(myTask.preCleanWorkingDir) # Product4 project myProjName = "Product4" myTasks = myProjects[myProjName]['tasks'] self.assertEqual(len(myTasks), 1) self.assertEqual( len([task for task in myTasks if isinstance(task, maketask.MakeTask)]), 1) self.assertEqual(myProjects[myProjName]['failOnError'], True) self.assertEqual(myProjects[myProjName]['emailFrom'], '') self.assertEqual(myProjects[myProjName]['emailTo'], []) myTask = myTasks[0] self.assertTrue(isinstance(myTask, maketask.MakeTask)) self.assertEqual(myTask.workingDir, "/ProductBuilds/SysInfra/Projects/common") self.assertEqual(myTask.args, "") self.assertEqual(myTask.timeout, 600) # Product5 project myProjName = "Product5" self.assertEqual( myProjects[myProjName]['emailFrom'], '*****@*****.**') self.assertEqual( myProjects[myProjName]['emailTo'], [ '*****@*****.**', '*****@*****.**']) self.assertEqual(myProjects[myProjName]['emailFormat'], util.EmailFormat.plain) self.assertEqual(myProjects[myProjName]['emailServerHost'], 'localhost') self.assertEqual(myProjects[myProjName]['emailServerPort'], 25) self.assertEqual(myProjects[myProjName]['emailServerUsername'], None) self.assertEqual(myProjects[myProjName]['emailServerPassword'], None) self.assertEqual(myProjects[myProjName]['emailAttachments'], []) # Product6 project myProjName = "Product6" self.assertEqual( myProjects[myProjName]['emailFrom'], '*****@*****.**') self.assertEqual(myProjects[myProjName]['emailTo'], ['*****@*****.**']) self.assertEqual(myProjects[myProjName]['emailFormat'], util.EmailFormat.html) self.assertEqual(myProjects[myProjName]['emailServerHost'], 'smtp.mymail.com') self.assertEqual(myProjects[myProjName]['emailServerPort'], 2626) self.assertEqual(myProjects[myProjName]['emailServerUsername'], 'jos') self.assertEqual(myProjects[myProjName]['emailServerPassword'], 'topsecret') self.assertEqual(myProjects[myProjName]['emailAttachments'], ['/var/log/messages', '/var/log/messages.1']) except BaseException as e: print(("Error. %s. %s. %s" % (type(e), str(e), util.formatTb()))) self.assertTrue(False)