def test_get_image_meta_no_image_no_image_system_meta(self): e = exception.ImageNotFound(image_id='fake-image') self.mock_image_api.get.side_effect = e for k in self.instance_obj.system_metadata.keys(): if k.startswith('image_'): del self.instance_obj.system_metadata[k] with mock.patch('patron.objects.Flavor.get_by_flavor_id') as get: get.return_value = objects.Flavor(extra_specs={}) image_meta = compute_utils.get_image_metadata( self.ctx, self.mock_image_api, 'fake-image', self.instance_obj) expected = {'properties': 'DONTCARE'} self.assertThat(expected, matchers.DictMatches(image_meta))
def test_availability_zone_detail_no_services(self): expected_response = { 'availabilityZoneInfo': [{ 'zoneState': { 'available': True }, 'hosts': {}, 'zoneName': 'patron' }] } self.stubs.Set(availability_zones, 'get_availability_zones', fake_get_availability_zones) resp_dict = self.controller.detail(self.req) self.assertThat(resp_dict, matchers.DictMatches(expected_response))
def test_get_flavor_with_limit(self): req = self.fake_request.blank(self._prefix + '/flavors?limit=2') response = self.controller.index(req) response_list = response["flavors"] response_links = response["flavors_links"] expected_flavors = [{ "id": "1", "name": "flavor 1", "links": [ { "rel": "self", "href": "http://localhost/" + self._rspv + "/flavors/1", }, { "rel": "bookmark", "href": "http://localhost" + self._fake + "/flavors/1", }, ], }, { "id": "2", "name": "flavor 2", "links": [ { "rel": "self", "href": "http://localhost/" + self._rspv + "/flavors/2", }, { "rel": "bookmark", "href": "http://localhost" + self._fake + "/flavors/2", }, ], }] self.assertEqual(response_list, expected_flavors) self.assertEqual(response_links[0]['rel'], 'next') href_parts = urlparse.urlparse(response_links[0]['href']) self.assertEqual('/' + self._rspv + '/flavors', href_parts.path) params = urlparse.parse_qs(href_parts.query) self.assertThat({ 'limit': ['2'], 'marker': ['2'] }, matchers.DictMatches(params))
def test_get_flavor_detail_with_limit(self): url = self._prefix + '/flavors/detail?limit=1' req = self.fake_request.blank(url) response = self.controller.detail(req) response_list = response["flavors"] response_links = response["flavors_links"] expected_flavors = [ { "id": "1", "name": "flavor 1", "ram": "256", "disk": "10", "vcpus": "", "links": [ { "rel": "self", "href": "http://localhost/" + self._rspv + "/flavors/1", }, { "rel": "bookmark", "href": "http://localhost" + self._fake + "/flavors/1", }, ], }, ] self._set_expected_body(expected_flavors[0], ephemeral='20', swap='10', disabled=False) self.assertEqual(response_list, expected_flavors) self.assertEqual(response_links[0]['rel'], 'next') href_parts = urlparse.urlparse(response_links[0]['href']) self.assertEqual('/' + self._rspv + '/flavors/detail', href_parts.path) params = urlparse.parse_qs(href_parts.query) self.assertThat({ 'limit': ['1'], 'marker': ['1'] }, matchers.DictMatches(params))
def test_non_exist_resource(self): res_ext = base_extensions.ResourceExtension( 'tweedles', StubController(response_body)) manager = StubExtensionManager(res_ext) app = compute.APIRouter(manager) request = webob.Request.blank("/fake/tweedles/1") response = request.get_response(app) self.assertEqual(404, response.status_int) self.assertEqual('application/json', response.content_type) body = jsonutils.loads(response.body) expected = { "itemNotFound": { "message": "The resource could not be found.", "code": 404 } } self.assertThat(expected, matchers.DictMatches(body))
def test_ensure_vlan_bridge_with_existing_dvs(self): network_ref = { 'dvpg': 'dvportgroup-2062', 'type': 'DistributedVirtualPortgroup' } self.mox.StubOutWithMock(network_util, 'get_network_with_the_name') self.mox.StubOutWithMock(network_util, 'get_vswitch_for_vlan_interface') self.mox.StubOutWithMock(network_util, 'check_if_vlan_interface_exists') self.mox.StubOutWithMock(network_util, 'create_port_group') network_util.get_network_with_the_name( self.session, 'fa0', self.cluster).AndReturn(network_ref) self.mox.ReplayAll() ref = vif.ensure_vlan_bridge(self.session, self.vif, create_vlan=False) self.assertThat(ref, matchers.DictMatches(network_ref))
def test_bad_request(self): res_ext = base_extensions.ResourceExtension( 'tweedles', StubController(response_body)) manager = StubExtensionManager(res_ext) app = compute.APIRouter(manager) request = webob.Request.blank("/fake/tweedles") request.method = "POST" response = request.get_response(app) self.assertEqual(400, response.status_int) self.assertEqual('application/json', response.content_type) body = jsonutils.loads(response.body) expected = { "badRequest": { "message": "All aboard the fail train!", "code": 400 } } self.assertThat(expected, matchers.DictMatches(body))
def test_show_console(self): def fake_get_console(cons_self, context, instance_id, console_id): self.assertEqual(instance_id, self.uuid) self.assertEqual(console_id, 20) pool = dict(console_type='fake_type', public_hostname='fake_hostname') return dict(id=console_id, password='******', port='fake_port', pool=pool, instance_name='inst-0001') expected = {'console': {'id': 20, 'port': 'fake_port', 'host': 'fake_hostname', 'password': '******', 'instance_name': 'inst-0001', 'console_type': 'fake_type'}} self.stubs.Set(console.api.API, 'get_console', fake_get_console) req = fakes.HTTPRequest.blank(self.url + '/20') res_dict = self.controller.show(req, self.uuid, '20') self.assertThat(res_dict, matchers.DictMatches(expected))
def test_volume_attach_no_check_driver_attach(self): test_bdm = self.driver_classes['volume'](self.volume_bdm) volume = {'id': 'fake-volume-id-1', 'attach_status': 'detached'} instance, expected_conn_info = self._test_volume_attach( test_bdm, self.volume_bdm, volume, check_attach=False, driver_attach=True) self.mox.ReplayAll() test_bdm.attach(self.context, instance, self.volume_api, self.virt_driver, do_check_attach=False, do_driver_attach=True) self.assertThat(test_bdm['connection_info'], matchers.DictMatches(expected_conn_info))
def test_get_image_with_custom_prefix(self, _get_mocked): self.flags(osapi_compute_link_prefix='https://zoo.com:42', osapi_glance_link_prefix='http://circus.com:34') fake_req = self.http_request.blank(self.url_base + 'images/124') actual_image = self.controller.show(fake_req, '124') expected_image = self.expected_image_124 expected_image["image"]["links"][0]["href"] = ( "https://zoo.com:42%s/images/124" % self.url_base) expected_image["image"]["links"][1]["href"] = ( "https://zoo.com:42%s/images/124" % self.bookmark_base) expected_image["image"]["links"][2]["href"] = ( "http://circus.com:34/images/124") expected_image["image"]["server"]["links"][0]["href"] = ( "https://zoo.com:42%s/servers/%s" % (self.url_base, self.server_uuid)) expected_image["image"]["server"]["links"][1]["href"] = ( "https://zoo.com:42%s/servers/%s" % (self.bookmark_base, self.server_uuid)) self.assertThat(actual_image, matchers.DictMatches(expected_image))
def test_build_limits(self): expected_limits = { "limits": { "rate": [{ "uri": "*", "regex": ".*", "limit": [{ "value": 10, "verb": "POST", "remaining": 2, "unit": "MINUTE", "next-available": "2011-07-21T18:17:06Z" }] }, { "uri": "*/servers", "regex": "^/servers", "limit": [{ "value": 50, "verb": "POST", "remaining": 10, "unit": "DAY", "next-available": "2011-07-21T18:17:06Z" }] }], "absolute": { "maxServerMeta": 1, "maxImageMeta": 1, "maxPersonality": 5, "maxPersonalitySize": 5 } } } output = self.view_builder.build(self.rate_limits, self.absolute_limits) self.assertThat(output, matchers.DictMatches(expected_limits))
def test_list_consoles(self): def fake_get_consoles(cons_self, context, instance_id): self.assertEqual(instance_id, self.uuid) pool1 = dict(console_type='fake_type', public_hostname='fake_hostname') cons1 = dict(id=10, password='******', port='fake_port', pool=pool1) pool2 = dict(console_type='fake_type2', public_hostname='fake_hostname2') cons2 = dict(id=11, password='******', port='fake_port2', pool=pool2) return [cons1, cons2] expected = {'consoles': [{'console': {'id': 10, 'console_type': 'fake_type'}}, {'console': {'id': 11, 'console_type': 'fake_type2'}}]} self.stubs.Set(console.api.API, 'get_consoles', fake_get_consoles) req = fakes.HTTPRequest.blank(self.url) res_dict = self.controller.index(req, self.uuid) self.assertThat(res_dict, matchers.DictMatches(expected))
def stub_update_aggregate(context, aggregate, values): self.assertEqual(context, self.context, "context") self.assertEqual("1", aggregate, "aggregate") self.assertThat(body["set_metadata"]['metadata'], matchers.DictMatches(values)) return AGGREGATE
def test_get_image(self, get_mocked): request = self.http_request.blank(self.url_base + 'images/123') actual_image = self.controller.show(request, '123') self.assertThat(actual_image, matchers.DictMatches(self.expected_image_123)) get_mocked.assert_called_once_with(mock.ANY, '123')
def create(*args, **kwargs): self.assertThat( block_device.BlockDeviceDict(self.bdm[0]), matchers.DictMatches(kwargs['block_device_mapping'][0])) return old_create(*args, **kwargs)
def test_dict_conversion(self): host_state = fakes.FakeHostState('somehost', None, {}) host = weights.WeighedHost(host_state, 'someweight') expected = {'weight': 'someweight', 'host': 'somehost'} self.assertThat(host.to_dict(), matchers.DictMatches(expected))
class TestDictMatches(testtools.TestCase, helpers.TestMatchersInterface): matches_matcher = matchers.DictMatches({ 'foo': 'bar', 'baz': 'DONTCARE', 'cat': { 'tabby': True, 'fluffy': False } }) matches_matches = [ { 'foo': 'bar', 'baz': 'noox', 'cat': { 'tabby': True, 'fluffy': False } }, { 'foo': 'bar', 'baz': 'quux', 'cat': { 'tabby': True, 'fluffy': False } }, ] matches_mismatches = [ {}, { 'foo': 'bar', 'baz': 'qux' }, { 'foo': 'bop', 'baz': 'qux', 'cat': { 'tabby': True, 'fluffy': False } }, { 'foo': 'bar', 'baz': 'quux', 'cat': { 'tabby': True, 'fluffy': True } }, { 'foo': 'bar', 'cat': { 'tabby': True, 'fluffy': False } }, ] str_examples = [ ("DictMatches({'baz': 'DONTCARE', 'cat':" " {'fluffy': False, 'tabby': True}, 'foo': 'bar'})", matches_matcher), ] describe_examples = [ ("Keys in d1 and not d2: set(['foo', 'baz', 'cat'])." " Keys in d2 and not d1: set([])", {}, matches_matcher), ("Dictionaries do not match at fluffy. d1: False d2: True", { 'foo': 'bar', 'baz': 'quux', 'cat': { 'tabby': True, 'fluffy': True } }, matches_matcher), ("Dictionaries do not match at foo. d1: bar d2: bop", { 'foo': 'bop', 'baz': 'quux', 'cat': { 'tabby': True, 'fluffy': False } }, matches_matcher), ]