def setUp(self): #Building a fake config timeout = 1 self.conf = Config() self.conf.addTeamInfo("Team1", "127.0.0.1", "127.0.0.0/24") self.conf.addTeamInfo("Team2", "127.0.1.1", "127.0.1.0/24") self.servicebot_conf = ServiceBotConfig() self.servicebot_conf.min_duration_seconds = 5 self.servicebot_conf.max_duration_seconds = 5 self.servicebot_conf.addServiceInfo( "Service1", modulePath() + "/testservices/ErrorService.py", timeout, 1, 1) self.servicebot_conf.addServiceInfo( "Service2", modulePath() + "/testservices/ErrorService.py", timeout, 1, 1) self.servicebot_conf.addServiceInfo( "Service3", modulePath() + "/testservices/ErrorService.py", timeout, 1, 1) gamestate = GameStateServerInfo("localhost", 4242, "0123456789012345", "ABCDEFGH") self.conf.setGameStateServerInfo(gamestate) self.conf.addSection("SERVICE_BOT", self.servicebot_conf)
def testStore(self): conf = Config() servicebot_conf = ServiceBotConfig() servicebot_conf.min_duration_seconds = 2 servicebot_conf.max_duration_seconds = 2 servicebot_conf.addServiceInfo( "Service", modulePath() + "/testservices/StoreService.py", 1, 1, 1) conf.addSection("SERVICE_BOT", servicebot_conf) conf.addTeamInfo("Team1", "127.0.0.1", "127.0.0.0/24") conf.addTeamInfo("Team1", "127.0.0.1", "127.0.0.0/24") gamestate = GameStateServerInfo("localhost", 4242, "0123456789012345", "ABCDEFGH") conf.setGameStateServerInfo(gamestate) scheduler = ServiceTaskScheduler(conf, True) scheduler.execute(0) results = scheduler.execute(1) scheduler.terminateAtRoundEnd() for i in range(0, conf.numTeams()): for j in range(0, servicebot_conf.numServices()): self.assert_(results[i][j].store() == "Worked")
def testStress(self): self.conf = Config() for i in range(0, 1000): self.conf.addTeamInfo("Team", "127.0.0.1", "127.0.0.0/24") self.conf.addServiceInfo( "Service", modulePath() + "/testservices/GoodService.py", 1, 1, 1) factory1 = ServiceTaskFactory(self.conf, True) factory2 = ServiceTaskFactory(self.conf, False)
def setUp(self): self.conf = Config() self.conf.addTeamInfo("Team1","127.0.0.1","127.0.0.0/24") self.conf.addTeamInfo("Team2","127.0.1.1","127.0.1.0/24") atkcfg = AttackConfig(True) atkcfg.exploit_dir = os.path.join(modulePath(),"testexploits") atkcfg.exploit_timeout = 5 atkcfg.round_interval = 5 atkcfg.gather_interval = 1 self.conf.addSection("ATTACK_BOT",atkcfg)
class TestAttackManager(unittest.TestCase): def setUp(self): self.conf = Config() self.conf.addTeamInfo("Team1","127.0.0.1","127.0.0.0/24") self.conf.addTeamInfo("Team2","127.0.1.1","127.0.1.0/24") atkcfg = AttackConfig(True) atkcfg.exploit_dir = os.path.join(modulePath(),"testexploits") atkcfg.exploit_timeout = 5 atkcfg.round_interval = 5 atkcfg.gather_interval = 1 self.conf.addSection("ATTACK_BOT",atkcfg) def testUpdateExploits(self): manager = AttackManager(self.conf,None,True,False) manager.start() manager.cmd(AttackManager.UPDATE_EXPLOITS) exploit_set = manager.test(AttackManager.TEST_GET_EXPLOITS) self.assertTrue("Succesful.py" in exploit_set) self.assertTrue("Forever.py" in exploit_set) self.assertTrue("Timeout.py" in exploit_set) def testLaunchExploit(self): manager = AttackManager(self.conf,None,True,False) manager.start() manager.cmd(AttackManager.UPDATE_EXPLOITS) manager.cmd(AttackManager.LAUNCH_EXPLOIT,"Succesful.py") time.sleep(1) manager.cmd(AttackManager.PROCESS_OUTPUT) time.sleep(1) manager.cmd(AttackManager.GATHER_FLAGS) time.sleep(1) results = manager.getFlags() self.assertEquals(len(results),2) r1 = results[0] r2 = results[1] self.assertEquals(r1[0],0) self.assertEquals(r2[0],1) self.assertEquals(r1[2],['foobarbaz']) self.assertEquals(r2[2],['foobarbaz'])
class TestFlagCollector(unittest.TestCase): def setUp(self): self.conf = Config() self.conf.setFlagDuration(60) self.conf.addTeamInfo("Team1","127.0.0.1","127.0.0.0/24") self.conf.addTeamInfo("Team2","127.0.1.1","127.0.1.0/24") def testBasicUsage(self): fc = FlagCollector() f1 = Flag(0,0,0,time.time()) f2 = Flag(0,1,0,time.time()) fc.enque(f1) fc.enque(f2) flags = fc.collect() self.assert_(len(flags) == 2) flags = fc.collect() self.assert_(len(flags) == 0) def testThreadSafety(self): fc = FlagCollector() p1 = FlagCollectorPopulator(fc,10000,10) p2 = FlagCollectorPopulator(fc,10000,20) p3 = FlagCollectorPopulator(fc,10000,30) p4 = FlagCollectorPopulator(fc,10000,30) p5 = FlagCollectorPopulator(fc,10000,30) p1.start() p2.start() p3.start() p4.start() p5.start() total = 0 for i in xrange(1000): total += len(fc.collect()) time.sleep(1.0/1000.0) p1.join() p2.join() p3.join() p4.join() p5.join() total += len(fc.collect()) self.assertEquals(total,50000)
def setUp(self): #Building a fake config timeout = 1 self.conf = Config() self.conf.addTeamInfo("Team1","127.0.0.1","127.0.0.0/24") self.conf.addTeamInfo("Team2","127.0.1.1","127.0.1.0/24") self.servicebot_conf = ServiceBotConfig() self.servicebot_conf.addServiceInfo( "Service1", modulePath()+"/testservices/GoodService.py", timeout, 1,1) self.servicebot_conf.addServiceInfo( "Service2", modulePath()+"/testservices/StoreService.py", timeout, 1,1) gamestate = GameStateServerInfo( "localhost", 4242, "0123456789012345", "ABCDEFGH") self.conf.setGameStateServerInfo(gamestate) self.conf.addSection("SERVICE_BOT",self.servicebot_conf)
class TestFlagCollector(unittest.TestCase): def setUp(self): self.conf = Config() self.conf.setFlagDuration(60) self.conf.addTeamInfo("Team1", "127.0.0.1", "127.0.0.0/24") self.conf.addTeamInfo("Team2", "127.0.1.1", "127.0.1.0/24") def testBasicUsage(self): fc = FlagCollector() f1 = Flag(0, 0, 0, time.time()) f2 = Flag(0, 1, 0, time.time()) fc.enque(f1) fc.enque(f2) flags = fc.collect() self.assert_(len(flags) == 2) flags = fc.collect() self.assert_(len(flags) == 0) def testThreadSafety(self): fc = FlagCollector() p1 = FlagCollectorPopulator(fc, 10000, 10) p2 = FlagCollectorPopulator(fc, 10000, 20) p3 = FlagCollectorPopulator(fc, 10000, 30) p4 = FlagCollectorPopulator(fc, 10000, 30) p5 = FlagCollectorPopulator(fc, 10000, 30) p1.start() p2.start() p3.start() p4.start() p5.start() total = 0 for i in xrange(1000): total += len(fc.collect()) time.sleep(1.0 / 1000.0) p1.join() p2.join() p3.join() p4.join() p5.join() total += len(fc.collect()) self.assertEquals(total, 50000)
def load(self,config_path): try: self.config_path = config_path self.read(config_path) conf = Config() assert(os.path.isfile(config_path)),"%s does not exsist!" % config_path assert("base_path" in self.defaults()),"Config file %s has no base_path set!" % config_path msg = "No such directory: %s, be sure to edit the base_path in the config file!" % self.defaults()["base_path"] assert(os.path.isdir(self.defaults()["base_path"])),msg conf.setBasePath(self.defaults()["base_path"]) #Set up logging first self.__parseLogging(conf) sections = self.sections() sections.remove("Logging") for section in sections: processed = False for handler in self.handlers: if(handler.canHandle(section)): processed = True handler.parse(self,section,conf) break if(processed == False): msg = "Unknown Section: %s" % section raise ConfigParser.ParsingError(msg) return conf except ConfigParser.ParsingError as e: err = "Error parsing %s: %s" %(self.config_path,e) sys.exit(err) except ConfigParser.NoSectionError as e: err = "Error parsing %s: A required section is missing: %s" %(self.config_path,e.section) sys.exit(err) except ConfigParser.NoOptionError as e: err = "Error parsing %s: A required option in section %s is missing: %s" % ( self.config_path,e.section,e.option) sys.exit(err) """
def testCookie(self): conf = Config() servicebot_conf = ServiceBotConfig() servicebot_conf.min_duration_seconds = 2 servicebot_conf.max_duration_seconds = 2 servicebot_conf.addServiceInfo( "Service", modulePath()+"/testservices/StoreService.py", 1, 1,1) conf.addSection("SERVICE_BOT",servicebot_conf) conf.addTeamInfo("Team1","127.0.0.1","127.0.0.0/24") conf.addTeamInfo("Team1","127.0.0.1","127.0.0.0/24") gamestate = GameStateServerInfo( "localhost", 4242, "0123456789012345", "ABCDEFGH") conf.setGameStateServerInfo(gamestate) scheduler = ServiceTaskScheduler(conf,True) scheduler.execute(0) results = scheduler.execute(1) for team,service,task in results: self.assertEquals(task.cookie(),"Worked") scheduler.quit()
def testStress(self): self.conf = Config() for i in range(0,1000): self.conf.addTeamInfo("Team","127.0.0.1","127.0.0.0/24") self.conf.addServiceInfo( "Service", modulePath()+"/testservices/GoodService.py", 1, 1,1) factory1 = ServiceTaskFactory(self.conf,True) factory2 = ServiceTaskFactory(self.conf,False)
def testStore(self): conf = Config() servicebot_conf = ServiceBotConfig() servicebot_conf.min_duration_seconds = 2 servicebot_conf.max_duration_seconds = 2 servicebot_conf.addServiceInfo("Service", modulePath() + "/testservices/StoreService.py", 1, 1, 1) conf.addSection("SERVICE_BOT", servicebot_conf) conf.addTeamInfo("Team1", "127.0.0.1", "127.0.0.0/24") conf.addTeamInfo("Team1", "127.0.0.1", "127.0.0.0/24") gamestate = GameStateServerInfo("localhost", 4242, "0123456789012345", "ABCDEFGH") conf.setGameStateServerInfo(gamestate) scheduler = ServiceTaskScheduler(conf, True) scheduler.execute(0) results = scheduler.execute(1) scheduler.terminateAtRoundEnd() for i in range(0, conf.numTeams()): for j in range(0, servicebot_conf.numServices()): self.assert_(results[i][j].store() == "Worked")
class TestServiceTaskFactory(unittest.TestCase): def setUp(self): #Building a fake config timeout = 1 self.conf = Config() self.conf.addTeamInfo("Team1","127.0.0.1","127.0.0.0/24") self.conf.addTeamInfo("Team2","127.0.1.1","127.0.1.0/24") self.servicebot_conf = ServiceBotConfig() self.servicebot_conf.addServiceInfo( "Service1", modulePath()+"/testservices/GoodService.py", timeout, 1,1) self.servicebot_conf.addServiceInfo( "Service2", modulePath()+"/testservices/StoreService.py", timeout, 1,1) gamestate = GameStateServerInfo( "localhost", 4242, "0123456789012345", "ABCDEFGH") self.conf.setGameStateServerInfo(gamestate) self.conf.addSection("SERVICE_BOT",self.servicebot_conf) def testBuildSimple(self): factory = ServiceTaskFactory(self.conf,True) task = factory.build(0,0,0) task.start() self.assert_(task.status() == ServiceTask.OK) def testStorePersistance(self): factory = ServiceTaskFactory(self.conf,True) tasks = [ factory.build(0,1,0), factory.build(1,1,0), ] for task in tasks: task.start() factory.update(tasks[0],0,1,1) factory.update(tasks[1],1,1,1) task = factory.build(1,1,0) task.start() self.assert_(task.store() == "Worked") def testSave(self): factory = ServiceTaskFactory(self.conf,True) tasks = [ factory.build(0,1,1), factory.build(1,1,1), ] for task in tasks: task.start() factory.update(tasks[0],0,1,0) factory.update(tasks[1],1,1,0) #Test a "crash recovery" by building a new factory factory.save() new_factory = ServiceTaskFactory(self.conf,False) task = new_factory.build(1,1,0) task.start() self.assert_(task.store() == "Worked") def testFlagUpdate(self): factory = ServiceTaskFactory(self.conf,True) #Simulate the service being down - prev flag should be invalid task = factory.build(0,0,6) task.start() self.assert_(task.status() == ServiceTask.OK) factory.update(task,0,0,6) self.assert_(task.status() == ServiceTask.INVALID_FLAG) #Simulate a round passing - prev flag should be valid task = factory.build(0,0,7) task.start() factory.update(task,0,0,7) self.assert_(task.status() == ServiceTask.OK) def testUpdateRoundOne(self): factory = ServiceTaskFactory(self.conf,True) #Clear prev flag task = factory.build(0,0,42) task.start() #Round 1 should be ok, even if the prev_flag is invalid task = factory.build(0,0,1) task.start() self.assertEquals(task.status(),ServiceTask.OK) factory.update(task,0,0,1) self.assertEquals(task.status(),ServiceTask.OK) def testStress(self): self.conf = Config() for i in range(0,1000): self.conf.addTeamInfo("Team","127.0.0.1","127.0.0.0/24") self.conf.addServiceInfo( "Service", modulePath()+"/testservices/GoodService.py", 1, 1,1) factory1 = ServiceTaskFactory(self.conf,True) factory2 = ServiceTaskFactory(self.conf,False)
class TestFlagValidator(unittest.TestCase): def setUp(self): self.conf = Config() self.conf.setFlagDuration(60) self.conf.addTeamInfo("Team1", "127.0.0.1", "127.0.0.0/24") self.conf.addTeamInfo("Team2", "127.0.1.1", "127.0.1.0/24") def testEnsureDifferentTeams(self): fv = FlagValidator(self.conf.numTeams(), self.conf.getFlagDuration()) flag = Flag(0, 0, 0, time.time()) self.assert_(fv.validate(0, flag) == FlagValidator.SAME_TEAM) self.assert_(fv.validate(1, flag) == FlagValidator.VALID) def testEnsureDuration(self): fv = FlagValidator(self.conf.numTeams(), self.conf.getFlagDuration()) flagExpired = Flag(0, 0, 0, time.time() - 61) flagOk = Flag(0, 0, 0, time.time() - 59) self.assert_(fv.validate(1, flagOk) == FlagValidator.VALID) self.assert_(fv.validate(1, flagExpired) == FlagValidator.EXPIRED) def testEnsureUnique(self): fv = FlagValidator(self.conf.numTeams(), self.conf.getFlagDuration()) flag = Flag(0, 0, 0, time.time()) self.assert_(fv.validate(1, flag) == FlagValidator.VALID) self.assert_(fv.validate(1, flag) == FlagValidator.REPEAT) def testEnsureUniqueBug(self): fv = FlagValidator(self.conf.numTeams(), self.conf.getFlagDuration()) flag = Flag(0, 0, 0, time.time()) self.assert_(fv.validate(1, flag) == FlagValidator.VALID) for i in xrange(100): self.assert_(fv.validate(1, flag) == FlagValidator.REPEAT) def testLargeRecord(self): fv = FlagValidator(self.conf.numTeams(), self.conf.getFlagDuration()) for i in xrange(1000): flag = Flag(0, i, 0, time.time()) self.assert_(fv.validate(1, flag) == FlagValidator.VALID) flag = Flag(0, 500, 0, time.time()) self.assert_(fv.validate(1, flag) == FlagValidator.REPEAT)
def setUp(self): self.conf = Config() self.conf.setFlagDuration(60) self.conf.addTeamInfo("Team1","127.0.0.1","127.0.0.0/24") self.conf.addTeamInfo("Team2","127.0.1.1","127.0.1.0/24")
class TestFlagValidator(unittest.TestCase): def setUp(self): self.conf = Config() self.conf.setFlagDuration(60) self.conf.addTeamInfo("Team1","127.0.0.1","127.0.0.0/24") self.conf.addTeamInfo("Team2","127.0.1.1","127.0.1.0/24") def testEnsureDifferentTeams(self): fv = FlagValidator(self.conf.numTeams(),self.conf.getFlagDuration()) flag = Flag(0,0,0,time.time()) self.assert_(fv.validate(0,flag) == FlagValidator.SAME_TEAM) self.assert_(fv.validate(1,flag) == FlagValidator.VALID) def testEnsureDuration(self): fv = FlagValidator(self.conf.numTeams(),self.conf.getFlagDuration()) flagExpired = Flag(0,0,0,time.time()-61) flagOk = Flag(0,0,0,time.time()-59) self.assert_(fv.validate(1,flagOk) == FlagValidator.VALID) self.assert_(fv.validate(1,flagExpired) == FlagValidator.EXPIRED) def testEnsureUnique(self): fv = FlagValidator(self.conf.numTeams(),self.conf.getFlagDuration()) flag = Flag(0,0,0,time.time()) self.assert_(fv.validate(1,flag) == FlagValidator.VALID) self.assert_(fv.validate(1,flag) == FlagValidator.REPEAT) def testEnsureUniqueBug(self): fv = FlagValidator(self.conf.numTeams(),self.conf.getFlagDuration()) flag = Flag(0,0,0,time.time()) self.assert_(fv.validate(1,flag) == FlagValidator.VALID) for i in xrange(100): self.assert_(fv.validate(1,flag) == FlagValidator.REPEAT) def testLargeRecord(self): fv = FlagValidator(self.conf.numTeams(),self.conf.getFlagDuration()) for i in xrange(1000): flag = Flag(0,i,0,time.time()) self.assert_(fv.validate(1,flag) == FlagValidator.VALID) flag = Flag(0,500,0,time.time()) self.assert_(fv.validate(1,flag) == FlagValidator.REPEAT)
class TestServiceTaskScheduler(unittest.TestCase): def setUp(self): #Building a fake config timeout = 1 self.conf = Config() self.conf.addTeamInfo("Team1","127.0.0.1","127.0.0.0/24") self.conf.addTeamInfo("Team2","127.0.1.1","127.0.1.0/24") self.servicebot_conf = ServiceBotConfig() self.servicebot_conf.min_duration_seconds = 5 self.servicebot_conf.max_duration_seconds = 5 self.servicebot_conf.addServiceInfo( "Service1", modulePath()+"/testservices/ErrorService.py", timeout, 1,1) self.servicebot_conf.addServiceInfo( "Service2", modulePath()+"/testservices/ErrorService.py", timeout, 1,1) self.servicebot_conf.addServiceInfo( "Service3", modulePath()+"/testservices/ErrorService.py", timeout, 1,1) gamestate = GameStateServerInfo( "localhost", 4242, "0123456789012345", "ABCDEFGH") self.conf.setGameStateServerInfo(gamestate) self.conf.addSection("SERVICE_BOT",self.servicebot_conf) def testSimpleSchedule(self): round = 0 scheduler = ServiceTaskScheduler(self.conf,True) start = time.time() results = scheduler.execute(round) end = time.time() run_time = end - start self.assert_(4.0 < run_time and run_time < 6.0) for team,service,task in results: self.assertNotEqual(None,task.error()) self.assertEquals(None,task.prevFlag()) scheduler.quit() def testCookie(self): conf = Config() servicebot_conf = ServiceBotConfig() servicebot_conf.min_duration_seconds = 2 servicebot_conf.max_duration_seconds = 2 servicebot_conf.addServiceInfo( "Service", modulePath()+"/testservices/StoreService.py", 1, 1,1) conf.addSection("SERVICE_BOT",servicebot_conf) conf.addTeamInfo("Team1","127.0.0.1","127.0.0.0/24") conf.addTeamInfo("Team1","127.0.0.1","127.0.0.0/24") gamestate = GameStateServerInfo( "localhost", 4242, "0123456789012345", "ABCDEFGH") conf.setGameStateServerInfo(gamestate) scheduler = ServiceTaskScheduler(conf,True) scheduler.execute(0) results = scheduler.execute(1) for team,service,task in results: self.assertEquals(task.cookie(),"Worked") scheduler.quit()
class TestServiceTaskScheduler(unittest.TestCase): def setUp(self): # Building a fake config timeout = 1 self.conf = Config() self.conf.addTeamInfo("Team1", "127.0.0.1", "127.0.0.0/24") self.conf.addTeamInfo("Team2", "127.0.1.1", "127.0.1.0/24") self.servicebot_conf = ServiceBotConfig() self.servicebot_conf.min_duration_seconds = 5 self.servicebot_conf.max_duration_seconds = 5 self.servicebot_conf.addServiceInfo("Service1", modulePath() + "/testservices/ErrorService.py", timeout, 1, 1) self.servicebot_conf.addServiceInfo("Service2", modulePath() + "/testservices/ErrorService.py", timeout, 1, 1) self.servicebot_conf.addServiceInfo("Service3", modulePath() + "/testservices/ErrorService.py", timeout, 1, 1) gamestate = GameStateServerInfo("localhost", 4242, "0123456789012345", "ABCDEFGH") self.conf.setGameStateServerInfo(gamestate) self.conf.addSection("SERVICE_BOT", self.servicebot_conf) def testSimpleSchedule(self): round = 0 scheduler = ServiceTaskScheduler(self.conf, True) start = time.time() results = scheduler.execute(round) scheduler.terminateAtRoundEnd() end = time.time() run_time = end - start self.assert_(4.0 < run_time and run_time < 6.0) for i in range(0, self.conf.numTeams()): for j in range(0, self.servicebot_conf.numServices()): self.assert_(results[i][j].status() == ServiceTask.ERROR) def testStore(self): conf = Config() servicebot_conf = ServiceBotConfig() servicebot_conf.min_duration_seconds = 2 servicebot_conf.max_duration_seconds = 2 servicebot_conf.addServiceInfo("Service", modulePath() + "/testservices/StoreService.py", 1, 1, 1) conf.addSection("SERVICE_BOT", servicebot_conf) conf.addTeamInfo("Team1", "127.0.0.1", "127.0.0.0/24") conf.addTeamInfo("Team1", "127.0.0.1", "127.0.0.0/24") gamestate = GameStateServerInfo("localhost", 4242, "0123456789012345", "ABCDEFGH") conf.setGameStateServerInfo(gamestate) scheduler = ServiceTaskScheduler(conf, True) scheduler.execute(0) results = scheduler.execute(1) scheduler.terminateAtRoundEnd() for i in range(0, conf.numTeams()): for j in range(0, servicebot_conf.numServices()): self.assert_(results[i][j].store() == "Worked")
def setUp(self): self.conf = Config() self.conf.setFlagDuration(60) self.conf.addTeamInfo("Team1", "127.0.0.1", "127.0.0.0/24") self.conf.addTeamInfo("Team2", "127.0.1.1", "127.0.1.0/24")
class TestServiceTaskScheduler(unittest.TestCase): def setUp(self): #Building a fake config timeout = 1 self.conf = Config() self.conf.addTeamInfo("Team1", "127.0.0.1", "127.0.0.0/24") self.conf.addTeamInfo("Team2", "127.0.1.1", "127.0.1.0/24") self.servicebot_conf = ServiceBotConfig() self.servicebot_conf.min_duration_seconds = 5 self.servicebot_conf.max_duration_seconds = 5 self.servicebot_conf.addServiceInfo( "Service1", modulePath() + "/testservices/ErrorService.py", timeout, 1, 1) self.servicebot_conf.addServiceInfo( "Service2", modulePath() + "/testservices/ErrorService.py", timeout, 1, 1) self.servicebot_conf.addServiceInfo( "Service3", modulePath() + "/testservices/ErrorService.py", timeout, 1, 1) gamestate = GameStateServerInfo("localhost", 4242, "0123456789012345", "ABCDEFGH") self.conf.setGameStateServerInfo(gamestate) self.conf.addSection("SERVICE_BOT", self.servicebot_conf) def testSimpleSchedule(self): round = 0 scheduler = ServiceTaskScheduler(self.conf, True) start = time.time() results = scheduler.execute(round) scheduler.terminateAtRoundEnd() end = time.time() run_time = end - start self.assert_(4.0 < run_time and run_time < 6.0) for i in range(0, self.conf.numTeams()): for j in range(0, self.servicebot_conf.numServices()): self.assert_(results[i][j].status() == ServiceTask.ERROR) def testStore(self): conf = Config() servicebot_conf = ServiceBotConfig() servicebot_conf.min_duration_seconds = 2 servicebot_conf.max_duration_seconds = 2 servicebot_conf.addServiceInfo( "Service", modulePath() + "/testservices/StoreService.py", 1, 1, 1) conf.addSection("SERVICE_BOT", servicebot_conf) conf.addTeamInfo("Team1", "127.0.0.1", "127.0.0.0/24") conf.addTeamInfo("Team1", "127.0.0.1", "127.0.0.0/24") gamestate = GameStateServerInfo("localhost", 4242, "0123456789012345", "ABCDEFGH") conf.setGameStateServerInfo(gamestate) scheduler = ServiceTaskScheduler(conf, True) scheduler.execute(0) results = scheduler.execute(1) scheduler.terminateAtRoundEnd() for i in range(0, conf.numTeams()): for j in range(0, servicebot_conf.numServices()): self.assert_(results[i][j].store() == "Worked")
class TestServiceTaskFactory(unittest.TestCase): def setUp(self): #Building a fake config timeout = 1 self.conf = Config() self.conf.addTeamInfo("Team1", "127.0.0.1", "127.0.0.0/24") self.conf.addTeamInfo("Team2", "127.0.1.1", "127.0.1.0/24") self.servicebot_conf = ServiceBotConfig() self.servicebot_conf.addServiceInfo( "Service1", modulePath() + "/testservices/GoodService.py", timeout, 1, 1) self.servicebot_conf.addServiceInfo( "Service2", modulePath() + "/testservices/StoreService.py", timeout, 1, 1) gamestate = GameStateServerInfo("localhost", 4242, "0123456789012345", "ABCDEFGH") self.conf.setGameStateServerInfo(gamestate) self.conf.addSection("SERVICE_BOT", self.servicebot_conf) def testBuildSimple(self): factory = ServiceTaskFactory(self.conf, True) task = factory.build(0, 0, 0) task.start() self.assert_(task.status() == ServiceTask.OK) def testStorePersistance(self): factory = ServiceTaskFactory(self.conf, True) tasks = [ factory.build(0, 1, 0), factory.build(1, 1, 0), ] for task in tasks: task.start() factory.update(tasks[0], 0, 1, 1) factory.update(tasks[1], 1, 1, 1) task = factory.build(1, 1, 0) task.start() self.assert_(task.store() == "Worked") def testSave(self): factory = ServiceTaskFactory(self.conf, True) tasks = [ factory.build(0, 1, 1), factory.build(1, 1, 1), ] for task in tasks: task.start() factory.update(tasks[0], 0, 1, 0) factory.update(tasks[1], 1, 1, 0) #Test a "crash recovery" by building a new factory factory.save() new_factory = ServiceTaskFactory(self.conf, False) task = new_factory.build(1, 1, 0) task.start() self.assert_(task.store() == "Worked") def testFlagUpdate(self): factory = ServiceTaskFactory(self.conf, True) #Simulate the service being down - prev flag should be invalid task = factory.build(0, 0, 6) task.start() self.assert_(task.status() == ServiceTask.OK) factory.update(task, 0, 0, 6) self.assert_(task.status() == ServiceTask.INVALID_FLAG) #Simulate a round passing - prev flag should be valid task = factory.build(0, 0, 7) task.start() factory.update(task, 0, 0, 7) self.assert_(task.status() == ServiceTask.OK) def testUpdateRoundOne(self): factory = ServiceTaskFactory(self.conf, True) #Clear prev flag task = factory.build(0, 0, 42) task.start() #Round 1 should be ok, even if the prev_flag is invalid task = factory.build(0, 0, 1) task.start() self.assertEquals(task.status(), ServiceTask.OK) factory.update(task, 0, 0, 1) self.assertEquals(task.status(), ServiceTask.OK) def testStress(self): self.conf = Config() for i in range(0, 1000): self.conf.addTeamInfo("Team", "127.0.0.1", "127.0.0.0/24") self.conf.addServiceInfo( "Service", modulePath() + "/testservices/GoodService.py", 1, 1, 1) factory1 = ServiceTaskFactory(self.conf, True) factory2 = ServiceTaskFactory(self.conf, False)