Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

334 lines
10 KiB
Python
Raw Normal View History

# 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 unittest import mock
from openstackclient.common import extension
from openstackclient.tests.unit.compute.v2 import fakes as compute_fakes
from openstackclient.tests.unit.identity.v2_0 import fakes as identity_fakes
from openstackclient.tests.unit.network.v2 import fakes as network_fakes
from openstackclient.tests.unit import utils
from openstackclient.tests.unit import utils as tests_utils
from openstackclient.tests.unit.volume.v3 import fakes as volume_fakes
class TestExtension(
network_fakes.FakeClientMixin,
compute_fakes.FakeClientMixin,
volume_fakes.FakeClientMixin,
identity_fakes.FakeClientMixin,
utils.TestCommand,
): ...
class TestExtensionList(TestExtension):
columns = ('Name', 'Alias', 'Description')
long_columns = (
'Name',
'Alias',
'Description',
'Namespace',
'Updated At',
'Links',
)
volume_extension = volume_fakes.create_one_extension()
identity_extension = identity_fakes.FakeExtension.create_one_extension()
compute_extension = compute_fakes.create_one_extension()
network_extension = network_fakes.create_one_extension()
def setUp(self):
super().setUp()
self.identity_client.extensions.list.return_value = [
self.identity_extension
]
self.compute_sdk_client.extensions.return_value = [
self.compute_extension
]
self.volume_sdk_client.extensions.return_value = [
self.volume_extension
]
self.network_client.extensions.return_value = [self.network_extension]
# Get the command object to test
self.cmd = extension.ListExtension(self.app, None)
def _test_extension_list_helper(
self, arglist, verifylist, expected_data, long=False
):
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
Fix DisplayCommandBase comments for cliff Lister subclass tests As bug #1477199 describes, the wrong comment below is all over the unit test code of OSC. # DisplayCommandBase.take_action() returns two tuples There is no such class named DisplayCommandBase in OSC. It is in cliff. All OSC command classes inherit from the base classes in cliff, class Command, class Lister and class ShowOne. It is like this: Object |--> Command |--> DisplayCommandBase |--> Lister |--> ShowOne take_action() is an abstract method of class Command, and generally is overwritten by subclasses. * Command.take_action() returns nothing. * Lister.take_action() returns a tuple which contains a tuple of columns and a generator used to generate the data. * ShowOne.take_action() returns an iterator which contains a tuple of columns and a tuple of data. So, this problem should be fixed in 3 steps: 1. Remove all DisplayCommandBase comments for tests of classes inheriting from class Command in cliff as it returns nothing. 2. Fix all DisplayCommandBase comments for tests of classes inheriting from class Lister in cliff. Lister.take_action() returns a tuple and a generator. 3. Fix all DisplayCommandBase comments for tests of classes inheriting from class ShowOne in cliff. ShowOne.take_action() returns two tuples. This patch finishes step 2 in all but identity tests. There are too many such comments in identity tests. So fix them all in another patch. Change-Id: I00f38d12f55abe20fa708f6349073da658622f8d Partial-bug: #1477199
2016-02-09 17:30:39 +08:00
# In base command class Lister in cliff, abstract method take_action()
# returns a tuple containing the column names and an iterable
# containing the data to be listed.
columns, data = self.cmd.take_action(parsed_args)
if long:
self.assertEqual(self.long_columns, columns)
else:
self.assertEqual(self.columns, columns)
self.assertEqual(expected_data, tuple(data))
def test_extension_list_no_options(self):
arglist = []
verifylist = []
datalist = (
(
self.identity_extension.name,
self.identity_extension.alias,
self.identity_extension.description,
),
(
self.compute_extension.name,
self.compute_extension.alias,
self.compute_extension.description,
),
(
self.volume_extension.name,
self.volume_extension.alias,
self.volume_extension.description,
),
(
self.network_extension.name,
self.network_extension.alias,
self.network_extension.description,
),
)
self._test_extension_list_helper(arglist, verifylist, datalist)
self.identity_client.extensions.list.assert_called_with()
self.compute_sdk_client.extensions.assert_called_with()
self.volume_sdk_client.extensions.assert_called_with()
self.network_client.extensions.assert_called_with()
def test_extension_list_long(self):
arglist = [
'--long',
]
verifylist = [
('long', True),
]
datalist = (
(
self.identity_extension.name,
self.identity_extension.alias,
self.identity_extension.description,
self.identity_extension.namespace,
'',
self.identity_extension.links,
),
(
self.compute_extension.name,
self.compute_extension.alias,
self.compute_extension.description,
self.compute_extension.namespace,
self.compute_extension.updated_at,
self.compute_extension.links,
),
(
self.volume_extension.name,
self.volume_extension.alias,
self.volume_extension.description,
'',
self.volume_extension.updated_at,
self.volume_extension.links,
),
(
self.network_extension.name,
self.network_extension.alias,
self.network_extension.description,
'',
self.network_extension.updated_at,
self.network_extension.links,
),
)
self._test_extension_list_helper(arglist, verifylist, datalist, True)
self.identity_client.extensions.list.assert_called_with()
self.compute_sdk_client.extensions.assert_called_with()
self.volume_sdk_client.extensions.assert_called_with()
self.network_client.extensions.assert_called_with()
def test_extension_list_identity(self):
arglist = [
'--identity',
]
verifylist = [
('identity', True),
]
datalist = (
(
self.identity_extension.name,
self.identity_extension.alias,
self.identity_extension.description,
),
)
self._test_extension_list_helper(arglist, verifylist, datalist)
self.identity_client.extensions.list.assert_called_with()
def test_extension_list_network(self):
arglist = [
'--network',
]
verifylist = [
('network', True),
]
datalist = (
(
self.network_extension.name,
self.network_extension.alias,
self.network_extension.description,
),
)
self._test_extension_list_helper(arglist, verifylist, datalist)
self.network_client.extensions.assert_called_with()
def test_extension_list_network_with_long(self):
arglist = [
'--network',
'--long',
]
verifylist = [
('network', True),
('long', True),
]
datalist = (
(
self.network_extension.name,
self.network_extension.alias,
self.network_extension.description,
'',
self.network_extension.updated_at,
self.network_extension.links,
),
)
self._test_extension_list_helper(
arglist, verifylist, datalist, long=True
)
self.network_client.extensions.assert_called_with()
def test_extension_list_compute(self):
arglist = [
'--compute',
]
verifylist = [
('compute', True),
]
datalist = (
(
self.compute_extension.name,
self.compute_extension.alias,
self.compute_extension.description,
),
)
self._test_extension_list_helper(arglist, verifylist, datalist)
self.compute_sdk_client.extensions.assert_called_with()
def test_extension_list_compute_and_network(self):
arglist = [
'--compute',
'--network',
]
verifylist = [
('compute', True),
('network', True),
]
datalist = (
(
self.compute_extension.name,
self.compute_extension.alias,
self.compute_extension.description,
),
(
self.network_extension.name,
self.network_extension.alias,
self.network_extension.description,
),
)
self._test_extension_list_helper(arglist, verifylist, datalist)
self.compute_sdk_client.extensions.assert_called_with()
self.network_client.extensions.assert_called_with()
def test_extension_list_volume(self):
arglist = [
'--volume',
]
verifylist = [
('volume', True),
]
datalist = (
(
self.volume_extension.name,
self.volume_extension.alias,
self.volume_extension.description,
),
)
self._test_extension_list_helper(arglist, verifylist, datalist)
self.volume_sdk_client.extensions.assert_called_with()
class TestExtensionShow(TestExtension):
extension_details = network_fakes.create_one_extension()
columns = (
'alias',
'description',
'name',
'updated_at',
)
data = (
extension_details.alias,
extension_details.description,
extension_details.name,
extension_details.updated_at,
)
def setUp(self):
super().setUp()
self.cmd = extension.ShowExtension(self.app, None)
self.app.client_manager.network.find_extension = mock.Mock(
return_value=self.extension_details
)
def test_show_no_options(self):
arglist = []
verifylist = []
self.assertRaises(
tests_utils.ParserException,
self.check_parser,
self.cmd,
arglist,
verifylist,
)
def test_show_all_options(self):
arglist = [
self.extension_details.alias,
]
verifylist = [
('extension', self.extension_details.alias),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args)
self.app.client_manager.network.find_extension.assert_called_with(
self.extension_details.alias, ignore_missing=False
)
self.assertEqual(self.columns, columns)
self.assertEqual(self.data, data)