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 Dec 4, 2023
1 parent 1209a56 commit 47a8267
Show file tree
Hide file tree
Showing 14 changed files with 1,593 additions and 0 deletions.
28 changes: 28 additions & 0 deletions coriolis/tests/api/v1/data/create_migration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

- migrations:
migration:
user_scripts:
mock_user_scripts: null
instances: ["mock_instance1", "mock_instance2"]
replica_id: 'mock_replica_id'
clone_disks: True
force: False
skip_os_morphing: False
instance_osmorphing_minion_pool_mappings:
mock_mapping: "mock_value"
expected_api_method: "deploy_replica_instances"
validation_expected: False

- migrations:
migration:
user_scripts:
mock_user_scripts: null
instances: ["mock_instance1", "mock_instance2"]
replica_id: null
clone_disks: True
force: False
skip_os_morphing: False
instance_osmorphing_minion_pool_mappings:
mock_mapping: "mock_value"
expected_api_method: "migrate_instances"
validation_expected: True
48 changes: 48 additions & 0 deletions coriolis/tests/api/v1/data/validate_migration_input.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

- migrations:
migration:
origin_endpoint_id: "mock_origin_endpoint_id"
destination_endpoint_id: "mock_destination_endpoint_id"
origin_minion_pool_id: "mock_origin_minion_pool_id"
destination_minion_pool_id: "mock_destination_minion_pool_id"
instance_osmorphing_minion_pool_mappings:
mock_instance_1: "mock_pool"
mock_instance_2: "mock_pool"
instances: ['mock_instance_1', 'mock_instance_2']
notes: "mock_notes"
skip_os_morphing: false
shutdown_instances: false
replication_count: 2
source_environment: {}
network_map: {}
destination_environment:
network_map: {}
storage_mappings: {}
storage_mappings: {}
raises_value_error: false

- migrations:
migration:
origin_endpoint_id: "mock_origin_endpoint_id"
destination_endpoint_id: "mock_destination_endpoint_id"
origin_minion_pool_id: "mock_origin_minion_pool_id"
destination_minion_pool_id: "mock_destination_minion_pool_id"
instance_osmorphing_minion_pool_mappings:
mock_instance_1: "mock_pool"
mock_instance_2: "mock_pool"
instances: ['mock_instance_1', 'mock_instance_3']
raises_value_error: true


- migrations:
migration:
origin_endpoint_id: "mock_origin_endpoint_id"
destination_endpoint_id: "mock_destination_endpoint_id"
origin_minion_pool_id: "mock_origin_minion_pool_id"
destination_minion_pool_id: "mock_destination_minion_pool_id"
instance_osmorphing_minion_pool_mappings:
mock_instance_1: "mock_pool"
mock_instance_2: "mock_pool"
instances: ['mock_instance_1', 'mock_instance_2']
replication_count: 13
raises_value_error: true
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_once_with("get")
mock_get.assert_called_once_with(mock_context)
mock_collection.assert_called_once_with(mock_get.return_value)
self.assertEqual(result, mock_collection.return_value)
97 changes: 97 additions & 0 deletions coriolis/tests/api/v1/test_endpoint_actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Copyright 2023 Cloudbase Solutions Srl
# All Rights Reserved.

from unittest import mock

from webob import exc

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


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')
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 = testutils.get_wrapped_function(
self.endpoint_api._validate_connection)(
mock_req,
id,
body # type: ignore
)

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

@mock.patch.object(api.API, 'validate_connection')
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,
testutils.get_wrapped_function(
self.endpoint_api._validate_connection),
mock_req,
id,
body
)
mock_validate_connection.assert_called_once_with(mock_context, id)

@mock.patch.object(api.API, 'validate_connection')
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,
testutils.get_wrapped_function(
self.endpoint_api._validate_connection),
mock_req,
id,
body
)
mock_validate_connection.assert_called_once_with(mock_context, id)
101 changes: 101 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,101 @@
# 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.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.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
)
Loading

0 comments on commit 47a8267

Please sign in to comment.