1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
# Copyright 2024 Cloudbase Solutions Srl
# All Rights Reserved.
from cliff import lister
from cliff import show
from unittest import mock
from coriolisclient.cli import endpoint_instances
from coriolisclient.cli import utils as cli_utils
from coriolisclient.tests import test_base
class EndpointInstanceFormatterTestCase(test_base.CoriolisBaseTestCase):
"""Test suite for the Coriolis Client Endpoint Instance Formatter."""
def setUp(self):
super(EndpointInstanceFormatterTestCase, self).setUp()
self.endpoint = endpoint_instances.EndpointInstanceFormatter()
def test_get_formatted_data(self):
obj = mock.Mock()
obj.id = mock.sentinel.id
obj.flavor_name = mock.sentinel.flavor_name
obj.memory_mb = mock.sentinel.memory_mb
obj.num_cpu = mock.sentinel.num_cpu
obj.os_type = mock.sentinel.os_type
obj.to_dict = mock.Mock(
return_value={"instance_name": mock.sentinel.instance_name}
)
result = self.endpoint._get_formatted_data(obj)
self.assertEqual(
(
mock.sentinel.id,
mock.sentinel.instance_name,
mock.sentinel.flavor_name,
mock.sentinel.memory_mb,
mock.sentinel.num_cpu,
mock.sentinel.os_type
),
result
)
class InstancesDetailFormatterTestCase(test_base.CoriolisBaseTestCase):
"""Test suite for the Coriolis Client Instances Detail Formatter."""
def setUp(self):
super(InstancesDetailFormatterTestCase, self).setUp()
self.endpoint = endpoint_instances.InstancesDetailFormatter()
def test_get_formatted_data(self):
obj = mock.Mock()
obj.id = mock.sentinel.id
obj.name = mock.sentinel.name
obj.flavor_name = mock.sentinel.flavor_name
obj.memory_mb = mock.sentinel.memory_mb
obj.num_cpu = mock.sentinel.num_cpu
obj.os_type = mock.sentinel.os_type
obj.to_dict = mock.Mock(
return_value={"instance_name": mock.sentinel.instance_name,
"firmware_type": mock.sentinel.firmware_type}
)
devices = {
"controllers": ["controller1", "controller2"],
"disks": ["disk1", "disk2"],
"nics": ["nic1", "nic2"],
"cdroms": ["cdrom1", "cdrom2"],
"floppies": ["floppy1", "floppy2"],
"serial_ports": ["serial_port1", "serial_port2"]
}
obj.devices = devices
result = self.endpoint._get_formatted_data(obj)
self.assertEqual(
[
mock.sentinel.id,
mock.sentinel.name,
mock.sentinel.instance_name,
mock.sentinel.flavor_name,
mock.sentinel.memory_mb,
mock.sentinel.num_cpu,
mock.sentinel.os_type,
mock.sentinel.firmware_type,
'[\n "controller1",\n "controller2"\n]',
'[\n "disk1",\n "disk2"\n]',
'[\n "nic1",\n "nic2"\n]',
'[\n "cdrom1",\n "cdrom2"\n]',
'[\n "floppy1",\n "floppy2"\n]',
'[\n "serial_port1",\n "serial_port2"\n]'
],
result
)
class ListEndpointInstanceTestCase(
test_base.CoriolisBaseTestCase):
"""Test suite for the Coriolis Client List Endpoint Instance."""
def setUp(self):
self.mock_app = mock.Mock()
super(ListEndpointInstanceTestCase, self).setUp()
self.endpoint = endpoint_instances.ListEndpointInstance(
self.mock_app, mock.sentinel.app_args)
@mock.patch.object(cli_utils, 'add_args_for_json_option_to_parser')
@mock.patch.object(lister.Lister, 'get_parser')
def test_get_parser(
self,
mock_get_parser,
mock_add_args_for_json_option_to_parser
):
result = self.endpoint.get_parser(mock.sentinel.prog_name)
self.assertEqual(
mock_get_parser.return_value,
result
)
mock_get_parser.assert_called_once_with(mock.sentinel.prog_name)
mock_add_args_for_json_option_to_parser.assert_called_once_with(
mock_get_parser.return_value, 'environment')
@mock.patch.object(endpoint_instances.EndpointInstanceFormatter,
'list_objects')
@mock.patch.object(cli_utils, 'get_option_value_from_args')
def test_take_action(
self,
mock_get_option_value_from_args,
mock_list_objects
):
args = mock.Mock()
args.marker = mock.sentinel.marker
args.limit = mock.sentinel.limit
args.name = mock.sentinel.name
mock_endpoints = mock.Mock()
mock_ei = mock.Mock()
self.mock_app.client_manager.coriolis.endpoints = mock_endpoints
mock_ei = self.mock_app.client_manager.coriolis.endpoint_instances
result = self.endpoint.take_action(args)
self.assertEqual(
mock_list_objects.return_value,
result
)
mock_get_option_value_from_args.assert_called_once_with(
args, 'environment', error_on_no_value=False)
mock_ei.list.assert_called_once_with(
mock_endpoints.get_endpoint_id_for_name(args.endpoint),
mock_get_option_value_from_args.return_value,
mock.sentinel.marker,
mock.sentinel.limit,
mock.sentinel.name
)
mock_list_objects.assert_called_once_with(mock_ei.list.return_value)
class ShowEndpointInstanceTestCase(
test_base.CoriolisBaseTestCase):
"""Test suite for the Coriolis Client List Show Endpoint Instance"""
def setUp(self):
self.mock_app = mock.Mock()
super(ShowEndpointInstanceTestCase, self).setUp()
self.endpoint = endpoint_instances.ShowEndpointInstance(
self.mock_app, mock.sentinel.app_args)
@mock.patch.object(cli_utils, 'add_args_for_json_option_to_parser')
@mock.patch.object(show.ShowOne, 'get_parser')
def test_get_parser(
self,
mock_get_parser,
mock_add_args_for_json_option_to_parser
):
result = self.endpoint.get_parser(mock.sentinel.prog_name)
self.assertEqual(
mock_get_parser.return_value,
result
)
mock_get_parser.assert_called_once_with(mock.sentinel.prog_name)
mock_add_args_for_json_option_to_parser.assert_called_once_with(
mock_get_parser.return_value, 'environment')
@mock.patch.object(endpoint_instances.InstancesDetailFormatter,
'get_formatted_entity')
@mock.patch.object(cli_utils, 'get_option_value_from_args')
def test_take_action(
self,
mock_get_option_value_from_args,
mock_get_formatted_entity
):
args = mock.Mock()
args.instance = mock.sentinel.instance
mock_endpoints = mock.Mock()
mock_ei = mock.Mock()
self.mock_app.client_manager.coriolis.endpoints = mock_endpoints
mock_ei = self.mock_app.client_manager.coriolis.endpoint_instances
result = self.endpoint.take_action(args)
self.assertEqual(
mock_get_formatted_entity.return_value,
result
)
mock_get_option_value_from_args.assert_called_once_with(
args, 'environment', error_on_no_value=False)
mock_ei.get.assert_called_once_with(
mock_endpoints.get_endpoint_id_for_name(args.endpoint),
mock.sentinel.instance,
mock_get_option_value_from_args.return_value,
)
mock_get_formatted_entity.assert_called_once_with(
mock_ei.get.return_value)
|