Migrate final flavor tests to requests_mock

There were some tests in test_shade that should really be in
test_flavors. Move them there, then transition them to requests_mock. To
support the actions they need, add some additional flavors to the fake
flavor list.

Change-Id: Ide92fb0a926ba69b03132205f1467816844377d8
This commit is contained in:
Monty Taylor 2017-01-31 09:56:58 -06:00
parent 7ca9d762f1
commit 2e79cffb98
4 changed files with 107 additions and 82 deletions

View File

@ -18,30 +18,40 @@ Fakes used for testing
"""
FLAVOR_ID = '0c1d9008-f546-4608-9e8f-f8bdaec8dddd'
FLAVOR_ID = u'0c1d9008-f546-4608-9e8f-f8bdaec8dddd'
CHOCOLATE_FLAVOR_ID = u'0c1d9008-f546-4608-9e8f-f8bdaec8ddde'
STRAWBERRY_FLAVOR_ID = u'0c1d9008-f546-4608-9e8f-f8bdaec8dddf'
ENDPOINT = 'https://compute.example.com/v2.1/1c36b64c840a42cd9e9b931a369337f0'
FAKE_FLAVOR = {
u'OS-FLV-DISABLED:disabled': False,
u'OS-FLV-EXT-DATA:ephemeral': 0,
u'disk': 1600,
u'id': u'0c1d9008-f546-4608-9e8f-f8bdaec8dddd',
u'links': [{
u'href': u'{endpoint}/flavors/{id}'.format(
endpoint=ENDPOINT, id=FLAVOR_ID),
u'rel': u'self'
}, {
u'href': u'{endpoint}/flavors/{id}'.format(
endpoint=ENDPOINT, id=FLAVOR_ID),
u'rel': u'bookmark'
}],
u'name': u'vanilla',
u'os-flavor-access:is_public': True,
u'ram': 65536,
u'rxtx_factor': 1.0,
u'swap': u'',
u'vcpus': 24
}
FAKE_FLAVOR_LIST = [FAKE_FLAVOR]
def make_fake_flavor(flavor_id, name, ram=100, disk=1600, vcpus=24):
return {
u'OS-FLV-DISABLED:disabled': False,
u'OS-FLV-EXT-DATA:ephemeral': 0,
u'disk': disk,
u'id': flavor_id,
u'links': [{
u'href': u'{endpoint}/flavors/{id}'.format(
endpoint=ENDPOINT, id=flavor_id),
u'rel': u'self'
}, {
u'href': u'{endpoint}/flavors/{id}'.format(
endpoint=ENDPOINT, id=flavor_id),
u'rel': u'bookmark'
}],
u'name': name,
u'os-flavor-access:is_public': True,
u'ram': ram,
u'rxtx_factor': 1.0,
u'swap': u'',
u'vcpus': vcpus
}
FAKE_FLAVOR = make_fake_flavor(FLAVOR_ID, 'vanilla')
FAKE_CHOCOLATE_FLAVOR = make_fake_flavor(
CHOCOLATE_FLAVOR_ID, 'chocolate', ram=200)
FAKE_STRAWBERRY_FLAVOR = make_fake_flavor(
STRAWBERRY_FLAVOR_ID, 'strawberry', ram=300)
FAKE_FLAVOR_LIST = [FAKE_FLAVOR, FAKE_CHOCOLATE_FLAVOR, FAKE_STRAWBERRY_FLAVOR]
class FakeEndpoint(object):

View File

@ -309,18 +309,20 @@ class TestMemoryCache(base.RequestsMockTestCase):
'GET', '{endpoint}/flavors/detail?is_public=None'.format(
endpoint=fakes.ENDPOINT),
json={'flavors': fakes.FAKE_FLAVOR_LIST})
self.register_uri(
'GET', '{endpoint}/flavors/{id}/os-extra_specs'.format(
endpoint=fakes.ENDPOINT, id=fakes.FLAVOR_ID),
json={'extra_specs': {}})
for flavor in fakes.FAKE_FLAVOR_LIST:
self.register_uri(
'GET', '{endpoint}/flavors/{id}/os-extra_specs'.format(
endpoint=fakes.ENDPOINT, id=flavor['id']),
json={'extra_specs': {}})
self.assertEqual([], self.cloud.list_flavors())
self.assertEqual([], self.cloud.list_flavors())
fake_flavor_dict = self.cloud._normalize_flavor(fakes.FAKE_FLAVOR)
fake_flavor_dicts = self.cloud._normalize_flavors(
fakes.FAKE_FLAVOR_LIST)
self.cloud.list_flavors.invalidate(self.cloud)
self.assertEqual([fake_flavor_dict], self.cloud.list_flavors())
self.assertEqual(fake_flavor_dicts, self.cloud.list_flavors())
self.assert_calls()

View File

@ -63,7 +63,7 @@ class TestFlavors(base.RequestsMockTestCase):
self.register_uri(
'GET', '{endpoint}/flavors/detail?is_public=None'.format(
endpoint=fakes.ENDPOINT),
json={'flavors': []})
json={'flavors': fakes.FAKE_FLAVOR_LIST})
self.assertFalse(self.op_cloud.delete_flavor('invalid'))
@ -86,10 +86,11 @@ class TestFlavors(base.RequestsMockTestCase):
'GET', '{endpoint}/flavors/detail?is_public=None'.format(
endpoint=fakes.ENDPOINT),
json={'flavors': fakes.FAKE_FLAVOR_LIST})
self.register_uri(
'GET', '{endpoint}/flavors/{id}/os-extra_specs'.format(
endpoint=fakes.ENDPOINT, id=fakes.FLAVOR_ID),
json={'extra_specs': {}})
for flavor in fakes.FAKE_FLAVOR_LIST:
self.register_uri(
'GET', '{endpoint}/flavors/{id}/os-extra_specs'.format(
endpoint=fakes.ENDPOINT, id=flavor['id']),
json={'extra_specs': {}})
flavors = self.cloud.list_flavors()
@ -106,6 +107,66 @@ class TestFlavors(base.RequestsMockTestCase):
self.assertTrue(needed_keys.issubset(flavor.keys()))
self.assert_calls()
def test_get_flavor_by_ram(self):
self.register_uri(
'GET', '{endpoint}/flavors/detail?is_public=None'.format(
endpoint=fakes.ENDPOINT),
json={'flavors': fakes.FAKE_FLAVOR_LIST})
for flavor in fakes.FAKE_FLAVOR_LIST:
self.register_uri(
'GET', '{endpoint}/flavors/{id}/os-extra_specs'.format(
endpoint=fakes.ENDPOINT, id=flavor['id']),
json={'extra_specs': {}})
flavor = self.cloud.get_flavor_by_ram(ram=250)
self.assertEqual(fakes.STRAWBERRY_FLAVOR_ID, flavor['id'])
def test_get_flavor_by_ram_and_include(self):
self.register_uri(
'GET', '{endpoint}/flavors/detail?is_public=None'.format(
endpoint=fakes.ENDPOINT),
json={'flavors': fakes.FAKE_FLAVOR_LIST})
for flavor in fakes.FAKE_FLAVOR_LIST:
self.register_uri(
'GET', '{endpoint}/flavors/{id}/os-extra_specs'.format(
endpoint=fakes.ENDPOINT, id=flavor['id']),
json={'extra_specs': {}})
flavor = self.cloud.get_flavor_by_ram(ram=150, include='strawberry')
self.assertEqual(fakes.STRAWBERRY_FLAVOR_ID, flavor['id'])
def test_get_flavor_by_ram_not_found(self):
self.register_uri(
'GET', '{endpoint}/flavors/detail?is_public=None'.format(
endpoint=fakes.ENDPOINT),
json={'flavors': []})
self.assertRaises(
shade.OpenStackCloudException,
self.cloud.get_flavor_by_ram,
ram=100)
def test_get_flavor_string_and_int(self):
self.register_uri(
'GET', '{endpoint}/flavors/detail?is_public=None'.format(
endpoint=fakes.ENDPOINT),
json={'flavors': [fakes.make_fake_flavor('1', 'vanilla')]})
self.register_uri(
'GET', '{endpoint}/flavors/1/os-extra_specs'.format(
endpoint=fakes.ENDPOINT),
json={'extra_specs': {}})
self.register_uri(
'GET', '{endpoint}/flavors/detail?is_public=None'.format(
endpoint=fakes.ENDPOINT),
json={'flavors': [fakes.make_fake_flavor('1', 'vanilla')]})
self.register_uri(
'GET', '{endpoint}/flavors/1/os-extra_specs'.format(
endpoint=fakes.ENDPOINT),
json={'extra_specs': {}})
flavor1 = self.cloud.get_flavor('1')
self.assertEqual('1', flavor1['id'])
flavor2 = self.cloud.get_flavor(1)
self.assertEqual('1', flavor2['id'])
def test_set_flavor_specs(self):
extra_specs = dict(key1='value1')
self.register_uri(

View File

@ -512,54 +512,6 @@ class TestShade(base.TestCase):
self.cloud.update_subnet,
'456', gateway_ip=gateway, disable_gateway_ip=True)
@mock.patch.object(shade.OpenStackCloud, '_compute_client')
@mock.patch.object(shade.OpenStackCloud, 'nova_client')
def test_get_flavor_by_ram(self, mock_nova_client, mock_compute):
vanilla = fakes.FakeFlavor('1', 'vanilla ice cream', 100)
chocolate = fakes.FakeFlavor('1', 'chocolate ice cream', 200)
mock_nova_client.flavors.list.return_value = [vanilla, chocolate]
mock_response = mock.Mock()
mock_response.json.return_value = dict(extra_specs=[])
mock_compute.get.return_value = mock_response
flavor = self.cloud.get_flavor_by_ram(ram=150)
self.assertEqual(chocolate.id, flavor['id'])
@mock.patch.object(shade.OpenStackCloud, '_compute_client')
@mock.patch.object(shade.OpenStackCloud, 'nova_client')
def test_get_flavor_by_ram_and_include(
self, mock_nova_client, mock_compute):
vanilla = fakes.FakeFlavor('1', 'vanilla ice cream', 100)
chocolate = fakes.FakeFlavor('2', 'chocoliate ice cream', 200)
strawberry = fakes.FakeFlavor('3', 'strawberry ice cream', 250)
mock_response = mock.Mock()
mock_response.json.return_value = dict(extra_specs=[])
mock_compute.get.return_value = mock_response
mock_nova_client.flavors.list.return_value = [
vanilla, chocolate, strawberry]
flavor = self.cloud.get_flavor_by_ram(ram=150, include='strawberry')
self.assertEqual(strawberry.id, flavor['id'])
@mock.patch.object(shade.OpenStackCloud, 'nova_client')
def test_get_flavor_by_ram_not_found(self, mock_nova_client):
mock_nova_client.flavors.list.return_value = []
self.assertRaises(shade.OpenStackCloudException,
self.cloud.get_flavor_by_ram,
ram=100)
@mock.patch.object(shade.OpenStackCloud, '_compute_client')
@mock.patch.object(shade.OpenStackCloud, 'nova_client')
def test_get_flavor_string_and_int(
self, mock_nova_client, mock_compute):
vanilla = fakes.FakeFlavor('1', 'vanilla ice cream', 100)
mock_nova_client.flavors.list.return_value = [vanilla]
mock_response = mock.Mock()
mock_response.json.return_value = dict(extra_specs=[])
mock_compute.get.return_value = mock_response
flavor1 = self.cloud.get_flavor('1')
self.assertEqual(vanilla.id, flavor1['id'])
flavor2 = self.cloud.get_flavor(1)
self.assertEqual(vanilla.id, flavor2['id'])
def test__neutron_exceptions_resource_not_found(self):
with mock.patch.object(
shade._tasks, 'NetworkList',