summaryrefslogtreecommitdiff
path: root/tests/unittests/config
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unittests/config')
-rw-r--r--tests/unittests/config/test_cc_apk_configure.py87
-rw-r--r--tests/unittests/config/test_cc_apt_pipelining.py49
-rw-r--r--tests/unittests/config/test_cc_bootcmd.py3
-rw-r--r--tests/unittests/config/test_cc_ntp.py21
-rw-r--r--tests/unittests/config/test_cc_resizefs.py8
-rw-r--r--tests/unittests/config/test_cc_runcmd.py2
-rw-r--r--tests/unittests/config/test_cc_snap.py49
-rw-r--r--tests/unittests/config/test_cc_ubuntu_advantage.py17
-rw-r--r--tests/unittests/config/test_cc_write_files.py15
-rw-r--r--tests/unittests/config/test_cc_write_files_deferred.py6
-rw-r--r--tests/unittests/config/test_schema.py168
11 files changed, 327 insertions, 98 deletions
diff --git a/tests/unittests/config/test_cc_apk_configure.py b/tests/unittests/config/test_cc_apk_configure.py
index 6fbc3dec..85dd028f 100644
--- a/tests/unittests/config/test_cc_apk_configure.py
+++ b/tests/unittests/config/test_cc_apk_configure.py
@@ -6,11 +6,23 @@ Test creation of repositories file
import logging
import os
+import re
import textwrap
+import pytest
+
from cloudinit import cloud, helpers, util
from cloudinit.config import cc_apk_configure
-from tests.unittests.helpers import FilesystemMockingTestCase, mock
+from cloudinit.config.schema import (
+ SchemaValidationError,
+ get_schema,
+ validate_cloudconfig_schema,
+)
+from tests.unittests.helpers import (
+ FilesystemMockingTestCase,
+ mock,
+ skipUnlessJsonSchema,
+)
REPO_FILE = "/etc/apk/repositories"
DEFAULT_MIRROR_URL = "https://alpine.global.ssl.fastly.net/alpine"
@@ -322,4 +334,77 @@ class TestConfig(FilesystemMockingTestCase):
self.assertEqual(expected_content, util.load_file(REPO_FILE))
+class TestApkConfigureSchema:
+ @pytest.mark.parametrize(
+ "config, error_msg",
+ (
+ # Valid schemas
+ ({"apk_repos": {"preserve_repositories": True}}, None),
+ ({"apk_repos": {"alpine_repo": None}}, None),
+ ({"apk_repos": {"alpine_repo": {"version": "v3.21"}}}, None),
+ (
+ {
+ "apk_repos": {
+ "alpine_repo": {
+ "base_url": "http://yep",
+ "community_enabled": True,
+ "testing_enabled": True,
+ "version": "v3.21",
+ }
+ }
+ },
+ None,
+ ),
+ ({"apk_repos": {"local_repo_base_url": "http://some"}}, None),
+ # Invalid schemas
+ (
+ {"apk_repos": {"alpine_repo": {"version": False}}},
+ "apk_repos.alpine_repo.version: False is not of type"
+ " 'string'",
+ ),
+ (
+ {
+ "apk_repos": {
+ "alpine_repo": {"version": "v3.12", "bogus": 1}
+ }
+ },
+ re.escape(
+ "apk_repos.alpine_repo: Additional properties are not"
+ " allowed ('bogus' was unexpected)"
+ ),
+ ),
+ (
+ {"apk_repos": {"alpine_repo": {}}},
+ "apk_repos.alpine_repo: 'version' is a required property,"
+ " apk_repos.alpine_repo: {} does not have enough properties",
+ ),
+ (
+ {"apk_repos": {"alpine_repo": True}},
+ "apk_repos.alpine_repo: True is not of type 'object', 'null'",
+ ),
+ (
+ {"apk_repos": {"preserve_repositories": "wrongtype"}},
+ "apk_repos.preserve_repositories: 'wrongtype' is not of type"
+ " 'boolean'",
+ ),
+ (
+ {"apk_repos": {}},
+ "apk_repos: {} does not have enough properties",
+ ),
+ (
+ {"apk_repos": {"local_repo_base_url": None}},
+ "apk_repos.local_repo_base_url: None is not of type 'string'",
+ ),
+ ),
+ )
+ @skipUnlessJsonSchema()
+ def test_schema_validation(self, config, error_msg):
+ schema = get_schema()
+ if error_msg is None:
+ validate_cloudconfig_schema(config, schema, strict=True)
+ else:
+ with pytest.raises(SchemaValidationError, match=error_msg):
+ validate_cloudconfig_schema(config, schema, strict=True)
+
+
# vi: ts=4 expandtab
diff --git a/tests/unittests/config/test_cc_apt_pipelining.py b/tests/unittests/config/test_cc_apt_pipelining.py
index b4497156..0f72d32b 100644
--- a/tests/unittests/config/test_cc_apt_pipelining.py
+++ b/tests/unittests/config/test_cc_apt_pipelining.py
@@ -2,16 +2,23 @@
"""Tests cc_apt_pipelining handler"""
+import pytest
+
import cloudinit.config.cc_apt_pipelining as cc_apt_pipelining
-from tests.unittests.helpers import CiTestCase, mock
+from cloudinit.config.schema import (
+ SchemaValidationError,
+ get_schema,
+ validate_cloudconfig_schema,
+)
+from tests.unittests.helpers import mock, skipUnlessJsonSchema
-class TestAptPipelining(CiTestCase):
+class TestAptPipelining:
@mock.patch("cloudinit.config.cc_apt_pipelining.util.write_file")
def test_not_disabled_by_default(self, m_write_file):
"""ensure that default behaviour is to not disable pipelining"""
cc_apt_pipelining.handle("foo", {}, None, mock.MagicMock(), None)
- self.assertEqual(0, m_write_file.call_count)
+ assert 0 == m_write_file.call_count
@mock.patch("cloudinit.config.cc_apt_pipelining.util.write_file")
def test_false_disables_pipelining(self, m_write_file):
@@ -19,10 +26,40 @@ class TestAptPipelining(CiTestCase):
cc_apt_pipelining.handle(
"foo", {"apt_pipelining": "false"}, None, mock.MagicMock(), None
)
- self.assertEqual(1, m_write_file.call_count)
+ assert 1 == m_write_file.call_count
args, _ = m_write_file.call_args
- self.assertEqual(cc_apt_pipelining.DEFAULT_FILE, args[0])
- self.assertIn('Pipeline-Depth "0"', args[1])
+ assert cc_apt_pipelining.DEFAULT_FILE == args[0]
+ assert 'Pipeline-Depth "0"' in args[1]
+
+ @pytest.mark.parametrize(
+ "config, error_msg",
+ (
+ # Valid schemas
+ ({}, None),
+ ({"apt_pipelining": 1}, None),
+ ({"apt_pipelining": True}, None),
+ ({"apt_pipelining": False}, None),
+ ({"apt_pipelining": "none"}, None),
+ ({"apt_pipelining": "unchanged"}, None),
+ ({"apt_pipelining": "os"}, None),
+ # Invalid schemas
+ (
+ {"apt_pipelining": "bogus"},
+ "Cloud config schema errors: apt_pipelining: 'bogus' is not"
+ " valid under any of the given schema",
+ ),
+ ),
+ )
+ @skipUnlessJsonSchema()
+ def test_schema_validation(self, config, error_msg):
+ """Assert expected schema validation and error messages."""
+ # New-style schema $defs exist in config/cloud-init-schema*.json
+ schema = get_schema()
+ if error_msg is None:
+ validate_cloudconfig_schema(config, schema, strict=True)
+ else:
+ with pytest.raises(SchemaValidationError, match=error_msg):
+ validate_cloudconfig_schema(config, schema, strict=True)
# vi: ts=4 expandtab
diff --git a/tests/unittests/config/test_cc_bootcmd.py b/tests/unittests/config/test_cc_bootcmd.py
index 6d8793b9..17033596 100644
--- a/tests/unittests/config/test_cc_bootcmd.py
+++ b/tests/unittests/config/test_cc_bootcmd.py
@@ -77,7 +77,8 @@ class TestBootcmd(CiTestCase):
with self.assertRaises(TypeError):
handle("cc_bootcmd", invalid_config, cc, LOG, [])
self.assertIn(
- "Invalid config:\nbootcmd: 1 is not of type 'array'",
+ "Invalid cloud-config provided:\nbootcmd: 1 is not of type"
+ " 'array'",
self.logs.getvalue(),
)
self.assertIn("Failed to shellify", self.logs.getvalue())
diff --git a/tests/unittests/config/test_cc_ntp.py b/tests/unittests/config/test_cc_ntp.py
index 7da82cee..fba141aa 100644
--- a/tests/unittests/config/test_cc_ntp.py
+++ b/tests/unittests/config/test_cc_ntp.py
@@ -385,7 +385,9 @@ class TestNtp(FilesystemMockingTestCase):
"servers []\npools {0}\n".format(pools),
util.load_file(confpath),
)
- self.assertNotIn("Invalid config:", self.logs.getvalue())
+ self.assertNotIn(
+ "Invalid cloud-config provided:", self.logs.getvalue()
+ )
@skipUnlessJsonSchema()
@mock.patch("cloudinit.config.cc_ntp.select_ntp_client")
@@ -404,8 +406,8 @@ class TestNtp(FilesystemMockingTestCase):
m_sel.return_value = ntpconfig
cc_ntp.handle("cc_ntp", invalid_config, mycloud, None, [])
self.assertIn(
- "Invalid config:\nntp.pools.0: 123 is not of type 'string'\n"
- "ntp.servers.1: None is not of type 'string'",
+ "Invalid cloud-config provided:\nntp.pools.0: 123 is not of"
+ " type 'string'\nntp.servers.1: None is not of type 'string'",
self.logs.getvalue(),
)
self.assertEqual(
@@ -431,8 +433,8 @@ class TestNtp(FilesystemMockingTestCase):
m_select.return_value = ntpconfig
cc_ntp.handle("cc_ntp", invalid_config, mycloud, None, [])
self.assertIn(
- "Invalid config:\nntp.pools: 123 is not of type 'array'\n"
- "ntp.servers: 'non-array' is not of type 'array'",
+ "Invalid cloud-config provided:\nntp.pools: 123 is not of type"
+ " 'array'\nntp.servers: 'non-array' is not of type 'array'",
self.logs.getvalue(),
)
self.assertEqual(
@@ -459,8 +461,9 @@ class TestNtp(FilesystemMockingTestCase):
m_select.return_value = ntpconfig
cc_ntp.handle("cc_ntp", invalid_config, mycloud, None, [])
self.assertIn(
- "Invalid config:\nntp: Additional properties are not "
- "allowed ('invalidkey' was unexpected)",
+ "Invalid cloud-config provided:\nntp: Additional"
+ " properties are not allowed ('invalidkey' was"
+ " unexpected)",
self.logs.getvalue(),
)
self.assertEqual(
@@ -488,8 +491,8 @@ class TestNtp(FilesystemMockingTestCase):
m_select.return_value = ntpconfig
cc_ntp.handle("cc_ntp", invalid_config, mycloud, None, [])
self.assertIn(
- "Invalid config:\nntp.pools: ['0.mypool.org', '0.mypool.org']"
- " has non-unique elements\nntp.servers: "
+ "Invalid cloud-config provided:\nntp.pools: ['0.mypool.org',"
+ " '0.mypool.org'] has non-unique elements\nntp.servers: "
"['10.0.0.1', '10.0.0.1'] has non-unique elements",
self.logs.getvalue(),
)
diff --git a/tests/unittests/config/test_cc_resizefs.py b/tests/unittests/config/test_cc_resizefs.py
index 228f1e45..9981dcea 100644
--- a/tests/unittests/config/test_cc_resizefs.py
+++ b/tests/unittests/config/test_cc_resizefs.py
@@ -92,8 +92,8 @@ class TestResizefs(CiTestCase):
handle("cc_resizefs", cfg, _cloud=None, log=LOG, args=[])
logs = self.logs.getvalue()
self.assertIn(
- "WARNING: Invalid config:\nresize_rootfs: 'junk' is not one of"
- " [True, False, 'noblock']",
+ "WARNING: Invalid cloud-config provided:\nresize_rootfs: 'junk' is"
+ " not one of [True, False, 'noblock']",
logs,
)
self.assertIn(
@@ -108,7 +108,9 @@ class TestResizefs(CiTestCase):
cfg = {"resize_rootfs": True}
handle("cc_resizefs", cfg, _cloud=None, log=LOG, args=[])
logs = self.logs.getvalue()
- self.assertNotIn("WARNING: Invalid config:\nresize_rootfs:", logs)
+ self.assertNotIn(
+ "WARNING: Invalid cloud-config provided:\nresize_rootfs:", logs
+ )
self.assertIn(
"WARNING: Could not determine filesystem type of /\n", logs
)
diff --git a/tests/unittests/config/test_cc_runcmd.py b/tests/unittests/config/test_cc_runcmd.py
index 34b3fb77..59490d67 100644
--- a/tests/unittests/config/test_cc_runcmd.py
+++ b/tests/unittests/config/test_cc_runcmd.py
@@ -73,7 +73,7 @@ class TestRuncmd(FilesystemMockingTestCase):
with self.assertRaises(TypeError) as cm:
handle("cc_runcmd", invalid_config, cc, LOG, [])
self.assertIn(
- "Invalid config:\nruncmd: 1 is not of type 'array'",
+ "Invalid cloud-config provided:\nruncmd: 1 is not of type 'array'",
self.logs.getvalue(),
)
self.assertIn("Failed to shellify", str(cm.exception))
diff --git a/tests/unittests/config/test_cc_snap.py b/tests/unittests/config/test_cc_snap.py
index f7e66ad2..1632676d 100644
--- a/tests/unittests/config/test_cc_snap.py
+++ b/tests/unittests/config/test_cc_snap.py
@@ -284,8 +284,8 @@ class TestSchema(CiTestCase, SchemaTestCaseMixin):
"""If the snap configuration is not a dict, emit a warning."""
validate_cloudconfig_schema({"snap": "wrong type"}, schema)
self.assertEqual(
- "WARNING: Invalid config:\nsnap: 'wrong type' is not of type"
- " 'object'\n",
+ "WARNING: Invalid cloud-config provided:\nsnap: 'wrong type'"
+ " is not of type 'object'\n",
self.logs.getvalue(),
)
@@ -296,8 +296,8 @@ class TestSchema(CiTestCase, SchemaTestCaseMixin):
{"snap": {"commands": ["ls"], "invalid-key": ""}}, schema
)
self.assertIn(
- "WARNING: Invalid config:\nsnap: Additional properties are not"
- " allowed ('invalid-key' was unexpected)",
+ "WARNING: Invalid cloud-config provided:\nsnap: Additional"
+ " properties are not allowed ('invalid-key' was unexpected)",
self.logs.getvalue(),
)
@@ -305,8 +305,8 @@ class TestSchema(CiTestCase, SchemaTestCaseMixin):
"""Warn when snap configuration lacks both commands and assertions."""
validate_cloudconfig_schema({"snap": {}}, schema)
self.assertIn(
- "WARNING: Invalid config:\nsnap: {} does not have enough"
- " properties",
+ "WARNING: Invalid cloud-config provided:\nsnap: {} does not"
+ " have enough properties",
self.logs.getvalue(),
)
@@ -315,8 +315,8 @@ class TestSchema(CiTestCase, SchemaTestCaseMixin):
"""Warn when snap:commands config is not a list or dict."""
validate_cloudconfig_schema({"snap": {"commands": "broken"}}, schema)
self.assertEqual(
- "WARNING: Invalid config:\nsnap.commands: 'broken' is not of type"
- " 'object', 'array'\n",
+ "WARNING: Invalid cloud-config provided:\nsnap.commands: 'broken'"
+ " is not of type 'object', 'array'\n",
self.logs.getvalue(),
)
@@ -326,9 +326,9 @@ class TestSchema(CiTestCase, SchemaTestCaseMixin):
validate_cloudconfig_schema({"snap": {"commands": []}}, schema)
validate_cloudconfig_schema({"snap": {"commands": {}}}, schema)
self.assertEqual(
- "WARNING: Invalid config:\nsnap.commands: [] is too short\n"
- "WARNING: Invalid config:\nsnap.commands: {} does not have enough"
- " properties\n",
+ "WARNING: Invalid cloud-config provided:\nsnap.commands: [] is"
+ " too short\nWARNING: Invalid cloud-config provided:\n"
+ "snap.commands: {} does not have enough properties\n",
self.logs.getvalue(),
)
@@ -349,10 +349,10 @@ class TestSchema(CiTestCase, SchemaTestCaseMixin):
{"snap": {"commands": {"01": 123}}}, schema
)
self.assertEqual(
- "WARNING: Invalid config:\n"
+ "WARNING: Invalid cloud-config provided:\n"
"snap.commands.0: 123 is not valid under any of the given"
" schemas\n"
- "WARNING: Invalid config:\n"
+ "WARNING: Invalid cloud-config provided:\n"
"snap.commands.01: 123 is not valid under any of the given"
" schemas\n",
self.logs.getvalue(),
@@ -368,10 +368,10 @@ class TestSchema(CiTestCase, SchemaTestCaseMixin):
{"snap": {"commands": {"01": ["snap", "install", 123]}}}, schema
)
self.assertEqual(
- "WARNING: Invalid config:\n"
+ "WARNING: Invalid cloud-config provided:\n"
"snap.commands.0: ['snap', 'install', 123] is not valid under any"
" of the given schemas\n",
- "WARNING: Invalid config:\n"
+ "WARNING: Invalid cloud-config provided:\n"
"snap.commands.0: ['snap', 'install', 123] is not valid under any"
" of the given schemas\n",
self.logs.getvalue(),
@@ -385,9 +385,9 @@ class TestSchema(CiTestCase, SchemaTestCaseMixin):
{"snap": {"assertions": {"01": 123}}}, schema
)
self.assertEqual(
- "WARNING: Invalid config:\n"
+ "WARNING: Invalid cloud-config provided:\n"
"snap.assertions.0: 123 is not of type 'string'\n"
- "WARNING: Invalid config:\n"
+ "WARNING: Invalid cloud-config provided:\n"
"snap.assertions.01: 123 is not of type 'string'\n",
self.logs.getvalue(),
)
@@ -397,8 +397,8 @@ class TestSchema(CiTestCase, SchemaTestCaseMixin):
"""Warn when snap:assertions config is not a list or dict."""
validate_cloudconfig_schema({"snap": {"assertions": "broken"}}, schema)
self.assertEqual(
- "WARNING: Invalid config:\nsnap.assertions: 'broken' is not of"
- " type 'object', 'array'\n",
+ "WARNING: Invalid cloud-config provided:\nsnap.assertions:"
+ " 'broken' is not of type 'object', 'array'\n",
self.logs.getvalue(),
)
@@ -408,9 +408,10 @@ class TestSchema(CiTestCase, SchemaTestCaseMixin):
validate_cloudconfig_schema({"snap": {"assertions": []}}, schema)
validate_cloudconfig_schema({"snap": {"assertions": {}}}, schema)
self.assertEqual(
- "WARNING: Invalid config:\nsnap.assertions: [] is too short\n"
- "WARNING: Invalid config:\nsnap.assertions: {} does not have"
- " enough properties\n",
+ "WARNING: Invalid cloud-config provided:\nsnap.assertions: []"
+ " is too short\n"
+ "WARNING: Invalid cloud-config provided:\nsnap.assertions: {}"
+ " does not have enough properties\n",
self.logs.getvalue(),
)
@@ -574,8 +575,8 @@ class TestHandle(CiTestCase):
args=None,
)
self.assertEqual(
- "WARNING: Invalid config:\nsnap: Additional properties are not"
- " allowed ('invalid' was unexpected)\n",
+ "WARNING: Invalid cloud-config provided:\nsnap: Additional"
+ " properties are not allowed ('invalid' was unexpected)\n",
self.logs.getvalue(),
)
diff --git a/tests/unittests/config/test_cc_ubuntu_advantage.py b/tests/unittests/config/test_cc_ubuntu_advantage.py
index c39e421f..2037c5ed 100644
--- a/tests/unittests/config/test_cc_ubuntu_advantage.py
+++ b/tests/unittests/config/test_cc_ubuntu_advantage.py
@@ -184,8 +184,8 @@ class TestSchema(CiTestCase, SchemaTestCaseMixin):
"""If ubuntu_advantage configuration is not a dict, emit a warning."""
validate_cloudconfig_schema({"ubuntu_advantage": "wrong type"}, schema)
self.assertEqual(
- "WARNING: Invalid config:\nubuntu_advantage: 'wrong type' is not"
- " of type 'object'\n",
+ "WARNING: Invalid cloud-config provided:\nubuntu_advantage:"
+ " 'wrong type' is not of type 'object'\n",
self.logs.getvalue(),
)
@@ -198,8 +198,9 @@ class TestSchema(CiTestCase, SchemaTestCaseMixin):
schema,
)
self.assertIn(
- "WARNING: Invalid config:\nubuntu_advantage: Additional properties"
- " are not allowed ('invalid-key' was unexpected)",
+ "WARNING: Invalid cloud-config provided:\nubuntu_advantage:"
+ " Additional properties are not allowed ('invalid-key' was"
+ " unexpected)",
self.logs.getvalue(),
)
@@ -211,7 +212,7 @@ class TestSchema(CiTestCase, SchemaTestCaseMixin):
{"ubuntu_advantage": {"enable": ["esm"]}}, schema
)
self.assertEqual(
- "WARNING: Invalid config:\nubuntu_advantage:"
+ "WARNING: Invalid cloud-config provided:\nubuntu_advantage:"
" 'token' is a required property\n",
self.logs.getvalue(),
)
@@ -224,9 +225,9 @@ class TestSchema(CiTestCase, SchemaTestCaseMixin):
{"ubuntu_advantage": {"enable": "needslist"}}, schema
)
self.assertEqual(
- "WARNING: Invalid config:\nubuntu_advantage: 'token' is a"
- " required property\nubuntu_advantage.enable: 'needslist'"
- " is not of type 'array'\n",
+ "WARNING: Invalid cloud-config provided:\nubuntu_advantage:"
+ " 'token' is a required property\nubuntu_advantage.enable:"
+ " 'needslist' is not of type 'array'\n",
self.logs.getvalue(),
)
diff --git a/tests/unittests/config/test_cc_write_files.py b/tests/unittests/config/test_cc_write_files.py
index 7eea99d3..faea5885 100644
--- a/tests/unittests/config/test_cc_write_files.py
+++ b/tests/unittests/config/test_cc_write_files.py
@@ -79,9 +79,11 @@ class TestWriteFilesSchema(CiTestCase):
cc = self.tmp_cloud("ubuntu")
valid_config = {"write_files": [{"path": "/some/path"}]}
handle("cc_write_file", valid_config, cc, LOG, [])
- self.assertNotIn("Invalid config:", self.logs.getvalue())
+ self.assertNotIn(
+ "Invalid cloud-config provided:", self.logs.getvalue()
+ )
handle("cc_write_file", INVALID_SCHEMA, cc, LOG, [])
- self.assertIn("Invalid config:", self.logs.getvalue())
+ self.assertIn("Invalid cloud-config provided:", self.logs.getvalue())
self.assertIn("'path' is a required property", self.logs.getvalue())
def test_schema_validation_warns_non_string_type_for_files(
@@ -105,7 +107,7 @@ class TestWriteFilesSchema(CiTestCase):
"write_files.0.%s: 1 is not of type '%s'" % (key, key_type),
self.logs.getvalue(),
)
- self.assertIn("Invalid config:", self.logs.getvalue())
+ self.assertIn("Invalid cloud-config provided:", self.logs.getvalue())
def test_schema_validation_warns_on_additional_undefined_propertes(
self, m_write_files
@@ -116,8 +118,8 @@ class TestWriteFilesSchema(CiTestCase):
invalid_config["write_files"][0]["bogus"] = "value"
handle("cc_write_file", invalid_config, cc, LOG, [])
self.assertIn(
- "Invalid config:\nwrite_files.0: Additional properties"
- " are not allowed ('bogus' was unexpected)",
+ "Invalid cloud-config provided:\nwrite_files.0: Additional"
+ " properties are not allowed ('bogus' was unexpected)",
self.logs.getvalue(),
)
@@ -139,7 +141,8 @@ class TestWriteFiles(FilesystemMockingTestCase):
with self.assertRaises(TypeError):
handle("cc_write_file", invalid_config, cc, LOG, [])
self.assertIn(
- "Invalid config:\nwrite_files: 1 is not of type 'array'",
+ "Invalid cloud-config provided:\nwrite_files: 1 is not of type"
+ " 'array'",
self.logs.getvalue(),
)
diff --git a/tests/unittests/config/test_cc_write_files_deferred.py b/tests/unittests/config/test_cc_write_files_deferred.py
index 3faac1bf..17203233 100644
--- a/tests/unittests/config/test_cc_write_files_deferred.py
+++ b/tests/unittests/config/test_cc_write_files_deferred.py
@@ -43,9 +43,11 @@ class TestWriteFilesDeferredSchema(CiTestCase):
cc = self.tmp_cloud("ubuntu")
handle("cc_write_files_deferred", valid_config, cc, LOG, [])
- self.assertNotIn("Invalid config:", self.logs.getvalue())
+ self.assertNotIn(
+ "Invalid cloud-config provided:", self.logs.getvalue()
+ )
handle("cc_write_files_deferred", invalid_config, cc, LOG, [])
- self.assertIn("Invalid config:", self.logs.getvalue())
+ self.assertIn("Invalid cloud-config provided:", self.logs.getvalue())
self.assertIn(
"defer: 'no' is not of type 'boolean'", self.logs.getvalue()
)
diff --git a/tests/unittests/config/test_schema.py b/tests/unittests/config/test_schema.py
index 93206bdd..5cb00c5d 100644
--- a/tests/unittests/config/test_schema.py
+++ b/tests/unittests/config/test_schema.py
@@ -11,16 +11,19 @@ from pathlib import Path
from textwrap import dedent
import pytest
+import yaml
from yaml import safe_load
from cloudinit.config.schema import (
CLOUD_CONFIG_HEADER,
MetaSchema,
SchemaValidationError,
+ _schemapath_for_cloudconfig,
annotated_cloudconfig_file,
get_jsonschema_validator,
get_meta_doc,
get_schema,
+ load_doc,
main,
validate_cloudconfig_file,
validate_cloudconfig_metaschema,
@@ -36,7 +39,7 @@ from tests.unittests.helpers import (
def get_schemas() -> dict:
- """Return all module schemas
+ """Return all legacy module schemas
Assumes that module schemas have the variable name "schema"
"""
@@ -78,14 +81,15 @@ def get_module_variable(var_name) -> dict:
return schemas
-class GetSchemaTest(CiTestCase):
+class TestGetSchema:
def test_get_schema_coalesces_known_schema(self):
"""Every cloudconfig module with schema is listed in allOf keyword."""
schema = get_schema()
- self.assertCountEqual(
+ assert sorted(
[
"cc_apk_configure",
"cc_apt_configure",
+ "cc_apt_pipelining",
"cc_bootcmd",
"cc_keyboard",
"cc_locale",
@@ -99,14 +103,88 @@ class GetSchemaTest(CiTestCase):
"cc_zypper_add_repo",
"cc_chef",
"cc_install_hotplug",
- ],
- [meta["id"] for meta in get_metas().values() if meta is not None],
- )
- self.assertEqual("cloud-config-schema", schema["id"])
- self.assertEqual(
- "http://json-schema.org/draft-04/schema#", schema["$schema"]
+ ]
+ ) == sorted(
+ [meta["id"] for meta in get_metas().values() if meta is not None]
+ )
+ assert "http://json-schema.org/draft-04/schema#" == schema["$schema"]
+ assert ["$defs", "$schema", "allOf"] == sorted(list(schema.keys()))
+ # New style schema should be defined in static schema file in $defs
+ expected_subschema_defs = [
+ {"$ref": "#/$defs/cc_apk_configure"},
+ {"$ref": "#/$defs/cc_apt_pipelining"},
+ ]
+ found_subschema_defs = []
+ legacy_schema_keys = []
+ for subschema in schema["allOf"]:
+ if "$ref" in subschema:
+ found_subschema_defs.append(subschema)
+ else: # Legacy subschema sourced from cc_* module 'schema' attr
+ legacy_schema_keys.extend(subschema["properties"].keys())
+
+ assert expected_subschema_defs == found_subschema_defs
+ # This list will dwindle as we move legacy schema to new $defs
+ assert [
+ "apt",
+ "bootcmd",
+ "chef",
+ "drivers",
+ "keyboard",
+ "locale",
+ "locale_configfile",
+ "ntp",
+ "resize_rootfs",
+ "runcmd",
+ "snap",
+ "ubuntu_advantage",
+ "updates",
+ "write_files",
+ "write_files",
+ "zypper",
+ ] == sorted(legacy_schema_keys)
+
+
+class TestLoadDoc:
+
+ docs = get_module_variable("__doc__")
+
+ # TODO( Drop legacy test when all sub-schemas in cloud-init-schema.json )
+ @pytest.mark.parametrize(
+ "module_name",
+ (
+ "cc_apt_pipelining", # new style composite schema file
+ "cc_bootcmd", # legacy sub-schema defined in module
+ ),
+ )
+ def test_report_docs_for_legacy_and_consolidated_schema(self, module_name):
+ doc = load_doc([module_name])
+ assert doc, "Unexpected empty docs for {}".format(module_name)
+ assert self.docs[module_name] == doc
+
+
+class Test_SchemapathForCloudconfig:
+ """Coverage tests for supported YAML formats."""
+
+ @pytest.mark.parametrize(
+ "source_content, expected",
+ (
+ (b"{}", {}), # assert empty config handled
+ # Multiple keys account for comments and whitespace lines
+ (b"#\na: va\n \nb: vb\n#\nc: vc", {"a": 2, "b": 4, "c": 6}),
+ # List items represented on correct line number
+ (b"a:\n - a1\n\n - a2\n", {"a": 1, "a.0": 2, "a.1": 4}),
+ # Nested dicts represented on correct line number
+ (b"a:\n a1:\n\n aa1: aa1v\n", {"a": 1, "a.a1": 2, "a.a1.aa1": 4}),
+ ),
+ )
+ def test_schemapaths_representatative_of_source_yaml(
+ self, source_content, expected
+ ):
+ """Validate schemapaths dict accurately represents source YAML line."""
+ cfg = yaml.safe_load(source_content)
+ assert expected == _schemapath_for_cloudconfig(
+ config=cfg, original_content=source_content
)
- self.assertCountEqual(["id", "$schema", "allOf"], get_schema().keys())
class SchemaValidationErrorTest(CiTestCase):
@@ -129,69 +207,87 @@ class SchemaValidationErrorTest(CiTestCase):
self.assertTrue(isinstance(exception, ValueError))
-class ValidateCloudConfigSchemaTest(CiTestCase):
+class TestValidateCloudConfigSchema:
"""Tests for validate_cloudconfig_schema."""
with_logs = True
+ @pytest.mark.parametrize(
+ "schema, call_count",
+ ((None, 1), ({"properties": {"p1": {"type": "string"}}}, 0)),
+ )
@skipUnlessJsonSchema()
- def test_validateconfig_schema_non_strict_emits_warnings(self):
+ @mock.patch("cloudinit.config.schema.get_schema")
+ def test_validateconfig_schema_use_full_schema_when_no_schema_param(
+ self, get_schema, schema, call_count
+ ):
+ """Use full schema when schema param is absent."""
+ get_schema.return_value = {"properties": {"p1": {"type": "string"}}}
+ kwargs = {"config": {"p1": "valid"}}
+ if schema:
+ kwargs["schema"] = schema
+ validate_cloudconfig_schema(**kwargs)
+ assert call_count == get_schema.call_count
+
+ @skipUnlessJsonSchema()
+ def test_validateconfig_schema_non_strict_emits_warnings(self, caplog):
"""When strict is False validate_cloudconfig_schema emits warnings."""
schema = {"properties": {"p1": {"type": "string"}}}
validate_cloudconfig_schema({"p1": -1}, schema, strict=False)
- self.assertIn(
- "Invalid config:\np1: -1 is not of type 'string'\n",
- self.logs.getvalue(),
+ assert (
+ "Invalid cloud-config provided:\np1: -1 is not of type 'string'\n"
+ in (caplog.text)
)
@skipUnlessJsonSchema()
- def test_validateconfig_schema_emits_warning_on_missing_jsonschema(self):
+ def test_validateconfig_schema_emits_warning_on_missing_jsonschema(
+ self, caplog
+ ):
"""Warning from validate_cloudconfig_schema when missing jsonschema."""
schema = {"properties": {"p1": {"type": "string"}}}
with mock.patch.dict("sys.modules", **{"jsonschema": ImportError()}):
validate_cloudconfig_schema({"p1": -1}, schema, strict=True)
- self.assertIn(
- "Ignoring schema validation. jsonschema is not present",
- self.logs.getvalue(),
+ assert "Ignoring schema validation. jsonschema is not present" in (
+ caplog.text
)
@skipUnlessJsonSchema()
def test_validateconfig_schema_strict_raises_errors(self):
"""When strict is True validate_cloudconfig_schema raises errors."""
schema = {"properties": {"p1": {"type": "string"}}}
- with self.assertRaises(SchemaValidationError) as context_mgr:
+ with pytest.raises(SchemaValidationError) as context_mgr:
validate_cloudconfig_schema({"p1": -1}, schema, strict=True)
- self.assertEqual(
- "Cloud config schema errors: p1: -1 is not of type 'string'",
- str(context_mgr.exception),
+ assert (
+ "Cloud config schema errors: p1: -1 is not of type 'string'"
+ == (str(context_mgr.value))
)
@skipUnlessJsonSchema()
def test_validateconfig_schema_honors_formats(self):
"""With strict True, validate_cloudconfig_schema errors on format."""
schema = {"properties": {"p1": {"type": "string", "format": "email"}}}
- with self.assertRaises(SchemaValidationError) as context_mgr:
+ with pytest.raises(SchemaValidationError) as context_mgr:
validate_cloudconfig_schema({"p1": "-1"}, schema, strict=True)
- self.assertEqual(
- "Cloud config schema errors: p1: '-1' is not a 'email'",
- str(context_mgr.exception),
+ assert "Cloud config schema errors: p1: '-1' is not a 'email'" == (
+ str(context_mgr.value)
)
@skipUnlessJsonSchema()
def test_validateconfig_schema_honors_formats_strict_metaschema(self):
"""With strict and strict_metaschema True, ensure errors on format"""
schema = {"properties": {"p1": {"type": "string", "format": "email"}}}
- with self.assertRaises(SchemaValidationError) as context_mgr:
+ with pytest.raises(SchemaValidationError) as context_mgr:
validate_cloudconfig_schema(
{"p1": "-1"}, schema, strict=True, strict_metaschema=True
)
- self.assertEqual(
- "Cloud config schema errors: p1: '-1' is not a 'email'",
- str(context_mgr.exception),
+ assert "Cloud config schema errors: p1: '-1' is not a 'email'" == str(
+ context_mgr.value
)
@skipUnlessJsonSchema()
- def test_validateconfig_strict_metaschema_do_not_raise_exception(self):
+ def test_validateconfig_strict_metaschema_do_not_raise_exception(
+ self, caplog
+ ):
"""With strict_metaschema=True, do not raise exceptions.
This flag is currently unused, but is intended for run-time validation.
@@ -203,12 +299,11 @@ class ValidateCloudConfigSchemaTest(CiTestCase):
)
assert (
"Meta-schema validation failed, attempting to validate config"
- in self.logs.getvalue()
+ in caplog.text
)
class TestCloudConfigExamples:
- schema = get_schemas()
metas = get_metas()
params = [
(meta["id"], example)
@@ -223,10 +318,9 @@ class TestCloudConfigExamples:
"""For a given example in a config module we test if it is valid
according to the unified schema of all config modules
"""
+ schema = get_schema()
config_load = safe_load(example)
- validate_cloudconfig_schema(
- config_load, self.schema[schema_id], strict=True
- )
+ validate_cloudconfig_schema(config_load, schema, strict=True)
class ValidateCloudConfigFileTest(CiTestCase):