-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for
coriolis.api.v1.utils
module
- Loading branch information
1 parent
62b2252
commit a2164d9
Showing
3 changed files
with
273 additions
and
0 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
coriolis/tests/api/v1/data/utils_validate_instances_list_for_transfer.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
- instances: ['mock_instance_1', 'mock_instance_2'] | ||
exception_raised: False | ||
expected_result: ['mock_instance_1', 'mock_instance_2'] | ||
|
||
- instances: | ||
exception_raised: True | ||
expected_result: | ||
|
||
- instances: | ||
mock_instance_1: 'mock_value_1' | ||
mock_instance_2: 'mock_value_2' | ||
exception_raised: True | ||
expected_result: | ||
|
||
- instances: ['mock_instance_1', 'mock_instance_2', 'mock_instance_2'] | ||
exception_raised: True | ||
expected_result: |
16 changes: 16 additions & 0 deletions
16
coriolis/tests/api/v1/data/utils_validate_user_scripts_raises.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
- user_scripts: "invalid" | ||
|
||
- user_scripts: | ||
global: "invalid" | ||
|
||
- user_scripts: | ||
global: | ||
invalid_os: 'mock_scripts_invalid_os' | ||
windows: 'mock_scripts_windows' | ||
|
||
- user_scripts: | ||
global: | ||
linux: 'mock_scripts_linux' | ||
windows: 'mock_scripts_windows' | ||
instances: "invalid" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
# Copyright 2023 Cloudbase Solutions Srl | ||
# All Rights Reserved. | ||
|
||
from unittest import mock | ||
|
||
import ddt | ||
from webob import exc | ||
|
||
from coriolis.api.v1 import utils | ||
from coriolis import exception | ||
from coriolis import schemas | ||
from coriolis.tests import test_base | ||
|
||
|
||
@ddt.ddt | ||
class UtilsTestCase(test_base.CoriolisBaseTestCase): | ||
"""Test suite for the Coriolis Utils v1 API""" | ||
|
||
def test_get_show_deleted( | ||
self | ||
): | ||
val = """true""" | ||
|
||
result = utils._get_show_deleted(val) | ||
|
||
self.assertEqual( | ||
True, | ||
result | ||
) | ||
|
||
def test_get_show_deleted_none( | ||
self | ||
): | ||
result = utils._get_show_deleted(None) | ||
|
||
self.assertEqual( | ||
None, | ||
result | ||
) | ||
|
||
def test_get_show_deleted_invalid_json( | ||
self | ||
): | ||
val = """}invalid{""" | ||
|
||
with self.assertLogs('coriolis.api.v1.utils', level='WARN'): | ||
result = utils._get_show_deleted(val) | ||
|
||
self.assertEqual( | ||
None, | ||
result | ||
) | ||
|
||
@mock.patch.object(schemas, 'validate_value') | ||
def test_validate_network_map( | ||
self, | ||
mock_validate_value | ||
): | ||
network_map = "mock_network_map" | ||
|
||
utils.validate_network_map(network_map) | ||
|
||
mock_validate_value.assert_called_once_with( | ||
network_map, schemas.CORIOLIS_NETWORK_MAP_SCHEMA) | ||
|
||
@mock.patch.object(schemas, 'validate_value') | ||
def test_validate_network_map_schema_validation_exception( | ||
self, | ||
mock_validate_value | ||
): | ||
network_map = "mock_network_map" | ||
mock_validate_value.side_effect = exception.SchemaValidationException() | ||
|
||
self.assertRaises( | ||
exc.HTTPBadRequest, | ||
utils.validate_network_map, | ||
network_map | ||
) | ||
|
||
mock_validate_value.assert_called_once_with( | ||
network_map, schemas.CORIOLIS_NETWORK_MAP_SCHEMA) | ||
|
||
@mock.patch.object(schemas, 'validate_value') | ||
def test_validate_storage_mappings( | ||
self, | ||
mock_validate_value | ||
): | ||
storage_mappings = "mock_storage_mappings" | ||
|
||
utils.validate_storage_mappings(storage_mappings) | ||
|
||
mock_validate_value.assert_called_once_with( | ||
storage_mappings, schemas.CORIOLIS_STORAGE_MAPPINGS_SCHEMA) | ||
|
||
@mock.patch.object(schemas, 'validate_value') | ||
def test_validate_storage_mappings_schema_validation_exception( | ||
self, | ||
mock_validate_value | ||
): | ||
storage_mappings = "mock_storage_mappings" | ||
mock_validate_value.side_effect = exception.SchemaValidationException() | ||
|
||
self.assertRaises( | ||
exc.HTTPBadRequest, | ||
utils.validate_storage_mappings, | ||
storage_mappings | ||
) | ||
|
||
mock_validate_value.assert_called_once_with( | ||
storage_mappings, schemas.CORIOLIS_STORAGE_MAPPINGS_SCHEMA) | ||
|
||
def test_build_keyerror_message_resouce_equals_key( | ||
self, | ||
): | ||
resource = "mock_resource" | ||
method = "create" | ||
key = "mock_resource" | ||
|
||
result = utils._build_keyerror_message(resource, method, key) | ||
|
||
self.assertEqual( | ||
'The mock_resource creation body needs to be encased inside the' | ||
' "mock_resource" key', | ||
result | ||
) | ||
|
||
def test_build_keyerror_message_resouce_not_equals_key( | ||
self, | ||
): | ||
resource = "mock_resource" | ||
method = "update" | ||
key = "mock_key" | ||
|
||
result = utils._build_keyerror_message(resource, method, key) | ||
|
||
self.assertEqual( | ||
'The mock_resource update body lacks a required attribute: ' | ||
'"mock_key"', | ||
result | ||
) | ||
|
||
def test_validate_user_scripts( | ||
self, | ||
): | ||
user_scripts = { | ||
'global': { | ||
'linux': 'mock_scripts_linux', | ||
'windows': 'mock_scripts_windows' | ||
}, | ||
'instances': {} | ||
} | ||
|
||
result = utils.validate_user_scripts(user_scripts) | ||
|
||
self.assertEqual( | ||
user_scripts, | ||
result | ||
) | ||
|
||
def test_validate_user_scripts_none( | ||
self, | ||
): | ||
user_scripts = None | ||
|
||
result = utils.validate_user_scripts(user_scripts) | ||
|
||
self.assertEqual( | ||
{}, | ||
result | ||
) | ||
|
||
@ddt.file_data('data/utils_validate_user_scripts_raises.yml') | ||
def test_validate_user_scripts_invalid_input( | ||
self, | ||
user_scripts | ||
): | ||
self.assertRaises( | ||
exception.InvalidInput, | ||
utils.validate_user_scripts, | ||
user_scripts | ||
) | ||
|
||
def test_normalize_user_scripts( | ||
self | ||
): | ||
user_scripts = { | ||
'instances': { | ||
"mock_instance_1": "mock_value_1", | ||
"mock_instance_2": "mock_value_2" | ||
} | ||
} | ||
instances = ["mock_instance_2", "mock_instance_3"] | ||
expected_result = { | ||
'instances': { | ||
"mock_instance_2": "mock_value_2" | ||
} | ||
} | ||
|
||
with self.assertLogs('coriolis.api.v1.utils', level='WARN'): | ||
result = utils.normalize_user_scripts(user_scripts, instances) | ||
|
||
self.assertEqual( | ||
expected_result, | ||
result | ||
) | ||
|
||
def test_normalize_user_scripts_none( | ||
self | ||
): | ||
user_scripts = None | ||
instances = None | ||
|
||
result = utils.normalize_user_scripts(user_scripts, instances) | ||
|
||
self.assertEqual( | ||
{}, | ||
result | ||
) | ||
|
||
@ddt.file_data('data/utils_validate_instances_list_for_transfer.yml') | ||
def test_validate_instances_list_for_transfer( | ||
self, | ||
instances, | ||
expected_result, | ||
exception_raised | ||
): | ||
if exception_raised: | ||
self.assertRaises( | ||
exception.InvalidInput, | ||
utils.validate_instances_list_for_transfer, | ||
instances | ||
) | ||
else: | ||
result = utils.validate_instances_list_for_transfer(instances) | ||
|
||
self.assertEqual( | ||
expected_result, | ||
result | ||
) |