summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJames Falcon <TheRealFalcon@users.noreply.github.com>2020-05-08 16:38:51 -0500
committerGitHub <noreply@github.com>2020-05-08 17:38:51 -0400
commit34b868020254106af8c79ffb85e04e294b19e44f (patch)
tree7b7e285f0b9c13a7d9551d61ec5ef0b38b6e3263 /tests
parentb24b376fabd54e4dc96ffc1b90a984addf42fc4c (diff)
downloadvyos-cloud-init-34b868020254106af8c79ffb85e04e294b19e44f.tar.gz
vyos-cloud-init-34b868020254106af8c79ffb85e04e294b19e44f.zip
Add test to ensure docs examples are valid cloud-init configs (#355)
Also update all examples to include the cloud-config header if they don't have it LP: #1876414
Diffstat (limited to 'tests')
-rw-r--r--tests/unittests/test_handler/test_schema.py33
1 files changed, 26 insertions, 7 deletions
diff --git a/tests/unittests/test_handler/test_schema.py b/tests/unittests/test_handler/test_schema.py
index ae3838d8..071b98bc 100644
--- a/tests/unittests/test_handler/test_schema.py
+++ b/tests/unittests/test_handler/test_schema.py
@@ -1,5 +1,5 @@
# This file is part of cloud-init. See LICENSE file for license information.
-
+import cloudinit
from cloudinit.config.schema import (
CLOUD_CONFIG_HEADER, SchemaValidationError, annotated_cloudconfig_file,
get_schema_doc, get_schema, validate_cloudconfig_file,
@@ -12,6 +12,7 @@ from copy import copy
import os
import pytest
from io import StringIO
+from pathlib import Path
from textwrap import dedent
from yaml import safe_load
@@ -114,13 +115,12 @@ class ValidateCloudConfigSchemaTest(CiTestCase):
class TestCloudConfigExamples:
-
- SCHEMA = get_schema()
- PARAMS = [
+ schema = get_schema()
+ params = [
(schema["id"], example)
- for schema in SCHEMA["allOf"] for example in schema["examples"]]
+ for schema in schema["allOf"] for example in schema["examples"]]
- @pytest.mark.parametrize("schema_id,example", PARAMS)
+ @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
@@ -128,7 +128,7 @@ class TestCloudConfigExamples:
"""
config_load = safe_load(example)
validate_cloudconfig_schema(
- config_load, TestCloudConfigExamples.SCHEMA, strict=True)
+ config_load, self.schema, strict=True)
class ValidateCloudConfigFileTest(CiTestCase):
@@ -447,4 +447,23 @@ class CloudTestsIntegrationTest(CiTestCase):
if errors:
raise AssertionError(', '.join(errors))
+
+def _get_schema_doc_examples():
+ examples_dir = Path(
+ cloudinit.__file__).parent.parent / 'doc' / 'examples'
+ assert examples_dir.is_dir()
+
+ all_text_files = (f for f in examples_dir.glob('cloud-config*.txt')
+ if not f.name.startswith('cloud-config-archive'))
+ return all_text_files
+
+
+class TestSchemaDocExamples:
+ schema = get_schema()
+
+ @pytest.mark.parametrize("example_path", _get_schema_doc_examples())
+ @skipUnlessJsonSchema()
+ def test_schema_doc_examples(self, example_path):
+ validate_cloudconfig_file(str(example_path), self.schema)
+
# vi: ts=4 expandtab syntax=python