Sergio Cazzolato fc8579dfa8 Flavor ExtraSpecs containing '/' cannot be deleted
This change applies a regular expression in order to filter
flavor extraspects keys with invalid characters.
The characters allowed are: letters, numbers, underscores,
periods, colons, spaces and hyphens.
A new test flavor has been created which doesn't check the
keys in the post body. This flavor has been created in the
third place (instead of in the last) in order to keep
working existent test cases which depend on the last flavor
received in the get method.

Change-Id: Ifd86bed23a05a5946ae8b9ba6f6c9bf4b24b1d4c
Partial-Bug: #1256119
2014-01-31 17:38:19 -05:00

213 lines
8.3 KiB
Python

# Copyright (c) 2013, OpenStack
# All Rights Reserved.
#
# 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.
from novaclient import exceptions
from novaclient.tests import utils
from novaclient.tests.v1_1 import fakes
from novaclient.v1_1 import flavors
class FlavorsTest(utils.TestCase):
def setUp(self):
super(FlavorsTest, self).setUp()
self.cs = self._get_fake_client()
self.flavor_type = self._get_flavor_type()
def _get_fake_client(self):
return fakes.FakeClient()
def _get_flavor_type(self):
return flavors.Flavor
def test_list_flavors(self):
fl = self.cs.flavors.list()
self.cs.assert_called('GET', '/flavors/detail')
for flavor in fl:
self.assertIsInstance(flavor, self.flavor_type)
def test_list_flavors_undetailed(self):
fl = self.cs.flavors.list(detailed=False)
self.cs.assert_called('GET', '/flavors')
for flavor in fl:
self.assertIsInstance(flavor, self.flavor_type)
def test_list_flavors_is_public_none(self):
fl = self.cs.flavors.list(is_public=None)
self.cs.assert_called('GET', '/flavors/detail?is_public=None')
for flavor in fl:
self.assertIsInstance(flavor, self.flavor_type)
def test_list_flavors_is_public_false(self):
fl = self.cs.flavors.list(is_public=False)
self.cs.assert_called('GET', '/flavors/detail?is_public=False')
for flavor in fl:
self.assertIsInstance(flavor, self.flavor_type)
def test_list_flavors_is_public_true(self):
fl = self.cs.flavors.list(is_public=True)
self.cs.assert_called('GET', '/flavors/detail')
for flavor in fl:
self.assertIsInstance(flavor, self.flavor_type)
def test_get_flavor_details(self):
f = self.cs.flavors.get(1)
self.cs.assert_called('GET', '/flavors/1')
self.assertIsInstance(f, self.flavor_type)
self.assertEqual(f.ram, 256)
self.assertEqual(f.disk, 10)
self.assertEqual(f.ephemeral, 10)
self.assertEqual(f.is_public, True)
def test_get_flavor_details_alphanum_id(self):
f = self.cs.flavors.get('aa1')
self.cs.assert_called('GET', '/flavors/aa1')
self.assertIsInstance(f, self.flavor_type)
self.assertEqual(f.ram, 128)
self.assertEqual(f.disk, 0)
self.assertEqual(f.ephemeral, 0)
self.assertEqual(f.is_public, True)
def test_get_flavor_details_diablo(self):
f = self.cs.flavors.get(3)
self.cs.assert_called('GET', '/flavors/3')
self.assertIsInstance(f, self.flavor_type)
self.assertEqual(f.ram, 256)
self.assertEqual(f.disk, 10)
self.assertEqual(f.ephemeral, 'N/A')
self.assertEqual(f.is_public, 'N/A')
def test_find(self):
f = self.cs.flavors.find(ram=256)
self.cs.assert_called('GET', '/flavors/detail')
self.assertEqual(f.name, '256 MB Server')
f = self.cs.flavors.find(disk=0)
self.assertEqual(f.name, '128 MB Server')
self.assertRaises(exceptions.NotFound, self.cs.flavors.find,
disk=12345)
def _create_body(self, name, ram, vcpus, disk, ephemeral, id, swap,
rxtx_factor, is_public):
return {
"flavor": {
"name": name,
"ram": ram,
"vcpus": vcpus,
"disk": disk,
"OS-FLV-EXT-DATA:ephemeral": ephemeral,
"id": id,
"swap": swap,
"rxtx_factor": rxtx_factor,
"os-flavor-access:is_public": is_public,
}
}
def test_create(self):
f = self.cs.flavors.create("flavorcreate", 512, 1, 10, 1234,
ephemeral=10, is_public=False)
body = self._create_body("flavorcreate", 512, 1, 10, 10, 1234, 0, 1.0,
False)
self.cs.assert_called('POST', '/flavors', body)
self.assertIsInstance(f, self.flavor_type)
def test_create_with_id_as_string(self):
flavor_id = 'foobar'
f = self.cs.flavors.create("flavorcreate", 512,
1, 10, flavor_id, ephemeral=10,
is_public=False)
body = self._create_body("flavorcreate", 512, 1, 10, 10, flavor_id, 0,
1.0, False)
self.cs.assert_called('POST', '/flavors', body)
self.assertIsInstance(f, self.flavor_type)
def test_create_ephemeral_ispublic_defaults(self):
f = self.cs.flavors.create("flavorcreate", 512, 1, 10, 1234)
body = self._create_body("flavorcreate", 512, 1, 10, 0, 1234, 0,
1.0, True)
self.cs.assert_called('POST', '/flavors', body)
self.assertIsInstance(f, self.flavor_type)
def test_invalid_parameters_create(self):
self.assertRaises(exceptions.CommandError, self.cs.flavors.create,
"flavorcreate", "invalid", 1, 10, 1234, swap=0,
ephemeral=0, rxtx_factor=1.0, is_public=True)
self.assertRaises(exceptions.CommandError, self.cs.flavors.create,
"flavorcreate", 512, "invalid", 10, 1234, swap=0,
ephemeral=0, rxtx_factor=1.0, is_public=True)
self.assertRaises(exceptions.CommandError, self.cs.flavors.create,
"flavorcreate", 512, 1, "invalid", 1234, swap=0,
ephemeral=0, rxtx_factor=1.0, is_public=True)
self.assertRaises(exceptions.CommandError, self.cs.flavors.create,
"flavorcreate", 512, 1, 10, 1234, swap="invalid",
ephemeral=0, rxtx_factor=1.0, is_public=True)
self.assertRaises(exceptions.CommandError, self.cs.flavors.create,
"flavorcreate", 512, 1, 10, 1234, swap=0,
ephemeral="invalid", rxtx_factor=1.0, is_public=True)
self.assertRaises(exceptions.CommandError, self.cs.flavors.create,
"flavorcreate", 512, 1, 10, 1234, swap=0,
ephemeral=0, rxtx_factor="invalid", is_public=True)
self.assertRaises(exceptions.CommandError, self.cs.flavors.create,
"flavorcreate", 512, 1, 10, 1234, swap=0,
ephemeral=0, rxtx_factor=1.0, is_public='invalid')
def test_delete(self):
self.cs.flavors.delete("flavordelete")
self.cs.assert_called('DELETE', '/flavors/flavordelete')
def test_delete_with_flavor_instance(self):
f = self.cs.flavors.get(2)
self.cs.flavors.delete(f)
self.cs.assert_called('DELETE', '/flavors/2')
def test_delete_with_flavor_instance_method(self):
f = self.cs.flavors.get(2)
f.delete()
self.cs.assert_called('DELETE', '/flavors/2')
def test_set_keys(self):
f = self.cs.flavors.get(1)
f.set_keys({'k1': 'v1'})
self.cs.assert_called('POST', '/flavors/1/os-extra_specs',
{"extra_specs": {'k1': 'v1'}})
def test_set_with_valid_keys(self):
valid_keys = ['key4', 'month.price', 'I-Am:AK-ey.44-',
'key with spaces and _']
f = self.cs.flavors.get(4)
for key in valid_keys:
f.set_keys({key: 'v4'})
self.cs.assert_called('POST', '/flavors/4/os-extra_specs',
{"extra_specs": {key: 'v4'}})
def test_set_with_invalid_keys(self):
invalid_keys = ['/1', '?1', '%1', '<', '>']
f = self.cs.flavors.get(1)
for key in invalid_keys:
self.assertRaises(exceptions.CommandError, f.set_keys, {key: 'v1'})
def test_unset_keys(self):
f = self.cs.flavors.get(1)
f.unset_keys(['k1'])
self.cs.assert_called('DELETE', '/flavors/1/os-extra_specs/k1')