From b4edbd55f2d22214fa9ebdba4b772d2c9d7b9e36 Mon Sep 17 00:00:00 2001 From: Tang Chen Date: Sat, 20 Feb 2016 10:34:40 +0800 Subject: [PATCH] Add unit test for "flavor show" command Change-Id: I1591649e5b97a885707042fcccad3335ee8c7aec --- openstackclient/tests/compute/v2/fakes.py | 12 +++ .../tests/compute/v2/test_flavor.py | 87 ++++++++++++++++--- 2 files changed, 89 insertions(+), 10 deletions(-) diff --git a/openstackclient/tests/compute/v2/fakes.py b/openstackclient/tests/compute/v2/fakes.py index 974095ea32..96d85b0c06 100644 --- a/openstackclient/tests/compute/v2/fakes.py +++ b/openstackclient/tests/compute/v2/fakes.py @@ -404,6 +404,12 @@ class FakeFlavor(object): 'name': 'flavor-name-' + uuid.uuid4().hex, 'ram': 8192, 'vcpus': 4, + 'disk': 128, + 'swap': '', + 'rxtx_factor': '1.0', + 'OS-FLV-DISABLED:disabled': False, + 'os-flavor-access:is_public': True, + 'OS-FLV-EXT-DATA:ephemeral': 0, } # Overwrite default attributes. @@ -411,6 +417,12 @@ class FakeFlavor(object): flavor = FakeFlavorResource(info=copy.deepcopy(flavor_info), loaded=True) + + # Set attributes with special mappings in nova client. + flavor.disabled = flavor_info['OS-FLV-DISABLED:disabled'] + flavor.is_public = flavor_info['os-flavor-access:is_public'] + flavor.ephemeral = flavor_info['OS-FLV-EXT-DATA:ephemeral'] + return flavor @staticmethod diff --git a/openstackclient/tests/compute/v2/test_flavor.py b/openstackclient/tests/compute/v2/test_flavor.py index bf78bee821..781e3068ae 100644 --- a/openstackclient/tests/compute/v2/test_flavor.py +++ b/openstackclient/tests/compute/v2/test_flavor.py @@ -14,8 +14,10 @@ # from openstackclient.common import exceptions +from openstackclient.common import utils from openstackclient.compute.v2 import flavor from openstackclient.tests.compute.v2 import fakes as compute_fakes +from openstackclient.tests import utils as tests_utils class TestFlavor(compute_fakes.TestComputev2): @@ -97,15 +99,15 @@ class TestFlavorList(TestFlavor): flavors[0].id, flavors[0].name, flavors[0].ram, - '', - '', + flavors[0].disk, + flavors[0].ephemeral, flavors[0].vcpus, - '' + flavors[0].is_public, ), ) data_long = (data[0] + ( - '', - '', - 'property=\'value\'' + flavors[0].swap, + flavors[0].rxtx_factor, + u'property=\'value\'' ), ) def setUp(self): @@ -290,8 +292,73 @@ class TestFlavorSet(TestFlavor): self.flavors_mock.find.assert_called_with(name='baremetal') - self.assertEqual('properties', columns[2]) - self.assertIn('FOO=\'"B A R"\'', data[2]) + self.assertEqual('properties', columns[6]) + self.assertIn('FOO=\'"B A R"\'', data[6]) + + +class TestFlavorShow(TestFlavor): + + # Return value of self.flavors_mock.find(). + flavor = compute_fakes.FakeFlavor.create_one_flavor() + + columns = ( + 'OS-FLV-DISABLED:disabled', + 'OS-FLV-EXT-DATA:ephemeral', + 'disk', + 'id', + 'name', + 'os-flavor-access:is_public', + 'properties', + 'ram', + 'rxtx_factor', + 'swap', + 'vcpus', + ) + + data = ( + flavor.disabled, + flavor.ephemeral, + flavor.disk, + flavor.id, + flavor.name, + flavor.is_public, + utils.format_dict(flavor.get_keys()), + flavor.ram, + flavor.rxtx_factor, + flavor.swap, + flavor.vcpus, + ) + + def setUp(self): + super(TestFlavorShow, self).setUp() + + # Return value of utils.find_resource() + self.flavors_mock.get.return_value = self.flavor + + self.cmd = flavor.ShowFlavor(self.app, None) + + def test_show_no_options(self): + arglist = [] + verifylist = [] + + # Missing required args should boil here + self.assertRaises(tests_utils.ParserException, self.check_parser, + self.cmd, arglist, verifylist) + + def test_flavor_show(self): + arglist = [ + self.flavor.name, + ] + verifylist = [ + ('flavor', self.flavor.name), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, data) class TestFlavorUnset(TestFlavor): @@ -322,5 +389,5 @@ class TestFlavorUnset(TestFlavor): self.flavors_mock.find.assert_called_with(name='baremetal') - self.assertEqual('properties', columns[2]) - self.assertNotIn('property', data[2]) + self.assertEqual('properties', columns[6]) + self.assertNotIn('property', data[6])