|
|
@@ -2,11 +2,13 @@
|
|
|
# All Rights Reserved.
|
|
|
|
|
|
import json
|
|
|
+import logging
|
|
|
+from unittest import mock
|
|
|
|
|
|
import jinja2
|
|
|
import jsonschema
|
|
|
-from unittest import mock
|
|
|
|
|
|
+from coriolis import exception
|
|
|
from coriolis import schemas
|
|
|
from coriolis.tests import test_base
|
|
|
|
|
|
@@ -66,6 +68,40 @@ class SchemasTestCase(test_base.CoriolisBaseTestCase):
|
|
|
|
|
|
self.assertEqual(res, test_loaded_schema)
|
|
|
|
|
|
+ @mock.patch.object(jinja2, 'Environment')
|
|
|
+ @mock.patch.object(jinja2, 'PackageLoader')
|
|
|
+ def test_get_schema_parent_package_exists(self, mock_loader, mock_environ):
|
|
|
+ test_schema_name = 'test.schema.name'
|
|
|
+ test_package_name = 'test.package.name'
|
|
|
+
|
|
|
+ mock_template = mock.MagicMock()
|
|
|
+ mock_template.render.return_value = '{}'
|
|
|
+
|
|
|
+ mock_environ.return_value.get_template.return_value = mock_template
|
|
|
+
|
|
|
+ mock_loader.side_effect = [ValueError(), mock.MagicMock()]
|
|
|
+
|
|
|
+ schemas.get_schema(test_package_name, test_schema_name)
|
|
|
+
|
|
|
+ parent_package = "test.package"
|
|
|
+ mock_loader.assert_has_calls([
|
|
|
+ mock.call(test_package_name, schemas.DEFAULT_SCHEMAS_DIRECTORY),
|
|
|
+ mock.call(parent_package, schemas.DEFAULT_SCHEMAS_DIRECTORY)])
|
|
|
+
|
|
|
+ @mock.patch.object(jinja2, 'PackageLoader')
|
|
|
+ def test_get_schema_parent_package_not_exists(self, mock_loader):
|
|
|
+ test_schema_name = 'test.schema.name'
|
|
|
+ test_package_name = 'testpackage'
|
|
|
+
|
|
|
+ mock_loader.side_effect = ValueError()
|
|
|
+
|
|
|
+ self.assertRaises(
|
|
|
+ ValueError, schemas.get_schema, test_package_name,
|
|
|
+ test_schema_name)
|
|
|
+
|
|
|
+ mock_loader.assert_called_once_with(
|
|
|
+ test_package_name, schemas.DEFAULT_SCHEMAS_DIRECTORY)
|
|
|
+
|
|
|
@mock.patch.object(jsonschema, 'validate')
|
|
|
def test_validate_value(self, mock_validate):
|
|
|
test_value = mock.sentinel.test_value
|
|
|
@@ -90,3 +126,28 @@ class SchemasTestCase(test_base.CoriolisBaseTestCase):
|
|
|
mock_loads.assert_called_once_with(test_string)
|
|
|
mock_validate.assert_called_once_with(
|
|
|
test_value, test_schema, format_checker=None)
|
|
|
+
|
|
|
+ @mock.patch.object(jsonschema, 'validate')
|
|
|
+ def test_validate_value_raises_on_error(self, mock_validate):
|
|
|
+ test_value = mock.sentinel.test_value
|
|
|
+ test_schema = mock.sentinel.test_schema
|
|
|
+
|
|
|
+ mock_validate.side_effect = jsonschema.exceptions.ValidationError(
|
|
|
+ 'Test Error')
|
|
|
+
|
|
|
+ self.assertRaises(
|
|
|
+ exception.SchemaValidationException, schemas.validate_value,
|
|
|
+ test_value, test_schema, raise_on_error=True)
|
|
|
+
|
|
|
+ @mock.patch.object(jsonschema, 'validate')
|
|
|
+ def test_validate_value_no_raise_on_error(self, mock_validate):
|
|
|
+ test_value = mock.sentinel.test_value
|
|
|
+ test_schema = mock.sentinel.test_schema
|
|
|
+
|
|
|
+ mock_validate.side_effect = jsonschema.exceptions.ValidationError(
|
|
|
+ 'Test Error')
|
|
|
+
|
|
|
+ with self.assertLogs('coriolis.schemas', level=logging.WARN):
|
|
|
+ result = schemas.validate_value(test_value, test_schema,
|
|
|
+ raise_on_error=False)
|
|
|
+ self.assertEqual(result, False)
|