From 022122a54e895468d054ed162fdf1b403b7aaf3f Mon Sep 17 00:00:00 2001 From: lucasmoura Date: Wed, 6 May 2020 17:19:35 -0300 Subject: Create tests to validate schema examples (#348) Add a unit test to validate if the examples provided in the config modules are conforming to the concatenated schema of all config modules. The rationale behind that is not only to verify if the examples are correctly written but to assert that no config schema is interfering with each other. Failures in validate_cloudconfig_schema raise the SchemaValidationError by using strict=True, so I have only called the function passing the right schema examples to validate. This branch also fixes an invalid schema example in cc_snap. LP: #1876412 --- tests/unittests/test_handler/test_schema.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'tests') diff --git a/tests/unittests/test_handler/test_schema.py b/tests/unittests/test_handler/test_schema.py index 1059fb8b..ae3838d8 100644 --- a/tests/unittests/test_handler/test_schema.py +++ b/tests/unittests/test_handler/test_schema.py @@ -10,6 +10,7 @@ from cloudinit.tests.helpers import CiTestCase, mock, skipUnlessJsonSchema from copy import copy import os +import pytest from io import StringIO from textwrap import dedent from yaml import safe_load @@ -112,6 +113,24 @@ class ValidateCloudConfigSchemaTest(CiTestCase): str(context_mgr.exception)) +class TestCloudConfigExamples: + + SCHEMA = get_schema() + PARAMS = [ + (schema["id"], example) + for schema in SCHEMA["allOf"] for example in schema["examples"]] + + @pytest.mark.parametrize("schema_id,example", PARAMS) + @skipUnlessJsonSchema() + def test_validateconfig_schema_of_example(self, schema_id, example): + """ For a given example in a config module we test if it is valid + according to the unified schema of all config modules + """ + config_load = safe_load(example) + validate_cloudconfig_schema( + config_load, TestCloudConfigExamples.SCHEMA, strict=True) + + class ValidateCloudConfigFileTest(CiTestCase): """Tests for validate_cloudconfig_file.""" -- cgit v1.2.3