summaryrefslogtreecommitdiff
path: root/cloudinit
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2018-04-20 20:25:48 -0400
committerScott Moser <smoser@brickies.net>2018-04-20 20:25:48 -0400
commit5037252ca5dc54c6d978b23dba063ac5fabc98fa (patch)
treed39ee7c87b9d471a10a300766570a838c04cbc24 /cloudinit
parent4952a8545b61ceb2fe26a933d2f64020d0281703 (diff)
downloadvyos-cloud-init-5037252ca5dc54c6d978b23dba063ac5fabc98fa.tar.gz
vyos-cloud-init-5037252ca5dc54c6d978b23dba063ac5fabc98fa.zip
schema: in validation, raise ImportError if strict but no jsonschema.
validate_cloudconfig_schema with strict=True would not actually validate if there was no jsonschema available. That seems kind of strange. The change here is to make it raise an exception if strict was passed in. And then to fix the one test that needed a skipIfJsonSchema wrapper.
Diffstat (limited to 'cloudinit')
-rw-r--r--cloudinit/config/tests/test_snap.py41
-rw-r--r--cloudinit/config/tests/test_ubuntu_advantage.py30
-rw-r--r--cloudinit/tests/helpers.py19
3 files changed, 62 insertions, 28 deletions
diff --git a/cloudinit/config/tests/test_snap.py b/cloudinit/config/tests/test_snap.py
index 492d2d46..34c80f1e 100644
--- a/cloudinit/config/tests/test_snap.py
+++ b/cloudinit/config/tests/test_snap.py
@@ -9,7 +9,7 @@ from cloudinit.config.cc_snap import (
from cloudinit.config.schema import validate_cloudconfig_schema
from cloudinit import util
from cloudinit.tests.helpers import (
- CiTestCase, mock, wrap_and_call, skipUnlessJsonSchema)
+ CiTestCase, SchemaTestCaseMixin, mock, wrap_and_call, skipUnlessJsonSchema)
SYSTEM_USER_ASSERTION = """\
@@ -245,9 +245,10 @@ class TestRunCommands(CiTestCase):
@skipUnlessJsonSchema()
-class TestSchema(CiTestCase):
+class TestSchema(CiTestCase, SchemaTestCaseMixin):
with_logs = True
+ schema = schema
def test_schema_warns_on_snap_not_as_dict(self):
"""If the snap configuration is not a dict, emit a warning."""
@@ -342,39 +343,27 @@ class TestSchema(CiTestCase):
def test_duplicates_are_fine_array_array(self):
"""Duplicated commands array/array entries are allowed."""
- byebye = ["echo", "bye"]
- try:
- cfg = {'snap': {'commands': [byebye, byebye]}}
- validate_cloudconfig_schema(cfg, schema, strict=True)
- except schema.SchemaValidationError as e:
- self.fail("command entries can be duplicate.")
+ self.assertSchemaValid(
+ {'commands': [["echo", "bye"], ["echo" "bye"]]},
+ "command entries can be duplicate.")
def test_duplicates_are_fine_array_string(self):
"""Duplicated commands array/string entries are allowed."""
- byebye = "echo bye"
- try:
- cfg = {'snap': {'commands': [byebye, byebye]}}
- validate_cloudconfig_schema(cfg, schema, strict=True)
- except schema.SchemaValidationError as e:
- self.fail("command entries can be duplicate.")
+ self.assertSchemaValid(
+ {'commands': ["echo bye", "echo bye"]},
+ "command entries can be duplicate.")
def test_duplicates_are_fine_dict_array(self):
"""Duplicated commands dict/array entries are allowed."""
- byebye = ["echo", "bye"]
- try:
- cfg = {'snap': {'commands': {'00': byebye, '01': byebye}}}
- validate_cloudconfig_schema(cfg, schema, strict=True)
- except schema.SchemaValidationError as e:
- self.fail("command entries can be duplicate.")
+ self.assertSchemaValid(
+ {'commands': {'00': ["echo", "bye"], '01': ["echo", "bye"]}},
+ "command entries can be duplicate.")
def test_duplicates_are_fine_dict_string(self):
"""Duplicated commands dict/string entries are allowed."""
- byebye = "echo bye"
- try:
- cfg = {'snap': {'commands': {'00': byebye, '01': byebye}}}
- validate_cloudconfig_schema(cfg, schema, strict=True)
- except schema.SchemaValidationError as e:
- self.fail("command entries can be duplicate.")
+ self.assertSchemaValid(
+ {'commands': {'00': "echo bye", '01': "echo bye"}},
+ "command entries can be duplicate.")
class TestHandle(CiTestCase):
diff --git a/cloudinit/config/tests/test_ubuntu_advantage.py b/cloudinit/config/tests/test_ubuntu_advantage.py
index f2a59faf..f1beeff8 100644
--- a/cloudinit/config/tests/test_ubuntu_advantage.py
+++ b/cloudinit/config/tests/test_ubuntu_advantage.py
@@ -7,7 +7,8 @@ from cloudinit.config.cc_ubuntu_advantage import (
handle, maybe_install_ua_tools, run_commands, schema)
from cloudinit.config.schema import validate_cloudconfig_schema
from cloudinit import util
-from cloudinit.tests.helpers import CiTestCase, mock, skipUnlessJsonSchema
+from cloudinit.tests.helpers import (
+ CiTestCase, mock, SchemaTestCaseMixin, skipUnlessJsonSchema)
# Module path used in mocks
@@ -105,9 +106,10 @@ class TestRunCommands(CiTestCase):
@skipUnlessJsonSchema()
-class TestSchema(CiTestCase):
+class TestSchema(CiTestCase, SchemaTestCaseMixin):
with_logs = True
+ schema = schema
def test_schema_warns_on_ubuntu_advantage_not_as_dict(self):
"""If ubuntu-advantage configuration is not a dict, emit a warning."""
@@ -169,6 +171,30 @@ class TestSchema(CiTestCase):
{'ubuntu-advantage': {'commands': {'01': 'also valid'}}}, schema)
self.assertEqual('', self.logs.getvalue())
+ def test_duplicates_are_fine_array_array(self):
+ """Duplicated commands array/array entries are allowed."""
+ self.assertSchemaValid(
+ {'commands': [["echo", "bye"], ["echo" "bye"]]},
+ "command entries can be duplicate.")
+
+ def test_duplicates_are_fine_array_string(self):
+ """Duplicated commands array/string entries are allowed."""
+ self.assertSchemaValid(
+ {'commands': ["echo bye", "echo bye"]},
+ "command entries can be duplicate.")
+
+ def test_duplicates_are_fine_dict_array(self):
+ """Duplicated commands dict/array entries are allowed."""
+ self.assertSchemaValid(
+ {'commands': {'00': ["echo", "bye"], '01': ["echo", "bye"]}},
+ "command entries can be duplicate.")
+
+ def test_duplicates_are_fine_dict_string(self):
+ """Duplicated commands dict/string entries are allowed."""
+ self.assertSchemaValid(
+ {'commands': {'00': "echo bye", '01': "echo bye"}},
+ "command entries can be duplicate.")
+
class TestHandle(CiTestCase):
diff --git a/cloudinit/tests/helpers.py b/cloudinit/tests/helpers.py
index 5aada6e7..4999f1f6 100644
--- a/cloudinit/tests/helpers.py
+++ b/cloudinit/tests/helpers.py
@@ -24,6 +24,8 @@ try:
except ImportError:
from ConfigParser import ConfigParser
+from cloudinit.config.schema import (
+ SchemaValidationError, validate_cloudconfig_schema)
from cloudinit import helpers as ch
from cloudinit import util
@@ -312,6 +314,23 @@ class HttprettyTestCase(CiTestCase):
super(HttprettyTestCase, self).tearDown()
+class SchemaTestCaseMixin(unittest2.TestCase):
+
+ def assertSchemaValid(self, cfg, msg="Valid Schema failed validation."):
+ """Assert the config is valid per self.schema.
+
+ If there is only one top level key in the schema properties, then
+ the cfg will be put under that key."""
+ props = list(self.schema.get('properties'))
+ # put cfg under top level key if there is only one in the schema
+ if len(props) == 1:
+ cfg = {props[0]: cfg}
+ try:
+ validate_cloudconfig_schema(cfg, self.schema, strict=True)
+ except SchemaValidationError:
+ self.fail(msg)
+
+
def populate_dir(path, files):
if not os.path.exists(path):
os.makedirs(path)