def testGetIdleInstanceExcludingImmatureInstance(self): LoadInfo.InitializeTable() LoadInfo.AddInstance('test-instance1') LoadInfo.RegisterInstanceIpAddress('test-instance1', '1.2.3.4') LoadInfo.UpdateLoadInfo('test-instance1', 55) LoadInfo.AddInstance('test-instance2') LoadInfo.RegisterInstanceIpAddress('test-instance2', '5.6.7.8') # test-instance2 hasn't yet reported load information. self.assertEqual('1.2.3.4', LoadInfo.GetIdleInstanceIpAddress())
def testAverageLoadExcludingImmatureInstance(self): LoadInfo.InitializeTable() LoadInfo.AddInstance('test-instance1') LoadInfo.RegisterInstanceIpAddress('test-instance1', '1.2.3.4') # test-instance1 hasn't yet reported load information. LoadInfo.AddInstance('test-instance2') LoadInfo.RegisterInstanceIpAddress('test-instance2', '5.6.7.8') LoadInfo.UpdateLoadInfo('test-instance2', 33) self.assertEqual((1, 33), LoadInfo.GetAverageLoad())
def testAverageLoad(self): LoadInfo.InitializeTable() LoadInfo.AddInstance('test-instance1') LoadInfo.RegisterInstanceIpAddress('test-instance1', '1.2.3.4') LoadInfo.UpdateLoadInfo('test-instance1', 55) LoadInfo.AddInstance('test-instance2') LoadInfo.RegisterInstanceIpAddress('test-instance2', '5.6.7.8') LoadInfo.UpdateLoadInfo('test-instance2', 11) LoadInfo.AddInstance('test-instance3') LoadInfo.RegisterInstanceIpAddress('test-instance3', '9.10.11.12') LoadInfo.UpdateLoadInfo('test-instance3', 66) self.assertEqual((3, 44), LoadInfo.GetAverageLoad())
def testGetIdleInstanceIpAddress(self): LoadInfo.InitializeTable() LoadInfo.AddInstance('test-instance1') LoadInfo.RegisterInstanceIpAddress('test-instance1', '1.2.3.4') LoadInfo.UpdateLoadInfo('test-instance1', 55) LoadInfo.AddInstance('test-instance2') LoadInfo.RegisterInstanceIpAddress('test-instance2', '5.6.7.8') LoadInfo.UpdateLoadInfo('test-instance2', 11) LoadInfo.AddInstance('test-instance3') LoadInfo.RegisterInstanceIpAddress('test-instance3', '9.10.11.12') LoadInfo.UpdateLoadInfo('test-instance3', 66) LoadInfo.AddInstance('test-instance4') LoadInfo.RegisterInstanceIpAddress('test-instance4', '13.14.15.16') LoadInfo.UpdateLoadInfo('test-instance4', 22) # IP address should be picked up from one of the 3 least loaded instances. self.assertIn(LoadInfo.GetIdleInstanceIpAddress(), ('1.2.3.4', '5.6.7.8', '13.14.15.16'))
def testAddInstance(self): LoadInfo.InitializeTable() LoadInfo.AddInstance('test-instance') self.assertEqual(['test-instance'], memcache.get(LoadInfo.ALL_INSTANCES)) self.assertEqual({}, LoadInfo.GetAll()) self.assertIsNone(memcache.get('test-instance')) self.assertIsNotNone(SingleInstance.GetByName('test-instance')) self.assertRaises(ValueError, SingleInstance.GetByName('test-instance').ip_address)
def testRemoveInstanceFromTwo(self): LoadInfo.InitializeTable() LoadInfo.AddInstance('test-instance1') LoadInfo.RegisterInstanceIpAddress('test-instance1', '1.2.3.4') LoadInfo.UpdateLoadInfo('test-instance1', 55) LoadInfo.AddInstance('test-instance2') LoadInfo.RegisterInstanceIpAddress('test-instance2', '5.6.7.8') LoadInfo.UpdateLoadInfo('test-instance2', 22) LoadInfo.RemoveInstance('test-instance1') self.assertEqual( { 'test-instance2': { 'ip_address': '5.6.7.8', 'load': 22, 'force': False } }, LoadInfo.GetAll()) self.assertIsNone(memcache.get('test-instance1')) self.assertIsNone(SingleInstance.GetByName('test-instance1'))
def testRemoveInstanceFromOne(self): LoadInfo.InitializeTable() LoadInfo.AddInstance('test-instance') LoadInfo.RegisterInstanceIpAddress('test-instance', '1.2.3.4') LoadInfo.UpdateLoadInfo('test-instance', 55) LoadInfo.RemoveInstance('test-instance') self.assertEqual({}, LoadInfo.GetAll()) self.assertEqual([], memcache.get(LoadInfo.ALL_INSTANCES)) self.assertIsNone(memcache.get('test-instance')) self.assertIsNone(SingleInstance.GetByName('test-instance'))
def IncreaseEngine(self, increase_count): """Starts specified number of Compute Engine instances. Args: increase_count: Number of instances to increase. """ for _ in xrange(increase_count): instance_name = self.WORKER_NAME_PREFIX + str(uuid.uuid4()) # Add instance name to load information before actually creating the # instance to avoid, to make sure the instance is managed # when it registers IP address. LoadInfo.AddInstance(instance_name) self._StartInstance(instance_name)
def testRegisterInstance(self): LoadInfo.InitializeTable() LoadInfo.AddInstance('test-instance') LoadInfo.RegisterInstanceIpAddress('test-instance', '1.2.3.4') self.assertEqual(['test-instance'], memcache.get(LoadInfo.ALL_INSTANCES)) self.assertEqual({'test-instance': { 'ip_address': '1.2.3.4' }}, LoadInfo.GetAll()) self.assertEqual({'ip_address': '1.2.3.4'}, memcache.get('test-instance')) self.assertEqual('1.2.3.4', SingleInstance.GetByName('test-instance').ip_address)
def testMemcacheClear(self): LoadInfo.InitializeTable() LoadInfo.AddInstance('test-instance') LoadInfo.RegisterInstanceIpAddress('test-instance', '1.2.3.4') LoadInfo.UpdateLoadInfo('test-instance', 55) # Simulate loss of all data in Memcache. memcache.flush_all() LoadInfo.UpdateLoadInfo('test-instance', 38) self.assertEqual( { 'test-instance': { 'ip_address': '1.2.3.4', 'load': 38, 'force': False } }, LoadInfo.GetAll())
def testUpdateLoadInfo(self): LoadInfo.InitializeTable() LoadInfo.AddInstance('test-instance') LoadInfo.RegisterInstanceIpAddress('test-instance', '1.2.3.4') LoadInfo.UpdateLoadInfo('test-instance', 55) self.assertEqual( { 'test-instance': { 'ip_address': '1.2.3.4', 'load': 55, 'force': False } }, LoadInfo.GetAll()) LoadInfo.UpdateLoadInfo('test-instance', 73) self.assertEqual( { 'test-instance': { 'ip_address': '1.2.3.4', 'load': 73, 'force': False } }, LoadInfo.GetAll())
def IncreaseEngine(self): instance_name = self.WORKER_NAME_PREFIX + str(uuid.uuid4()) response = self._StartInstance(instance_name) LoadInfo.AddInstance(instance_name, response)
def testUpdateLoadInfoForce(self): LoadInfo.InitializeTable() LoadInfo.AddInstance('test-instance') LoadInfo.RegisterInstanceIpAddress('test-instance', '1.2.3.4') LoadInfo.UpdateLoadInfo('test-instance', 55) self.assertEqual( { 'test-instance': { 'ip_address': '1.2.3.4', 'load': 55, 'force': False } }, LoadInfo.GetAll()) LoadInfo.UpdateLoadInfo('test-instance', 92, True) self.assertEqual( { 'test-instance': { 'ip_address': '1.2.3.4', 'load': 92, 'force': True } }, LoadInfo.GetAll()) # This update is ignored since force flag is set in data and this is not # force update. LoadInfo.UpdateLoadInfo('test-instance', 15) self.assertEqual( { 'test-instance': { 'ip_address': '1.2.3.4', 'load': 92, 'force': True } }, LoadInfo.GetAll()) # Updated because of force_set flag. LoadInfo.UpdateLoadInfo('test-instance', 8, True) self.assertEqual( { 'test-instance': { 'ip_address': '1.2.3.4', 'load': 8, 'force': True } }, LoadInfo.GetAll()) LoadInfo.UpdateLoadInfo('test-instance', 41, False) self.assertEqual( { 'test-instance': { 'ip_address': '1.2.3.4', 'load': 41, 'force': False } }, LoadInfo.GetAll()) LoadInfo.UpdateLoadInfo('test-instance', 28) self.assertEqual( { 'test-instance': { 'ip_address': '1.2.3.4', 'load': 28, 'force': False } }, LoadInfo.GetAll())