Skip to content

Commit

Permalink
Add unit test for Coriolis api v1
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristi1324 committed Nov 28, 2023
1 parent bfad098 commit 7b666f8
Show file tree
Hide file tree
Showing 12 changed files with 1,512 additions and 0 deletions.
38 changes: 38 additions & 0 deletions coriolis/tests/api/v1/test_diagnostics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2023 Cloudbase Solutions Srl
# All Rights Reserved.

from unittest import mock

from coriolis.api.v1 import diagnostics as diag
from coriolis.api.v1.views import diagnostic_view
from coriolis.diagnostics import api
from coriolis.policies import diagnostics
from coriolis.tests import test_base


class DiagnosticsControllerTestCase(test_base.CoriolisBaseTestCase):
"Test suite for the Coriolis api v1."""

def setUp(self):
super(DiagnosticsControllerTestCase, self).setUp()
self.diag_api = diag.DiagnosticsController()

@mock.patch.object(api.API, 'get')
@mock.patch.object(diagnostic_view, 'collection')
@mock.patch.object(diagnostics, 'get_diagnostics_policy_label')
def test_index(
self,
mock_get_diagnostics_policy_label,
mock_collection,
mock_get
):
mock_req = mock.Mock()
mock_context = mock.Mock()
mock_req.environ = {'coriolis.context': mock_context}

result = self.diag_api.index(mock_req)

mock_get_diagnostics_policy_label.assert_called_with("get")
mock_get.assert_called_with(mock_context)
mock_collection.assert_called_with(mock_get.return_value)
self.assertEqual(result, mock_collection.return_value)
99 changes: 99 additions & 0 deletions coriolis/tests/api/v1/test_endpoint_actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Copyright 2023 Cloudbase Solutions Srl
# All Rights Reserved.

from unittest import mock

from coriolis.api.v1 import endpoint_actions
from coriolis.endpoints import api
from coriolis import exception
from coriolis.tests import test_base

from webob import exc


class EndpointActionsControllerTestCase(test_base.CoriolisBaseTestCase):
"Test suite for the Coriolis api v1."""

def setUp(self):
super(EndpointActionsControllerTestCase, self).setUp()
self.endpoint_api = endpoint_actions.EndpointActionsController()

@mock.patch.object(api.API, 'validate_connection')
@mock.patch('coriolis.policies.endpoints.ENDPOINTS_POLICY_PREFIX',
'mock_prefix')
def test_validate_connection(
self,
mock_validate_connection
):
mock_req = mock.Mock()
mock_context = mock.Mock()
mock_req.environ = {'coriolis.context': mock_context}
id = mock.sentinel.id
body = mock.sentinel.body
is_valid = True
message = 'mock_message'
mock_validate_connection.return_value = (is_valid, message)

expected_result = {
"validate-connection":
{"valid": is_valid, "message": message}
}
result = self.endpoint_api._validate_connection(
mock_req,
id,
body
)

mock_validate_connection.assert_called_with(mock_context, id)
self.assertEqual(
expected_result,
result
)

@mock.patch.object(api.API, 'validate_connection')
@mock.patch('coriolis.policies.endpoints.ENDPOINTS_POLICY_PREFIX',
'mock_prefix')
def test_validate_connection_except_not_found(
self,
mock_validate_connection
):
mock_req = mock.Mock()
mock_context = mock.Mock()
mock_req.environ = {'coriolis.context': mock_context}
id = mock.sentinel.id
body = mock.sentinel.body
mock_validate_connection.side_effect = exception.NotFound

self.assertRaises(
exc.HTTPNotFound,
self.endpoint_api._validate_connection,
mock_req,
id,
body
)
mock_validate_connection.assert_called_with(mock_context, id)

@mock.patch.object(api.API, 'validate_connection')
@mock.patch('coriolis.policies.endpoints.ENDPOINTS_POLICY_PREFIX',
'mock_prefix')
def test_validate_connection_except_invalid_parameter_value(
self,
mock_validate_connection
):
mock_req = mock.Mock()
mock_context = mock.Mock()
mock_req.environ = {'coriolis.context': mock_context}
id = mock.sentinel.id
body = mock.sentinel.body
mock_validate_connection.side_effect = exception.InvalidParameterValue(
"mock_err"
)

self.assertRaises(
exc.HTTPNotFound,
self.endpoint_api._validate_connection,
mock_req,
id,
body
)
mock_validate_connection.assert_called_with(mock_context, id)
107 changes: 107 additions & 0 deletions coriolis/tests/api/v1/test_endpoint_destination_minion_pool_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Copyright 2023 Cloudbase Solutions Srl
# All Rights Reserved.

from unittest import mock

from coriolis.api.v1 import endpoint_destination_minion_pool_options \
as endpoint
from coriolis.api.v1.views import endpoint_options_view
from coriolis.endpoint_options import api
from coriolis.tests import test_base
from coriolis import utils


class EndpointDestinationMinionPoolOptionsControllerTestCase(
test_base.CoriolisBaseTestCase
):
"Test suite for the Coriolis api v1."""

def setUp(self):
super(
EndpointDestinationMinionPoolOptionsControllerTestCase,
self
).setUp()
self.minion_api = \
endpoint.EndpointDestinationMinionPoolOptionsController()

@mock.patch('coriolis.policies.endpoints.ENDPOINTS_POLICY_PREFIX',
'mock_prefix')
@mock.patch.object(utils, 'decode_base64_param')
@mock.patch.object(endpoint_options_view,
'destination_minion_pool_options_collection')
@mock.patch.object(api.API,
'get_endpoint_destination_minion_pool_options')
def test_index(
self,
mock_get_endpoint_destination_minion_pool_options,
mock_destination_minion_pool_options_collection,
mock_decode_base64_param,
):
mock_req = mock.Mock()
mock_context = mock.Mock()
endpoint_id = mock.sentinel.endpoint_id
mock_req.environ = {'coriolis.context': mock_context}
env = mock.sentinel.env
options = mock.sentinel.options
mock_req.GET = {
'env': env,
'options': options
}
mock_decode_base64_param.side_effect = [env, options]

expected_calls = [
mock.call.mock_get_diagnostics_policy_label(
env, is_json=True),
mock.call.mock_get_diagnostics_policy_label(
options, is_json=True)]

result = self.minion_api.index(mock_req, endpoint_id)

mock_decode_base64_param.has_calls(expected_calls)
(mock_get_endpoint_destination_minion_pool_options.
assert_called_once_with)(
mock_context, endpoint_id,
env=env,
option_names=options)
(mock_destination_minion_pool_options_collection.
assert_called_once_with)(
mock_get_endpoint_destination_minion_pool_options.return_value)
self.assertEqual(
mock_destination_minion_pool_options_collection.return_value,
result
)

@mock.patch('coriolis.policies.endpoints.ENDPOINTS_POLICY_PREFIX',
'mock_prefix')
@mock.patch.object(utils, 'decode_base64_param')
@mock.patch.object(endpoint_options_view,
'destination_minion_pool_options_collection')
@mock.patch.object(api.API,
'get_endpoint_destination_minion_pool_options')
def test_index_no_env_and_options(
self,
mock_get_endpoint_destination_minion_pool_options,
mock_destination_minion_pool_options_collection,
mock_decode_base64_param,
):
mock_req = mock.Mock()
mock_context = mock.Mock()
endpoint_id = mock.sentinel.endpoint_id
mock_req.environ = {'coriolis.context': mock_context}
mock_req.GET = {}

result = self.minion_api.index(mock_req, endpoint_id)

mock_decode_base64_param.assert_not_called()
(mock_get_endpoint_destination_minion_pool_options.
assert_called_once_with)(
mock_context, endpoint_id,
env={},
option_names={})
(mock_destination_minion_pool_options_collection.
assert_called_once_with)(
mock_get_endpoint_destination_minion_pool_options.return_value)
self.assertEqual(
mock_destination_minion_pool_options_collection.return_value,
result
)
106 changes: 106 additions & 0 deletions coriolis/tests/api/v1/test_endpoint_destination_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Copyright 2023 Cloudbase Solutions Srl
# All Rights Reserved.

from unittest import mock

from coriolis.api.v1 import endpoint_destination_options as endpoint
from coriolis.api.v1.views import endpoint_options_view
from coriolis.endpoint_options import api
from coriolis.tests import test_base
from coriolis import utils


class EndpointDestinationOptionsControllerTestCase(
test_base.CoriolisBaseTestCase
):
"Test suite for the Coriolis api v1."""

def setUp(self):
super(
EndpointDestinationOptionsControllerTestCase,
self
).setUp()
self.endpoint_api = \
endpoint.EndpointDestinationOptionsController()

@mock.patch('coriolis.policies.endpoints.ENDPOINTS_POLICY_PREFIX',
'mock_prefix')
@mock.patch.object(utils, 'decode_base64_param')
@mock.patch.object(endpoint_options_view,
'destination_options_collection')
@mock.patch.object(api.API,
'get_endpoint_destination_options')
def test_index(
self,
mock_get_endpoint_destination_options,
mock_destination_options_collection,
mock_decode_base64_param,
):
mock_req = mock.Mock()
mock_context = mock.Mock()
endpoint_id = mock.sentinel.endpoint_id
mock_req.environ = {'coriolis.context': mock_context}
env = mock.sentinel.env
options = mock.sentinel.options
mock_req.GET = {
'env': env,
'options': options
}
mock_decode_base64_param.side_effect = [env, options]

expected_calls = [
mock.call.mock_decode_base64_param(
env, is_json=True),
mock.call.mock_decode_base64_param(
options, is_json=True)]

result = self.endpoint_api.index(mock_req, endpoint_id)

mock_decode_base64_param.has_calls(expected_calls)
(mock_get_endpoint_destination_options.
assert_called_once_with)(
mock_context, endpoint_id,
env=env,
option_names=options)
(mock_destination_options_collection.
assert_called_once_with)(
mock_get_endpoint_destination_options.return_value)
self.assertEqual(
mock_destination_options_collection.return_value,
result
)

@mock.patch('coriolis.policies.endpoints.ENDPOINTS_POLICY_PREFIX',
'mock_prefix')
@mock.patch.object(utils, 'decode_base64_param')
@mock.patch.object(endpoint_options_view,
'destination_options_collection')
@mock.patch.object(api.API,
'get_endpoint_destination_options')
def test_index_no_env_and_options(
self,
mock_get_endpoint_destination_options,
mock_destination_options_collection,
mock_decode_base64_param,
):
mock_req = mock.Mock()
mock_context = mock.Mock()
endpoint_id = mock.sentinel.endpoint_id
mock_req.environ = {'coriolis.context': mock_context}
mock_req.GET = {}

result = self.endpoint_api.index(mock_req, endpoint_id)

mock_decode_base64_param.assert_not_called()
(mock_get_endpoint_destination_options.
assert_called_once_with)(
mock_context, endpoint_id,
env={},
option_names={})
(mock_destination_options_collection.
assert_called_once_with)(
mock_get_endpoint_destination_options.return_value)
self.assertEqual(
mock_destination_options_collection.return_value,
result
)
Loading

0 comments on commit 7b666f8

Please sign in to comment.