def fake_request(stubs, mdinst, relpath, address="127.0.0.1", fake_get_metadata=None, headers=None, fake_get_metadata_by_instance_id=None): def get_metadata_by_remote_address(address): return mdinst app = handler.MetadataRequestHandler() if fake_get_metadata is None: fake_get_metadata = get_metadata_by_remote_address if stubs: stubs.Set(app, 'get_metadata_by_remote_address', fake_get_metadata) if fake_get_metadata_by_instance_id: stubs.Set(app, 'get_metadata_by_instance_id', fake_get_metadata_by_instance_id) request = webob.Request.blank(relpath) request.remote_addr = address if headers is not None: request.headers.update(headers) response = request.get_response(app) return response
def test_metadata_handler_with_remote_address_no_cache(self, get_by_uuid): # test twice to ensure that disabling the cache works get_by_uuid.return_value = self.mdinst self.flags(metadata_cache_expiration=0) hnd = handler.MetadataRequestHandler() self._metadata_handler_with_remote_address(hnd) self._metadata_handler_with_remote_address(hnd) self.assertEqual(2, get_by_uuid.call_count)
def test_metadata_handler_with_instance_id(self, get_by_uuid): # test twice to ensure that the cache works get_by_uuid.return_value = self.mdinst self.flags(metadata_cache_expiration=15) hnd = handler.MetadataRequestHandler() self._metadata_handler_with_instance_id(hnd) self._metadata_handler_with_instance_id(hnd) self.assertEqual(1, get_by_uuid.call_count)
def setUp(self): super(MetadataTestCase, self).setUp() self.instance = ({ 'id': 1, 'name': 'fake', 'project_id': 'test', 'key_name': None, 'host': 'test', 'launch_index': 1, 'instance_type': { 'name': 'm1.tiny' }, 'reservation_id': 'r-xxxxxxxx', 'user_data': '', 'image_ref': 7, 'vcpus': 1, 'fixed_ips': [], 'root_device_name': '/dev/sda1', 'hostname': 'test' }) def fake_get_instance_nw_info(self, context, instance): return [(None, { 'label': 'public', 'ips': [{ 'ip': '192.168.0.3' }, { 'ip': '192.168.0.4' }], 'ip6s': [{ 'ip': 'fe80::beef' }] })] def fake_get_floating_ips_by_fixed_address(self, context, fixed_ip): return ['1.2.3.4', '5.6.7.8'] def instance_get(*args, **kwargs): return self.instance def instance_get_list(*args, **kwargs): return [self.instance] def floating_get(*args, **kwargs): return '99.99.99.99' self.stubs.Set(network.API, 'get_instance_nw_info', fake_get_instance_nw_info) self.stubs.Set(network.API, 'get_floating_ips_by_fixed_address', fake_get_floating_ips_by_fixed_address) self.stubs.Set(api, 'instance_get', instance_get) self.stubs.Set(api, 'instance_get_all_by_filters', instance_get_list) self.stubs.Set(api, 'instance_get_floating_address', floating_get) self.app = handler.MetadataRequestHandler() network_manager = fake_network.FakeNetworkManager() self.stubs.Set(self.app.compute_api.network_api, 'get_instance_uuids_by_ip_filter', network_manager.get_instance_uuids_by_ip_filter)
def test_by_instance_id_uses_constant_time_compare(self, mock_compare): mock_compare.side_effect = test.TestingException req = webob.Request.blank('/') hnd = handler.MetadataRequestHandler() req.headers['X-Instance-ID'] = 'fake-inst' req.headers['X-Tenant-ID'] = 'fake-proj' self.assertRaises(test.TestingException, hnd._handle_instance_id_request, req) self.assertEqual(1, mock_compare.call_count)
def setUp(self): super(MetadataTestCase, self).setUp() self.instance = ({ 'id': 1, 'uuid': 'b65cee2f-8c69-4aeb-be2f-f79742548fc2', 'name': 'fake', 'project_id': 'test', 'key_name': None, 'host': 'test', 'launch_index': 1, 'instance_type': { 'name': 'm1.tiny' }, 'reservation_id': 'r-xxxxxxxx', 'user_data': '', 'image_ref': 7, 'vcpus': 1, 'fixed_ips': [], 'root_device_name': '/dev/sda1', 'info_cache': { 'network_info': [] }, 'hostname': 'test' }) def fake_get_floating_ips_by_fixed_address(self, context, fixed_ip): return ['1.2.3.4', '5.6.7.8'] def instance_get(*args, **kwargs): return self.instance def instance_get_list(*args, **kwargs): return [self.instance] def get_fixed_ip_by_address(*args, **kwargs): return {'instance_id': self.instance['id']} fake_network.stub_out_nw_api_get_instance_nw_info(self.stubs, spectacular=True) self.stubs.Set(network.API, 'get_floating_ips_by_fixed_address', fake_get_floating_ips_by_fixed_address) self.stubs.Set(network.API, 'get_fixed_ip_by_address', get_fixed_ip_by_address) self.stubs.Set(api, 'instance_get', instance_get) self.stubs.Set(api, 'instance_get_all_by_filters', instance_get_list) self.app = handler.MetadataRequestHandler() network_manager = fake_network.FakeNetworkManager() self.stubs.Set(self.app.compute_api.network_api, 'get_instance_uuids_by_ip_filter', network_manager.get_instance_uuids_by_ip_filter)