From 796192db2776819b8143d584b45a469204aa751b Mon Sep 17 00:00:00 2001 From: Cristian Matiut Date: Fri, 17 May 2024 19:07:06 +0300 Subject: [PATCH 01/10] Add unit tests for `coriolisclient.v1.common` module --- coriolisclient/tests/v1/__init__.py | 0 coriolisclient/tests/v1/test_common.py | 65 ++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 coriolisclient/tests/v1/__init__.py create mode 100644 coriolisclient/tests/v1/test_common.py diff --git a/coriolisclient/tests/v1/__init__.py b/coriolisclient/tests/v1/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/coriolisclient/tests/v1/test_common.py b/coriolisclient/tests/v1/test_common.py new file mode 100644 index 0000000..0f0086c --- /dev/null +++ b/coriolisclient/tests/v1/test_common.py @@ -0,0 +1,65 @@ +# Copyright 2024 Cloudbase Solutions Srl +# All Rights Reserved. + +import ddt + +from coriolisclient import exceptions +from coriolisclient.tests import test_base +from coriolisclient.v1 import common + + +class TaskTestCase(test_base.CoriolisBaseTestCase): + """Test suite for the Coriolis v1 Task.""" + + def setUp(self): + super(TaskTestCase, self).setUp() + self.task = common.Task( + None, + { + "progress_updates": [ + {"progress_update1": "mock_update1"}, + {"progress_update2": "mock_update2"} + ] + }, + loaded=False + ) + + def test_progress_updates(self): + result = self.task.progress_updates + + self.assertEqual( + ("mock_update1", "mock_update2"), + (result[0].progress_update1, result[1].progress_update2) + ) + + +@ddt.ddt +class CommonTestCase(test_base.CoriolisBaseTestCase): + """Test suite for the Coriolis v1 Common.""" + + @ddt.data( + ("value", False, "dmFsdWU="), + ({"key": "value"}, True, "eyJrZXkiOiAidmFsdWUifQ==") + ) + @ddt.unpack + def test_encode_base64_param(self, param, is_json, expected_result): + result = common.encode_base64_param(param, is_json=is_json) + + self.assertEqual( + result, + expected_result + ) + + @ddt.data( + (12345, False), + (None, False), + ({"key value"}, True) + ) + @ddt.unpack + def test_encode_base64_param_raises(self, param, is_json): + self.assertRaises( + exceptions.CoriolisException, + common.encode_base64_param, + param, + is_json=is_json + ) From 922a0c2f2ab5346d93c8da35ad084fc67cea9165 Mon Sep 17 00:00:00 2001 From: Cristian Matiut Date: Wed, 22 May 2024 14:27:15 +0300 Subject: [PATCH 02/10] Add unit tests for `coriolisclient.v1.diagnostics` module --- coriolisclient/tests/v1/test_diagnostics.py | 26 +++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 coriolisclient/tests/v1/test_diagnostics.py diff --git a/coriolisclient/tests/v1/test_diagnostics.py b/coriolisclient/tests/v1/test_diagnostics.py new file mode 100644 index 0000000..fdb7337 --- /dev/null +++ b/coriolisclient/tests/v1/test_diagnostics.py @@ -0,0 +1,26 @@ +# Copyright 2024 Cloudbase Solutions Srl +# All Rights Reserved. + +from unittest import mock + +from coriolisclient.tests import test_base +from coriolisclient.v1 import diagnostics + + +class DiagnosticsManagerTestCase(test_base.CoriolisBaseTestCase): + """Test suite for the Coriolis v1 Diagnostics Manager.""" + + def setUp(self): + mock_client = mock.Mock() + super(DiagnosticsManagerTestCase, self).setUp() + self.diag = diagnostics.DiagnosticsManager(mock_client) + + @mock.patch.object(diagnostics.DiagnosticsManager, '_list') + def test_get(self, mock_list): + result = self.diag.get() + + self.assertEqual( + mock_list.return_value, + result + ) + mock_list.assert_called_once_with('/diagnostics', 'diagnostics') From e6936ccca23fc9b267be01fb665aa7b8b5f421dd Mon Sep 17 00:00:00 2001 From: Cristian Matiut Date: Wed, 22 May 2024 14:27:47 +0300 Subject: [PATCH 03/10] Add unit tests for `coriolisclient.v1.endpoint_destination_minion_pool_options` module --- ...ndpoint_destination_minion_pool_options.py | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 coriolisclient/tests/v1/test_endpoint_destination_minion_pool_options.py diff --git a/coriolisclient/tests/v1/test_endpoint_destination_minion_pool_options.py b/coriolisclient/tests/v1/test_endpoint_destination_minion_pool_options.py new file mode 100644 index 0000000..00d808c --- /dev/null +++ b/coriolisclient/tests/v1/test_endpoint_destination_minion_pool_options.py @@ -0,0 +1,56 @@ +# Copyright 2024 Cloudbase Solutions Srl +# All Rights Reserved. + +from unittest import mock + +from coriolisclient import base +from coriolisclient.tests import test_base +from coriolisclient.v1 import common +from coriolisclient.v1 import endpoint_destination_minion_pool_options + + +class EndpointDestinationMinionPoolOptionsManagerTestCase( + test_base.CoriolisBaseTestCase): + """ + Test suite for the Coriolis v1 Endpoint Destination Minion Pool Options + Manager. + """ + + def setUp(self): + mock_client = mock.Mock() + super(EndpointDestinationMinionPoolOptionsManagerTestCase, self + ).setUp() + self.endpoint = ( + endpoint_destination_minion_pool_options. + EndpointDestinationMinionPoolOptionsManager)(mock_client) + + @mock.patch.object(endpoint_destination_minion_pool_options. + EndpointDestinationMinionPoolOptionsManager, '_list') + @mock.patch.object(common, 'encode_base64_param') + @mock.patch.object(base, 'getid') + def test_list( + self, + mock_getid, + mock_encode_base64_param, + mock_list + ): + mock_endpoint = mock.Mock() + mock_getid.return_value = mock.sentinel.id + mock_encode_base64_param.side_effect = [ + mock.sentinel.encoded_env, mock.sentinel.encoded_option_names] + + result = self.endpoint.list( + mock_endpoint, + mock.sentinel.environment, + mock.sentinel.option_names + ) + + self.assertEqual( + mock_list.return_value, + result + ) + mock_list.assert_called_once_with( + ('/endpoints/sentinel.id/destination-minion-pool-options' + '?env=sentinel.encoded_env' + '&options=sentinel.encoded_option_names'), + 'destination_minion_pool_options') From f2df8c2819f532b1f0d982b7f61144fd1ca42db8 Mon Sep 17 00:00:00 2001 From: Cristian Matiut Date: Wed, 22 May 2024 14:27:59 +0300 Subject: [PATCH 04/10] Add unit tests for `coriolisclient.v1.endpoint_destination_options` module --- .../v1/test_endpoint_destination_options.py | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 coriolisclient/tests/v1/test_endpoint_destination_options.py diff --git a/coriolisclient/tests/v1/test_endpoint_destination_options.py b/coriolisclient/tests/v1/test_endpoint_destination_options.py new file mode 100644 index 0000000..5766202 --- /dev/null +++ b/coriolisclient/tests/v1/test_endpoint_destination_options.py @@ -0,0 +1,57 @@ +# Copyright 2024 Cloudbase Solutions Srl +# All Rights Reserved. + +from unittest import mock + +from coriolisclient import base +from coriolisclient.tests import test_base +from coriolisclient.v1 import common +from coriolisclient.v1 import endpoint_destination_options + + +class EndpointDestinationOptionsManagerTestCase( + test_base.CoriolisBaseTestCase): + """Test suite for the Coriolis v1 Endpoint Destination Options Manager.""" + + def setUp(self): + mock_client = mock.Mock() + super(EndpointDestinationOptionsManagerTestCase, self).setUp() + self.endpoint = ( + endpoint_destination_options. + EndpointDestinationOptionsManager)(mock_client) + + @mock.patch.object(endpoint_destination_options. + EndpointDestinationOptionsManager, '_list') + @mock.patch.object(common, 'encode_base64_param') + @mock.patch.object(base, 'getid') + def test_list( + self, + mock_getid, + mock_encode_base64_param, + mock_list + ): + mock_endpoint = mock.Mock() + mock_getid.return_value = mock.sentinel.id + mock_encode_base64_param.side_effect = [ + mock.sentinel.encoded_env, mock.sentinel.encoded_option_names] + + result = self.endpoint.list( + mock_endpoint, + mock.sentinel.environment, + mock.sentinel.option_names + ) + + self.assertEqual( + mock_list.return_value, + result + ) + mock_getid.assert_called_once_with(mock_endpoint) + mock_encode_base64_param.assert_has_calls([ + mock.call(mock.sentinel.environment, is_json=True), + mock.call(mock.sentinel.option_names, is_json=True) + ]) + mock_list.assert_called_once_with( + ('/endpoints/sentinel.id/destination-options' + '?env=sentinel.encoded_env' + '&options=sentinel.encoded_option_names'), + 'destination_options') From 6edf5aa068df8aea5ccf69a09f95b68f544d3ceb Mon Sep 17 00:00:00 2001 From: Cristian Matiut Date: Wed, 22 May 2024 14:28:12 +0300 Subject: [PATCH 05/10] Add unit tests for `coriolisclient.v1.endpoint_destination_instances` module --- .../tests/v1/test_endpoint_instances.py | 154 ++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 coriolisclient/tests/v1/test_endpoint_instances.py diff --git a/coriolisclient/tests/v1/test_endpoint_instances.py b/coriolisclient/tests/v1/test_endpoint_instances.py new file mode 100644 index 0000000..89361e9 --- /dev/null +++ b/coriolisclient/tests/v1/test_endpoint_instances.py @@ -0,0 +1,154 @@ +# Copyright 2024 Cloudbase Solutions Srl +# All Rights Reserved. + +from six.moves.urllib import parse as urlparse +from unittest import mock + +from coriolisclient import base +from coriolisclient.tests import test_base +from coriolisclient.v1 import common +from coriolisclient.v1 import endpoint_instances + + +class EndpointInstanceTestCase( + test_base.CoriolisBaseTestCase): + """Test suite for the Coriolis v1 Endpoint Instance.""" + + def setUp(self): + mock_client = mock.Mock() + super(EndpointInstanceTestCase, self).setUp() + self.endpoint = endpoint_instances.EndpointInstance( + mock_client, + {"flavor_name": mock.sentinel.flavor_name}) + + def test_flavor_name(self): + result = self.endpoint.flavor_name + + self.assertEqual( + mock.sentinel.flavor_name, + result + ) + + +class EndpointInstanceManagerTestCase( + test_base.CoriolisBaseTestCase): + """Test suite for the Coriolis v1 Endpoint Instance Manager.""" + + def setUp(self): + mock_client = mock.Mock() + super(EndpointInstanceManagerTestCase, self).setUp() + self.endpoint = endpoint_instances.EndpointInstanceManager(mock_client) + + @mock.patch.object(endpoint_instances.EndpointInstanceManager, '_list') + @mock.patch.object(urlparse, 'urlencode') + @mock.patch.object(common, 'encode_base64_param') + @mock.patch.object(base, 'getid') + def test_list( + self, + mock_getid, + mock_encode_base64_param, + mock_urlencode, + mock_list + ): + mock_endpoint = mock.Mock() + mock_getid.return_value = mock.sentinel.id + mock_encode_base64_param.return_value = mock.sentinel.encoded_env + mock_urlencode.return_value = "mock_url_query" + + result = self.endpoint.list( + mock_endpoint, + env={"env": mock.sentinel.env}, + marker=mock.sentinel.marker, + limit=mock.sentinel.limit, + name=mock.sentinel.name + ) + + self.assertEqual( + mock_list.return_value, + result + ) + mock_getid.assert_called_once_with(mock_endpoint) + mock_encode_base64_param.assert_called_once_with( + {"env": mock.sentinel.env}, is_json=True) + mock_urlencode.assert_called_once_with( + { + "marker": mock.sentinel.marker, + "limit": mock.sentinel.limit, + "name": mock.sentinel.name, + "env": mock_encode_base64_param.return_value + } + ) + mock_list.assert_called_once_with( + ('/endpoints/sentinel.id/instances' + '?mock_url_query'), + 'instances') + + @mock.patch.object(common, 'encode_base64_param') + def test_list_value_error( + self, + mock_encode_base64_param + ): + mock_endpoint = mock.Mock() + mock_encode_base64_param.return_value = mock.sentinel.encoded_env + + self.assertRaises( + ValueError, + self.endpoint.list, + mock_endpoint, + env=mock.sentinel.env + ) + + mock_encode_base64_param.assert_not_called() + + @mock.patch.object(endpoint_instances.EndpointInstanceManager, '_get') + @mock.patch.object(common, 'encode_base64_param') + @mock.patch.object(base, 'getid') + def test_get( + self, + mock_getid, + mock_encode_base64_param, + mock_get + ): + mock_endpoint = mock.Mock() + mock_getid.return_value = mock.sentinel.id + mock_encode_base64_param.side_effect = [ + mock.sentinel.encoded_id, mock.sentinel.encoded_env] + + result = self.endpoint.get( + mock_endpoint, + mock.sentinel.instance_id, + env={"env": mock.sentinel.env} + ) + + self.assertEqual( + mock_get.return_value, + result + ) + mock_getid.assert_called_once_with(mock_endpoint) + mock_encode_base64_param.assert_has_calls([ + mock.call(mock.sentinel.instance_id), + mock.call({"env": mock.sentinel.env}, is_json=True) + ]) + mock_get.assert_called_once_with( + ('/endpoints/sentinel.id/instances/sentinel.encoded_id' + '?env=sentinel.encoded_env'), + 'instance') + + @mock.patch.object(common, 'encode_base64_param') + def test_get_value_error( + self, + mock_encode_base64_param + ): + mock_endpoint = mock.Mock() + mock_encode_base64_param.return_value = mock.sentinel.encoded_env + + self.assertRaises( + ValueError, + self.endpoint.get, + mock_endpoint, + mock.sentinel.instance_id, + env=mock.sentinel.env + ) + + mock_encode_base64_param.assert_called_once_with( + mock.sentinel.instance_id) From 447660bd5b0b5dd921ee460b92a38c36c9c83c4c Mon Sep 17 00:00:00 2001 From: Cristian Matiut Date: Wed, 22 May 2024 14:28:27 +0300 Subject: [PATCH 06/10] Add unit tests for `coriolisclient.v1.endpoint_destination_networks` module --- .../tests/v1/test_endpoint_networks.py | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 coriolisclient/tests/v1/test_endpoint_networks.py diff --git a/coriolisclient/tests/v1/test_endpoint_networks.py b/coriolisclient/tests/v1/test_endpoint_networks.py new file mode 100644 index 0000000..a8cd6d2 --- /dev/null +++ b/coriolisclient/tests/v1/test_endpoint_networks.py @@ -0,0 +1,46 @@ +# Copyright 2024 Cloudbase Solutions Srl +# All Rights Reserved. + +from unittest import mock + +from coriolisclient import base +from coriolisclient.tests import test_base +from coriolisclient.v1 import common +from coriolisclient.v1 import endpoint_networks + + +class EndpointNetworkManagerTestCase( + test_base.CoriolisBaseTestCase): + """Test suite for the Coriolis v1 Endpoint Network Manager.""" + + def setUp(self): + mock_client = mock.Mock() + super(EndpointNetworkManagerTestCase, self).setUp() + self.endpoint = endpoint_networks.EndpointNetworkManager(mock_client) + + @mock.patch.object(endpoint_networks.EndpointNetworkManager, '_list') + @mock.patch.object(common, 'encode_base64_param') + @mock.patch.object(base, 'getid') + def test_list( + self, + mock_getid, + mock_encode_base64_param, + mock_list + ): + mock_endpoint = mock.Mock() + mock_getid.return_value = mock.sentinel.id + mock_encode_base64_param.return_value = mock.sentinel.encoded_env + + result = self.endpoint.list( + mock_endpoint, + mock.sentinel.environment, + ) + + self.assertEqual( + mock_list.return_value, + result + ) + mock_list.assert_called_once_with( + ('/endpoints/sentinel.id/networks' + '?env=sentinel.encoded_env'), + 'networks') From d90c55dd36d517c741fa43f53db70f5957752b11 Mon Sep 17 00:00:00 2001 From: Cristian Matiut Date: Wed, 22 May 2024 14:28:40 +0300 Subject: [PATCH 07/10] Add unit tests for `coriolisclient.v1.endpoint_source_minion_pool_options` module --- ...est_endpoint_source_minion_pool_options.py | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 coriolisclient/tests/v1/test_endpoint_source_minion_pool_options.py diff --git a/coriolisclient/tests/v1/test_endpoint_source_minion_pool_options.py b/coriolisclient/tests/v1/test_endpoint_source_minion_pool_options.py new file mode 100644 index 0000000..8bb2290 --- /dev/null +++ b/coriolisclient/tests/v1/test_endpoint_source_minion_pool_options.py @@ -0,0 +1,56 @@ +# Copyright 2024 Cloudbase Solutions Srl +# All Rights Reserved. + +from unittest import mock + +from coriolisclient import base +from coriolisclient.tests import test_base +from coriolisclient.v1 import common +from coriolisclient.v1 import endpoint_source_minion_pool_options + + +class EndpointSourceMinionPoolOptionsManagerTestCase( + test_base.CoriolisBaseTestCase): + """ + Test suite for the Coriolis v1 Endpoint Source Minion Pool Options + Manager. + """ + + def setUp(self): + mock_client = mock.Mock() + super(EndpointSourceMinionPoolOptionsManagerTestCase, self + ).setUp() + self.endpoint = ( + endpoint_source_minion_pool_options. + EndpointSourceMinionPoolOptionsManager)(mock_client) + + @mock.patch.object(endpoint_source_minion_pool_options. + EndpointSourceMinionPoolOptionsManager, '_list') + @mock.patch.object(common, 'encode_base64_param') + @mock.patch.object(base, 'getid') + def test_list( + self, + mock_getid, + mock_encode_base64_param, + mock_list + ): + mock_endpoint = mock.Mock() + mock_getid.return_value = mock.sentinel.id + mock_encode_base64_param.side_effect = [ + mock.sentinel.encoded_env, mock.sentinel.encoded_option_names] + + result = self.endpoint.list( + mock_endpoint, + mock.sentinel.environment, + mock.sentinel.option_names + ) + + self.assertEqual( + mock_list.return_value, + result + ) + mock_list.assert_called_once_with( + ('/endpoints/sentinel.id/source-minion-pool-options' + '?env=sentinel.encoded_env' + '&options=sentinel.encoded_option_names'), + 'source_minion_pool_options') From 5443ad3972be797effda57d990d3df6cfe5f1ce7 Mon Sep 17 00:00:00 2001 From: Cristian Matiut Date: Wed, 22 May 2024 14:28:48 +0300 Subject: [PATCH 08/10] Add unit tests for `coriolisclient.v1.endpoint_source_options` module --- .../tests/v1/test_endpoint_source_options.py | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 coriolisclient/tests/v1/test_endpoint_source_options.py diff --git a/coriolisclient/tests/v1/test_endpoint_source_options.py b/coriolisclient/tests/v1/test_endpoint_source_options.py new file mode 100644 index 0000000..7bc16d5 --- /dev/null +++ b/coriolisclient/tests/v1/test_endpoint_source_options.py @@ -0,0 +1,57 @@ +# Copyright 2024 Cloudbase Solutions Srl +# All Rights Reserved. + +from unittest import mock + +from coriolisclient import base +from coriolisclient.tests import test_base +from coriolisclient.v1 import common +from coriolisclient.v1 import endpoint_source_options + + +class EndpointSourceOptionsManagerTestCase( + test_base.CoriolisBaseTestCase): + """Test suite for the Coriolis v1 Endpoint Source Options Manager.""" + + def setUp(self): + mock_client = mock.Mock() + super(EndpointSourceOptionsManagerTestCase, self).setUp() + self.endpoint = ( + endpoint_source_options. + EndpointSourceOptionsManager)(mock_client) + + @mock.patch.object(endpoint_source_options. + EndpointSourceOptionsManager, '_list') + @mock.patch.object(common, 'encode_base64_param') + @mock.patch.object(base, 'getid') + def test_list( + self, + mock_getid, + mock_encode_base64_param, + mock_list + ): + mock_endpoint = mock.Mock() + mock_getid.return_value = mock.sentinel.id + mock_encode_base64_param.side_effect = [ + mock.sentinel.encoded_env, mock.sentinel.encoded_option_names] + + result = self.endpoint.list( + mock_endpoint, + mock.sentinel.environment, + mock.sentinel.option_names + ) + + self.assertEqual( + mock_list.return_value, + result + ) + mock_getid.assert_called_once_with(mock_endpoint) + mock_encode_base64_param.assert_has_calls([ + mock.call(mock.sentinel.environment, is_json=True), + mock.call(mock.sentinel.option_names, is_json=True) + ]) + mock_list.assert_called_once_with( + ('/endpoints/sentinel.id/source-options' + '?env=sentinel.encoded_env' + '&options=sentinel.encoded_option_names'), + 'source_options') From f5dff2069d3fae7060c9f0d4028399a9cfdde39f Mon Sep 17 00:00:00 2001 From: Cristian Matiut Date: Wed, 22 May 2024 14:29:04 +0300 Subject: [PATCH 09/10] Add unit tests for `coriolisclient.v1.endpoint_storage` module --- .../tests/v1/test_endpoint_storage.py | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 coriolisclient/tests/v1/test_endpoint_storage.py diff --git a/coriolisclient/tests/v1/test_endpoint_storage.py b/coriolisclient/tests/v1/test_endpoint_storage.py new file mode 100644 index 0000000..261f820 --- /dev/null +++ b/coriolisclient/tests/v1/test_endpoint_storage.py @@ -0,0 +1,76 @@ +# Copyright 2024 Cloudbase Solutions Srl +# All Rights Reserved. + +from unittest import mock + +from coriolisclient import base +from coriolisclient.tests import test_base +from coriolisclient.v1 import common +from coriolisclient.v1 import endpoint_storage + + +class EndpointStorageManagerTestCase( + test_base.CoriolisBaseTestCase): + """Test suite for the Coriolis v1 Endpoint Storage Manager.""" + + def setUp(self): + mock_client = mock.Mock() + super(EndpointStorageManagerTestCase, self).setUp() + self.endpoint = endpoint_storage.EndpointStorageManager(mock_client) + + @mock.patch.object(endpoint_storage.EndpointStorageManager, '_list') + @mock.patch.object(common, 'encode_base64_param') + @mock.patch.object(base, 'getid') + def test_list( + self, + mock_getid, + mock_encode_base64_param, + mock_list + ): + mock_endpoint = mock.Mock() + mock_getid.return_value = mock.sentinel.id + mock_encode_base64_param.return_value = mock.sentinel.encoded_env + + result = self.endpoint.list( + mock_endpoint, + mock.sentinel.environment, + ) + + self.assertEqual( + mock_list.return_value, + result + ) + mock_list.assert_called_once_with( + ('/endpoints/sentinel.id/storage' + '?env=sentinel.encoded_env'), + 'storage', + values_key='storage_backends') + + @mock.patch.object(endpoint_storage.EndpointStorageManager, '_get') + @mock.patch.object(common, 'encode_base64_param') + @mock.patch.object(base, 'getid') + def test_get_default( + self, + mock_getid, + mock_encode_base64_param, + mock_get + ): + mock_endpoint = mock.Mock() + mock_getid.return_value = mock.sentinel.id + mock_encode_base64_param.return_value = mock.sentinel.encoded_env + mock_get.return_value = { + "config_default": mock.sentinel.config_default} + + result = self.endpoint.get_default( + mock_endpoint, + mock.sentinel.environment, + ) + + self.assertEqual( + mock.sentinel.config_default, + result + ) + mock_get.assert_called_once_with( + ('/endpoints/sentinel.id/storage' + '?env=sentinel.encoded_env'), + 'storage') From b4af9d61a6001032446fa558dbac84abd13703e5 Mon Sep 17 00:00:00 2001 From: Cristian Matiut Date: Wed, 22 May 2024 14:29:12 +0300 Subject: [PATCH 10/10] Add unit tests for `coriolisclient.v1.endpoints` module --- coriolisclient/tests/v1/test_endpoints.py | 271 ++++++++++++++++++++++ 1 file changed, 271 insertions(+) create mode 100644 coriolisclient/tests/v1/test_endpoints.py diff --git a/coriolisclient/tests/v1/test_endpoints.py b/coriolisclient/tests/v1/test_endpoints.py new file mode 100644 index 0000000..127cff5 --- /dev/null +++ b/coriolisclient/tests/v1/test_endpoints.py @@ -0,0 +1,271 @@ +# Copyright 2024 Cloudbase Solutions Srl +# All Rights Reserved. + +from unittest import mock + +from coriolisclient import base +from coriolisclient.cli import utils +from coriolisclient import exceptions +from coriolisclient.tests import test_base +from coriolisclient.v1 import endpoints + + +class EndpointTestCase( + test_base.CoriolisBaseTestCase): + """Test suite for the Coriolis v1 Endpoint.""" + + def setUp(self): + super(EndpointTestCase, self).setUp() + self.endpoint = endpoints.Endpoint( + None, + { + "connection_info": { + "connection_info1": mock.sentinel.connection_info} + } + ) + + def test_flavor_name(self): + result = self.endpoint.connection_info + + self.assertEqual( + mock.sentinel.connection_info, + result.connection_info1 + ) + + +class EndpointManagerTestCase( + test_base.CoriolisBaseTestCase): + """Test suite for the Coriolis v1 Endpoint Manager.""" + + def setUp(self): + self.mock_client = mock.Mock() + super(EndpointManagerTestCase, self).setUp() + self.endpoint = endpoints.EndpointManager(self.mock_client) + + @mock.patch.object(endpoints.EndpointManager, '_list') + def test_list( + self, + mock_list + ): + result = self.endpoint.list() + + self.assertEqual( + mock_list.return_value, + result + ) + mock_list.assert_called_once_with('/endpoints', 'endpoints') + + @mock.patch.object(base, 'getid') + @mock.patch.object(endpoints.EndpointManager, '_get') + def test_get( + self, + mock_get, + mock_getid + ): + mock_endpoint = mock.Mock() + mock_getid.return_value = mock.sentinel.id + + result = self.endpoint.get(mock_endpoint) + + self.assertEqual( + mock_get.return_value, + result + ) + mock_get.assert_called_once_with('/endpoints/sentinel.id', 'endpoint') + + @mock.patch.object(endpoints.EndpointManager, '_post') + def test_create( + self, + mock_post + ): + result = self.endpoint.create( + mock.sentinel.name, + mock.sentinel.endpoint_type, + mock.sentinel.connection_info, + mock.sentinel.description, + mock.sentinel.regions + ) + expected_data = { + "endpoint": { + "name": mock.sentinel.name, + "type": mock.sentinel.endpoint_type, + "description": mock.sentinel.description, + "connection_info": mock.sentinel.connection_info, + "mapped_regions": mock.sentinel.regions + } + } + + self.assertEqual( + mock_post.return_value, + result + ) + mock_post.assert_called_once_with( + '/endpoints', expected_data, 'endpoint') + + @mock.patch.object(base, 'getid') + @mock.patch.object(endpoints.EndpointManager, '_put') + def test_update( + self, + mock_put, + mock_getid + ): + mock_endpoint = mock.Mock() + mock_getid.return_value = mock.sentinel.id + + result = self.endpoint.update( + mock_endpoint, + mock.sentinel.updated_values + ) + expected_data = { + "endpoint": mock.sentinel.updated_values + } + + self.assertEqual( + mock_put.return_value, + result + ) + mock_put.assert_called_once_with( + '/endpoints/sentinel.id', expected_data, 'endpoint') + + @mock.patch.object(base, 'getid') + @mock.patch.object(endpoints.EndpointManager, '_delete') + def test_delete( + self, + mock_delete, + mock_getid + ): + mock_endpoint = mock.Mock() + mock_getid.return_value = mock.sentinel.id + + result = self.endpoint.delete(mock_endpoint) + + self.assertEqual( + mock_delete.return_value, + result + ) + mock_delete.assert_called_once_with('/endpoints/sentinel.id') + + @mock.patch.object(base, 'getid') + def test_validate_connection( + self, + mock_getid + ): + mock_endpoint = mock.Mock() + mock_getid.return_value = mock.sentinel.id + self.mock_client.post.return_value.json.return_value = { + "validate-connection": { + "valid": mock.sentinel.valid, + "message": mock.sentinel.message + } + } + + result = self.endpoint.validate_connection(mock_endpoint) + + self.assertEqual( + (mock.sentinel.valid, mock.sentinel.message), + result + ) + self.mock_client.post.assert_called_once_with( + '/endpoints/sentinel.id/actions', + json={'validate-connection': None}) + + @mock.patch.object(endpoints.EndpointManager, '_get_endpoint_id_for_name') + @mock.patch.object(utils, 'validate_uuid_string') + def test_get_endpoint_id_for_name( + self, + mock_validate_uuid_string, + mock_get_endpoint_id_for_name + ): + mock_endpoint = mock.Mock() + + result = self.endpoint.get_endpoint_id_for_name(mock_endpoint) + + self.assertEqual( + mock_endpoint, + result + ) + mock_get_endpoint_id_for_name.assert_not_called() + + mock_validate_uuid_string.return_value = None + + result = self.endpoint.get_endpoint_id_for_name(mock_endpoint) + + self.assertEqual( + mock_get_endpoint_id_for_name.return_value, + result + ) + + @mock.patch.object(endpoints.EndpointManager, 'list') + @mock.patch.object(utils, 'validate_uuid_string') + def test__get_endpoint_id_for_name( + self, + mock_validate_uuid_string, + mock_list + ): + obj1 = mock.Mock() + obj2 = mock.Mock() + obj3 = mock.Mock() + obj1.id = '1' + obj2.id = '2' + obj3.id = '3' + obj1.name = 'mock_name1' + obj2.name = 'mock_name2' + obj3.name = 'mock_name3' + obj_list = [obj1, obj2, obj3] + mock_list.return_value = obj_list + endpoint_name = "mock_name2" + + result = self.endpoint._get_endpoint_id_for_name(endpoint_name) + + self.assertEqual( + '2', + result + ) + + @mock.patch.object(endpoints.EndpointManager, 'list') + def test__get_endpoint_id_for_name_not_found( + self, + mock_list + ): + obj1 = mock.Mock() + obj2 = mock.Mock() + obj3 = mock.Mock() + obj1.id = '1' + obj2.id = '2' + obj3.id = '3' + obj1.name = 'mock_name1' + obj2.name = 'mock_name2' + obj3.name = 'mock_name3' + obj_list = [obj1, obj2, obj3] + mock_list.return_value = obj_list + endpoint_name = "mock_name4" + + self.assertRaises( + exceptions.EndpointIDNotFound, + self.endpoint._get_endpoint_id_for_name, + endpoint_name + ) + + @mock.patch.object(endpoints.EndpointManager, 'list') + def test__get_endpoint_id_for_name_not_unique( + self, + mock_list + ): + obj1 = mock.Mock() + obj2 = mock.Mock() + obj3 = mock.Mock() + obj1.id = '1' + obj2.id = '2' + obj3.id = '3' + obj1.name = 'mock_name1' + obj2.name = 'mock_name2' + obj3.name = 'mock_name2' + obj_list = [obj1, obj2, obj3] + mock_list.return_value = obj_list + endpoint_name = "mock_name2" + + self.assertRaises( + exceptions.NoUniqueEndpointNameMatch, + self.endpoint._get_endpoint_id_for_name, + endpoint_name + )