David Shrewsbury 55db0a565c Bug fix: Make set/unset of flavor specs work again
The 1.2.0 release broke the API methods to set and unset
flavor extra specs because we need the raw object, as returned
from the nova client, to make method calls to change those
values. As a result, a new 'raw' parameter was added to the
submitTask() method of TaskManager to allow us to get these
raw objects back.

Additionally, we were never displaying the 'extra_specs' for
a flavor. This, too, requires a raw object method call.

Flavors are now normalized to remove client cruft and make sure
that 'extra_specs' is always an attribute.

As if that weren't enough, we now do functional tests for these
things! What more could one ask for???

Change-Id: Ie5c132317392cf26df2c8f43e9f07d040119eca0
2016-03-19 13:24:39 -05:00

161 lines
5.9 KiB
Python

# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
#
# See the License for the specific language governing permissions and
# limitations under the License.
"""
test_flavor
----------------------------------
Functional tests for `shade` flavor resource.
"""
import shade
from shade.exc import OpenStackCloudException
from shade.tests import base
class TestFlavor(base.TestCase):
def setUp(self):
super(TestFlavor, self).setUp()
self.demo_cloud = shade.openstack_cloud(cloud='devstack')
self.operator_cloud = shade.operator_cloud(cloud='devstack-admin')
# Generate a random name for flavors in this test
self.new_item_name = self.getUniqueString('flavor')
self.addCleanup(self._cleanup_flavors)
def _cleanup_flavors(self):
exception_list = list()
for f in self.operator_cloud.list_flavors():
if f['name'].startswith(self.new_item_name):
try:
self.operator_cloud.delete_flavor(f['id'])
except Exception as e:
# We were unable to delete a flavor, let's try with next
exception_list.append(str(e))
continue
if exception_list:
# Raise an error: we must make users aware that something went
# wrong
raise OpenStackCloudException('\n'.join(exception_list))
def test_create_flavor(self):
flavor_name = self.new_item_name + '_create'
flavor_kwargs = dict(
name=flavor_name, ram=1024, vcpus=2, disk=10, ephemeral=5,
swap=100, rxtx_factor=1.5, is_public=True
)
flavor = self.operator_cloud.create_flavor(**flavor_kwargs)
self.assertIsNotNone(flavor['id'])
# When properly normalized, we should always get an extra_specs
# and expect empty dict on create.
self.assertIn('extra_specs', flavor)
self.assertEqual({}, flavor['extra_specs'])
for key in flavor_kwargs.keys():
self.assertIn(key, flavor)
for key, value in flavor_kwargs.items():
self.assertEqual(value, flavor[key])
def test_list_flavors(self):
pub_flavor_name = self.new_item_name + '_public'
priv_flavor_name = self.new_item_name + '_private'
public_kwargs = dict(
name=pub_flavor_name, ram=1024, vcpus=2, disk=10, is_public=True
)
private_kwargs = dict(
name=priv_flavor_name, ram=1024, vcpus=2, disk=10, is_public=False
)
# Create a public and private flavor. We expect both to be listed
# for an operator.
self.operator_cloud.create_flavor(**public_kwargs)
self.operator_cloud.create_flavor(**private_kwargs)
flavors = self.operator_cloud.list_flavors()
# Flavor list will include the standard devstack flavors. We just want
# to make sure both of the flavors we just created are present.
found = []
for f in flavors:
# extra_specs should be added within list_flavors()
self.assertIn('extra_specs', f)
if f['name'] in (pub_flavor_name, priv_flavor_name):
found.append(f)
self.assertEqual(2, len(found))
def test_flavor_access(self):
priv_flavor_name = self.new_item_name + '_private'
private_kwargs = dict(
name=priv_flavor_name, ram=1024, vcpus=2, disk=10, is_public=False
)
new_flavor = self.operator_cloud.create_flavor(**private_kwargs)
# Validate the 'demo' user cannot see the new flavor
flavors = self.demo_cloud.search_flavors(priv_flavor_name)
self.assertEqual(0, len(flavors))
# We need the tenant ID for the 'demo' user
project = self.operator_cloud.get_project('demo')
self.assertIsNotNone(project)
# Now give 'demo' access
self.operator_cloud.add_flavor_access(new_flavor['id'], project['id'])
# Now see if the 'demo' user has access to it
flavors = self.demo_cloud.search_flavors(priv_flavor_name)
self.assertEqual(1, len(flavors))
self.assertEqual(priv_flavor_name, flavors[0]['name'])
# Now revoke the access and make sure we can't find it
self.operator_cloud.remove_flavor_access(new_flavor['id'],
project['id'])
flavors = self.demo_cloud.search_flavors(priv_flavor_name)
self.assertEqual(0, len(flavors))
def test_set_unset_flavor_specs(self):
"""
Test setting and unsetting flavor extra specs
"""
flavor_name = self.new_item_name + '_spec_test'
kwargs = dict(
name=flavor_name, ram=1024, vcpus=2, disk=10
)
new_flavor = self.operator_cloud.create_flavor(**kwargs)
# Expect no extra_specs
self.assertEqual({}, new_flavor['extra_specs'])
# Now set them
extra_specs = {'foo': 'aaa', 'bar': 'bbb'}
self.operator_cloud.set_flavor_specs(new_flavor['id'], extra_specs)
mod_flavor = self.operator_cloud.get_flavor(new_flavor['id'])
# Verify extra_specs were set
self.assertIn('extra_specs', mod_flavor)
self.assertEqual(extra_specs, mod_flavor['extra_specs'])
# Unset the 'foo' value
self.operator_cloud.unset_flavor_specs(mod_flavor['id'], ['foo'])
mod_flavor = self.operator_cloud.get_flavor(new_flavor['id'])
# Verify 'foo' is unset and 'bar' is still set
self.assertEqual({'bar': 'bbb'}, mod_flavor['extra_specs'])