← Back to team overview

cloud-init-dev team mailing list archive

[Merge] ~chad.smith/cloud-init:skip-jsonschema-unittest-when-missing-deps into cloud-init:master

 

Chad Smith has proposed merging ~chad.smith/cloud-init:skip-jsonschema-unittest-when-missing-deps into cloud-init:master.

Requested reviews:
  cloud-init commiters (cloud-init-dev)

For more details, see:
https://code.launchpad.net/~chad.smith/cloud-init/+git/cloud-init/+merge/325024

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


-- 
Your team cloud-init commiters is requested to review the proposed merge of ~chad.smith/cloud-init:skip-jsonschema-unittest-when-missing-deps into cloud-init:master.
diff --git a/tests/unittests/test_handler/test_handler_ntp.py b/tests/unittests/test_handler/test_handler_ntp.py
index 6cafa63..25d7365 100644
--- a/tests/unittests/test_handler/test_handler_ntp.py
+++ b/tests/unittests/test_handler/test_handler_ntp.py
@@ -3,7 +3,7 @@
 from cloudinit.config import cc_ntp
 from cloudinit.sources import DataSourceNone
 from cloudinit import (distros, helpers, cloud, util)
-from ..helpers import FilesystemMockingTestCase, mock
+from ..helpers import FilesystemMockingTestCase, mock, skipIf
 
 
 import os
@@ -16,6 +16,12 @@ servers {{servers}}
 pools {{pools}}
 """
 
+try:
+    import jsonschema  # NOQA
+    _missing_jsonschema_dep = False
+except ImportError:
+    _missing_jsonschema_dep = True
+
 
 class TestNtp(FilesystemMockingTestCase):
 
@@ -232,6 +238,7 @@ class TestNtp(FilesystemMockingTestCase):
             "servers []\npools {0}\n".format(default_pools),
             content)
 
+    @skipIf(_missing_jsonschema_dep, "No python-jsonschema dependency")
     def test_ntp_handler_schema_validation_warns_non_string_item_type(self):
         """Ntp schema validation warns of non-strings in pools or servers.
 
@@ -252,6 +259,7 @@ class TestNtp(FilesystemMockingTestCase):
             content = stream.read()
         self.assertEqual("servers ['valid', None]\npools [123]\n", content)
 
+    @skipIf(_missing_jsonschema_dep, "No python-jsonschema dependency")
     def test_ntp_handler_schema_validation_warns_of_non_array_type(self):
         """Ntp schema validation warns of non-array pools or servers types.
 
@@ -272,6 +280,7 @@ class TestNtp(FilesystemMockingTestCase):
             content = stream.read()
         self.assertEqual("servers non-array\npools 123\n", content)
 
+    @skipIf(_missing_jsonschema_dep, "No python-jsonschema dependency")
     def test_ntp_handler_schema_validation_warns_invalid_key_present(self):
         """Ntp schema validation warns of invalid keys present in ntp config.
 
@@ -295,6 +304,7 @@ class TestNtp(FilesystemMockingTestCase):
             "servers []\npools ['0.mycompany.pool.ntp.org']\n",
             content)
 
+    @skipIf(_missing_jsonschema_dep, "No python-jsonschema dependency")
     def test_ntp_handler_schema_validation_warns_of_duplicates(self):
         """Ntp schema validation warns of duplicates in servers or pools.
 
diff --git a/tests/unittests/test_handler/test_schema.py b/tests/unittests/test_handler/test_schema.py
index 3239e32..fd0e4f5 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):

Follow ups