From 4c637458c40b80cb56464ecbcdd8a004a1ea2ae6 Mon Sep 17 00:00:00 2001 From: Viacheslav Mefodin Date: Sun, 27 Sep 2020 13:30:11 +0300 Subject: [PATCH 1/9] Cover with tests 'version' and 'licence' CLI commands --- tests/cli.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 tests/cli.py diff --git a/tests/cli.py b/tests/cli.py new file mode 100644 index 0000000..4596185 --- /dev/null +++ b/tests/cli.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +# ISC License +# +# Copyright 2019 FL Fintech E GmbH +# +# Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +from click.testing import CliRunner +from k8t import __version__, __license__ +from k8t.cli import root + +def test_print_version(): + runner = CliRunner() + + result = runner.invoke(root, ['--version']) + assert result.exit_code == 0 + assert __version__ in result.output + +def test_print_license(): + runner = CliRunner() + + result = runner.invoke(root, ['license']) + assert result.exit_code == 0 + assert __license__ in result.output + +# vim: fenc=utf-8:ts=4:sw=4:expandtab From b897c4ed23b409f4916fdfc7fafe6ccd8f4d0524 Mon Sep 17 00:00:00 2001 From: Viacheslav Mefodin Date: Sun, 27 Sep 2020 13:51:51 +0300 Subject: [PATCH 2/9] Cover with tests 'new' CLI command --- tests/cli.py | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/tests/cli.py b/tests/cli.py index 4596185..a122dc3 100644 --- a/tests/cli.py +++ b/tests/cli.py @@ -7,9 +7,11 @@ # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +import os from click.testing import CliRunner from k8t import __version__, __license__ from k8t.cli import root +from k8t.scaffolding import list_available_templates def test_print_version(): runner = CliRunner() @@ -25,4 +27,110 @@ def test_print_license(): assert result.exit_code == 0 assert __license__ in result.output +def test_new_project(): + runner = CliRunner() + + with runner.isolated_filesystem(): + result = runner.invoke(root, ['new', 'project', 'test']) + assert result.exit_code == 0 + assert 'Directory created: test' in result.output + assert 'File created: test/.k8t' in result.output + assert 'File created: test/values.yaml' in result.output + assert 'File created: test/config.yaml' in result.output + assert os.path.exists('test/.k8t') + assert os.path.exists('test/values.yaml') + assert os.path.exists('test/config.yaml') + +def test_new_cluster(): + runner = CliRunner() + + with runner.isolated_filesystem(): + open('.k8t', 'w') + + result = runner.invoke(root, ['new', 'cluster', 'cluster-1', '.']) + assert result.exit_code == 0 + assert 'Directory created: ./clusters/cluster-1' in result.output + assert 'File created: ./clusters/cluster-1/values.yaml' in result.output + assert 'File created: ./clusters/cluster-1/config.yaml' in result.output + assert os.path.exists('./clusters/cluster-1/values.yaml') + assert os.path.exists('./clusters/cluster-1/config.yaml') + +def test_new_environment(): + runner = CliRunner() + + with runner.isolated_filesystem(): + open('.k8t', 'w') + + result = runner.invoke(root, ['new', 'environment', 'staging', '.']) + assert result.exit_code == 0 + assert 'Directory created: ./environments/staging' in result.output + assert 'File created: ./environments/staging/values.yaml' in result.output + assert 'File created: ./environments/staging/config.yaml' in result.output + assert os.path.exists('./environments/staging/values.yaml') + assert os.path.exists('./environments/staging/config.yaml') + + os.makedirs('clusters/cluster-1') + + result = runner.invoke(root, ['new', 'environment', '-c', 'cluster-1', 'production', '.']) + assert result.exit_code == 0 + assert 'Directory created: ./clusters/cluster-1/environments/production' in result.output + assert 'File created: ./clusters/cluster-1/environments/production/values.yaml' in result.output + assert 'File created: ./clusters/cluster-1/environments/production/config.yaml' in result.output + assert os.path.exists('./clusters/cluster-1/environments/production/values.yaml') + assert os.path.exists('./clusters/cluster-1/environments/production/config.yaml') + +def test_new_template(): + template_type = list(list_available_templates())[0] + runner = CliRunner() + + with runner.isolated_filesystem(): + open('.k8t', 'w') + + result = runner.invoke(root, ['new', 'template', '-n', 'bar', template_type, '.']) + assert result.exit_code == 0 + assert 'Directory created: ./templates' in result.output + assert f'Template created: ./templates/bar-{template_type}.yaml.j2' in result.output + assert os.path.exists(f'./templates/bar-{template_type}.yaml.j2') + + result = runner.invoke(root, ['new', 'template', '-p', 'foo', template_type, '.']) + assert result.exit_code == 0 + assert f'Template created: ./templates/foo{template_type}.yaml.j2' in result.output + assert os.path.exists(f'./templates/foo{template_type}.yaml.j2') + + result = runner.invoke(root, ['new', 'template', '-p', 'foo', '-n', 'bar', template_type, '.']) + assert result.exit_code == 0 + assert f'Template created: ./templates/foobar-{template_type}.yaml.j2' in result.output + assert os.path.exists(f'./templates/foobar-{template_type}.yaml.j2') + + os.makedirs('environments/staging') + + result = runner.invoke(root, ['new', 'template', '-e', 'staging', template_type, '.']) + assert result.exit_code == 0 + assert 'Directory created: ./environments/staging/templates' in result.output + assert f'Template created: ./environments/staging/templates/{template_type}.yaml.j2' in result.output + assert os.path.exists(f'./environments/staging/templates/{template_type}.yaml.j2') + + os.makedirs('clusters/cluster-1') + + result = runner.invoke(root, ['new', 'template', '-c', 'cluster-1', template_type, '.']) + assert result.exit_code == 0 + assert 'Directory created: ./clusters/cluster-1/templates' in result.output + assert f'Template created: ./clusters/cluster-1/templates/{template_type}.yaml.j2' in result.output + assert os.path.exists(f'./clusters/cluster-1/templates/{template_type}.yaml.j2') + + os.makedirs('clusters/cluster-1/environments/production') + + result = runner.invoke(root, ['new', 'template', '-c', 'cluster-1', '-e', 'production', template_type, '.']) + assert result.exit_code == 0 + assert 'Directory created: ./clusters/cluster-1/environments/production/templates' in result.output + assert f'Template created: ./clusters/cluster-1/environments/production/templates/{template_type}.yaml.j2' in result.output + assert os.path.exists(f'./clusters/cluster-1/environments/production/templates/{template_type}.yaml.j2') + + for template_type in list_available_templates(): + result = runner.invoke(root, ['new', 'template', template_type, '.']) + assert result.exit_code == 0 + assert f'Template created: ./templates/{template_type}.yaml.j2' in result.output + assert os.path.exists(f'./templates/{template_type}.yaml.j2') + + # vim: fenc=utf-8:ts=4:sw=4:expandtab From 2a9ee53e0fa1ac556eae2b91f03bb4b7f9ae08d9 Mon Sep 17 00:00:00 2001 From: Viacheslav Mefodin Date: Sun, 27 Sep 2020 14:21:39 +0300 Subject: [PATCH 3/9] Cover with tests 'get' CLI command --- tests/cli.py | 81 +++++++++++++++++++ tests/dummy/k8t/.k8t | 0 .../templates/cluster-1-env-template.yaml.j2 | 6 ++ .../cluster-specific-env/values.yaml | 1 + .../templates/cluster-1-template.yaml.j2 | 7 ++ .../environments/common-env/values.yaml | 1 + .../templates/cluster-1-template.yaml.j2 | 6 ++ .../dummy/k8t/clusters/cluster-1/values.yaml | 1 + .../dummy/k8t/clusters/cluster-2/values.yaml | 2 + tests/dummy/k8t/config.yaml | 2 + .../templates/common-env-template.yaml.j2 | 6 ++ .../k8t/environments/common-env/values.yaml | 1 + .../k8t/environments/some-env/values.yaml | 1 + .../k8t/templates/common-template.yaml.j2 | 6 ++ tests/dummy/k8t/values.yaml | 5 ++ 15 files changed, 126 insertions(+) create mode 100644 tests/dummy/k8t/.k8t create mode 100644 tests/dummy/k8t/clusters/cluster-1/environments/cluster-specific-env/templates/cluster-1-env-template.yaml.j2 create mode 100644 tests/dummy/k8t/clusters/cluster-1/environments/cluster-specific-env/values.yaml create mode 100644 tests/dummy/k8t/clusters/cluster-1/environments/common-env/templates/cluster-1-template.yaml.j2 create mode 100644 tests/dummy/k8t/clusters/cluster-1/environments/common-env/values.yaml create mode 100644 tests/dummy/k8t/clusters/cluster-1/templates/cluster-1-template.yaml.j2 create mode 100644 tests/dummy/k8t/clusters/cluster-1/values.yaml create mode 100644 tests/dummy/k8t/clusters/cluster-2/values.yaml create mode 100644 tests/dummy/k8t/config.yaml create mode 100644 tests/dummy/k8t/environments/common-env/templates/common-env-template.yaml.j2 create mode 100644 tests/dummy/k8t/environments/common-env/values.yaml create mode 100644 tests/dummy/k8t/environments/some-env/values.yaml create mode 100644 tests/dummy/k8t/templates/common-template.yaml.j2 create mode 100644 tests/dummy/k8t/values.yaml diff --git a/tests/cli.py b/tests/cli.py index a122dc3..9159687 100644 --- a/tests/cli.py +++ b/tests/cli.py @@ -132,5 +132,86 @@ def test_new_template(): assert f'Template created: ./templates/{template_type}.yaml.j2' in result.output assert os.path.exists(f'./templates/{template_type}.yaml.j2') +def test_get_clusters(): + runner = CliRunner() + + result = runner.invoke(root, ['get', 'clusters', 'tests/dummy/k8t']) + assert result.exit_code == 0 + # TODO: Fix that, should return clusters, not environments + assert 'some-env' in result.output + assert 'common-env' in result.output + assert 'cluster-specific-env' not in result.output + +def test_get_environments(): + runner = CliRunner() + + result = runner.invoke(root, ['get', 'environments', 'tests/dummy/k8t']) + assert result.exit_code == 0 + assert 'some-env' in result.output + assert 'common-env' in result.output + assert 'cluster-specific-env' in result.output + + result = runner.invoke(root, ['get', 'environments', '-c', 'cluster-1', 'tests/dummy/k8t']) + assert result.exit_code == 0 + assert 'some-env' not in result.output + assert 'common-env' in result.output + assert 'cluster-specific-env' in result.output + + result = runner.invoke(root, ['get', 'environments', '-c', 'cluster-2', 'tests/dummy/k8t']) + assert result.exit_code == 0 + assert not result.output + +def test_get_templates(): + runner = CliRunner() + + result = runner.invoke(root, ['get', 'templates', 'tests/dummy/k8t']) + assert result.exit_code == 0 + assert 'common-template.yaml.j2' in result.output + assert 'common-env-template.yaml.j2' not in result.output + assert 'cluster-1-template.yaml.j2' not in result.output + assert 'cluster-1-env-template.yaml.j2' not in result.output + + result = runner.invoke(root, ['get', 'templates', '-e', 'common-env', 'tests/dummy/k8t']) + assert result.exit_code == 0 + assert 'common-template.yaml.j2' in result.output + assert 'common-env-template.yaml.j2' in result.output + assert 'cluster-1-template.yaml.j2' not in result.output + assert 'cluster-1-env-template.yaml.j2' not in result.output + + result = runner.invoke(root, ['get', 'templates', '-e', 'some-env', 'tests/dummy/k8t']) + assert result.exit_code == 0 + assert 'common-template.yaml.j2' in result.output + assert 'common-env-template.yaml.j2' not in result.output + assert 'cluster-1-template.yaml.j2' not in result.output + assert 'cluster-1-env-template.yaml.j2' not in result.output + + result = runner.invoke(root, ['get', 'templates', '-c', 'cluster-1', 'tests/dummy/k8t']) + assert result.exit_code == 0 + assert 'common-template.yaml.j2' in result.output + assert 'common-env-template.yaml.j2' not in result.output + assert 'cluster-1-template.yaml.j2' in result.output + assert 'cluster-1-env-template.yaml.j2' not in result.output + + result = runner.invoke(root, ['get', 'templates', '-c', 'cluster-1', '-e', 'cluster-specific-env', 'tests/dummy/k8t']) + assert result.exit_code == 0 + assert 'common-template.yaml.j2' in result.output + assert 'common-env-template.yaml.j2' not in result.output + assert 'cluster-1-template.yaml.j2' in result.output + assert 'cluster-1-env-template.yaml.j2' in result.output + + result = runner.invoke(root, ['get', 'templates', '-c', 'cluster-1', '-e', 'common-env', 'tests/dummy/k8t']) + assert result.exit_code == 0 + assert 'common-template.yaml.j2' in result.output + assert 'common-env-template.yaml.j2' in result.output + assert 'cluster-1-template.yaml.j2' in result.output + assert 'cluster-1-env-template.yaml.j2' not in result.output + + result = runner.invoke(root, ['get', 'templates', '-c', 'cluster-2', 'tests/dummy/k8t']) + assert result.exit_code == 0 + assert 'common-template.yaml.j2' in result.output + assert 'common-env-template.yaml.j2' not in result.output + assert 'cluster-1-template.yaml.j2' not in result.output + assert 'cluster-1-env-template.yaml.j2' not in result.output + # vim: fenc=utf-8:ts=4:sw=4:expandtab diff --git a/tests/dummy/k8t/.k8t b/tests/dummy/k8t/.k8t new file mode 100644 index 0000000..e69de29 diff --git a/tests/dummy/k8t/clusters/cluster-1/environments/cluster-specific-env/templates/cluster-1-env-template.yaml.j2 b/tests/dummy/k8t/clusters/cluster-1/environments/cluster-specific-env/templates/cluster-1-env-template.yaml.j2 new file mode 100644 index 0000000..922b0a5 --- /dev/null +++ b/tests/dummy/k8t/clusters/cluster-1/environments/cluster-specific-env/templates/cluster-1-env-template.yaml.j2 @@ -0,0 +1,6 @@ +test: "{{test}}" +env: "{{env}}" +cluster: "{{cls}}" +foo: "{{foo}}" +bar: "{{bar}}" +buz: "{{buz | default('buz_default')}}" diff --git a/tests/dummy/k8t/clusters/cluster-1/environments/cluster-specific-env/values.yaml b/tests/dummy/k8t/clusters/cluster-1/environments/cluster-specific-env/values.yaml new file mode 100644 index 0000000..cdac3ea --- /dev/null +++ b/tests/dummy/k8t/clusters/cluster-1/environments/cluster-specific-env/values.yaml @@ -0,0 +1 @@ +env: cluster-1-env diff --git a/tests/dummy/k8t/clusters/cluster-1/environments/common-env/templates/cluster-1-template.yaml.j2 b/tests/dummy/k8t/clusters/cluster-1/environments/common-env/templates/cluster-1-template.yaml.j2 new file mode 100644 index 0000000..be44aab --- /dev/null +++ b/tests/dummy/k8t/clusters/cluster-1/environments/common-env/templates/cluster-1-template.yaml.j2 @@ -0,0 +1,7 @@ +test: "{{test}}" +env: "{{env}}" +cluster: "{{cls}}" +foo: "{{foo}}" +bar: "{{bar}}" +buz: "{{buz | default('buz_default')}}" +overwritten-in-env: true diff --git a/tests/dummy/k8t/clusters/cluster-1/environments/common-env/values.yaml b/tests/dummy/k8t/clusters/cluster-1/environments/common-env/values.yaml new file mode 100644 index 0000000..4efba26 --- /dev/null +++ b/tests/dummy/k8t/clusters/cluster-1/environments/common-env/values.yaml @@ -0,0 +1 @@ +env: cluster-1-common-env diff --git a/tests/dummy/k8t/clusters/cluster-1/templates/cluster-1-template.yaml.j2 b/tests/dummy/k8t/clusters/cluster-1/templates/cluster-1-template.yaml.j2 new file mode 100644 index 0000000..922b0a5 --- /dev/null +++ b/tests/dummy/k8t/clusters/cluster-1/templates/cluster-1-template.yaml.j2 @@ -0,0 +1,6 @@ +test: "{{test}}" +env: "{{env}}" +cluster: "{{cls}}" +foo: "{{foo}}" +bar: "{{bar}}" +buz: "{{buz | default('buz_default')}}" diff --git a/tests/dummy/k8t/clusters/cluster-1/values.yaml b/tests/dummy/k8t/clusters/cluster-1/values.yaml new file mode 100644 index 0000000..1f337f7 --- /dev/null +++ b/tests/dummy/k8t/clusters/cluster-1/values.yaml @@ -0,0 +1 @@ +cls: cluster-1 diff --git a/tests/dummy/k8t/clusters/cluster-2/values.yaml b/tests/dummy/k8t/clusters/cluster-2/values.yaml new file mode 100644 index 0000000..52d40ee --- /dev/null +++ b/tests/dummy/k8t/clusters/cluster-2/values.yaml @@ -0,0 +1,2 @@ +cls: cluster-2 +buz: 'buz' diff --git a/tests/dummy/k8t/config.yaml b/tests/dummy/k8t/config.yaml new file mode 100644 index 0000000..589328c --- /dev/null +++ b/tests/dummy/k8t/config.yaml @@ -0,0 +1,2 @@ +secrets: + provider: random diff --git a/tests/dummy/k8t/environments/common-env/templates/common-env-template.yaml.j2 b/tests/dummy/k8t/environments/common-env/templates/common-env-template.yaml.j2 new file mode 100644 index 0000000..922b0a5 --- /dev/null +++ b/tests/dummy/k8t/environments/common-env/templates/common-env-template.yaml.j2 @@ -0,0 +1,6 @@ +test: "{{test}}" +env: "{{env}}" +cluster: "{{cls}}" +foo: "{{foo}}" +bar: "{{bar}}" +buz: "{{buz | default('buz_default')}}" diff --git a/tests/dummy/k8t/environments/common-env/values.yaml b/tests/dummy/k8t/environments/common-env/values.yaml new file mode 100644 index 0000000..9551dba --- /dev/null +++ b/tests/dummy/k8t/environments/common-env/values.yaml @@ -0,0 +1 @@ +env: common-env diff --git a/tests/dummy/k8t/environments/some-env/values.yaml b/tests/dummy/k8t/environments/some-env/values.yaml new file mode 100644 index 0000000..d93717d --- /dev/null +++ b/tests/dummy/k8t/environments/some-env/values.yaml @@ -0,0 +1 @@ +env: some-env diff --git a/tests/dummy/k8t/templates/common-template.yaml.j2 b/tests/dummy/k8t/templates/common-template.yaml.j2 new file mode 100644 index 0000000..922b0a5 --- /dev/null +++ b/tests/dummy/k8t/templates/common-template.yaml.j2 @@ -0,0 +1,6 @@ +test: "{{test}}" +env: "{{env}}" +cluster: "{{cls}}" +foo: "{{foo}}" +bar: "{{bar}}" +buz: "{{buz | default('buz_default')}}" diff --git a/tests/dummy/k8t/values.yaml b/tests/dummy/k8t/values.yaml new file mode 100644 index 0000000..4e98eba --- /dev/null +++ b/tests/dummy/k8t/values.yaml @@ -0,0 +1,5 @@ +test: default +cls: none +env: none +foo: 1 +bar: test From a391bc97769a6b8982750dac346bd9113594e29e Mon Sep 17 00:00:00 2001 From: Viacheslav Mefodin Date: Sun, 27 Sep 2020 15:15:21 +0300 Subject: [PATCH 4/9] Cover with tests 'validate' CLI command --- tests/cli.py | 83 +++++++++++++++++++ tests/dummy/bad/.k8t | 0 tests/dummy/bad/config.yaml | 0 .../bad/templates/filter-template.yaml.j2 | 4 + .../nested-value-adding-template.yaml.j2 | 4 + .../templates/nested-value-template.yaml.j2 | 4 + .../bad/templates/secret-template.yaml.j2 | 4 + .../bad/templates/several-template.yaml.j2 | 4 + .../bad/templates/value-template.yaml.j2 | 4 + tests/dummy/bad/values.yaml | 2 + .../dummy/k8t/clusters/cluster-1/config.yaml | 0 .../cluster-specific-env/config.yaml | 0 .../environments/common-env/config.yaml | 0 .../dummy/k8t/clusters/cluster-2/config.yaml | 0 .../k8t/environments/common-env/config.yaml | 0 .../k8t/environments/some-env/config.yaml | 0 tests/dummy/missing_values/.k8t | 0 tests/dummy/missing_values/config.yaml | 0 .../missing_values/templates/template.yaml.j2 | 2 + tests/dummy/missing_values/value-file.yaml | 1 + tests/dummy/missing_values/values.yaml | 0 21 files changed, 112 insertions(+) create mode 100644 tests/dummy/bad/.k8t create mode 100644 tests/dummy/bad/config.yaml create mode 100644 tests/dummy/bad/templates/filter-template.yaml.j2 create mode 100644 tests/dummy/bad/templates/nested-value-adding-template.yaml.j2 create mode 100644 tests/dummy/bad/templates/nested-value-template.yaml.j2 create mode 100644 tests/dummy/bad/templates/secret-template.yaml.j2 create mode 100644 tests/dummy/bad/templates/several-template.yaml.j2 create mode 100644 tests/dummy/bad/templates/value-template.yaml.j2 create mode 100644 tests/dummy/bad/values.yaml create mode 100644 tests/dummy/k8t/clusters/cluster-1/config.yaml create mode 100644 tests/dummy/k8t/clusters/cluster-1/environments/cluster-specific-env/config.yaml create mode 100644 tests/dummy/k8t/clusters/cluster-1/environments/common-env/config.yaml create mode 100644 tests/dummy/k8t/clusters/cluster-2/config.yaml create mode 100644 tests/dummy/k8t/environments/common-env/config.yaml create mode 100644 tests/dummy/k8t/environments/some-env/config.yaml create mode 100644 tests/dummy/missing_values/.k8t create mode 100644 tests/dummy/missing_values/config.yaml create mode 100644 tests/dummy/missing_values/templates/template.yaml.j2 create mode 100644 tests/dummy/missing_values/value-file.yaml create mode 100644 tests/dummy/missing_values/values.yaml diff --git a/tests/cli.py b/tests/cli.py index 9159687..9cf0cb0 100644 --- a/tests/cli.py +++ b/tests/cli.py @@ -213,5 +213,88 @@ def test_get_templates(): assert 'cluster-1-template.yaml.j2' not in result.output assert 'cluster-1-env-template.yaml.j2' not in result.output +def test_validate_successful(): + runner = CliRunner() + + result = runner.invoke(root, ['validate', 'tests/dummy/k8t']) + assert result.exit_code == 0 + assert 'common-template.yaml.j2: ✔' in result.output + assert 'common-env-template.yaml.j2: ✔' not in result.output + assert 'cluster-1-template.yaml.j2: ✔' not in result.output + assert 'cluster-1-env-template.yaml.j2: ✔' not in result.output + + result = runner.invoke(root, ['validate', '-e', 'common-env', 'tests/dummy/k8t']) + assert result.exit_code == 0 + assert 'common-template.yaml.j2: ✔' in result.output + assert 'common-env-template.yaml.j2: ✔' in result.output + assert 'cluster-1-template.yaml.j2: ✔' not in result.output + assert 'cluster-1-env-template.yaml.j2: ✔' not in result.output + + result = runner.invoke(root, ['validate', '-e', 'some-env', 'tests/dummy/k8t']) + assert result.exit_code == 0 + assert 'common-template.yaml.j2: ✔' in result.output + assert 'common-env-template.yaml.j2: ✔' not in result.output + assert 'cluster-1-template.yaml.j2: ✔' not in result.output + assert 'cluster-1-env-template.yaml.j2: ✔' not in result.output + + result = runner.invoke(root, ['validate', '-c', 'cluster-1', 'tests/dummy/k8t']) + assert result.exit_code == 0 + assert 'common-template.yaml.j2: ✔' in result.output + assert 'common-env-template.yaml.j2: ✔' not in result.output + assert 'cluster-1-template.yaml.j2: ✔' in result.output + assert 'cluster-1-env-template.yaml.j2: ✔' not in result.output + + result = runner.invoke(root, ['validate', '-c', 'cluster-1', '-e', 'cluster-specific-env', 'tests/dummy/k8t']) + assert result.exit_code == 0 + assert 'common-template.yaml.j2: ✔' in result.output + assert 'common-env-template.yaml.j2: ✔' not in result.output + assert 'cluster-1-template.yaml.j2: ✔' in result.output + assert 'cluster-1-env-template.yaml.j2: ✔' in result.output + + result = runner.invoke(root, ['validate', '-c', 'cluster-1', '-e', 'common-env', 'tests/dummy/k8t']) + assert result.exit_code == 0 + assert 'common-template.yaml.j2: ✔' in result.output + assert 'common-env-template.yaml.j2: ✔' in result.output + assert 'cluster-1-template.yaml.j2: ✔' in result.output + assert 'cluster-1-env-template.yaml.j2: ✔' not in result.output + + result = runner.invoke(root, ['validate', '-c', 'cluster-2', 'tests/dummy/k8t']) + assert result.exit_code == 0 + assert 'common-template.yaml.j2: ✔' in result.output + assert 'common-env-template.yaml.j2: ✔' not in result.output + assert 'cluster-1-template.yaml.j2: ✔' not in result.output + assert 'cluster-1-env-template.yaml.j2: ✔: ✔' not in result.output + +def test_validate_with_values(): + runner = CliRunner() + + result = runner.invoke(root, ['validate', 'tests/dummy/missing_values']) + assert result.exit_code # TODO: Should be 1 + assert 'template.yaml.j2: ✗' in result.output + assert '- undefined variable: test' in result.output + + result = runner.invoke(root, ['validate', '--value', 'test', 'something', 'tests/dummy/missing_values']) + assert result.exit_code == 0 + assert 'template.yaml.j2: ✔' in result.output + + result = runner.invoke( + root, + ['validate', '--value-file', 'tests/dummy/missing_values/value-file.yaml', 'tests/dummy/missing_values'] + ) + assert result.exit_code == 0 + assert 'template.yaml.j2: ✔' in result.output + +def test_validate_failure(): + runner = CliRunner() + + result = runner.invoke(root, ['validate', 'tests/dummy/bad']) + # TODO: Fix and uncomment + # assert 'filter-template.yaml.j2: ✗' in result.output + # assert 'nested-value-adding-template.yaml.j2: ✗' in result.output + # assert 'nested-value-template.yaml.j2: ✗' in result.output + assert 'secret-template.yaml.j2: ✗' in result.output # TODO: Crushes, check file + assert 'several-template.yaml.j2: ✗' in result.output + assert 'value-template.yaml.j2: ✗' in result.output + # vim: fenc=utf-8:ts=4:sw=4:expandtab diff --git a/tests/dummy/bad/.k8t b/tests/dummy/bad/.k8t new file mode 100644 index 0000000..e69de29 diff --git a/tests/dummy/bad/config.yaml b/tests/dummy/bad/config.yaml new file mode 100644 index 0000000..e69de29 diff --git a/tests/dummy/bad/templates/filter-template.yaml.j2 b/tests/dummy/bad/templates/filter-template.yaml.j2 new file mode 100644 index 0000000..4537f2b --- /dev/null +++ b/tests/dummy/bad/templates/filter-template.yaml.j2 @@ -0,0 +1,4 @@ +one: 1 +{# two: {{ 2 | not_existing_filter }} #} +three: {{ three | default(3) }} +four: {{ test.four | default(4) }} diff --git a/tests/dummy/bad/templates/nested-value-adding-template.yaml.j2 b/tests/dummy/bad/templates/nested-value-adding-template.yaml.j2 new file mode 100644 index 0000000..3943db6 --- /dev/null +++ b/tests/dummy/bad/templates/nested-value-adding-template.yaml.j2 @@ -0,0 +1,4 @@ +one: 1 +two: {{ test.one + 1 }} +three: 3 +four: {{2 + 2}} diff --git a/tests/dummy/bad/templates/nested-value-template.yaml.j2 b/tests/dummy/bad/templates/nested-value-template.yaml.j2 new file mode 100644 index 0000000..2ac1af8 --- /dev/null +++ b/tests/dummy/bad/templates/nested-value-template.yaml.j2 @@ -0,0 +1,4 @@ +one: 1 +two: {{ test.two }} +three: 3 +four: {{2 + 2}} diff --git a/tests/dummy/bad/templates/secret-template.yaml.j2 b/tests/dummy/bad/templates/secret-template.yaml.j2 new file mode 100644 index 0000000..47f78c2 --- /dev/null +++ b/tests/dummy/bad/templates/secret-template.yaml.j2 @@ -0,0 +1,4 @@ +one: "{{ get_secret('/one', 24) | b64encode }}" +two: 2 +three: {{ 2 + 1 }} +four: {{ four }} \ No newline at end of file diff --git a/tests/dummy/bad/templates/several-template.yaml.j2 b/tests/dummy/bad/templates/several-template.yaml.j2 new file mode 100644 index 0000000..f28ce88 --- /dev/null +++ b/tests/dummy/bad/templates/several-template.yaml.j2 @@ -0,0 +1,4 @@ +one: {{ one }} +two: {{ two }} +three: {{three }} +four: {{ four }} diff --git a/tests/dummy/bad/templates/value-template.yaml.j2 b/tests/dummy/bad/templates/value-template.yaml.j2 new file mode 100644 index 0000000..fcb8f1d --- /dev/null +++ b/tests/dummy/bad/templates/value-template.yaml.j2 @@ -0,0 +1,4 @@ +one: 1 +two: 2 +three: {{ 2 + 1 }} +four: {{ four }} diff --git a/tests/dummy/bad/values.yaml b/tests/dummy/bad/values.yaml new file mode 100644 index 0000000..f329961 --- /dev/null +++ b/tests/dummy/bad/values.yaml @@ -0,0 +1,2 @@ +test: + foo: bar \ No newline at end of file diff --git a/tests/dummy/k8t/clusters/cluster-1/config.yaml b/tests/dummy/k8t/clusters/cluster-1/config.yaml new file mode 100644 index 0000000..e69de29 diff --git a/tests/dummy/k8t/clusters/cluster-1/environments/cluster-specific-env/config.yaml b/tests/dummy/k8t/clusters/cluster-1/environments/cluster-specific-env/config.yaml new file mode 100644 index 0000000..e69de29 diff --git a/tests/dummy/k8t/clusters/cluster-1/environments/common-env/config.yaml b/tests/dummy/k8t/clusters/cluster-1/environments/common-env/config.yaml new file mode 100644 index 0000000..e69de29 diff --git a/tests/dummy/k8t/clusters/cluster-2/config.yaml b/tests/dummy/k8t/clusters/cluster-2/config.yaml new file mode 100644 index 0000000..e69de29 diff --git a/tests/dummy/k8t/environments/common-env/config.yaml b/tests/dummy/k8t/environments/common-env/config.yaml new file mode 100644 index 0000000..e69de29 diff --git a/tests/dummy/k8t/environments/some-env/config.yaml b/tests/dummy/k8t/environments/some-env/config.yaml new file mode 100644 index 0000000..e69de29 diff --git a/tests/dummy/missing_values/.k8t b/tests/dummy/missing_values/.k8t new file mode 100644 index 0000000..e69de29 diff --git a/tests/dummy/missing_values/config.yaml b/tests/dummy/missing_values/config.yaml new file mode 100644 index 0000000..e69de29 diff --git a/tests/dummy/missing_values/templates/template.yaml.j2 b/tests/dummy/missing_values/templates/template.yaml.j2 new file mode 100644 index 0000000..af219c3 --- /dev/null +++ b/tests/dummy/missing_values/templates/template.yaml.j2 @@ -0,0 +1,2 @@ +test: "{{test}}" +buz: "{{buz | default('buz_default')}}" diff --git a/tests/dummy/missing_values/value-file.yaml b/tests/dummy/missing_values/value-file.yaml new file mode 100644 index 0000000..20d6bf5 --- /dev/null +++ b/tests/dummy/missing_values/value-file.yaml @@ -0,0 +1 @@ +test: something diff --git a/tests/dummy/missing_values/values.yaml b/tests/dummy/missing_values/values.yaml new file mode 100644 index 0000000..e69de29 From 899ba1353dcdcc53e152602d96b6f6915b4200cc Mon Sep 17 00:00:00 2001 From: Viacheslav Mefodin Date: Sun, 27 Sep 2020 16:03:35 +0300 Subject: [PATCH 5/9] Cover with tests 'gen' CLI command --- tests/cli.py | 50 +++++++++++++++++++ .../dummy/k8t/clusters/cluster-1/config.yaml | 4 ++ .../templates/cluster-1-env-template.yaml.j2 | 1 - .../templates/cluster-1-template.yaml.j2 | 1 - .../templates/cluster-1-template.yaml.j2 | 2 +- .../templates/common-env-template.yaml.j2 | 1 - .../k8t/templates/common-template.yaml.j2 | 1 - tests/dummy/k8t/values.yaml | 1 - .../cluster-1-cluster-specific-env.yaml | 22 ++++++++ tests/dummy/results/cluster-1-common-env.yaml | 22 ++++++++ tests/dummy/results/cluster-1.yaml | 15 ++++++ tests/dummy/results/cluster-2.yaml | 7 +++ tests/dummy/results/common-env.yaml | 14 ++++++ tests/dummy/results/default.yaml | 7 +++ tests/dummy/results/some-env.yaml | 7 +++ 15 files changed, 149 insertions(+), 6 deletions(-) create mode 100644 tests/dummy/results/cluster-1-cluster-specific-env.yaml create mode 100644 tests/dummy/results/cluster-1-common-env.yaml create mode 100644 tests/dummy/results/cluster-1.yaml create mode 100644 tests/dummy/results/cluster-2.yaml create mode 100644 tests/dummy/results/common-env.yaml create mode 100644 tests/dummy/results/default.yaml create mode 100644 tests/dummy/results/some-env.yaml diff --git a/tests/cli.py b/tests/cli.py index 9cf0cb0..ea08c6b 100644 --- a/tests/cli.py +++ b/tests/cli.py @@ -8,6 +8,8 @@ # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. import os +import boto3 +from moto import mock_ssm from click.testing import CliRunner from k8t import __version__, __license__ from k8t.cli import root @@ -296,5 +298,53 @@ def test_validate_failure(): assert 'several-template.yaml.j2: ✗' in result.output assert 'value-template.yaml.j2: ✗' in result.output +@mock_ssm +def test_gen(): + client = boto3.client('ssm', region_name='eu-central-1') + + client.put_parameter( + Name="/cluster-1/test", + Description="Environment specific simple parameter", + Value="string_value", + Type="String", + ) + + runner = CliRunner() + + with open('tests/dummy/results/default.yaml', 'r') as file: + result = runner.invoke(root, ['gen', 'tests/dummy/k8t']) + assert result.exit_code == 0 + assert result.output == file.read() + + with open('tests/dummy/results/some-env.yaml', 'r') as file: + result = runner.invoke(root, ['gen', '-e', 'some-env', 'tests/dummy/k8t']) + assert result.exit_code == 0 + assert result.output == file.read() + + with open('tests/dummy/results/common-env.yaml', 'r') as file: + result = runner.invoke(root, ['gen', '-e', 'common-env', 'tests/dummy/k8t']) + assert result.exit_code == 0 + assert result.output == file.read() + + with open('tests/dummy/results/cluster-1.yaml', 'r') as file: + result = runner.invoke(root, ['gen', '-c', 'cluster-1', 'tests/dummy/k8t']) + assert result.exit_code == 0 + assert result.output == file.read() + + with open('tests/dummy/results/cluster-1-common-env.yaml', 'r') as file: + result = runner.invoke(root, ['gen', '-c', 'cluster-1', '-e', 'common-env', 'tests/dummy/k8t']) + assert result.exit_code == 0 + assert result.output == file.read() + + with open('tests/dummy/results/cluster-1-cluster-specific-env.yaml', 'r') as file: + result = runner.invoke(root, ['gen', '-c', 'cluster-1', '-e', 'cluster-specific-env', 'tests/dummy/k8t']) + assert result.exit_code == 0 + assert result.output == file.read() + + with open('tests/dummy/results/cluster-2.yaml', 'r') as file: + result = runner.invoke(root, ['gen', '-c', 'cluster-2', 'tests/dummy/k8t']) + assert result.exit_code == 0 + assert result.output == file.read() + # vim: fenc=utf-8:ts=4:sw=4:expandtab diff --git a/tests/dummy/k8t/clusters/cluster-1/config.yaml b/tests/dummy/k8t/clusters/cluster-1/config.yaml index e69de29..05928db 100644 --- a/tests/dummy/k8t/clusters/cluster-1/config.yaml +++ b/tests/dummy/k8t/clusters/cluster-1/config.yaml @@ -0,0 +1,4 @@ +secrets: + provider: ssm + region: 'eu-central-1' + prefix: '/cluster-1' diff --git a/tests/dummy/k8t/clusters/cluster-1/environments/cluster-specific-env/templates/cluster-1-env-template.yaml.j2 b/tests/dummy/k8t/clusters/cluster-1/environments/cluster-specific-env/templates/cluster-1-env-template.yaml.j2 index 922b0a5..9e4a546 100644 --- a/tests/dummy/k8t/clusters/cluster-1/environments/cluster-specific-env/templates/cluster-1-env-template.yaml.j2 +++ b/tests/dummy/k8t/clusters/cluster-1/environments/cluster-specific-env/templates/cluster-1-env-template.yaml.j2 @@ -1,4 +1,3 @@ -test: "{{test}}" env: "{{env}}" cluster: "{{cls}}" foo: "{{foo}}" diff --git a/tests/dummy/k8t/clusters/cluster-1/environments/common-env/templates/cluster-1-template.yaml.j2 b/tests/dummy/k8t/clusters/cluster-1/environments/common-env/templates/cluster-1-template.yaml.j2 index be44aab..f860830 100644 --- a/tests/dummy/k8t/clusters/cluster-1/environments/common-env/templates/cluster-1-template.yaml.j2 +++ b/tests/dummy/k8t/clusters/cluster-1/environments/common-env/templates/cluster-1-template.yaml.j2 @@ -1,4 +1,3 @@ -test: "{{test}}" env: "{{env}}" cluster: "{{cls}}" foo: "{{foo}}" diff --git a/tests/dummy/k8t/clusters/cluster-1/templates/cluster-1-template.yaml.j2 b/tests/dummy/k8t/clusters/cluster-1/templates/cluster-1-template.yaml.j2 index 922b0a5..4a178dc 100644 --- a/tests/dummy/k8t/clusters/cluster-1/templates/cluster-1-template.yaml.j2 +++ b/tests/dummy/k8t/clusters/cluster-1/templates/cluster-1-template.yaml.j2 @@ -1,6 +1,6 @@ -test: "{{test}}" env: "{{env}}" cluster: "{{cls}}" foo: "{{foo}}" bar: "{{bar}}" buz: "{{buz | default('buz_default')}}" +secret: "{{get_secret('/test', 12) | b64encode}}" diff --git a/tests/dummy/k8t/environments/common-env/templates/common-env-template.yaml.j2 b/tests/dummy/k8t/environments/common-env/templates/common-env-template.yaml.j2 index 922b0a5..9e4a546 100644 --- a/tests/dummy/k8t/environments/common-env/templates/common-env-template.yaml.j2 +++ b/tests/dummy/k8t/environments/common-env/templates/common-env-template.yaml.j2 @@ -1,4 +1,3 @@ -test: "{{test}}" env: "{{env}}" cluster: "{{cls}}" foo: "{{foo}}" diff --git a/tests/dummy/k8t/templates/common-template.yaml.j2 b/tests/dummy/k8t/templates/common-template.yaml.j2 index 922b0a5..9e4a546 100644 --- a/tests/dummy/k8t/templates/common-template.yaml.j2 +++ b/tests/dummy/k8t/templates/common-template.yaml.j2 @@ -1,4 +1,3 @@ -test: "{{test}}" env: "{{env}}" cluster: "{{cls}}" foo: "{{foo}}" diff --git a/tests/dummy/k8t/values.yaml b/tests/dummy/k8t/values.yaml index 4e98eba..3d21692 100644 --- a/tests/dummy/k8t/values.yaml +++ b/tests/dummy/k8t/values.yaml @@ -1,4 +1,3 @@ -test: default cls: none env: none foo: 1 diff --git a/tests/dummy/results/cluster-1-cluster-specific-env.yaml b/tests/dummy/results/cluster-1-cluster-specific-env.yaml new file mode 100644 index 0000000..11a1760 --- /dev/null +++ b/tests/dummy/results/cluster-1-cluster-specific-env.yaml @@ -0,0 +1,22 @@ +--- +# Source: cluster-1-env-template.yaml.j2 +env: "cluster-1-env" +cluster: "cluster-1" +foo: "1" +bar: "test" +buz: "buz_default" +--- +# Source: cluster-1-template.yaml.j2 +env: "cluster-1-env" +cluster: "cluster-1" +foo: "1" +bar: "test" +buz: "buz_default" +secret: "c3RyaW5nX3ZhbHVl" +--- +# Source: common-template.yaml.j2 +env: "cluster-1-env" +cluster: "cluster-1" +foo: "1" +bar: "test" +buz: "buz_default" diff --git a/tests/dummy/results/cluster-1-common-env.yaml b/tests/dummy/results/cluster-1-common-env.yaml new file mode 100644 index 0000000..2de01d8 --- /dev/null +++ b/tests/dummy/results/cluster-1-common-env.yaml @@ -0,0 +1,22 @@ +--- +# Source: cluster-1-template.yaml.j2 +env: "cluster-1-common-env" +cluster: "cluster-1" +foo: "1" +bar: "test" +buz: "buz_default" +overwritten-in-env: true +--- +# Source: common-env-template.yaml.j2 +env: "cluster-1-common-env" +cluster: "cluster-1" +foo: "1" +bar: "test" +buz: "buz_default" +--- +# Source: common-template.yaml.j2 +env: "cluster-1-common-env" +cluster: "cluster-1" +foo: "1" +bar: "test" +buz: "buz_default" diff --git a/tests/dummy/results/cluster-1.yaml b/tests/dummy/results/cluster-1.yaml new file mode 100644 index 0000000..7d33c4a --- /dev/null +++ b/tests/dummy/results/cluster-1.yaml @@ -0,0 +1,15 @@ +--- +# Source: cluster-1-template.yaml.j2 +env: "none" +cluster: "cluster-1" +foo: "1" +bar: "test" +buz: "buz_default" +secret: "c3RyaW5nX3ZhbHVl" +--- +# Source: common-template.yaml.j2 +env: "none" +cluster: "cluster-1" +foo: "1" +bar: "test" +buz: "buz_default" diff --git a/tests/dummy/results/cluster-2.yaml b/tests/dummy/results/cluster-2.yaml new file mode 100644 index 0000000..5ca77ae --- /dev/null +++ b/tests/dummy/results/cluster-2.yaml @@ -0,0 +1,7 @@ +--- +# Source: common-template.yaml.j2 +env: "none" +cluster: "cluster-2" +foo: "1" +bar: "test" +buz: "buz" diff --git a/tests/dummy/results/common-env.yaml b/tests/dummy/results/common-env.yaml new file mode 100644 index 0000000..12e05ea --- /dev/null +++ b/tests/dummy/results/common-env.yaml @@ -0,0 +1,14 @@ +--- +# Source: common-env-template.yaml.j2 +env: "common-env" +cluster: "none" +foo: "1" +bar: "test" +buz: "buz_default" +--- +# Source: common-template.yaml.j2 +env: "common-env" +cluster: "none" +foo: "1" +bar: "test" +buz: "buz_default" diff --git a/tests/dummy/results/default.yaml b/tests/dummy/results/default.yaml new file mode 100644 index 0000000..b54a5c1 --- /dev/null +++ b/tests/dummy/results/default.yaml @@ -0,0 +1,7 @@ +--- +# Source: common-template.yaml.j2 +env: "none" +cluster: "none" +foo: "1" +bar: "test" +buz: "buz_default" diff --git a/tests/dummy/results/some-env.yaml b/tests/dummy/results/some-env.yaml new file mode 100644 index 0000000..96f7d47 --- /dev/null +++ b/tests/dummy/results/some-env.yaml @@ -0,0 +1,7 @@ +--- +# Source: common-template.yaml.j2 +env: "some-env" +cluster: "none" +foo: "1" +bar: "test" +buz: "buz_default" From 697fd4f5f99d39808021ac238b10f50eed3dcd66 Mon Sep 17 00:00:00 2001 From: Viacheslav Mefodin Date: Mon, 28 Sep 2020 08:16:13 +0300 Subject: [PATCH 6/9] Add composite and invalid yaml test cases --- tests/cli.py | 19 ++++++++++++++++++- .../composite-invalid-yaml-template.yaml.j2 | 11 +++++++++++ .../bad/templates/composite-template.yaml.j2 | 10 ++++++++++ .../templates/invalid-yaml-template.yaml.j2 | 3 +++ .../templates/composite-template.yaml.j2 | 8 ++++++++ tests/dummy/results/cluster-2.yaml | 10 ++++++++++ 6 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 tests/dummy/bad/templates/composite-invalid-yaml-template.yaml.j2 create mode 100644 tests/dummy/bad/templates/composite-template.yaml.j2 create mode 100644 tests/dummy/bad/templates/invalid-yaml-template.yaml.j2 create mode 100644 tests/dummy/k8t/clusters/cluster-2/templates/composite-template.yaml.j2 diff --git a/tests/cli.py b/tests/cli.py index ea08c6b..3d8e066 100644 --- a/tests/cli.py +++ b/tests/cli.py @@ -172,6 +172,7 @@ def test_get_templates(): assert 'common-env-template.yaml.j2' not in result.output assert 'cluster-1-template.yaml.j2' not in result.output assert 'cluster-1-env-template.yaml.j2' not in result.output + assert 'composite-template.yaml.j2' not in result.output result = runner.invoke(root, ['get', 'templates', '-e', 'common-env', 'tests/dummy/k8t']) assert result.exit_code == 0 @@ -179,6 +180,7 @@ def test_get_templates(): assert 'common-env-template.yaml.j2' in result.output assert 'cluster-1-template.yaml.j2' not in result.output assert 'cluster-1-env-template.yaml.j2' not in result.output + assert 'composite-template.yaml.j2' not in result.output result = runner.invoke(root, ['get', 'templates', '-e', 'some-env', 'tests/dummy/k8t']) assert result.exit_code == 0 @@ -186,6 +188,7 @@ def test_get_templates(): assert 'common-env-template.yaml.j2' not in result.output assert 'cluster-1-template.yaml.j2' not in result.output assert 'cluster-1-env-template.yaml.j2' not in result.output + assert 'composite-template.yaml.j2' not in result.output result = runner.invoke(root, ['get', 'templates', '-c', 'cluster-1', 'tests/dummy/k8t']) assert result.exit_code == 0 @@ -193,6 +196,7 @@ def test_get_templates(): assert 'common-env-template.yaml.j2' not in result.output assert 'cluster-1-template.yaml.j2' in result.output assert 'cluster-1-env-template.yaml.j2' not in result.output + assert 'composite-template.yaml.j2' not in result.output result = runner.invoke(root, ['get', 'templates', '-c', 'cluster-1', '-e', 'cluster-specific-env', 'tests/dummy/k8t']) assert result.exit_code == 0 @@ -200,6 +204,7 @@ def test_get_templates(): assert 'common-env-template.yaml.j2' not in result.output assert 'cluster-1-template.yaml.j2' in result.output assert 'cluster-1-env-template.yaml.j2' in result.output + assert 'composite-template.yaml.j2' not in result.output result = runner.invoke(root, ['get', 'templates', '-c', 'cluster-1', '-e', 'common-env', 'tests/dummy/k8t']) assert result.exit_code == 0 @@ -207,6 +212,7 @@ def test_get_templates(): assert 'common-env-template.yaml.j2' in result.output assert 'cluster-1-template.yaml.j2' in result.output assert 'cluster-1-env-template.yaml.j2' not in result.output + assert 'composite-template.yaml.j2' not in result.output result = runner.invoke(root, ['get', 'templates', '-c', 'cluster-2', 'tests/dummy/k8t']) assert result.exit_code == 0 @@ -214,6 +220,7 @@ def test_get_templates(): assert 'common-env-template.yaml.j2' not in result.output assert 'cluster-1-template.yaml.j2' not in result.output assert 'cluster-1-env-template.yaml.j2' not in result.output + assert 'composite-template.yaml.j2' in result.output def test_validate_successful(): runner = CliRunner() @@ -224,6 +231,7 @@ def test_validate_successful(): assert 'common-env-template.yaml.j2: ✔' not in result.output assert 'cluster-1-template.yaml.j2: ✔' not in result.output assert 'cluster-1-env-template.yaml.j2: ✔' not in result.output + assert 'composite-template.yaml.j2: ✔' not in result.output result = runner.invoke(root, ['validate', '-e', 'common-env', 'tests/dummy/k8t']) assert result.exit_code == 0 @@ -231,6 +239,7 @@ def test_validate_successful(): assert 'common-env-template.yaml.j2: ✔' in result.output assert 'cluster-1-template.yaml.j2: ✔' not in result.output assert 'cluster-1-env-template.yaml.j2: ✔' not in result.output + assert 'composite-template.yaml.j2: ✔' not in result.output result = runner.invoke(root, ['validate', '-e', 'some-env', 'tests/dummy/k8t']) assert result.exit_code == 0 @@ -238,6 +247,7 @@ def test_validate_successful(): assert 'common-env-template.yaml.j2: ✔' not in result.output assert 'cluster-1-template.yaml.j2: ✔' not in result.output assert 'cluster-1-env-template.yaml.j2: ✔' not in result.output + assert 'composite-template.yaml.j2: ✔' not in result.output result = runner.invoke(root, ['validate', '-c', 'cluster-1', 'tests/dummy/k8t']) assert result.exit_code == 0 @@ -245,6 +255,7 @@ def test_validate_successful(): assert 'common-env-template.yaml.j2: ✔' not in result.output assert 'cluster-1-template.yaml.j2: ✔' in result.output assert 'cluster-1-env-template.yaml.j2: ✔' not in result.output + assert 'composite-template.yaml.j2: ✔' not in result.output result = runner.invoke(root, ['validate', '-c', 'cluster-1', '-e', 'cluster-specific-env', 'tests/dummy/k8t']) assert result.exit_code == 0 @@ -252,6 +263,7 @@ def test_validate_successful(): assert 'common-env-template.yaml.j2: ✔' not in result.output assert 'cluster-1-template.yaml.j2: ✔' in result.output assert 'cluster-1-env-template.yaml.j2: ✔' in result.output + assert 'composite-template.yaml.j2: ✔' not in result.output result = runner.invoke(root, ['validate', '-c', 'cluster-1', '-e', 'common-env', 'tests/dummy/k8t']) assert result.exit_code == 0 @@ -259,13 +271,15 @@ def test_validate_successful(): assert 'common-env-template.yaml.j2: ✔' in result.output assert 'cluster-1-template.yaml.j2: ✔' in result.output assert 'cluster-1-env-template.yaml.j2: ✔' not in result.output + assert 'composite-template.yaml.j2: ✔' not in result.output result = runner.invoke(root, ['validate', '-c', 'cluster-2', 'tests/dummy/k8t']) assert result.exit_code == 0 assert 'common-template.yaml.j2: ✔' in result.output assert 'common-env-template.yaml.j2: ✔' not in result.output assert 'cluster-1-template.yaml.j2: ✔' not in result.output - assert 'cluster-1-env-template.yaml.j2: ✔: ✔' not in result.output + assert 'cluster-1-env-template.yaml.j2: ✔' not in result.output + assert 'composite-template.yaml.j2: ✔' in result.output def test_validate_with_values(): runner = CliRunner() @@ -297,6 +311,9 @@ def test_validate_failure(): assert 'secret-template.yaml.j2: ✗' in result.output # TODO: Crushes, check file assert 'several-template.yaml.j2: ✗' in result.output assert 'value-template.yaml.j2: ✗' in result.output + assert 'composite-template.yaml.j2: ✗' in result.output + # assert 'invalid-yaml-template.yaml.j2: ✗' in result.output + # assert 'composite-invalid-yaml-template.yaml.j2: ✗' in result.output @mock_ssm def test_gen(): diff --git a/tests/dummy/bad/templates/composite-invalid-yaml-template.yaml.j2 b/tests/dummy/bad/templates/composite-invalid-yaml-template.yaml.j2 new file mode 100644 index 0000000..2039d76 --- /dev/null +++ b/tests/dummy/bad/templates/composite-invalid-yaml-template.yaml.j2 @@ -0,0 +1,11 @@ +foo: 1 +bar: 2 +buz: 3 +--- +foo: 1 +bar: { +buz: 3 +--- +foo: { +bar: 2 +buz: 3 diff --git a/tests/dummy/bad/templates/composite-template.yaml.j2 b/tests/dummy/bad/templates/composite-template.yaml.j2 new file mode 100644 index 0000000..aed9813 --- /dev/null +++ b/tests/dummy/bad/templates/composite-template.yaml.j2 @@ -0,0 +1,10 @@ +--- +one: 1 +two: 2 +three: {{ 2 + 1 }} +four: {{ four }} +--- +one: 1 +two: 2 +three: {{ 2 + 1 }} +four: {{ four }} diff --git a/tests/dummy/bad/templates/invalid-yaml-template.yaml.j2 b/tests/dummy/bad/templates/invalid-yaml-template.yaml.j2 new file mode 100644 index 0000000..a13a2a9 --- /dev/null +++ b/tests/dummy/bad/templates/invalid-yaml-template.yaml.j2 @@ -0,0 +1,3 @@ +foo: 1 +bar: { +buz: 3 diff --git a/tests/dummy/k8t/clusters/cluster-2/templates/composite-template.yaml.j2 b/tests/dummy/k8t/clusters/cluster-2/templates/composite-template.yaml.j2 new file mode 100644 index 0000000..ac47aa0 --- /dev/null +++ b/tests/dummy/k8t/clusters/cluster-2/templates/composite-template.yaml.j2 @@ -0,0 +1,8 @@ +--- +template: 1 +env: "{{env}}" +cluster: "{{cls}}" +--- +template: 2 +env: "{{env}}" +cluster: "{{cls}}" diff --git a/tests/dummy/results/cluster-2.yaml b/tests/dummy/results/cluster-2.yaml index 5ca77ae..9ec85ad 100644 --- a/tests/dummy/results/cluster-2.yaml +++ b/tests/dummy/results/cluster-2.yaml @@ -5,3 +5,13 @@ cluster: "cluster-2" foo: "1" bar: "test" buz: "buz" +--- +# Source: composite-template.yaml.j2 +--- +template: 1 +env: "none" +cluster: "cluster-2" +--- +template: 2 +env: "none" +cluster: "cluster-2" From 7e2d9faebcf245faf9f27dacadf3aecb3f658936 Mon Sep 17 00:00:00 2001 From: Aljosha Friemann Date: Tue, 3 Nov 2020 11:19:31 +0100 Subject: [PATCH 7/9] renamed `dummy` to `resources` and resource folder `k8t` to `good` --- tests/cli.py | 81 ++++++++++--------- tests/{dummy => resources}/bad/.k8t | 0 tests/{dummy => resources}/bad/config.yaml | 0 .../composite-invalid-yaml-template.yaml.j2 | 0 .../bad/templates/composite-template.yaml.j2 | 0 .../bad/templates/filter-template.yaml.j2 | 0 .../templates/invalid-yaml-template.yaml.j2 | 0 .../nested-value-adding-template.yaml.j2 | 0 .../templates/nested-value-template.yaml.j2 | 0 .../bad/templates/secret-template.yaml.j2 | 0 .../bad/templates/several-template.yaml.j2 | 0 .../bad/templates/value-template.yaml.j2 | 0 tests/{dummy => resources}/bad/values.yaml | 0 tests/{dummy/k8t => resources/good}/.k8t | 0 .../good}/clusters/cluster-1/config.yaml | 0 .../cluster-specific-env/config.yaml | 0 .../templates/cluster-1-env-template.yaml.j2 | 0 .../cluster-specific-env/values.yaml | 0 .../environments/common-env/config.yaml | 0 .../templates/cluster-1-template.yaml.j2 | 0 .../environments/common-env/values.yaml | 0 .../templates/cluster-1-template.yaml.j2 | 0 .../good}/clusters/cluster-1/values.yaml | 0 .../good}/clusters/cluster-2/config.yaml | 0 .../templates/composite-template.yaml.j2 | 0 .../good}/clusters/cluster-2/values.yaml | 0 .../{dummy/k8t => resources/good}/config.yaml | 0 .../good}/environments/common-env/config.yaml | 0 .../templates/common-env-template.yaml.j2 | 0 .../good}/environments/common-env/values.yaml | 0 .../good}/environments/some-env/config.yaml | 0 .../good}/environments/some-env/values.yaml | 0 .../good}/templates/common-template.yaml.j2 | 0 .../{dummy/k8t => resources/good}/values.yaml | 0 .../{dummy => resources}/missing_values/.k8t | 0 .../missing_values/config.yaml | 0 .../missing_values/templates/template.yaml.j2 | 0 .../missing_values/value-file.yaml | 0 .../missing_values/values.yaml | 0 .../cluster-1-cluster-specific-env.yaml | 0 .../results/cluster-1-common-env.yaml | 0 .../results/cluster-1.yaml | 0 .../results/cluster-2.yaml | 0 .../results/common-env.yaml | 0 .../{dummy => resources}/results/default.yaml | 0 .../results/some-env.yaml | 0 46 files changed, 42 insertions(+), 39 deletions(-) rename tests/{dummy => resources}/bad/.k8t (100%) rename tests/{dummy => resources}/bad/config.yaml (100%) rename tests/{dummy => resources}/bad/templates/composite-invalid-yaml-template.yaml.j2 (100%) rename tests/{dummy => resources}/bad/templates/composite-template.yaml.j2 (100%) rename tests/{dummy => resources}/bad/templates/filter-template.yaml.j2 (100%) rename tests/{dummy => resources}/bad/templates/invalid-yaml-template.yaml.j2 (100%) rename tests/{dummy => resources}/bad/templates/nested-value-adding-template.yaml.j2 (100%) rename tests/{dummy => resources}/bad/templates/nested-value-template.yaml.j2 (100%) rename tests/{dummy => resources}/bad/templates/secret-template.yaml.j2 (100%) rename tests/{dummy => resources}/bad/templates/several-template.yaml.j2 (100%) rename tests/{dummy => resources}/bad/templates/value-template.yaml.j2 (100%) rename tests/{dummy => resources}/bad/values.yaml (100%) rename tests/{dummy/k8t => resources/good}/.k8t (100%) rename tests/{dummy/k8t => resources/good}/clusters/cluster-1/config.yaml (100%) rename tests/{dummy/k8t => resources/good}/clusters/cluster-1/environments/cluster-specific-env/config.yaml (100%) rename tests/{dummy/k8t => resources/good}/clusters/cluster-1/environments/cluster-specific-env/templates/cluster-1-env-template.yaml.j2 (100%) rename tests/{dummy/k8t => resources/good}/clusters/cluster-1/environments/cluster-specific-env/values.yaml (100%) rename tests/{dummy/k8t => resources/good}/clusters/cluster-1/environments/common-env/config.yaml (100%) rename tests/{dummy/k8t => resources/good}/clusters/cluster-1/environments/common-env/templates/cluster-1-template.yaml.j2 (100%) rename tests/{dummy/k8t => resources/good}/clusters/cluster-1/environments/common-env/values.yaml (100%) rename tests/{dummy/k8t => resources/good}/clusters/cluster-1/templates/cluster-1-template.yaml.j2 (100%) rename tests/{dummy/k8t => resources/good}/clusters/cluster-1/values.yaml (100%) rename tests/{dummy/k8t => resources/good}/clusters/cluster-2/config.yaml (100%) rename tests/{dummy/k8t => resources/good}/clusters/cluster-2/templates/composite-template.yaml.j2 (100%) rename tests/{dummy/k8t => resources/good}/clusters/cluster-2/values.yaml (100%) rename tests/{dummy/k8t => resources/good}/config.yaml (100%) rename tests/{dummy/k8t => resources/good}/environments/common-env/config.yaml (100%) rename tests/{dummy/k8t => resources/good}/environments/common-env/templates/common-env-template.yaml.j2 (100%) rename tests/{dummy/k8t => resources/good}/environments/common-env/values.yaml (100%) rename tests/{dummy/k8t => resources/good}/environments/some-env/config.yaml (100%) rename tests/{dummy/k8t => resources/good}/environments/some-env/values.yaml (100%) rename tests/{dummy/k8t => resources/good}/templates/common-template.yaml.j2 (100%) rename tests/{dummy/k8t => resources/good}/values.yaml (100%) rename tests/{dummy => resources}/missing_values/.k8t (100%) rename tests/{dummy => resources}/missing_values/config.yaml (100%) rename tests/{dummy => resources}/missing_values/templates/template.yaml.j2 (100%) rename tests/{dummy => resources}/missing_values/value-file.yaml (100%) rename tests/{dummy => resources}/missing_values/values.yaml (100%) rename tests/{dummy => resources}/results/cluster-1-cluster-specific-env.yaml (100%) rename tests/{dummy => resources}/results/cluster-1-common-env.yaml (100%) rename tests/{dummy => resources}/results/cluster-1.yaml (100%) rename tests/{dummy => resources}/results/cluster-2.yaml (100%) rename tests/{dummy => resources}/results/common-env.yaml (100%) rename tests/{dummy => resources}/results/default.yaml (100%) rename tests/{dummy => resources}/results/some-env.yaml (100%) diff --git a/tests/cli.py b/tests/cli.py index 3d8e066..cf36868 100644 --- a/tests/cli.py +++ b/tests/cli.py @@ -8,12 +8,15 @@ # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. import os -import boto3 -from moto import mock_ssm + from click.testing import CliRunner -from k8t import __version__, __license__ + +import boto3 +from k8t import __license__, __version__ from k8t.cli import root from k8t.scaffolding import list_available_templates +from moto import mock_ssm + def test_print_version(): runner = CliRunner() @@ -137,7 +140,7 @@ def test_new_template(): def test_get_clusters(): runner = CliRunner() - result = runner.invoke(root, ['get', 'clusters', 'tests/dummy/k8t']) + result = runner.invoke(root, ['get', 'clusters', 'tests/resources/good']) assert result.exit_code == 0 # TODO: Fix that, should return clusters, not environments assert 'some-env' in result.output @@ -147,26 +150,26 @@ def test_get_clusters(): def test_get_environments(): runner = CliRunner() - result = runner.invoke(root, ['get', 'environments', 'tests/dummy/k8t']) + result = runner.invoke(root, ['get', 'environments', 'tests/resources/good']) assert result.exit_code == 0 assert 'some-env' in result.output assert 'common-env' in result.output assert 'cluster-specific-env' in result.output - result = runner.invoke(root, ['get', 'environments', '-c', 'cluster-1', 'tests/dummy/k8t']) + result = runner.invoke(root, ['get', 'environments', '-c', 'cluster-1', 'tests/resources/good']) assert result.exit_code == 0 assert 'some-env' not in result.output assert 'common-env' in result.output assert 'cluster-specific-env' in result.output - result = runner.invoke(root, ['get', 'environments', '-c', 'cluster-2', 'tests/dummy/k8t']) + result = runner.invoke(root, ['get', 'environments', '-c', 'cluster-2', 'tests/resources/good']) assert result.exit_code == 0 assert not result.output def test_get_templates(): runner = CliRunner() - result = runner.invoke(root, ['get', 'templates', 'tests/dummy/k8t']) + result = runner.invoke(root, ['get', 'templates', 'tests/resources/good']) assert result.exit_code == 0 assert 'common-template.yaml.j2' in result.output assert 'common-env-template.yaml.j2' not in result.output @@ -174,7 +177,7 @@ def test_get_templates(): assert 'cluster-1-env-template.yaml.j2' not in result.output assert 'composite-template.yaml.j2' not in result.output - result = runner.invoke(root, ['get', 'templates', '-e', 'common-env', 'tests/dummy/k8t']) + result = runner.invoke(root, ['get', 'templates', '-e', 'common-env', 'tests/resources/good']) assert result.exit_code == 0 assert 'common-template.yaml.j2' in result.output assert 'common-env-template.yaml.j2' in result.output @@ -182,7 +185,7 @@ def test_get_templates(): assert 'cluster-1-env-template.yaml.j2' not in result.output assert 'composite-template.yaml.j2' not in result.output - result = runner.invoke(root, ['get', 'templates', '-e', 'some-env', 'tests/dummy/k8t']) + result = runner.invoke(root, ['get', 'templates', '-e', 'some-env', 'tests/resources/good']) assert result.exit_code == 0 assert 'common-template.yaml.j2' in result.output assert 'common-env-template.yaml.j2' not in result.output @@ -190,7 +193,7 @@ def test_get_templates(): assert 'cluster-1-env-template.yaml.j2' not in result.output assert 'composite-template.yaml.j2' not in result.output - result = runner.invoke(root, ['get', 'templates', '-c', 'cluster-1', 'tests/dummy/k8t']) + result = runner.invoke(root, ['get', 'templates', '-c', 'cluster-1', 'tests/resources/good']) assert result.exit_code == 0 assert 'common-template.yaml.j2' in result.output assert 'common-env-template.yaml.j2' not in result.output @@ -198,7 +201,7 @@ def test_get_templates(): assert 'cluster-1-env-template.yaml.j2' not in result.output assert 'composite-template.yaml.j2' not in result.output - result = runner.invoke(root, ['get', 'templates', '-c', 'cluster-1', '-e', 'cluster-specific-env', 'tests/dummy/k8t']) + result = runner.invoke(root, ['get', 'templates', '-c', 'cluster-1', '-e', 'cluster-specific-env', 'tests/resources/good']) assert result.exit_code == 0 assert 'common-template.yaml.j2' in result.output assert 'common-env-template.yaml.j2' not in result.output @@ -206,7 +209,7 @@ def test_get_templates(): assert 'cluster-1-env-template.yaml.j2' in result.output assert 'composite-template.yaml.j2' not in result.output - result = runner.invoke(root, ['get', 'templates', '-c', 'cluster-1', '-e', 'common-env', 'tests/dummy/k8t']) + result = runner.invoke(root, ['get', 'templates', '-c', 'cluster-1', '-e', 'common-env', 'tests/resources/good']) assert result.exit_code == 0 assert 'common-template.yaml.j2' in result.output assert 'common-env-template.yaml.j2' in result.output @@ -214,7 +217,7 @@ def test_get_templates(): assert 'cluster-1-env-template.yaml.j2' not in result.output assert 'composite-template.yaml.j2' not in result.output - result = runner.invoke(root, ['get', 'templates', '-c', 'cluster-2', 'tests/dummy/k8t']) + result = runner.invoke(root, ['get', 'templates', '-c', 'cluster-2', 'tests/resources/good']) assert result.exit_code == 0 assert 'common-template.yaml.j2' in result.output assert 'common-env-template.yaml.j2' not in result.output @@ -225,7 +228,7 @@ def test_get_templates(): def test_validate_successful(): runner = CliRunner() - result = runner.invoke(root, ['validate', 'tests/dummy/k8t']) + result = runner.invoke(root, ['validate', 'tests/resources/good']) assert result.exit_code == 0 assert 'common-template.yaml.j2: ✔' in result.output assert 'common-env-template.yaml.j2: ✔' not in result.output @@ -233,7 +236,7 @@ def test_validate_successful(): assert 'cluster-1-env-template.yaml.j2: ✔' not in result.output assert 'composite-template.yaml.j2: ✔' not in result.output - result = runner.invoke(root, ['validate', '-e', 'common-env', 'tests/dummy/k8t']) + result = runner.invoke(root, ['validate', '-e', 'common-env', 'tests/resources/good']) assert result.exit_code == 0 assert 'common-template.yaml.j2: ✔' in result.output assert 'common-env-template.yaml.j2: ✔' in result.output @@ -241,7 +244,7 @@ def test_validate_successful(): assert 'cluster-1-env-template.yaml.j2: ✔' not in result.output assert 'composite-template.yaml.j2: ✔' not in result.output - result = runner.invoke(root, ['validate', '-e', 'some-env', 'tests/dummy/k8t']) + result = runner.invoke(root, ['validate', '-e', 'some-env', 'tests/resources/good']) assert result.exit_code == 0 assert 'common-template.yaml.j2: ✔' in result.output assert 'common-env-template.yaml.j2: ✔' not in result.output @@ -249,7 +252,7 @@ def test_validate_successful(): assert 'cluster-1-env-template.yaml.j2: ✔' not in result.output assert 'composite-template.yaml.j2: ✔' not in result.output - result = runner.invoke(root, ['validate', '-c', 'cluster-1', 'tests/dummy/k8t']) + result = runner.invoke(root, ['validate', '-c', 'cluster-1', 'tests/resources/good']) assert result.exit_code == 0 assert 'common-template.yaml.j2: ✔' in result.output assert 'common-env-template.yaml.j2: ✔' not in result.output @@ -257,7 +260,7 @@ def test_validate_successful(): assert 'cluster-1-env-template.yaml.j2: ✔' not in result.output assert 'composite-template.yaml.j2: ✔' not in result.output - result = runner.invoke(root, ['validate', '-c', 'cluster-1', '-e', 'cluster-specific-env', 'tests/dummy/k8t']) + result = runner.invoke(root, ['validate', '-c', 'cluster-1', '-e', 'cluster-specific-env', 'tests/resources/good']) assert result.exit_code == 0 assert 'common-template.yaml.j2: ✔' in result.output assert 'common-env-template.yaml.j2: ✔' not in result.output @@ -265,7 +268,7 @@ def test_validate_successful(): assert 'cluster-1-env-template.yaml.j2: ✔' in result.output assert 'composite-template.yaml.j2: ✔' not in result.output - result = runner.invoke(root, ['validate', '-c', 'cluster-1', '-e', 'common-env', 'tests/dummy/k8t']) + result = runner.invoke(root, ['validate', '-c', 'cluster-1', '-e', 'common-env', 'tests/resources/good']) assert result.exit_code == 0 assert 'common-template.yaml.j2: ✔' in result.output assert 'common-env-template.yaml.j2: ✔' in result.output @@ -273,7 +276,7 @@ def test_validate_successful(): assert 'cluster-1-env-template.yaml.j2: ✔' not in result.output assert 'composite-template.yaml.j2: ✔' not in result.output - result = runner.invoke(root, ['validate', '-c', 'cluster-2', 'tests/dummy/k8t']) + result = runner.invoke(root, ['validate', '-c', 'cluster-2', 'tests/resources/good']) assert result.exit_code == 0 assert 'common-template.yaml.j2: ✔' in result.output assert 'common-env-template.yaml.j2: ✔' not in result.output @@ -284,18 +287,18 @@ def test_validate_successful(): def test_validate_with_values(): runner = CliRunner() - result = runner.invoke(root, ['validate', 'tests/dummy/missing_values']) + result = runner.invoke(root, ['validate', 'tests/resources/missing_values']) assert result.exit_code # TODO: Should be 1 assert 'template.yaml.j2: ✗' in result.output assert '- undefined variable: test' in result.output - result = runner.invoke(root, ['validate', '--value', 'test', 'something', 'tests/dummy/missing_values']) + result = runner.invoke(root, ['validate', '--value', 'test', 'something', 'tests/resources/missing_values']) assert result.exit_code == 0 assert 'template.yaml.j2: ✔' in result.output result = runner.invoke( root, - ['validate', '--value-file', 'tests/dummy/missing_values/value-file.yaml', 'tests/dummy/missing_values'] + ['validate', '--value-file', 'tests/resources/missing_values/value-file.yaml', 'tests/resources/missing_values'] ) assert result.exit_code == 0 assert 'template.yaml.j2: ✔' in result.output @@ -303,7 +306,7 @@ def test_validate_with_values(): def test_validate_failure(): runner = CliRunner() - result = runner.invoke(root, ['validate', 'tests/dummy/bad']) + result = runner.invoke(root, ['validate', 'tests/resources/bad']) # TODO: Fix and uncomment # assert 'filter-template.yaml.j2: ✗' in result.output # assert 'nested-value-adding-template.yaml.j2: ✗' in result.output @@ -328,38 +331,38 @@ def test_gen(): runner = CliRunner() - with open('tests/dummy/results/default.yaml', 'r') as file: - result = runner.invoke(root, ['gen', 'tests/dummy/k8t']) + with open('tests/resources/results/default.yaml', 'r') as file: + result = runner.invoke(root, ['gen', 'tests/resources/good']) assert result.exit_code == 0 assert result.output == file.read() - with open('tests/dummy/results/some-env.yaml', 'r') as file: - result = runner.invoke(root, ['gen', '-e', 'some-env', 'tests/dummy/k8t']) + with open('tests/resources/results/some-env.yaml', 'r') as file: + result = runner.invoke(root, ['gen', '-e', 'some-env', 'tests/resources/good']) assert result.exit_code == 0 assert result.output == file.read() - with open('tests/dummy/results/common-env.yaml', 'r') as file: - result = runner.invoke(root, ['gen', '-e', 'common-env', 'tests/dummy/k8t']) + with open('tests/resources/results/common-env.yaml', 'r') as file: + result = runner.invoke(root, ['gen', '-e', 'common-env', 'tests/resources/good']) assert result.exit_code == 0 assert result.output == file.read() - with open('tests/dummy/results/cluster-1.yaml', 'r') as file: - result = runner.invoke(root, ['gen', '-c', 'cluster-1', 'tests/dummy/k8t']) + with open('tests/resources/results/cluster-1.yaml', 'r') as file: + result = runner.invoke(root, ['gen', '-c', 'cluster-1', 'tests/resources/good']) assert result.exit_code == 0 assert result.output == file.read() - with open('tests/dummy/results/cluster-1-common-env.yaml', 'r') as file: - result = runner.invoke(root, ['gen', '-c', 'cluster-1', '-e', 'common-env', 'tests/dummy/k8t']) + with open('tests/resources/results/cluster-1-common-env.yaml', 'r') as file: + result = runner.invoke(root, ['gen', '-c', 'cluster-1', '-e', 'common-env', 'tests/resources/good']) assert result.exit_code == 0 assert result.output == file.read() - with open('tests/dummy/results/cluster-1-cluster-specific-env.yaml', 'r') as file: - result = runner.invoke(root, ['gen', '-c', 'cluster-1', '-e', 'cluster-specific-env', 'tests/dummy/k8t']) + with open('tests/resources/results/cluster-1-cluster-specific-env.yaml', 'r') as file: + result = runner.invoke(root, ['gen', '-c', 'cluster-1', '-e', 'cluster-specific-env', 'tests/resources/good']) assert result.exit_code == 0 assert result.output == file.read() - with open('tests/dummy/results/cluster-2.yaml', 'r') as file: - result = runner.invoke(root, ['gen', '-c', 'cluster-2', 'tests/dummy/k8t']) + with open('tests/resources/results/cluster-2.yaml', 'r') as file: + result = runner.invoke(root, ['gen', '-c', 'cluster-2', 'tests/resources/good']) assert result.exit_code == 0 assert result.output == file.read() diff --git a/tests/dummy/bad/.k8t b/tests/resources/bad/.k8t similarity index 100% rename from tests/dummy/bad/.k8t rename to tests/resources/bad/.k8t diff --git a/tests/dummy/bad/config.yaml b/tests/resources/bad/config.yaml similarity index 100% rename from tests/dummy/bad/config.yaml rename to tests/resources/bad/config.yaml diff --git a/tests/dummy/bad/templates/composite-invalid-yaml-template.yaml.j2 b/tests/resources/bad/templates/composite-invalid-yaml-template.yaml.j2 similarity index 100% rename from tests/dummy/bad/templates/composite-invalid-yaml-template.yaml.j2 rename to tests/resources/bad/templates/composite-invalid-yaml-template.yaml.j2 diff --git a/tests/dummy/bad/templates/composite-template.yaml.j2 b/tests/resources/bad/templates/composite-template.yaml.j2 similarity index 100% rename from tests/dummy/bad/templates/composite-template.yaml.j2 rename to tests/resources/bad/templates/composite-template.yaml.j2 diff --git a/tests/dummy/bad/templates/filter-template.yaml.j2 b/tests/resources/bad/templates/filter-template.yaml.j2 similarity index 100% rename from tests/dummy/bad/templates/filter-template.yaml.j2 rename to tests/resources/bad/templates/filter-template.yaml.j2 diff --git a/tests/dummy/bad/templates/invalid-yaml-template.yaml.j2 b/tests/resources/bad/templates/invalid-yaml-template.yaml.j2 similarity index 100% rename from tests/dummy/bad/templates/invalid-yaml-template.yaml.j2 rename to tests/resources/bad/templates/invalid-yaml-template.yaml.j2 diff --git a/tests/dummy/bad/templates/nested-value-adding-template.yaml.j2 b/tests/resources/bad/templates/nested-value-adding-template.yaml.j2 similarity index 100% rename from tests/dummy/bad/templates/nested-value-adding-template.yaml.j2 rename to tests/resources/bad/templates/nested-value-adding-template.yaml.j2 diff --git a/tests/dummy/bad/templates/nested-value-template.yaml.j2 b/tests/resources/bad/templates/nested-value-template.yaml.j2 similarity index 100% rename from tests/dummy/bad/templates/nested-value-template.yaml.j2 rename to tests/resources/bad/templates/nested-value-template.yaml.j2 diff --git a/tests/dummy/bad/templates/secret-template.yaml.j2 b/tests/resources/bad/templates/secret-template.yaml.j2 similarity index 100% rename from tests/dummy/bad/templates/secret-template.yaml.j2 rename to tests/resources/bad/templates/secret-template.yaml.j2 diff --git a/tests/dummy/bad/templates/several-template.yaml.j2 b/tests/resources/bad/templates/several-template.yaml.j2 similarity index 100% rename from tests/dummy/bad/templates/several-template.yaml.j2 rename to tests/resources/bad/templates/several-template.yaml.j2 diff --git a/tests/dummy/bad/templates/value-template.yaml.j2 b/tests/resources/bad/templates/value-template.yaml.j2 similarity index 100% rename from tests/dummy/bad/templates/value-template.yaml.j2 rename to tests/resources/bad/templates/value-template.yaml.j2 diff --git a/tests/dummy/bad/values.yaml b/tests/resources/bad/values.yaml similarity index 100% rename from tests/dummy/bad/values.yaml rename to tests/resources/bad/values.yaml diff --git a/tests/dummy/k8t/.k8t b/tests/resources/good/.k8t similarity index 100% rename from tests/dummy/k8t/.k8t rename to tests/resources/good/.k8t diff --git a/tests/dummy/k8t/clusters/cluster-1/config.yaml b/tests/resources/good/clusters/cluster-1/config.yaml similarity index 100% rename from tests/dummy/k8t/clusters/cluster-1/config.yaml rename to tests/resources/good/clusters/cluster-1/config.yaml diff --git a/tests/dummy/k8t/clusters/cluster-1/environments/cluster-specific-env/config.yaml b/tests/resources/good/clusters/cluster-1/environments/cluster-specific-env/config.yaml similarity index 100% rename from tests/dummy/k8t/clusters/cluster-1/environments/cluster-specific-env/config.yaml rename to tests/resources/good/clusters/cluster-1/environments/cluster-specific-env/config.yaml diff --git a/tests/dummy/k8t/clusters/cluster-1/environments/cluster-specific-env/templates/cluster-1-env-template.yaml.j2 b/tests/resources/good/clusters/cluster-1/environments/cluster-specific-env/templates/cluster-1-env-template.yaml.j2 similarity index 100% rename from tests/dummy/k8t/clusters/cluster-1/environments/cluster-specific-env/templates/cluster-1-env-template.yaml.j2 rename to tests/resources/good/clusters/cluster-1/environments/cluster-specific-env/templates/cluster-1-env-template.yaml.j2 diff --git a/tests/dummy/k8t/clusters/cluster-1/environments/cluster-specific-env/values.yaml b/tests/resources/good/clusters/cluster-1/environments/cluster-specific-env/values.yaml similarity index 100% rename from tests/dummy/k8t/clusters/cluster-1/environments/cluster-specific-env/values.yaml rename to tests/resources/good/clusters/cluster-1/environments/cluster-specific-env/values.yaml diff --git a/tests/dummy/k8t/clusters/cluster-1/environments/common-env/config.yaml b/tests/resources/good/clusters/cluster-1/environments/common-env/config.yaml similarity index 100% rename from tests/dummy/k8t/clusters/cluster-1/environments/common-env/config.yaml rename to tests/resources/good/clusters/cluster-1/environments/common-env/config.yaml diff --git a/tests/dummy/k8t/clusters/cluster-1/environments/common-env/templates/cluster-1-template.yaml.j2 b/tests/resources/good/clusters/cluster-1/environments/common-env/templates/cluster-1-template.yaml.j2 similarity index 100% rename from tests/dummy/k8t/clusters/cluster-1/environments/common-env/templates/cluster-1-template.yaml.j2 rename to tests/resources/good/clusters/cluster-1/environments/common-env/templates/cluster-1-template.yaml.j2 diff --git a/tests/dummy/k8t/clusters/cluster-1/environments/common-env/values.yaml b/tests/resources/good/clusters/cluster-1/environments/common-env/values.yaml similarity index 100% rename from tests/dummy/k8t/clusters/cluster-1/environments/common-env/values.yaml rename to tests/resources/good/clusters/cluster-1/environments/common-env/values.yaml diff --git a/tests/dummy/k8t/clusters/cluster-1/templates/cluster-1-template.yaml.j2 b/tests/resources/good/clusters/cluster-1/templates/cluster-1-template.yaml.j2 similarity index 100% rename from tests/dummy/k8t/clusters/cluster-1/templates/cluster-1-template.yaml.j2 rename to tests/resources/good/clusters/cluster-1/templates/cluster-1-template.yaml.j2 diff --git a/tests/dummy/k8t/clusters/cluster-1/values.yaml b/tests/resources/good/clusters/cluster-1/values.yaml similarity index 100% rename from tests/dummy/k8t/clusters/cluster-1/values.yaml rename to tests/resources/good/clusters/cluster-1/values.yaml diff --git a/tests/dummy/k8t/clusters/cluster-2/config.yaml b/tests/resources/good/clusters/cluster-2/config.yaml similarity index 100% rename from tests/dummy/k8t/clusters/cluster-2/config.yaml rename to tests/resources/good/clusters/cluster-2/config.yaml diff --git a/tests/dummy/k8t/clusters/cluster-2/templates/composite-template.yaml.j2 b/tests/resources/good/clusters/cluster-2/templates/composite-template.yaml.j2 similarity index 100% rename from tests/dummy/k8t/clusters/cluster-2/templates/composite-template.yaml.j2 rename to tests/resources/good/clusters/cluster-2/templates/composite-template.yaml.j2 diff --git a/tests/dummy/k8t/clusters/cluster-2/values.yaml b/tests/resources/good/clusters/cluster-2/values.yaml similarity index 100% rename from tests/dummy/k8t/clusters/cluster-2/values.yaml rename to tests/resources/good/clusters/cluster-2/values.yaml diff --git a/tests/dummy/k8t/config.yaml b/tests/resources/good/config.yaml similarity index 100% rename from tests/dummy/k8t/config.yaml rename to tests/resources/good/config.yaml diff --git a/tests/dummy/k8t/environments/common-env/config.yaml b/tests/resources/good/environments/common-env/config.yaml similarity index 100% rename from tests/dummy/k8t/environments/common-env/config.yaml rename to tests/resources/good/environments/common-env/config.yaml diff --git a/tests/dummy/k8t/environments/common-env/templates/common-env-template.yaml.j2 b/tests/resources/good/environments/common-env/templates/common-env-template.yaml.j2 similarity index 100% rename from tests/dummy/k8t/environments/common-env/templates/common-env-template.yaml.j2 rename to tests/resources/good/environments/common-env/templates/common-env-template.yaml.j2 diff --git a/tests/dummy/k8t/environments/common-env/values.yaml b/tests/resources/good/environments/common-env/values.yaml similarity index 100% rename from tests/dummy/k8t/environments/common-env/values.yaml rename to tests/resources/good/environments/common-env/values.yaml diff --git a/tests/dummy/k8t/environments/some-env/config.yaml b/tests/resources/good/environments/some-env/config.yaml similarity index 100% rename from tests/dummy/k8t/environments/some-env/config.yaml rename to tests/resources/good/environments/some-env/config.yaml diff --git a/tests/dummy/k8t/environments/some-env/values.yaml b/tests/resources/good/environments/some-env/values.yaml similarity index 100% rename from tests/dummy/k8t/environments/some-env/values.yaml rename to tests/resources/good/environments/some-env/values.yaml diff --git a/tests/dummy/k8t/templates/common-template.yaml.j2 b/tests/resources/good/templates/common-template.yaml.j2 similarity index 100% rename from tests/dummy/k8t/templates/common-template.yaml.j2 rename to tests/resources/good/templates/common-template.yaml.j2 diff --git a/tests/dummy/k8t/values.yaml b/tests/resources/good/values.yaml similarity index 100% rename from tests/dummy/k8t/values.yaml rename to tests/resources/good/values.yaml diff --git a/tests/dummy/missing_values/.k8t b/tests/resources/missing_values/.k8t similarity index 100% rename from tests/dummy/missing_values/.k8t rename to tests/resources/missing_values/.k8t diff --git a/tests/dummy/missing_values/config.yaml b/tests/resources/missing_values/config.yaml similarity index 100% rename from tests/dummy/missing_values/config.yaml rename to tests/resources/missing_values/config.yaml diff --git a/tests/dummy/missing_values/templates/template.yaml.j2 b/tests/resources/missing_values/templates/template.yaml.j2 similarity index 100% rename from tests/dummy/missing_values/templates/template.yaml.j2 rename to tests/resources/missing_values/templates/template.yaml.j2 diff --git a/tests/dummy/missing_values/value-file.yaml b/tests/resources/missing_values/value-file.yaml similarity index 100% rename from tests/dummy/missing_values/value-file.yaml rename to tests/resources/missing_values/value-file.yaml diff --git a/tests/dummy/missing_values/values.yaml b/tests/resources/missing_values/values.yaml similarity index 100% rename from tests/dummy/missing_values/values.yaml rename to tests/resources/missing_values/values.yaml diff --git a/tests/dummy/results/cluster-1-cluster-specific-env.yaml b/tests/resources/results/cluster-1-cluster-specific-env.yaml similarity index 100% rename from tests/dummy/results/cluster-1-cluster-specific-env.yaml rename to tests/resources/results/cluster-1-cluster-specific-env.yaml diff --git a/tests/dummy/results/cluster-1-common-env.yaml b/tests/resources/results/cluster-1-common-env.yaml similarity index 100% rename from tests/dummy/results/cluster-1-common-env.yaml rename to tests/resources/results/cluster-1-common-env.yaml diff --git a/tests/dummy/results/cluster-1.yaml b/tests/resources/results/cluster-1.yaml similarity index 100% rename from tests/dummy/results/cluster-1.yaml rename to tests/resources/results/cluster-1.yaml diff --git a/tests/dummy/results/cluster-2.yaml b/tests/resources/results/cluster-2.yaml similarity index 100% rename from tests/dummy/results/cluster-2.yaml rename to tests/resources/results/cluster-2.yaml diff --git a/tests/dummy/results/common-env.yaml b/tests/resources/results/common-env.yaml similarity index 100% rename from tests/dummy/results/common-env.yaml rename to tests/resources/results/common-env.yaml diff --git a/tests/dummy/results/default.yaml b/tests/resources/results/default.yaml similarity index 100% rename from tests/dummy/results/default.yaml rename to tests/resources/results/default.yaml diff --git a/tests/dummy/results/some-env.yaml b/tests/resources/results/some-env.yaml similarity index 100% rename from tests/dummy/results/some-env.yaml rename to tests/resources/results/some-env.yaml From 3bd95092c6e58a35b55918534ffc762e7f41d7a0 Mon Sep 17 00:00:00 2001 From: Aljosha Friemann Date: Tue, 3 Nov 2020 11:52:19 +0100 Subject: [PATCH 8/9] get clusters fixed --- tests/cli.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/cli.py b/tests/cli.py index cf36868..cc269d3 100644 --- a/tests/cli.py +++ b/tests/cli.py @@ -143,9 +143,8 @@ def test_get_clusters(): result = runner.invoke(root, ['get', 'clusters', 'tests/resources/good']) assert result.exit_code == 0 # TODO: Fix that, should return clusters, not environments - assert 'some-env' in result.output - assert 'common-env' in result.output - assert 'cluster-specific-env' not in result.output + assert 'cluster-1' in result.output + assert 'cluster-2' in result.output def test_get_environments(): runner = CliRunner() From 2b44b681aa59dec6304d6f619428617d160cae73 Mon Sep 17 00:00:00 2001 From: Aljosha Friemann Date: Tue, 3 Nov 2020 12:02:33 +0100 Subject: [PATCH 9/9] add test for single-cluster example --- tests/examples.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 tests/examples.py diff --git a/tests/examples.py b/tests/examples.py new file mode 100644 index 0000000..aa35172 --- /dev/null +++ b/tests/examples.py @@ -0,0 +1,34 @@ +# ISC License +# +# Copyright 2019 FL Fintech E GmbH +# +# Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +import os + +from click.testing import CliRunner + +from k8t.cli import root + + +def test_single_cluster(): + runner = CliRunner() + + result = runner.invoke(root, ['get', 'clusters', 'examples/single-cluster']) + + assert result.exit_code == 0 + assert result.output == '' + + result = runner.invoke(root, ['get', 'environments', 'examples/single-cluster']) + + assert result.exit_code == 0 + assert 'production' in result.output + + result = runner.invoke(root, ['validate', 'examples/single-cluster']) + + assert result.exit_code == 0 + assert 'hello-world-deployment.yaml.j2: ✔' in result.output + assert 'hello-world-secret.yaml.j2: ✔' in result.output + assert 'hello-world-service.yaml.j2: ✔' in result.output