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 6, 2023
1 parent 1209a56 commit 619b3ff
Show file tree
Hide file tree
Showing 22 changed files with 2,116 additions and 18 deletions.
4 changes: 2 additions & 2 deletions coriolis/api/v1/minion_pool_actions.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Copyright 2016 Cloudbase Solutions Srl
# All Rights Reserved.

from webob import exc

from coriolis.api.v1.views import minion_pool_view
from coriolis.api import wsgi as api_wsgi
from coriolis import exception
from coriolis.minion_pools import api
from coriolis.policies import minion_pools as minion_pool_policies

from webob import exc


class MinionPoolActionsController(api_wsgi.Controller):
def __init__(self):
Expand Down
35 changes: 19 additions & 16 deletions coriolis/api/v1/minion_pools.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,23 @@ def create(self, req, body):
minion_max_idle_time, minion_retention_strategy, notes=notes,
skip_allocation=skip_allocation))

def _validate_updated_environment_options(self, context, minion_pool,
environment_options):
if minion_pool['platform'] == (
constants.PROVIDER_PLATFORM_SOURCE):
(self._endpoints_api.
validate_endpoint_source_minion_pool_options)(
context, minion_pool['endpoint_id'], environment_options)
elif minion_pool['platform'] == (
constants.PROVIDER_PLATFORM_DESTINATION):
(self._endpoints_api.
validate_endpoint_destination_minion_pool_options)(
context, minion_pool['endpoint_id'], environment_options)
else:
raise Exception(
"Unknown pool platform: %s" % minion_pool[
'platform'])

@api_utils.format_keyerror_message(resource='minion_pool',
method='update')
def _validate_update_body(self, id, context, body):
Expand Down Expand Up @@ -174,22 +191,8 @@ def _validate_update_body(self, id, context, body):
vals.get('minion_max_idle_time'))

if 'environment_options' in vals:
if minion_pool['platform'] == (
constants.PROVIDER_PLATFORM_SOURCE):
(self._endpoints_api.
validate_endpoint_source_minion_pool_options)(
context, minion_pool['endpoint_id'],
vals['environment_options'])
elif minion_pool['platform'] == (
constants.PROVIDER_PLATFORM_DESTINATION):
(self._endpoints_api.
validate_endpoint_destination_minion_pool_options)(
context, minion_pool['endpoint_id'],
vals['environment_options'])
else:
raise Exception(
"Unknown pool platform: %s" % minion_pool[
'platform'])
self._validate_updated_environment_options(
context, minion_pool, vals['environment_options'])
return vals

def update(self, req, id, body):
Expand Down
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 @@

- config:
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

- config:
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
2 changes: 2 additions & 0 deletions coriolis/tests/api/v1/data/minion_pool_exceptions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- NotFound
- InvalidParameterValue
34 changes: 34 additions & 0 deletions coriolis/tests/api/v1/data/minion_pool_numeric_values.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
- config:
{}
exception_raised: False

- config:
minimum_minions: 1
maximum_minions: 1
minion_max_idle_time: 0
exception_raised: False

- config:
minimum_minions: 0
exception_raised: "'minimum_minions' must be a strictly positive integer. "

- config:
minimum_minions: 1
maximum_minions: 0
exception_raised: "'maximum_minions' must be a strictly positive integer. "

- config:
minimum_minions: 1
maximum_minions: 1
exception_raised: False

- config:
minimum_minions: 2
maximum_minions: 1
exception_raised: "'maximum_minions' value .* must be at least as large as"

- config:
minimum_minions: 1
maximum_minions: 1
minion_max_idle_time: -1
exception_raised: "'minion_max_idle_time' must be a strictly positive "
15 changes: 15 additions & 0 deletions coriolis/tests/api/v1/data/minion_pool_retention_strategy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
- config:
exception_raised: True
pool_retention_strategy: null

- config:
exception_raised: False
pool_retention_strategy: 'MINION_POOL_MACHINE_RETENTION_STRATEGY_DELETE'

- config:
exception_raised: False
pool_retention_strategy: 'MINION_POOL_MACHINE_RETENTION_STRATEGY_POWEROFF'

- config:
exception_raised: True
pool_retention_strategy: 'invalid'
65 changes: 65 additions & 0 deletions coriolis/tests/api/v1/data/minion_validate_create_body.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

- config:
body:
minion_pool:
name: "mock_name"
endpoint_id: "mock_endpoint_id"
os_type: "OS_TYPE_LINUX"
platform: "PROVIDER_PLATFORM_SOURCE"
environment_options: "mock_environment_options"
notes: "mock_notes"
minion_retention_strategy: ""
expected_validation_api_method: "validate_endpoint_source_minion_pool_options"
exception_raised: False

- config:
body:
minion_pool:
name: "mock_name"
endpoint_id: "mock_endpoint_id"
os_type: "OS_TYPE_LINUX"
platform: "PROVIDER_PLATFORM_DESTINATION"
environment_options: "mock_environment_options"
notes: "mock_notes"
minion_retention_strategy: ""
expected_validation_api_method: "validate_endpoint_destination_minion_pool_options"
exception_raised: False

- config:
body:
minion_pool:
name: "mock_name"
endpoint_id: "mock_endpoint_id"
os_type: "invalid"
platform: "PROVIDER_PLATFORM_SOURCE"
environment_options: "mock_environment_options"
notes: "mock_notes"
minion_retention_strategy: ""
expected_validation_api_method: "validate_endpoint_source_minion_pool_options"
exception_raised: "The provided pool OS type .* is invalid."

- config:
body:
minion_pool:
name: "mock_name"
endpoint_id: "mock_endpoint_id"
os_type: "OS_TYPE_LINUX"
platform: "invalid"
environment_options: "mock_environment_options"
notes: "mock_notes"
minion_retention_strategy: ""
expected_validation_api_method: "validate_endpoint_source_minion_pool_options"
exception_raised: "The provided pool platform .* is invalid."

- config:
body:
minion_pool:
name: "mock_name"
endpoint_id: "mock_endpoint_id"
os_type: "OS_TYPE_WINDOWS"
platform: "PROVIDER_PLATFORM_SOURCE"
environment_options: "mock_environment_options"
notes: "mock_notes"
minion_retention_strategy: ""
expected_validation_api_method: "validate_endpoint_source_minion_pool_options"
exception_raised: "Source Minion Pools are required to be of OS type "
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 @@

- config:
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

- config:
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


- config:
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
35 changes: 35 additions & 0 deletions coriolis/tests/api/v1/test_diagnostics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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.tests import test_base


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

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

@mock.patch.object(api.API, 'get')
@mock.patch.object(diagnostic_view, 'collection')
def test_index(
self,
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_context.can.assert_called_once_with("migration:diagnostics: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)
Loading

0 comments on commit 619b3ff

Please sign in to comment.