Move flavor cache tests to requests_mock
Move the fake flavors from test_flavors to fakes so we can reuse them. This also fixes two small bugs that were found. extra_specs were Munch if fetched from nova and plain dict if they were defaulted. Also, if there was a problem fetching extra_specs, we defaulted them to empty list which is just plain wrong. Also, we were letting the request_ids attribute sneak through. Change-Id: I8ecf05580c557b21d123097e1f4be5c5664d366c
This commit is contained in:
parent
32d53d58ce
commit
7ca9d762f1
@ -161,6 +161,7 @@ class Normalizer(object):
|
||||
flavor.pop('NAME_ATTR', None)
|
||||
flavor.pop('HUMAN_ID', None)
|
||||
flavor.pop('human_id', None)
|
||||
flavor.pop('request_ids', None)
|
||||
|
||||
ephemeral = int(_pop_or_get(
|
||||
flavor, 'OS-FLV-EXT-DATA:ephemeral', 0, self.strict_mode))
|
||||
@ -173,6 +174,7 @@ class Normalizer(object):
|
||||
extra_specs = _pop_or_get(
|
||||
flavor, 'OS-FLV-WITH-EXT-SPECS:extra_specs', {}, self.strict_mode)
|
||||
extra_specs = flavor.pop('extra_specs', extra_specs)
|
||||
extra_specs = munch.Munch(extra_specs)
|
||||
|
||||
new_flavor['location'] = self.current_location
|
||||
new_flavor['id'] = flavor.pop('id')
|
||||
|
@ -1756,7 +1756,7 @@ class OpenStackCloud(_normalize.Normalizer):
|
||||
try:
|
||||
flavor.extra_specs = self._compute_client.get(endpoint)
|
||||
except OpenStackCloudHTTPError as e:
|
||||
flavor.extra_specs = []
|
||||
flavor.extra_specs = {}
|
||||
self.log.debug(
|
||||
'Fetching extra specs for flavor failed:'
|
||||
' %(msg)s', {'msg': str(e)})
|
||||
|
@ -18,6 +18,32 @@ Fakes used for testing
|
||||
"""
|
||||
|
||||
|
||||
FLAVOR_ID = '0c1d9008-f546-4608-9e8f-f8bdaec8dddd'
|
||||
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]
|
||||
|
||||
|
||||
class FakeEndpoint(object):
|
||||
def __init__(self, id, service_id, region, publicurl, internalurl=None,
|
||||
adminurl=None):
|
||||
|
@ -91,7 +91,7 @@ _TASK_SCHEMA = dict(
|
||||
)
|
||||
|
||||
|
||||
class TestMemoryCache(base.TestCase):
|
||||
class TestMemoryCache(base.RequestsMockTestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(TestMemoryCache, self).setUp(
|
||||
@ -255,6 +255,7 @@ class TestMemoryCache(base.TestCase):
|
||||
|
||||
@mock.patch.object(shade.OpenStackCloud, 'keystone_client')
|
||||
def test_modify_user_invalidates_cache(self, keystone_mock):
|
||||
self.use_keystone_v2()
|
||||
fake_user = fakes.FakeUser('abc123', 'abc123@domain.test',
|
||||
'abc123 name')
|
||||
# first cache an empty list
|
||||
@ -298,22 +299,31 @@ class TestMemoryCache(base.TestCase):
|
||||
self.assertEqual([], self.cloud.list_users())
|
||||
self.assertTrue(keystone_mock.users.delete.was_called)
|
||||
|
||||
@mock.patch.object(shade.OpenStackCloud, '_compute_client')
|
||||
@mock.patch.object(shade.OpenStackCloud, 'nova_client')
|
||||
def test_list_flavors(self, nova_mock, mock_compute):
|
||||
# TODO(mordred) Change this to request_mock
|
||||
nova_mock.flavors.list.return_value = []
|
||||
nova_mock.flavors.api.client.get.return_value = {}
|
||||
mock_compute.get.return_value = {}
|
||||
def test_list_flavors(self):
|
||||
self.register_uri(
|
||||
'GET', '{endpoint}/flavors/detail?is_public=None'.format(
|
||||
endpoint=fakes.ENDPOINT),
|
||||
json={'flavors': []})
|
||||
|
||||
self.register_uri(
|
||||
'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': {}})
|
||||
|
||||
self.assertEqual([], self.cloud.list_flavors())
|
||||
|
||||
fake_flavor = fakes.FakeFlavor('555', 'vanilla', 100)
|
||||
fake_flavor_dict = self.cloud._normalize_flavor(
|
||||
meta.obj_to_dict(fake_flavor))
|
||||
nova_mock.flavors.list.return_value = [fake_flavor]
|
||||
self.assertEqual([], self.cloud.list_flavors())
|
||||
|
||||
fake_flavor_dict = self.cloud._normalize_flavor(fakes.FAKE_FLAVOR)
|
||||
self.cloud.list_flavors.invalidate(self.cloud)
|
||||
self.assertEqual([fake_flavor_dict], self.cloud.list_flavors())
|
||||
|
||||
self.assert_calls()
|
||||
|
||||
@mock.patch.object(shade.OpenStackCloud, '_image_client')
|
||||
def test_list_images(self, mock_image_client):
|
||||
mock_image_client.get.return_value = []
|
||||
|
@ -12,33 +12,9 @@
|
||||
|
||||
|
||||
import shade
|
||||
from shade.tests import fakes
|
||||
from shade.tests.unit import base
|
||||
|
||||
FLAVOR_ID = '0c1d9008-f546-4608-9e8f-f8bdaec8dddd'
|
||||
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]
|
||||
|
||||
|
||||
class TestFlavors(base.RequestsMockTestCase):
|
||||
|
||||
@ -46,8 +22,8 @@ class TestFlavors(base.RequestsMockTestCase):
|
||||
|
||||
self.register_uri(
|
||||
'POST', '{endpoint}/flavors'.format(
|
||||
endpoint=ENDPOINT),
|
||||
json={'flavor': FAKE_FLAVOR},
|
||||
endpoint=fakes.ENDPOINT),
|
||||
json={'flavor': fakes.FAKE_FLAVOR},
|
||||
validate=dict(
|
||||
json={'flavor': {
|
||||
"name": "vanilla",
|
||||
@ -63,8 +39,8 @@ class TestFlavors(base.RequestsMockTestCase):
|
||||
|
||||
self.register_uri(
|
||||
'GET', '{endpoint}/flavors/{id}'.format(
|
||||
endpoint=ENDPOINT, id=FLAVOR_ID),
|
||||
json={'flavor': FAKE_FLAVOR})
|
||||
endpoint=fakes.ENDPOINT, id=fakes.FLAVOR_ID),
|
||||
json={'flavor': fakes.FAKE_FLAVOR})
|
||||
|
||||
self.op_cloud.create_flavor(
|
||||
'vanilla', ram=65536, disk=1600, vcpus=24,
|
||||
@ -74,11 +50,11 @@ class TestFlavors(base.RequestsMockTestCase):
|
||||
def test_delete_flavor(self):
|
||||
self.register_uri(
|
||||
'GET', '{endpoint}/flavors/detail?is_public=None'.format(
|
||||
endpoint=ENDPOINT),
|
||||
json={'flavors': FAKE_FLAVOR_LIST})
|
||||
endpoint=fakes.ENDPOINT),
|
||||
json={'flavors': fakes.FAKE_FLAVOR_LIST})
|
||||
self.register_uri(
|
||||
'DELETE', '{endpoint}/flavors/{id}'.format(
|
||||
endpoint=ENDPOINT, id=FLAVOR_ID))
|
||||
endpoint=fakes.ENDPOINT, id=fakes.FLAVOR_ID))
|
||||
self.assertTrue(self.op_cloud.delete_flavor('vanilla'))
|
||||
|
||||
self.assert_calls()
|
||||
@ -86,7 +62,7 @@ class TestFlavors(base.RequestsMockTestCase):
|
||||
def test_delete_flavor_not_found(self):
|
||||
self.register_uri(
|
||||
'GET', '{endpoint}/flavors/detail?is_public=None'.format(
|
||||
endpoint=ENDPOINT),
|
||||
endpoint=fakes.ENDPOINT),
|
||||
json={'flavors': []})
|
||||
|
||||
self.assertFalse(self.op_cloud.delete_flavor('invalid'))
|
||||
@ -96,11 +72,11 @@ class TestFlavors(base.RequestsMockTestCase):
|
||||
def test_delete_flavor_exception(self):
|
||||
self.register_uri(
|
||||
'GET', '{endpoint}/flavors/detail?is_public=None'.format(
|
||||
endpoint=ENDPOINT),
|
||||
json={'flavors': FAKE_FLAVOR_LIST})
|
||||
endpoint=fakes.ENDPOINT),
|
||||
json={'flavors': fakes.FAKE_FLAVOR_LIST})
|
||||
self.register_uri(
|
||||
'DELETE', '{endpoint}/flavors/{id}'.format(
|
||||
endpoint=ENDPOINT, id=FLAVOR_ID),
|
||||
endpoint=fakes.ENDPOINT, id=fakes.FLAVOR_ID),
|
||||
status_code=503)
|
||||
self.assertRaises(shade.OpenStackCloudException,
|
||||
self.op_cloud.delete_flavor, 'vanilla')
|
||||
@ -108,11 +84,11 @@ class TestFlavors(base.RequestsMockTestCase):
|
||||
def test_list_flavors(self):
|
||||
self.register_uri(
|
||||
'GET', '{endpoint}/flavors/detail?is_public=None'.format(
|
||||
endpoint=ENDPOINT),
|
||||
json={'flavors': FAKE_FLAVOR_LIST})
|
||||
endpoint=fakes.ENDPOINT),
|
||||
json={'flavors': fakes.FAKE_FLAVOR_LIST})
|
||||
self.register_uri(
|
||||
'GET', '{endpoint}/flavors/{id}/os-extra_specs'.format(
|
||||
endpoint=ENDPOINT, id=FLAVOR_ID),
|
||||
endpoint=fakes.ENDPOINT, id=fakes.FLAVOR_ID),
|
||||
json={'extra_specs': {}})
|
||||
|
||||
flavors = self.cloud.list_flavors()
|
||||
@ -134,7 +110,7 @@ class TestFlavors(base.RequestsMockTestCase):
|
||||
extra_specs = dict(key1='value1')
|
||||
self.register_uri(
|
||||
'POST', '{endpoint}/flavors/{id}/os-extra_specs'.format(
|
||||
endpoint=ENDPOINT, id=1),
|
||||
endpoint=fakes.ENDPOINT, id=1),
|
||||
json=dict(extra_specs=extra_specs))
|
||||
|
||||
self.op_cloud.set_flavor_specs(1, extra_specs)
|
||||
@ -146,7 +122,7 @@ class TestFlavors(base.RequestsMockTestCase):
|
||||
self.register_uri(
|
||||
'DELETE',
|
||||
'{endpoint}/flavors/{id}/os-extra_specs/{key}'.format(
|
||||
endpoint=ENDPOINT, id=1, key=key))
|
||||
endpoint=fakes.ENDPOINT, id=1, key=key))
|
||||
|
||||
self.op_cloud.unset_flavor_specs(1, keys)
|
||||
self.assert_calls()
|
||||
@ -154,7 +130,7 @@ class TestFlavors(base.RequestsMockTestCase):
|
||||
def test_add_flavor_access(self):
|
||||
self.register_uri(
|
||||
'POST', '{endpoint}/flavors/{id}/action'.format(
|
||||
endpoint=ENDPOINT, id='flavor_id'),
|
||||
endpoint=fakes.ENDPOINT, id='flavor_id'),
|
||||
json={
|
||||
'flavor_access': [{
|
||||
'flavor_id': 'flavor_id',
|
||||
@ -172,7 +148,7 @@ class TestFlavors(base.RequestsMockTestCase):
|
||||
def test_remove_flavor_access(self):
|
||||
self.register_uri(
|
||||
'POST', '{endpoint}/flavors/{id}/action'.format(
|
||||
endpoint=ENDPOINT, id='flavor_id'),
|
||||
endpoint=fakes.ENDPOINT, id='flavor_id'),
|
||||
json={'flavor_access': []},
|
||||
validate=dict(
|
||||
json={
|
||||
@ -186,7 +162,7 @@ class TestFlavors(base.RequestsMockTestCase):
|
||||
def test_list_flavor_access(self):
|
||||
self.register_uri(
|
||||
'GET', '{endpoint}/flavors/vanilla/os-flavor-access'.format(
|
||||
endpoint=ENDPOINT),
|
||||
endpoint=fakes.ENDPOINT),
|
||||
json={
|
||||
'flavor_access': [{
|
||||
'flavor_id': 'vanilla',
|
||||
|
@ -218,10 +218,8 @@ class TestUtils(base.TestCase):
|
||||
u'disk_io_index': u'40',
|
||||
u'number_of_data_disks': u'1',
|
||||
u'policy_class': u'performance_flavor',
|
||||
u'resize_policy_class': u'performance_flavor'},
|
||||
'request_ids': []},
|
||||
u'resize_policy_class': u'performance_flavor'}},
|
||||
'ram': 8192,
|
||||
'request_ids': [],
|
||||
'rxtx_factor': 1600.0,
|
||||
'swap': 0,
|
||||
'vcpus': 8}
|
||||
@ -252,8 +250,7 @@ class TestUtils(base.TestCase):
|
||||
'region_name': u'RegionOne',
|
||||
'zone': None},
|
||||
'name': u'8 GB Performance',
|
||||
'properties': {
|
||||
'request_ids': []},
|
||||
'properties': {},
|
||||
'ram': 8192,
|
||||
'rxtx_factor': 1600.0,
|
||||
'swap': 0,
|
||||
|
Loading…
x
Reference in New Issue
Block a user