summaryrefslogtreecommitdiff
path: root/tests/unittests/config/test_cc_keys_to_console.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unittests/config/test_cc_keys_to_console.py')
-rw-r--r--tests/unittests/config/test_cc_keys_to_console.py81
1 files changed, 80 insertions, 1 deletions
diff --git a/tests/unittests/config/test_cc_keys_to_console.py b/tests/unittests/config/test_cc_keys_to_console.py
index 9efc2b48..61f62e96 100644
--- a/tests/unittests/config/test_cc_keys_to_console.py
+++ b/tests/unittests/config/test_cc_keys_to_console.py
@@ -1,9 +1,16 @@
"""Tests for cc_keys_to_console."""
-from unittest import mock
+
+import re
import pytest
from cloudinit.config import cc_keys_to_console
+from cloudinit.config.schema import (
+ SchemaValidationError,
+ get_schema,
+ validate_cloudconfig_schema,
+)
+from tests.unittests.helpers import mock, skipUnlessJsonSchema
class TestHandle:
@@ -38,3 +45,75 @@ class TestHandle:
cc_keys_to_console.handle("name", cfg, mock.Mock(), mock.Mock(), ())
assert subp_called == (m_subp.call_count == 1)
+
+
+class TestKeysToConsoleSchema:
+ @pytest.mark.parametrize(
+ "config, error_msg",
+ (
+ # Valid schemas are covered by meta examples tests in test_schema
+ # Invalid schemas
+ (
+ {"ssh": {}},
+ "Cloud config schema errors: ssh: 'emit_keys_to_console' is"
+ " a required property",
+ ),
+ ( # Avoid common failure giving a string 'false' instead of false
+ {"ssh": {"emit_keys_to_console": "false"}},
+ "Cloud config schema errors: ssh.emit_keys_to_console: 'false'"
+ " is not of type 'boolean'",
+ ),
+ (
+ {"ssh": {"noextraprop": False, "emit_keys_to_console": False}},
+ re.escape(
+ "Cloud config schema errors: ssh: Additional properties"
+ " are not allowed ('noextraprop' was unexpected)"
+ ),
+ ),
+ ( # Avoid common failure giving a string 'false' instead of false
+ {"ssh": {"emit_keys_to_console": "false"}},
+ "Cloud config schema errors: ssh.emit_keys_to_console: 'false'"
+ " is not of type 'boolean'",
+ ),
+ ( # Avoid common failure giving a string 'false' instead of false
+ {"ssh_key_console_blacklist": False},
+ "Cloud config schema errors: ssh_key_console_blacklist: False"
+ " is not of type 'array'",
+ ),
+ ( # Avoid common failure giving a string 'false' instead of false
+ {"ssh_key_console_blacklist": [1]},
+ "Cloud config schema errors: ssh_key_console_blacklist.0: 1 is"
+ " not of type 'string'",
+ ),
+ ( # Avoid common failure giving a string 'false' instead of false
+ {"ssh_key_console_blacklist": [1]},
+ "Cloud config schema errors: ssh_key_console_blacklist.0: 1 is"
+ " not of type 'string'",
+ ),
+ ( # Avoid common failure giving a string 'false' instead of false
+ {"ssh_fp_console_blacklist": None},
+ "Cloud config schema errors: ssh_fp_console_blacklist: None"
+ " is not of type 'array'",
+ ),
+ ( # Avoid common failure giving a string 'false' instead of false
+ {"ssh_fp_console_blacklist": [1]},
+ "Cloud config schema errors: ssh_fp_console_blacklist.0: 1 is"
+ " not of type 'string'",
+ ),
+ ( # Avoid common failure giving a string 'false' instead of false
+ {"ssh_fp_console_blacklist": [1]},
+ "Cloud config schema errors: ssh_fp_console_blacklist.0: 1 is"
+ " not of type 'string'",
+ ),
+ ),
+ )
+ @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()
+ with pytest.raises(SchemaValidationError, match=error_msg):
+ validate_cloudconfig_schema(config, schema, strict=True)
+
+
+# vi: ts=4 expandtab