summaryrefslogtreecommitdiff
path: root/openstackclient/tests/unit
diff options
context:
space:
mode:
Diffstat (limited to 'openstackclient/tests/unit')
-rw-r--r--openstackclient/tests/unit/compute/v2/test_server.py128
-rw-r--r--openstackclient/tests/unit/image/v2/fakes.py73
-rw-r--r--openstackclient/tests/unit/image/v2/test_task.py187
-rw-r--r--openstackclient/tests/unit/network/v2/test_local_ip.py14
-rw-r--r--openstackclient/tests/unit/network/v2/test_subnet.py4
5 files changed, 391 insertions, 15 deletions
diff --git a/openstackclient/tests/unit/compute/v2/test_server.py b/openstackclient/tests/unit/compute/v2/test_server.py
index ba86dff3..0760fc66 100644
--- a/openstackclient/tests/unit/compute/v2/test_server.py
+++ b/openstackclient/tests/unit/compute/v2/test_server.py
@@ -5101,8 +5101,8 @@ class TestServerListV273(_TestServerList):
self.search_opts['locked'] = True
self.servers_mock.list.assert_called_with(**self.kwargs)
- self.assertItemsEqual(self.columns, columns)
- self.assertItemsEqual(self.data, tuple(data))
+ self.assertCountEqual(self.columns, columns)
+ self.assertCountEqual(self.data, tuple(data))
def test_server_list_with_unlocked_v273(self):
@@ -5121,8 +5121,8 @@ class TestServerListV273(_TestServerList):
self.search_opts['locked'] = False
self.servers_mock.list.assert_called_with(**self.kwargs)
- self.assertItemsEqual(self.columns, columns)
- self.assertItemsEqual(self.data, tuple(data))
+ self.assertCountEqual(self.columns, columns)
+ self.assertCountEqual(self.data, tuple(data))
def test_server_list_with_locked_and_unlocked(self):
@@ -5162,8 +5162,8 @@ class TestServerListV273(_TestServerList):
self.servers_mock.list.assert_called_with(**self.kwargs)
- self.assertItemsEqual(self.columns, columns)
- self.assertItemsEqual(self.data, tuple(data))
+ self.assertCountEqual(self.columns, columns)
+ self.assertCountEqual(self.data, tuple(data))
@mock.patch.object(iso8601, 'parse_date', side_effect=iso8601.ParseError)
def test_server_list_with_invalid_changes_before(
@@ -5781,6 +5781,25 @@ class TestServerRebuild(TestServer):
self.get_image_mock.assert_called_with(self.image.id)
self.server.rebuild.assert_called_with(self.image, None)
+ def test_rebuild_with_volume_backed_server_no_image(self):
+ # the volume-backed server will have the image attribute set to an
+ # empty string, not null/None
+ self.server.image = ''
+
+ arglist = [
+ self.server.id,
+ ]
+ verifylist = [
+ ('server', self.server.id),
+ ]
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+ exc = self.assertRaises(
+ exceptions.CommandError,
+ self.cmd.take_action,
+ parsed_args)
+ self.assertIn('The --image option is required', str(exc))
+
def test_rebuild_with_name(self):
name = 'test-server-xxx'
arglist = [
@@ -6275,6 +6294,103 @@ class TestServerRebuild(TestServer):
parsed_args)
+class TestServerRebuildVolumeBacked(TestServer):
+
+ def setUp(self):
+ super().setUp()
+
+ self.new_image = image_fakes.create_one_image()
+ self.find_image_mock.return_value = self.new_image
+
+ attrs = {
+ 'image': '',
+ 'networks': {},
+ 'adminPass': 'passw0rd',
+ }
+ new_server = compute_fakes.FakeServer.create_one_server(attrs=attrs)
+
+ # Fake the server to be rebuilt. The IDs of them should be the same.
+ attrs['id'] = new_server.id
+ methods = {
+ 'rebuild': new_server,
+ }
+ self.server = compute_fakes.FakeServer.create_one_server(
+ attrs=attrs,
+ methods=methods
+ )
+
+ # Return value for utils.find_resource for server.
+ self.servers_mock.get.return_value = self.server
+
+ self.cmd = server.RebuildServer(self.app, None)
+
+ def test_rebuild_with_reimage_boot_volume(self):
+ self.app.client_manager.compute.api_version = \
+ api_versions.APIVersion('2.93')
+
+ arglist = [
+ self.server.id,
+ '--reimage-boot-volume',
+ '--image', self.new_image.id
+ ]
+ verifylist = [
+ ('server', self.server.id),
+ ('reimage_boot_volume', True),
+ ('image', self.new_image.id)
+ ]
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+ self.cmd.take_action(parsed_args)
+
+ self.servers_mock.get.assert_called_with(self.server.id)
+ self.server.rebuild.assert_called_with(
+ self.new_image, None)
+
+ def test_rebuild_with_no_reimage_boot_volume(self):
+ self.app.client_manager.compute.api_version = \
+ api_versions.APIVersion('2.93')
+
+ arglist = [
+ self.server.id,
+ '--no-reimage-boot-volume',
+ '--image', self.new_image.id
+ ]
+ verifylist = [
+ ('server', self.server.id),
+ ('reimage_boot_volume', False),
+ ('image', self.new_image.id)
+ ]
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+ exc = self.assertRaises(
+ exceptions.CommandError,
+ self.cmd.take_action,
+ parsed_args)
+ self.assertIn('--reimage-boot-volume is required', str(exc))
+
+ def test_rebuild_with_reimage_boot_volume_pre_v293(self):
+ self.app.client_manager.compute.api_version = \
+ api_versions.APIVersion('2.92')
+
+ arglist = [
+ self.server.id,
+ '--reimage-boot-volume',
+ '--image', self.new_image.id
+ ]
+ verifylist = [
+ ('server', self.server.id),
+ ('reimage_boot_volume', True)
+ ]
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+ exc = self.assertRaises(
+ exceptions.CommandError,
+ self.cmd.take_action,
+ parsed_args)
+ self.assertIn(
+ '--os-compute-api-version 2.93 or greater is required', str(exc))
+
+
class TestEvacuateServer(TestServer):
def setUp(self):
diff --git a/openstackclient/tests/unit/image/v2/fakes.py b/openstackclient/tests/unit/image/v2/fakes.py
index a0eda6d2..f2015450 100644
--- a/openstackclient/tests/unit/image/v2/fakes.py
+++ b/openstackclient/tests/unit/image/v2/fakes.py
@@ -18,6 +18,7 @@ import uuid
from openstack.image.v2 import image
from openstack.image.v2 import member
+from openstack.image.v2 import task
from openstackclient.tests.unit import fakes
from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes
@@ -44,10 +45,16 @@ class FakeImagev2Client:
self.remove_tag = mock.Mock()
+ self.tasks = mock.Mock()
+ self.get_task = mock.Mock()
+
self.auth_token = kwargs['token']
self.management_url = kwargs['endpoint']
self.version = 2.0
+ self.tasks = mock.Mock()
+ self.tasks.resource_class = fakes.FakeResource(None, {})
+
class TestImagev2(utils.TestCommand):
@@ -129,3 +136,69 @@ def create_one_image_member(attrs=None):
image_member_info.update(attrs)
return member.Member(**image_member_info)
+
+
+def create_one_task(attrs=None):
+ """Create a fake task.
+
+ :param attrs: A dictionary with all attributes of task
+ :type attrs: dict
+ :return: A fake Task object.
+ :rtype: `openstack.image.v2.task.Task`
+ """
+ attrs = attrs or {}
+
+ # Set default attribute
+ task_info = {
+ 'created_at': '2016-06-29T16:13:07Z',
+ 'expires_at': '2016-07-01T16:13:07Z',
+ 'id': str(uuid.uuid4()),
+ 'input': {
+ 'image_properties': {
+ 'container_format': 'ovf',
+ 'disk_format': 'vhd'
+ },
+ 'import_from': 'https://apps.openstack.org/excellent-image',
+ 'import_from_format': 'qcow2'
+ },
+ 'message': '',
+ 'owner': str(uuid.uuid4()),
+ 'result': {
+ 'image_id': str(uuid.uuid4()),
+ },
+ 'schema': '/v2/schemas/task',
+ 'status': random.choice(
+ [
+ 'pending',
+ 'processing',
+ 'success',
+ 'failure',
+ ]
+ ),
+ # though not documented, the API only allows 'import'
+ # https://github.com/openstack/glance/blob/24.0.0/glance/api/v2/tasks.py#L186-L190
+ 'type': 'import',
+ 'updated_at': '2016-06-29T16:13:07Z',
+ }
+
+ # Overwrite default attributes if there are some attributes set
+ task_info.update(attrs)
+
+ return task.Task(**task_info)
+
+
+def create_tasks(attrs=None, count=2):
+ """Create multiple fake tasks.
+
+ :param attrs: A dictionary with all attributes of Task
+ :type attrs: dict
+ :param count: The number of tasks to be faked
+ :type count: int
+ :return: A list of fake Task objects
+ :rtype: list
+ """
+ tasks = []
+ for n in range(0, count):
+ tasks.append(create_one_task(attrs))
+
+ return tasks
diff --git a/openstackclient/tests/unit/image/v2/test_task.py b/openstackclient/tests/unit/image/v2/test_task.py
new file mode 100644
index 00000000..e077e2b1
--- /dev/null
+++ b/openstackclient/tests/unit/image/v2/test_task.py
@@ -0,0 +1,187 @@
+# 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 osc_lib.cli import format_columns
+
+from openstackclient.image.v2 import task
+from openstackclient.tests.unit.image.v2 import fakes as image_fakes
+
+
+class TestTask(image_fakes.TestImagev2):
+ def setUp(self):
+ super().setUp()
+
+ # Get shortcuts to mocked image client
+ self.client = self.app.client_manager.image
+
+
+class TestTaskShow(TestTask):
+
+ task = image_fakes.create_one_task()
+
+ columns = (
+ 'created_at',
+ 'expires_at',
+ 'id',
+ 'input',
+ 'message',
+ 'owner_id',
+ 'properties',
+ 'result',
+ 'status',
+ 'type',
+ 'updated_at',
+ )
+ data = (
+ task.created_at,
+ task.expires_at,
+ task.id,
+ task.input,
+ task.message,
+ task.owner_id,
+ format_columns.DictColumn({}),
+ task.result,
+ task.status,
+ task.type,
+ task.updated_at,
+ )
+
+ def setUp(self):
+ super().setUp()
+
+ self.client.get_task.return_value = self.task
+
+ # Get the command object to test
+ self.cmd = task.ShowTask(self.app, None)
+
+ def test_task_show(self):
+ arglist = [self.task.id]
+ verifylist = [
+ ('task', self.task.id),
+ ]
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+ # In base command class ShowOne in cliff, abstract method take_action()
+ # returns a two-part tuple with a tuple of column names and a tuple of
+ # data to be shown.
+ columns, data = self.cmd.take_action(parsed_args)
+ self.client.get_task.assert_called_with(self.task.id)
+
+ self.assertEqual(self.columns, columns)
+ self.assertCountEqual(self.data, data)
+
+
+class TestTaskList(TestTask):
+
+ tasks = image_fakes.create_tasks()
+
+ columns = (
+ 'ID',
+ 'Type',
+ 'Status',
+ 'Owner',
+ )
+ datalist = [
+ (
+ task.id,
+ task.type,
+ task.status,
+ task.owner_id,
+ )
+ for task in tasks
+ ]
+
+ def setUp(self):
+ super().setUp()
+
+ self.client.tasks.side_effect = [self.tasks, []]
+
+ # Get the command object to test
+ self.cmd = task.ListTask(self.app, None)
+
+ def test_task_list_no_options(self):
+ arglist = []
+ verifylist = [
+ ('sort_key', None),
+ ('sort_dir', None),
+ ('limit', None),
+ ('marker', None),
+ ('type', None),
+ ('status', None),
+ ]
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+ columns, data = self.cmd.take_action(parsed_args)
+
+ self.client.tasks.assert_called_with()
+
+ self.assertEqual(self.columns, columns)
+ self.assertCountEqual(self.datalist, data)
+
+ def test_task_list_sort_key_option(self):
+ arglist = ['--sort-key', 'created_at']
+ verifylist = [('sort_key', 'created_at')]
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+ columns, data = self.cmd.take_action(parsed_args)
+
+ self.client.tasks.assert_called_with(
+ sort_key=parsed_args.sort_key,
+ )
+
+ self.assertEqual(self.columns, columns)
+ self.assertCountEqual(self.datalist, data)
+
+ def test_task_list_sort_dir_option(self):
+ arglist = ['--sort-dir', 'desc']
+ verifylist = [('sort_dir', 'desc')]
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+ self.cmd.take_action(parsed_args)
+
+ self.client.tasks.assert_called_with(
+ sort_dir=parsed_args.sort_dir,
+ )
+
+ def test_task_list_pagination_options(self):
+ arglist = ['--limit', '1', '--marker', self.tasks[0].id]
+ verifylist = [('limit', 1), ('marker', self.tasks[0].id)]
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+ self.cmd.take_action(parsed_args)
+
+ self.client.tasks.assert_called_with(
+ limit=parsed_args.limit,
+ marker=parsed_args.marker,
+ )
+
+ def test_task_list_type_option(self):
+ arglist = ['--type', self.tasks[0].type]
+ verifylist = [('type', self.tasks[0].type)]
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+ self.cmd.take_action(parsed_args)
+
+ self.client.tasks.assert_called_with(
+ type=self.tasks[0].type,
+ )
+
+ def test_task_list_status_option(self):
+ arglist = ['--status', self.tasks[0].status]
+ verifylist = [('status', self.tasks[0].status)]
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+ self.cmd.take_action(parsed_args)
+
+ self.client.tasks.assert_called_with(
+ status=self.tasks[0].status,
+ )
diff --git a/openstackclient/tests/unit/network/v2/test_local_ip.py b/openstackclient/tests/unit/network/v2/test_local_ip.py
index 97e246df..be23365e 100644
--- a/openstackclient/tests/unit/network/v2/test_local_ip.py
+++ b/openstackclient/tests/unit/network/v2/test_local_ip.py
@@ -96,7 +96,7 @@ class TestCreateLocalIP(TestLocalIP):
self.network.create_local_ip.assert_called_once_with(**{})
self.assertEqual(set(self.columns), set(columns))
- self.assertItemsEqual(self.data, data)
+ self.assertCountEqual(self.data, data)
def test_create_all_options(self):
arglist = [
@@ -130,7 +130,7 @@ class TestCreateLocalIP(TestLocalIP):
'ip_mode': self.new_local_ip.ip_mode,
})
self.assertEqual(set(self.columns), set(columns))
- self.assertItemsEqual(self.data, data)
+ self.assertCountEqual(self.data, data)
class TestDeleteLocalIP(TestLocalIP):
@@ -263,7 +263,7 @@ class TestListLocalIP(TestLocalIP):
self.network.local_ips.assert_called_once_with(**{})
self.assertEqual(self.columns, columns)
- self.assertItemsEqual(self.data, list(data))
+ self.assertCountEqual(self.data, list(data))
def test_local_ip_list_name(self):
arglist = [
@@ -278,7 +278,7 @@ class TestListLocalIP(TestLocalIP):
self.network.local_ips.assert_called_once_with(
**{'name': self.local_ips[0].name})
self.assertEqual(self.columns, columns)
- self.assertItemsEqual(self.data, list(data))
+ self.assertCountEqual(self.data, list(data))
def test_local_ip_list_project(self):
project = identity_fakes_v3.FakeProject.create_one_project()
@@ -295,7 +295,7 @@ class TestListLocalIP(TestLocalIP):
self.network.local_ips.assert_called_once_with(
**{'project_id': project.id})
self.assertEqual(self.columns, columns)
- self.assertItemsEqual(self.data, list(data))
+ self.assertCountEqual(self.data, list(data))
def test_local_ip_project_domain(self):
project = identity_fakes_v3.FakeProject.create_one_project()
@@ -314,7 +314,7 @@ class TestListLocalIP(TestLocalIP):
self.network.local_ips.assert_called_once_with(**filters)
self.assertEqual(self.columns, columns)
- self.assertItemsEqual(self.data, list(data))
+ self.assertCountEqual(self.data, list(data))
def test_local_ip_list_network(self):
arglist = [
@@ -477,4 +477,4 @@ class TestShowLocalIP(TestLocalIP):
self.network.find_local_ip.assert_called_once_with(
self._local_ip.name, ignore_missing=False)
self.assertEqual(set(self.columns), set(columns))
- self.assertItemsEqual(self.data, list(data))
+ self.assertCountEqual(self.data, list(data))
diff --git a/openstackclient/tests/unit/network/v2/test_subnet.py b/openstackclient/tests/unit/network/v2/test_subnet.py
index 6b3ab2cc..7aaa583d 100644
--- a/openstackclient/tests/unit/network/v2/test_subnet.py
+++ b/openstackclient/tests/unit/network/v2/test_subnet.py
@@ -918,7 +918,7 @@ class TestListSubnet(TestSubnet):
self.network.subnets.assert_called_once_with(**filters)
self.assertEqual(self.columns, columns)
- self.assertItemsEqual(self.data, list(data))
+ self.assertCountEqual(self.data, list(data))
def test_subnet_list_subnetpool_by_id(self):
subnet_pool = network_fakes.FakeSubnetPool.create_one_subnet_pool()
@@ -939,7 +939,7 @@ class TestListSubnet(TestSubnet):
self.network.subnets.assert_called_once_with(**filters)
self.assertEqual(self.columns, columns)
- self.assertItemsEqual(self.data, list(data))
+ self.assertCountEqual(self.data, list(data))
def test_list_with_tag_options(self):
arglist = [