diff options
author | Chad Smith <chad.smith@canonical.com> | 2022-01-12 06:52:23 -0700 |
---|---|---|
committer | Chad Smith <chad.smith@canonical.com> | 2022-01-13 14:37:46 -0700 |
commit | dd7e36320de526fb6eaa91e1173396b288b0165a (patch) | |
tree | 910cce2a8ce0b68f248425f7e3ba4f5d45bdb7a4 | |
parent | cf10af4e6cd60b2dcbed9bc7794e046537cb4228 (diff) | |
download | cloud-init-git-dd7e36320de526fb6eaa91e1173396b288b0165a.tar.gz |
tests: apt_pipelining schema tests
-rw-r--r-- | tests/unittests/config/test_cc_apt_pipelining.py | 54 |
1 files changed, 51 insertions, 3 deletions
diff --git a/tests/unittests/config/test_cc_apt_pipelining.py b/tests/unittests/config/test_cc_apt_pipelining.py index c6a8e68e..d19533b3 100644 --- a/tests/unittests/config/test_cc_apt_pipelining.py +++ b/tests/unittests/config/test_cc_apt_pipelining.py @@ -2,8 +2,22 @@ """Tests cc_apt_pipelining handler""" +from pathlib import Path + +import pytest + import cloudinit.config.cc_apt_pipelining as cc_apt_pipelining -from tests.unittests.helpers import mock +from cloudinit import util +from cloudinit.config.schema import ( + SchemaValidationError, + get_schema, + validate_cloudconfig_schema, +) +from tests.unittests.helpers import ( + cloud_init_project_dir, + mock, + skipUnlessJsonSchema, +) class TestAptPipelining: @@ -19,12 +33,46 @@ class TestAptPipelining: cc_apt_pipelining.handle( "foo", {"apt_pipelining": "false"}, None, mock.MagicMock(), None ) - assert 1 == m_write_file.call_count + assert 1 == m_write_file.call_count args, _ = m_write_file.call_args assert cc_apt_pipelining.DEFAULT_FILE == args[0] assert 'Pipeline-Depth "0"' in args[1] - def test_failure_schema_ + @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() + @mock.patch("cloudinit.config.schema.read_cfg_paths") + def test_schema_vaidatoin(self, read_cfg_paths, config, error_msg, paths): + """Assert expected schema validation and error messages.""" + read_cfg_paths.return_value = paths + # New-style schema $defs exist in config/cloud-init-schema*.json + for schema_file in Path(cloud_init_project_dir("config/")).glob( + "cloud-init-schema*.json" + ): + util.copy(schema_file, paths.schema_dir) + 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 |