diff options
| author | Chad Smith <chad.smith@canonical.com> | 2017-06-02 14:42:26 -0600 | 
|---|---|---|
| committer | Scott Moser <smoser@brickies.net> | 2017-06-02 16:49:05 -0400 | 
| commit | a62a94b4edd7c61a268350c84e43b0aa8f68b0c2 (patch) | |
| tree | 852cd8c7a9512363f5c8d14c7bcf0fe5ecf3eb91 | |
| parent | 1cd4323b940408aa34dcaa01bd8a7ed43d9a966a (diff) | |
| download | vyos-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
| -rw-r--r-- | tests/unittests/test_handler/test_handler_ntp.py | 12 | ||||
| -rw-r--r-- | tests/unittests/test_handler/test_schema.py | 17 | 
2 files changed, 25 insertions, 4 deletions
| diff --git a/tests/unittests/test_handler/test_handler_ntp.py b/tests/unittests/test_handler/test_handler_ntp.py index 6cafa63d..25d7365a 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 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): | 
