Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for coriolisclient.v1.* modules #86

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Empty file.
65 changes: 65 additions & 0 deletions coriolisclient/tests/v1/test_common.py
Original file line number Diff line number Diff line change
@@ -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
)
26 changes: 26 additions & 0 deletions coriolisclient/tests/v1/test_diagnostics.py
Original file line number Diff line number Diff line change
@@ -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')
Original file line number Diff line number Diff line change
@@ -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')
57 changes: 57 additions & 0 deletions coriolisclient/tests/v1/test_endpoint_destination_options.py
Original file line number Diff line number Diff line change
@@ -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')
154 changes: 154 additions & 0 deletions coriolisclient/tests/v1/test_endpoint_instances.py
Original file line number Diff line number Diff line change
@@ -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)
Loading
Loading