Skip to content

Commit 324da05

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Add missing tests for flavor extra_specs mv 2.61"
2 parents 6c05a0d + 89f6d39 commit 324da05

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

nova/api/openstack/compute/flavor_manage.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,10 @@ def _create(self, req, body):
9191
req, flavors_view.FLAVOR_EXTRA_SPECS_MICROVERSION):
9292
include_extra_specs = context.can(
9393
fes_policies.POLICY_ROOT % 'index', fatal=False)
94-
# NOTE(yikun): This empty extra_spec only for keeping consistent
95-
# with other related flavor api.
94+
# NOTE(yikun): This empty extra_specs only for keeping consistent
95+
# with PUT and GET flavor APIs. extra_specs in flavor is added
96+
# after creating the flavor so to avoid the error in _view_builder
97+
# flavor.extra_specs is populated with the empty string.
9698
flavor.extra_specs = {}
9799

98100
return self._view_builder.show(req, flavor, include_description,

nova/tests/unit/api/openstack/compute/test_flavor_manage.py

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
# License for the specific language governing permissions and limitations
1414
# under the License.
1515

16+
import copy
17+
1618
import mock
1719
from oslo_serialization import jsonutils
1820
import six
@@ -341,6 +343,17 @@ def test_flavor_update_description(self):
341343
class FlavorManageTestV2_55(FlavorManageTestV21):
342344
microversion = '2.55'
343345

346+
def get_flavor(self, flavor):
347+
return objects.Flavor(
348+
flavorid=flavor['id'], name=flavor['name'],
349+
memory_mb=flavor['ram'], vcpus=flavor['vcpus'],
350+
root_gb=flavor['disk'], swap=flavor['swap'],
351+
ephemeral_gb=flavor['OS-FLV-EXT-DATA:ephemeral'],
352+
disabled=flavor['OS-FLV-DISABLED:disabled'],
353+
is_public=flavor['os-flavor-access:is_public'],
354+
rxtx_factor=flavor['rxtx_factor'],
355+
description=flavor['description'])
356+
344357
def setUp(self):
345358
super(FlavorManageTestV2_55, self).setUp()
346359
# Send a description in POST /flavors requests.
@@ -357,15 +370,7 @@ def test_flavor_update_description(self, mock_flavor_save, mock_get):
357370
# First create a flavor.
358371
flavor = self._create_flavor_success_case(self.request_body)['flavor']
359372
self.assertEqual('test description', flavor['description'])
360-
mock_get.return_value = objects.Flavor(
361-
flavorid=flavor['id'], name=flavor['name'],
362-
memory_mb=flavor['ram'], vcpus=flavor['vcpus'],
363-
root_gb=flavor['disk'], swap=flavor['swap'],
364-
ephemeral_gb=flavor['OS-FLV-EXT-DATA:ephemeral'],
365-
disabled=flavor['OS-FLV-DISABLED:disabled'],
366-
is_public=flavor['os-flavor-access:is_public'],
367-
rxtx_factor=flavor['rxtx_factor'],
368-
description=flavor['description'])
373+
mock_get.return_value = self.get_flavor(flavor)
369374
# Now null out the flavor description.
370375
flavor = self.controller._update(
371376
self._get_http_request(), flavor['id'],
@@ -428,6 +433,39 @@ def test_update_with_invalid_description(self, mock_flavor_save, mock_get):
428433
body={'flavor': {'description': description}})
429434

430435

436+
class FlavorManageTestV2_61(FlavorManageTestV2_55):
437+
"""Run the same tests as we would for v2.55 but with a extra_specs."""
438+
microversion = '2.61'
439+
440+
def get_flavor(self, flavor):
441+
return objects.Flavor(
442+
flavorid=flavor['id'], name=flavor['name'],
443+
memory_mb=flavor['ram'], vcpus=flavor['vcpus'],
444+
root_gb=flavor['disk'], swap=flavor['swap'],
445+
ephemeral_gb=flavor['OS-FLV-EXT-DATA:ephemeral'],
446+
disabled=flavor['OS-FLV-DISABLED:disabled'],
447+
is_public=flavor['os-flavor-access:is_public'],
448+
rxtx_factor=flavor['rxtx_factor'],
449+
description=flavor['description'],
450+
extra_specs={"key1": "value1"})
451+
452+
def setUp(self):
453+
super(FlavorManageTestV2_61, self).setUp()
454+
self.expected_flavor = copy.deepcopy(self.request_body)
455+
self.expected_flavor['flavor']['extra_specs'] = {}
456+
457+
@mock.patch('nova.objects.Flavor.get_by_flavor_id')
458+
@mock.patch('nova.objects.Flavor.save')
459+
def test_flavor_update_extra_spec(self, mock_flavor_save, mock_get):
460+
# First create a flavor.
461+
flavor = self._create_flavor_success_case(self.request_body)['flavor']
462+
mock_get.return_value = self.get_flavor(flavor)
463+
flavor = self.controller._update(
464+
self._get_http_request(), flavor['id'],
465+
body={'flavor': {'description': None}})['flavor']
466+
self.assertEqual({"key1": "value1"}, flavor['extra_specs'])
467+
468+
431469
class PrivateFlavorManageTestV21(test.TestCase):
432470
controller = flavormanage_v21.FlavorManageController()
433471
base_url = '/v2/fake/flavors'

0 commit comments

Comments
 (0)