From 87811c6d4e47a0c1f3f8ce7bffafd01ba7fb1ea2 Mon Sep 17 00:00:00 2001 From: James Falcon Date: Wed, 6 May 2020 15:33:29 -0500 Subject: [PATCH 1/5] Add test to ensure docs examples are valid cloud-init configs Also update all examples to include the cloud-config header if they don't have it LP: #1876414 --- doc/examples/cloud-config-apt.txt | 1 + doc/examples/cloud-config-datasources.txt | 2 ++ doc/examples/cloud-config-disk-setup.txt | 1 + doc/examples/cloud-config-landscape.txt | 1 + doc/examples/cloud-config-rsyslog.txt | 1 + tests/unittests/test_handler/test_schema.py | 24 +++++++++++++++++++++ 6 files changed, 30 insertions(+) diff --git a/doc/examples/cloud-config-apt.txt b/doc/examples/cloud-config-apt.txt index ec10ae94fa2..5a56cd1b6c2 100644 --- a/doc/examples/cloud-config-apt.txt +++ b/doc/examples/cloud-config-apt.txt @@ -1,3 +1,4 @@ +#cloud-config # apt_pipelining (configure Acquire::http::Pipeline-Depth) # Default: disables HTTP pipelining. Certain web servers, such # as S3 do not pipeline properly (LP: #948461). diff --git a/doc/examples/cloud-config-datasources.txt b/doc/examples/cloud-config-datasources.txt index e5780472038..13bb687c36b 100644 --- a/doc/examples/cloud-config-datasources.txt +++ b/doc/examples/cloud-config-datasources.txt @@ -1,3 +1,5 @@ +#cloud-config + # Documentation on data sources configuration options datasource: # Ec2 diff --git a/doc/examples/cloud-config-disk-setup.txt b/doc/examples/cloud-config-disk-setup.txt index 6b7112cbf6d..5c6de77e319 100644 --- a/doc/examples/cloud-config-disk-setup.txt +++ b/doc/examples/cloud-config-disk-setup.txt @@ -1,3 +1,4 @@ +#cloud-config # Cloud-init supports the creation of simple partition tables and file systems # on devices. diff --git a/doc/examples/cloud-config-landscape.txt b/doc/examples/cloud-config-landscape.txt index d7ff8ef8ada..88be57ced18 100644 --- a/doc/examples/cloud-config-landscape.txt +++ b/doc/examples/cloud-config-landscape.txt @@ -1,3 +1,4 @@ +#cloud-config # Landscape-client configuration # # Anything under the top 'landscape: client' entry diff --git a/doc/examples/cloud-config-rsyslog.txt b/doc/examples/cloud-config-rsyslog.txt index 8113272dd11..d28dd38ee4c 100644 --- a/doc/examples/cloud-config-rsyslog.txt +++ b/doc/examples/cloud-config-rsyslog.txt @@ -1,3 +1,4 @@ +#cloud-config ## the rsyslog module allows you to configure the systems syslog. ## configuration of syslog is under the top level cloud-config ## entry 'rsyslog'. diff --git a/tests/unittests/test_handler/test_schema.py b/tests/unittests/test_handler/test_schema.py index ae3838d87c8..dd9e8e606e0 100644 --- a/tests/unittests/test_handler/test_schema.py +++ b/tests/unittests/test_handler/test_schema.py @@ -12,6 +12,7 @@ import os import pytest from io import StringIO +from pathlib import Path from textwrap import dedent from yaml import safe_load @@ -447,4 +448,27 @@ def test_all_integration_test_cloud_config_schema(self): if errors: raise AssertionError(', '.join(errors)) + +class TestSchemaDocExamples: + @skipUnlessJsonSchema() + def test_schema_doc_examples(self): + ignored_files = [ + 'cloud-config-archive-launch-index.txt', + 'cloud-config-archive.txt', + ] + + examples_dir = Path( + __file__).parent.parent.parent.parent / 'doc' / 'examples' + # Make sure our path shenanigans found an actual directory + assert examples_dir.is_dir() + + all_text_files = [f for f in examples_dir.glob('cloud-config*.txt') + if f.name not in ignored_files] + for text_file_path in all_text_files: + try: + validate_cloudconfig_file(str(text_file_path), get_schema()) + except: # noqa + print('Failed on {}'.format(text_file_path)) + raise + # vi: ts=4 expandtab syntax=python From c7bbe26276ea3979fcf212516acf3fc4fc8c8d3c Mon Sep 17 00:00:00 2001 From: James Falcon Date: Wed, 6 May 2020 19:31:12 -0500 Subject: [PATCH 2/5] [squash] review comments --- tests/unittests/test_handler/test_schema.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/tests/unittests/test_handler/test_schema.py b/tests/unittests/test_handler/test_schema.py index dd9e8e606e0..c02a136f770 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, @@ -115,7 +115,6 @@ def test_validateconfig_schema_honors_formats(self): class TestCloudConfigExamples: - SCHEMA = get_schema() PARAMS = [ (schema["id"], example) @@ -452,18 +451,13 @@ def test_all_integration_test_cloud_config_schema(self): class TestSchemaDocExamples: @skipUnlessJsonSchema() def test_schema_doc_examples(self): - ignored_files = [ - 'cloud-config-archive-launch-index.txt', - 'cloud-config-archive.txt', - ] - examples_dir = Path( - __file__).parent.parent.parent.parent / 'doc' / 'examples' + cloudinit.__file__).parent.parent / 'doc' / 'examples' # Make sure our path shenanigans found an actual directory assert examples_dir.is_dir() all_text_files = [f for f in examples_dir.glob('cloud-config*.txt') - if f.name not in ignored_files] + if not f.name.startswith('cloud-config-archive')] for text_file_path in all_text_files: try: validate_cloudconfig_file(str(text_file_path), get_schema()) From f8639a957e30dc17eaea7d3c79fdd9821f78de93 Mon Sep 17 00:00:00 2001 From: James Falcon Date: Thu, 7 May 2020 09:06:21 -0500 Subject: [PATCH 3/5] [squash] parametrized test based on review comments --- tests/unittests/test_handler/test_schema.py | 27 ++++++++++----------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/tests/unittests/test_handler/test_schema.py b/tests/unittests/test_handler/test_schema.py index c02a136f770..b08068aa088 100644 --- a/tests/unittests/test_handler/test_schema.py +++ b/tests/unittests/test_handler/test_schema.py @@ -448,21 +448,20 @@ def test_all_integration_test_cloud_config_schema(self): 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: + @pytest.mark.parametrize("example_path", _get_schema_doc_examples()) @skipUnlessJsonSchema() - def test_schema_doc_examples(self): - examples_dir = Path( - cloudinit.__file__).parent.parent / 'doc' / 'examples' - # Make sure our path shenanigans found an actual directory - 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')] - for text_file_path in all_text_files: - try: - validate_cloudconfig_file(str(text_file_path), get_schema()) - except: # noqa - print('Failed on {}'.format(text_file_path)) - raise + def test_schema_doc_examples(self, example_path): + validate_cloudconfig_file(str(example_path), get_schema()) # vi: ts=4 expandtab syntax=python From fbc9c0e66473ddf5b132044ecb509af215739c5a Mon Sep 17 00:00:00 2001 From: James Falcon Date: Thu, 7 May 2020 09:13:03 -0500 Subject: [PATCH 4/5] [squash] only creating the schema once --- tests/unittests/test_handler/test_schema.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/unittests/test_handler/test_schema.py b/tests/unittests/test_handler/test_schema.py index b08068aa088..3151446374c 100644 --- a/tests/unittests/test_handler/test_schema.py +++ b/tests/unittests/test_handler/test_schema.py @@ -459,9 +459,11 @@ def _get_schema_doc_examples(): 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), get_schema()) + validate_cloudconfig_file(str(example_path), self.schema) # vi: ts=4 expandtab syntax=python From a3d0167532bd1aa00e6ab1f4b4523342adbe5096 Mon Sep 17 00:00:00 2001 From: James Falcon Date: Fri, 8 May 2020 13:29:31 -0500 Subject: [PATCH 5/5] [squash] review comments --- tests/unittests/test_handler/test_schema.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/unittests/test_handler/test_schema.py b/tests/unittests/test_handler/test_schema.py index 3151446374c..071b98bc8bc 100644 --- a/tests/unittests/test_handler/test_schema.py +++ b/tests/unittests/test_handler/test_schema.py @@ -115,12 +115,12 @@ def test_validateconfig_schema_honors_formats(self): 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 @@ def test_validateconfig_schema_of_example(self, schema_id, example): """ config_load = safe_load(example) validate_cloudconfig_schema( - config_load, TestCloudConfigExamples.SCHEMA, strict=True) + config_load, self.schema, strict=True) class ValidateCloudConfigFileTest(CiTestCase):