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 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 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 get(self): """Returns stats of game instances for non logged-in users.""" load_entries = [] all_load_info = LoadInfo.GetAll() for name, info in all_load_info.items(): load_entries.append({ 'host': name, 'ipaddress': info.get(LoadInfo.IP_ADDRESS, ''), 'load': info.get(LoadInfo.LOAD, 0), }) self.response.out.write(json.dumps(load_entries))
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 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 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 get(self): """Returns stats of managed Compute Engine instances for Admin UI.""" load_entries = [] instance_list = ComputeEngineController( decorator.credentials).ListInstances() all_load_info = LoadInfo.GetAll() # First, list managed instances whose Compute Engine status is found. for instance in instance_list: instance_name = instance['name'] if instance_name in all_load_info: info = all_load_info[instance_name] load_entries.append({ 'host': instance_name, 'ipaddress': info.get(LoadInfo.IP_ADDRESS, ''), 'status': instance['status'], 'load': info.get(LoadInfo.LOAD, 0), 'force_set': info.get(LoadInfo.FORCE, False), }) del all_load_info[instance_name] # Then, list managed instances without Compute Engine status. for name, info in all_load_info.items(): load_entries.append({ 'host': name, 'ipaddress': info.get(LoadInfo.IP_ADDRESS, ''), 'status': 'NOT FOUND', 'load': info.get(LoadInfo.LOAD, 0), 'force_set': info.get(LoadInfo.FORCE, False), }) self.response.out.write(json.dumps(load_entries))
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())
def testInitializeTable(self): LoadInfo.InitializeTable() self.assertEqual([], memcache.get(LoadInfo.ALL_INSTANCES)) self.assertEqual({}, LoadInfo.GetAll())