summaryrefslogtreecommitdiff
path: root/tests/unittests/test_handler/test_schema.py
diff options
context:
space:
mode:
authorChad Smith <chad.smith@canonical.com>2017-06-02 14:42:26 -0600
committerScott Moser <smoser@brickies.net>2017-06-02 16:49:05 -0400
commita62a94b4edd7c61a268350c84e43b0aa8f68b0c2 (patch)
tree852cd8c7a9512363f5c8d14c7bcf0fe5ecf3eb91 /tests/unittests/test_handler/test_schema.py
parent1cd4323b940408aa34dcaa01bd8a7ed43d9a966a (diff)
downloadvyos-cloud-init-a62a94b4edd7c61a268350c84e43b0aa8f68b0c2.tar.gz
vyos-cloud-init-a62a94b4edd7c61a268350c84e43b0aa8f68b0c2.zip
Tests: Skip jsonschema related unit tests when dependency is absent.
On some build environments we don't have python-jsonschema installed. Since this dependency is an optional runtime dependency, we can also make it an optional unit test dependency. Add a skip of related unittests when jsonschema is not present. Also, KeyError messages on CentOs don't have single quotes around the missing 'key-name'. Make our KeyError assertion a bit more flexible with the assertIn call. LP: #1695318
Diffstat (limited to 'tests/unittests/test_handler/test_schema.py')
-rw-r--r--tests/unittests/test_handler/test_schema.py17
1 files changed, 14 insertions, 3 deletions
diff --git a/tests/unittests/test_handler/test_schema.py b/tests/unittests/test_handler/test_schema.py
index 3239e326..fd0e4f5e 100644
--- a/tests/unittests/test_handler/test_schema.py
+++ b/tests/unittests/test_handler/test_schema.py
@@ -6,12 +6,18 @@ from cloudinit.config.schema import (
main)
from cloudinit.util import write_file
-from ..helpers import CiTestCase, mock
+from ..helpers import CiTestCase, mock, skipIf
from copy import copy
from six import StringIO
from textwrap import dedent
+try:
+ import jsonschema # NOQA
+ _missing_jsonschema_dep = False
+except ImportError:
+ _missing_jsonschema_dep = True
+
class SchemaValidationErrorTest(CiTestCase):
"""Test validate_cloudconfig_schema"""
@@ -35,6 +41,7 @@ class ValidateCloudConfigSchemaTest(CiTestCase):
with_logs = True
+ @skipIf(_missing_jsonschema_dep, "No python-jsonschema dependency")
def test_validateconfig_schema_non_strict_emits_warnings(self):
"""When strict is False validate_cloudconfig_schema emits warnings."""
schema = {'properties': {'p1': {'type': 'string'}}}
@@ -43,6 +50,7 @@ class ValidateCloudConfigSchemaTest(CiTestCase):
"Invalid config:\np1: -1 is not of type 'string'\n",
self.logs.getvalue())
+ @skipIf(_missing_jsonschema_dep, "No python-jsonschema dependency")
def test_validateconfig_schema_emits_warning_on_missing_jsonschema(self):
"""Warning from validate_cloudconfig_schema when missing jsonschema."""
schema = {'properties': {'p1': {'type': 'string'}}}
@@ -52,6 +60,7 @@ class ValidateCloudConfigSchemaTest(CiTestCase):
'Ignoring schema validation. python-jsonschema is not present',
self.logs.getvalue())
+ @skipIf(_missing_jsonschema_dep, "No python-jsonschema dependency")
def test_validateconfig_schema_strict_raises_errors(self):
"""When strict is True validate_cloudconfig_schema raises errors."""
schema = {'properties': {'p1': {'type': 'string'}}}
@@ -61,8 +70,9 @@ class ValidateCloudConfigSchemaTest(CiTestCase):
"Cloud config schema errors: p1: -1 is not of type 'string'",
str(context_mgr.exception))
+ @skipIf(_missing_jsonschema_dep, "No python-jsonschema dependency")
def test_validateconfig_schema_honors_formats(self):
- """When strict is True validate_cloudconfig_schema raises errors."""
+ """With strict True, validate_cloudconfig_schema errors on format."""
schema = {
'properties': {'p1': {'type': 'string', 'format': 'hostname'}}}
with self.assertRaises(SchemaValidationError) as context_mgr:
@@ -111,6 +121,7 @@ class ValidateCloudConfigFileTest(CiTestCase):
self.config_file),
str(context_mgr.exception))
+ @skipIf(_missing_jsonschema_dep, "No python-jsonschema dependency")
def test_validateconfig_file_sctricty_validates_schema(self):
"""validate_cloudconfig_file raises errors on invalid schema."""
schema = {
@@ -183,7 +194,7 @@ class GetSchemaDocTest(CiTestCase):
invalid_schema.pop(key)
with self.assertRaises(KeyError) as context_mgr:
get_schema_doc(invalid_schema)
- self.assertEqual("'{0}'".format(key), str(context_mgr.exception))
+ self.assertIn(key, str(context_mgr.exception))
class MainTest(CiTestCase):