From 11c800d6edce386138aeaa6df718e0e778be0d4e Mon Sep 17 00:00:00 2001 From: Mike Graves Date: Wed, 11 May 2022 14:56:23 -0400 Subject: [PATCH] [backport/2.2] Move integration test suite from molecule to ansible-test (#392) (#457) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [backport/2.2] Move integration test suite from molecule to ansible-test (#392) Move integration test suite from molecule to ansible-test SUMMARY molecule has been replaced with ansible-test some test cases have been updated k8s_apply : remove duplicated tasks increasing the running time of the test helm: use different namespaces for different test cases in order to wait for the namespace deletion before moving to the next test. all: remove wait: yes at the end of each test when deleting namespace, the role used to create namespace will ensure that it is deleted before if existing. ISSUE TYPE Feature Pull Request COMPONENT NAME integration testing Reviewed-by: Mike Graves mgraves@redhat.com Reviewed-by: Gonéri Le Bouder goneri@lebouder.net Reviewed-by: None (cherry picked from commit fd61f8b) SUMMARY ISSUE TYPE Bugfix Pull Request Docs Pull Request Feature Pull Request New Module Pull Request COMPONENT NAME ADDITIONAL INFORMATION --- .gitignore | 6 +- Makefile | 5 +- changelogs/fragments/exception.yml | 3 + molecule/default/converge.yml | 292 -------------- molecule/default/molecule.yml | 35 -- molecule/default/prepare.yml | 12 - .../k8scopy/library/kubectl_file_compare.py | 217 ---------- plugins/modules/k8s_drain.py | 41 +- tests/integration/targets/helm/aliases | 8 + .../targets}/helm/defaults/main.yml | 16 +- .../files/appversionless-chart-v2/Chart.yaml | 0 .../templates/configmap.yaml | 0 .../files/appversionless-chart/Chart.yaml | 0 .../templates/configmap.yaml | 0 .../helm/files/sample_plugin/plugin.yaml | 0 .../helm/files/test-chart-v2/Chart.yaml | 0 .../test-chart-v2/templates/configmap.yaml | 0 .../targets}/helm/files/test-chart/Chart.yaml | 0 .../files/test-chart/templates/configmap.yaml | 0 .../targets}/helm/files/test-crds/Chart.yaml | 0 .../helm/files/test-crds/crds/crd.yaml | 4 +- .../targets}/helm/files/values.yaml | 0 .../targets/helm/library/helm_test_version.py | 95 +++++ .../integration/targets/helm}/meta/main.yml | 2 + .../targets}/helm/tasks/install.yml | 0 .../integration/targets}/helm/tasks/main.yml | 0 .../targets}/helm/tasks/run_test.yml | 0 .../targets}/helm/tasks/test_crds.yml | 24 +- .../helm/tasks/test_helm_not_installed.yml | 2 +- .../targets}/helm/tasks/test_read_envvars.yml | 2 +- .../targets}/helm/tasks/tests_chart.yml | 51 +-- .../tasks/tests_chart/from_local_path.yml | 6 + .../tasks/tests_chart/from_repository.yml | 1 + .../helm/tasks/tests_chart/from_url.yml | 1 + .../targets}/helm/tasks/tests_helm_diff.yml | 5 +- .../targets}/helm/tasks/tests_helm_plugin.yml | 0 .../targets}/helm/tasks/tests_repository.yml | 0 .../integration/targets/inventory_k8s/aliases | 3 + .../targets/inventory_k8s/playbooks/play.yml | 102 +++++ .../playbooks/test.inventory_k8s.yml | 2 + .../inventory_k8s/playbooks/vars/main.yml | 38 ++ .../targets/inventory_k8s/runme.sh | 8 + .../targets/k8s_access_review/aliases | 2 + .../targets/k8s_access_review/tasks/main.yml | 0 .../targets/k8s_append_hash/aliases | 2 + .../targets/k8s_append_hash/defaults/main.yml | 2 + .../targets/k8s_append_hash/meta/main.yml | 2 + .../targets/k8s_append_hash/tasks/main.yml | 8 +- tests/integration/targets/k8s_apply/aliases | 5 + .../targets/k8s_apply/defaults}/main.yml | 4 +- .../targets/k8s_apply/meta/main.yml | 2 + .../targets/k8s_apply/tasks/main.yml | 371 ++---------------- .../targets/k8s_cluster_info/aliases | 2 + .../targets/k8s_cluster_info/tasks/main.yml | 0 tests/integration/targets/k8s_copy/aliases | 4 + .../targets/k8s_copy}/defaults/main.yml | 2 +- .../targets/k8s_copy}/files/archive.tar | Bin .../files/data/ansible/collection.txt | 0 .../k8s_copy}/files/data/ansible/module.txt | 0 .../targets/k8s_copy}/files/data/file.txt | 0 .../k8s_copy}/files/data/teams/ansible.txt | 0 .../targets/k8s_copy}/files/simple_file.txt | 0 .../k8s_copy}/files/simple_zip_file.txt.gz | Bin .../k8s_copy}/library/k8s_create_file.py | 32 +- .../k8s_copy/library/kubectl_file_compare.py | 247 ++++++++++++ .../targets/k8s_copy}/meta/main.yml | 2 + .../targets/k8s_copy}/tasks/main.yml | 3 + .../k8s_copy}/tasks/test_copy_directory.yml | 0 .../k8s_copy}/tasks/test_copy_errors.yml | 0 .../k8s_copy}/tasks/test_copy_file.yml | 0 .../k8s_copy}/tasks/test_copy_large_file.yml | 0 .../tasks/test_multi_container_pod.yml | 0 .../k8s_copy}/templates/pods_definition.j2 | 0 tests/integration/targets/k8s_crd/aliases | 2 + .../targets/k8s_crd/defaults/main.yml | 2 + .../files/crd-resource.yml | 0 .../files/setup-crd.yml | 0 .../integration/targets/k8s_crd/meta/main.yml | 3 + .../targets/k8s_crd/tasks/main.yml | 30 +- tests/integration/targets/k8s_delete/aliases | 3 + .../targets/k8s_delete/defaults/main.yml | 25 ++ .../targets/k8s_delete/meta/main.yml | 3 + .../targets/k8s_delete/tasks/main.yml | 25 +- tests/integration/targets/k8s_diff/aliases | 2 + .../targets/k8s_diff/defaults/main.yml | 3 + .../targets/k8s_diff/meta/main.yml | 2 + .../targets/k8s_diff/tasks/main.yml | 21 +- .../targets/k8s_diff/templates/pod.j2 | 0 tests/integration/targets/k8s_drain/aliases | 4 + .../targets/k8s_drain/defaults/main.yml | 3 + .../targets/k8s_drain/meta/main.yml | 3 + .../targets/k8s_drain/tasks/main.yml | 21 +- tests/integration/targets/k8s_exec/aliases | 3 + .../targets/k8s_exec/defaults/main.yml | 2 + .../targets/k8s_exec/meta/main.yml | 3 + .../targets/k8s_exec/tasks/main.yml | 17 +- tests/integration/targets/k8s_full/aliases | 3 + .../targets/k8s_full/defaults/main.yml | 10 + .../targets/k8s_full/meta/main.yml | 3 + .../targets/k8s_full/tasks/main.yml | 0 tests/integration/targets/k8s_gc/aliases | 1 + .../targets/k8s_gc/defaults/main.yml | 3 + .../integration/targets/k8s_gc/meta/main.yml | 3 + .../integration/targets/k8s_gc/tasks/main.yml | 22 +- tests/integration/targets/k8s_info/aliases | 3 + .../targets/k8s_info/defaults/main.yml | 3 + .../targets/k8s_info/meta/main.yml | 3 + .../targets/k8s_info/tasks/main.yml | 22 +- .../targets/k8s_json_patch/aliases | 3 + .../targets/k8s_json_patch/defaults/main.yml | 2 + .../targets/k8s_json_patch/meta/main.yml | 3 + .../targets/k8s_json_patch/tasks/main.yml | 29 +- .../targets/k8s_label_selectors/aliases | 3 + .../k8s_label_selectors/defaults/main.yml | 3 + .../targets/k8s_label_selectors/meta/main.yml | 3 + .../k8s_label_selectors/tasks/main.yml | 13 +- tests/integration/targets/k8s_lists/aliases | 3 + .../targets/k8s_lists/defaults/main.yml | 2 + .../targets/k8s_lists/meta/main.yml | 3 + .../targets/k8s_lists/tasks/main.yml | 30 +- tests/integration/targets/k8s_log/aliases | 2 + .../targets/k8s_log/defaults/main.yml | 3 + .../integration/targets/k8s_log/meta/main.yml | 3 + .../targets/k8s_log/tasks/main.yml | 25 +- .../targets/k8s_merge_type/aliases | 3 + .../targets/k8s_merge_type/defaults/main.yml | 2 + .../targets/k8s_merge_type/meta/main.yml | 2 + .../targets/k8s_merge_type/tasks/main.yml | 8 +- tests/integration/targets/k8s_patched/aliases | 3 + .../targets/k8s_patched/defaults/main.yml | 4 + .../targets/k8s_patched/meta/main.yml | 2 + .../targets/k8s_patched/tasks/main.yml | 36 +- .../integration/targets/k8s_rollback/aliases | 4 + .../targets/k8s_rollback/defaults/main.yml | 3 + .../targets/k8s_rollback/meta/main.yml | 2 + .../targets/k8s_rollback/tasks/main.yml | 19 +- tests/integration/targets/k8s_scale/aliases | 4 + .../targets/k8s_scale/defaults/main.yml | 42 ++ .../targets/k8s_scale}/files/deployment.yaml | 0 .../targets/k8s_scale/meta/main.yml | 2 + .../targets/k8s_scale/tasks/main.yml | 22 +- .../integration/targets/k8s_template/aliases | 4 + .../targets/k8s_template/defaults/main.yml | 3 + .../targets/k8s_template/meta/main.yml | 2 + .../targets/k8s_template/tasks/main.yml | 47 +-- .../targets/k8s_template/templates/pod_one.j2 | 0 .../k8s_template/templates/pod_three.j2 | 0 .../targets/k8s_template/templates/pod_two.j2 | 0 .../templates/pod_with_bad_namespace.j2 | 0 .../integration/targets/k8s_validate/aliases | 3 + .../targets/k8s_validate/defaults/main.yml | 2 + .../targets/k8s_validate/meta/main.yml | 2 + .../targets/k8s_validate/tasks/main.yml | 10 +- tests/integration/targets/k8s_waiter/aliases | 5 + .../targets/k8s_waiter/defaults/main.yml | 40 ++ .../targets/k8s_waiter/meta/main.yml | 2 + .../targets/k8s_waiter/tasks/main.yml | 33 +- .../integration/targets/kubernetes/README.md | 19 - tests/integration/targets/kubernetes/aliases | 2 - .../kubernetes/files/kuard-extra-property.yml | 22 -- .../kubernetes/files/kuard-invalid-type.yml | 21 - .../targets/kubernetes/handlers/main.yml | 6 - .../targets/kubernetes/library/README.md | 3 - .../kubernetes/library/test_tempfile.py | 121 ------ .../targets/kubernetes/meta/main.yml | 2 - .../targets/kubernetes/tasks/main.yml | 76 ---- .../kubernetes/tasks/new_config_getter.yml | 16 - .../kubernetes/tasks/setup_remote_tmp_dir.yml | 12 - .../kubernetes/tasks/validate_installed.yml | 126 ------ .../tasks/validate_not_installed.yml | 25 -- tests/integration/targets/lookup_k8s/aliases | 3 + .../targets/lookup_k8s/defaults/main.yml | 4 + .../targets/lookup_k8s/meta/main.yml | 2 + .../targets/lookup_k8s/tasks/main.yml | 27 +- .../targets/lookup_kustomize/aliases | 3 + .../lookup_kustomize/defaults/main.yml | 2 + .../targets/lookup_kustomize/meta/main.yml | 2 + .../targets/lookup_kustomize/tasks/main.yml | 6 +- .../targets/remove_namespace/aliases | 1 + .../targets/remove_namespace/tasks/main.yml | 17 + .../targets/setup_namespace/aliases | 1 + .../targets/setup_namespace/defaults/main.yml | 1 + .../targets/setup_namespace/tasks/create.yml | 22 ++ .../targets/setup_namespace/tasks/main.yml | 13 + tests/sanity/ignore-2.10.txt | 22 +- tests/sanity/ignore-2.11.txt | 22 +- tests/sanity/ignore-2.12.txt | 14 +- tests/sanity/ignore-2.13.txt | 12 +- tests/sanity/ignore-2.9.txt | 22 +- tox.ini | 17 +- 190 files changed, 1261 insertions(+), 1768 deletions(-) create mode 100644 changelogs/fragments/exception.yml delete mode 100644 molecule/default/converge.yml delete mode 100644 molecule/default/molecule.yml delete mode 100644 molecule/default/prepare.yml delete mode 100644 molecule/default/roles/k8scopy/library/kubectl_file_compare.py create mode 100644 tests/integration/targets/helm/aliases rename {molecule/default/roles => tests/integration/targets}/helm/defaults/main.yml (70%) rename {molecule/default/roles => tests/integration/targets}/helm/files/appversionless-chart-v2/Chart.yaml (100%) rename {molecule/default/roles => tests/integration/targets}/helm/files/appversionless-chart-v2/templates/configmap.yaml (100%) rename {molecule/default/roles => tests/integration/targets}/helm/files/appversionless-chart/Chart.yaml (100%) rename {molecule/default/roles => tests/integration/targets}/helm/files/appversionless-chart/templates/configmap.yaml (100%) rename {molecule/default/roles => tests/integration/targets}/helm/files/sample_plugin/plugin.yaml (100%) rename {molecule/default/roles => tests/integration/targets}/helm/files/test-chart-v2/Chart.yaml (100%) rename {molecule/default/roles => tests/integration/targets}/helm/files/test-chart-v2/templates/configmap.yaml (100%) rename {molecule/default/roles => tests/integration/targets}/helm/files/test-chart/Chart.yaml (100%) rename {molecule/default/roles => tests/integration/targets}/helm/files/test-chart/templates/configmap.yaml (100%) rename {molecule/default/roles => tests/integration/targets}/helm/files/test-crds/Chart.yaml (100%) rename {molecule/default/roles => tests/integration/targets}/helm/files/test-crds/crds/crd.yaml (88%) rename {molecule/default/roles => tests/integration/targets}/helm/files/values.yaml (100%) create mode 100644 tests/integration/targets/helm/library/helm_test_version.py rename {molecule/default/roles/k8scopy => tests/integration/targets/helm}/meta/main.yml (51%) rename {molecule/default/roles => tests/integration/targets}/helm/tasks/install.yml (100%) rename {molecule/default/roles => tests/integration/targets}/helm/tasks/main.yml (100%) rename {molecule/default/roles => tests/integration/targets}/helm/tasks/run_test.yml (100%) rename {molecule/default/roles => tests/integration/targets}/helm/tasks/test_crds.yml (81%) rename {molecule/default/roles => tests/integration/targets}/helm/tasks/test_helm_not_installed.yml (89%) rename {molecule/default/roles => tests/integration/targets}/helm/tasks/test_read_envvars.yml (89%) rename {molecule/default/roles => tests/integration/targets}/helm/tasks/tests_chart.yml (90%) rename {molecule/default/roles => tests/integration/targets}/helm/tasks/tests_chart/from_local_path.yml (94%) rename {molecule/default/roles => tests/integration/targets}/helm/tasks/tests_chart/from_repository.yml (92%) rename {molecule/default/roles => tests/integration/targets}/helm/tasks/tests_chart/from_url.yml (90%) rename {molecule/default/roles => tests/integration/targets}/helm/tasks/tests_helm_diff.yml (98%) rename {molecule/default/roles => tests/integration/targets}/helm/tasks/tests_helm_plugin.yml (100%) rename {molecule/default/roles => tests/integration/targets}/helm/tasks/tests_repository.yml (100%) create mode 100644 tests/integration/targets/inventory_k8s/aliases create mode 100644 tests/integration/targets/inventory_k8s/playbooks/play.yml create mode 100644 tests/integration/targets/inventory_k8s/playbooks/test.inventory_k8s.yml create mode 100644 tests/integration/targets/inventory_k8s/playbooks/vars/main.yml create mode 100755 tests/integration/targets/inventory_k8s/runme.sh create mode 100644 tests/integration/targets/k8s_access_review/aliases rename molecule/default/tasks/access_review.yml => tests/integration/targets/k8s_access_review/tasks/main.yml (100%) create mode 100644 tests/integration/targets/k8s_append_hash/aliases create mode 100644 tests/integration/targets/k8s_append_hash/defaults/main.yml create mode 100644 tests/integration/targets/k8s_append_hash/meta/main.yml rename molecule/default/tasks/append_hash.yml => tests/integration/targets/k8s_append_hash/tasks/main.yml (91%) create mode 100644 tests/integration/targets/k8s_apply/aliases rename {molecule/default/vars => tests/integration/targets/k8s_apply/defaults}/main.yml (92%) create mode 100644 tests/integration/targets/k8s_apply/meta/main.yml rename molecule/default/tasks/apply.yml => tests/integration/targets/k8s_apply/tasks/main.yml (58%) create mode 100644 tests/integration/targets/k8s_cluster_info/aliases rename molecule/default/tasks/cluster_info.yml => tests/integration/targets/k8s_cluster_info/tasks/main.yml (100%) create mode 100644 tests/integration/targets/k8s_copy/aliases rename {molecule/default/roles/k8scopy => tests/integration/targets/k8s_copy}/defaults/main.yml (91%) rename {molecule/default/roles/k8scopy => tests/integration/targets/k8s_copy}/files/archive.tar (100%) rename {molecule/default/roles/k8scopy => tests/integration/targets/k8s_copy}/files/data/ansible/collection.txt (100%) rename {molecule/default/roles/k8scopy => tests/integration/targets/k8s_copy}/files/data/ansible/module.txt (100%) rename {molecule/default/roles/k8scopy => tests/integration/targets/k8s_copy}/files/data/file.txt (100%) rename {molecule/default/roles/k8scopy => tests/integration/targets/k8s_copy}/files/data/teams/ansible.txt (100%) rename {molecule/default/roles/k8scopy => tests/integration/targets/k8s_copy}/files/simple_file.txt (100%) rename {molecule/default/roles/k8scopy => tests/integration/targets/k8s_copy}/files/simple_zip_file.txt.gz (100%) rename {molecule/default/roles/k8scopy => tests/integration/targets/k8s_copy}/library/k8s_create_file.py (76%) create mode 100644 tests/integration/targets/k8s_copy/library/kubectl_file_compare.py rename {molecule/default/roles/helm => tests/integration/targets/k8s_copy}/meta/main.yml (52%) rename {molecule/default/roles/k8scopy => tests/integration/targets/k8s_copy}/tasks/main.yml (95%) rename {molecule/default/roles/k8scopy => tests/integration/targets/k8s_copy}/tasks/test_copy_directory.yml (100%) rename {molecule/default/roles/k8scopy => tests/integration/targets/k8s_copy}/tasks/test_copy_errors.yml (100%) rename {molecule/default/roles/k8scopy => tests/integration/targets/k8s_copy}/tasks/test_copy_file.yml (100%) rename {molecule/default/roles/k8scopy => tests/integration/targets/k8s_copy}/tasks/test_copy_large_file.yml (100%) rename {molecule/default/roles/k8scopy => tests/integration/targets/k8s_copy}/tasks/test_multi_container_pod.yml (100%) rename {molecule/default/roles/k8scopy => tests/integration/targets/k8s_copy}/templates/pods_definition.j2 (100%) create mode 100644 tests/integration/targets/k8s_crd/aliases create mode 100644 tests/integration/targets/k8s_crd/defaults/main.yml rename tests/integration/targets/{kubernetes => k8s_crd}/files/crd-resource.yml (100%) rename tests/integration/targets/{kubernetes => k8s_crd}/files/setup-crd.yml (100%) create mode 100644 tests/integration/targets/k8s_crd/meta/main.yml rename molecule/default/tasks/crd.yml => tests/integration/targets/k8s_crd/tasks/main.yml (58%) create mode 100644 tests/integration/targets/k8s_delete/aliases create mode 100644 tests/integration/targets/k8s_delete/defaults/main.yml create mode 100644 tests/integration/targets/k8s_delete/meta/main.yml rename molecule/default/tasks/delete.yml => tests/integration/targets/k8s_delete/tasks/main.yml (77%) create mode 100644 tests/integration/targets/k8s_diff/aliases create mode 100644 tests/integration/targets/k8s_diff/defaults/main.yml create mode 100644 tests/integration/targets/k8s_diff/meta/main.yml rename molecule/default/tasks/diff.yml => tests/integration/targets/k8s_diff/tasks/main.yml (90%) rename molecule/default/templates/pod_diff.j2 => tests/integration/targets/k8s_diff/templates/pod.j2 (100%) create mode 100644 tests/integration/targets/k8s_drain/aliases create mode 100644 tests/integration/targets/k8s_drain/defaults/main.yml create mode 100644 tests/integration/targets/k8s_drain/meta/main.yml rename molecule/default/tasks/drain.yml => tests/integration/targets/k8s_drain/tasks/main.yml (93%) create mode 100644 tests/integration/targets/k8s_exec/aliases create mode 100644 tests/integration/targets/k8s_exec/defaults/main.yml create mode 100644 tests/integration/targets/k8s_exec/meta/main.yml rename molecule/default/tasks/exec.yml => tests/integration/targets/k8s_exec/tasks/main.yml (76%) create mode 100644 tests/integration/targets/k8s_full/aliases create mode 100644 tests/integration/targets/k8s_full/defaults/main.yml create mode 100644 tests/integration/targets/k8s_full/meta/main.yml rename molecule/default/tasks/full.yml => tests/integration/targets/k8s_full/tasks/main.yml (100%) create mode 100644 tests/integration/targets/k8s_gc/aliases create mode 100644 tests/integration/targets/k8s_gc/defaults/main.yml create mode 100644 tests/integration/targets/k8s_gc/meta/main.yml rename molecule/default/tasks/gc.yml => tests/integration/targets/k8s_gc/tasks/main.yml (91%) create mode 100644 tests/integration/targets/k8s_info/aliases create mode 100644 tests/integration/targets/k8s_info/defaults/main.yml create mode 100644 tests/integration/targets/k8s_info/meta/main.yml rename molecule/default/tasks/info.yml => tests/integration/targets/k8s_info/tasks/main.yml (92%) create mode 100644 tests/integration/targets/k8s_json_patch/aliases create mode 100644 tests/integration/targets/k8s_json_patch/defaults/main.yml create mode 100644 tests/integration/targets/k8s_json_patch/meta/main.yml rename molecule/default/tasks/json_patch.yml => tests/integration/targets/k8s_json_patch/tasks/main.yml (87%) create mode 100644 tests/integration/targets/k8s_label_selectors/aliases create mode 100644 tests/integration/targets/k8s_label_selectors/defaults/main.yml create mode 100644 tests/integration/targets/k8s_label_selectors/meta/main.yml rename molecule/default/tasks/label_selectors.yml => tests/integration/targets/k8s_label_selectors/tasks/main.yml (98%) create mode 100644 tests/integration/targets/k8s_lists/aliases create mode 100644 tests/integration/targets/k8s_lists/defaults/main.yml create mode 100644 tests/integration/targets/k8s_lists/meta/main.yml rename molecule/default/tasks/lists.yml => tests/integration/targets/k8s_lists/tasks/main.yml (86%) create mode 100644 tests/integration/targets/k8s_log/aliases create mode 100644 tests/integration/targets/k8s_log/defaults/main.yml create mode 100644 tests/integration/targets/k8s_log/meta/main.yml rename molecule/default/tasks/log.yml => tests/integration/targets/k8s_log/tasks/main.yml (85%) create mode 100644 tests/integration/targets/k8s_merge_type/aliases create mode 100644 tests/integration/targets/k8s_merge_type/defaults/main.yml create mode 100644 tests/integration/targets/k8s_merge_type/meta/main.yml rename molecule/default/tasks/merge_type.yml => tests/integration/targets/k8s_merge_type/tasks/main.yml (95%) create mode 100644 tests/integration/targets/k8s_patched/aliases create mode 100644 tests/integration/targets/k8s_patched/defaults/main.yml create mode 100644 tests/integration/targets/k8s_patched/meta/main.yml rename molecule/default/tasks/patched.yml => tests/integration/targets/k8s_patched/tasks/main.yml (71%) create mode 100644 tests/integration/targets/k8s_rollback/aliases create mode 100644 tests/integration/targets/k8s_rollback/defaults/main.yml create mode 100644 tests/integration/targets/k8s_rollback/meta/main.yml rename molecule/default/tasks/rollback.yml => tests/integration/targets/k8s_rollback/tasks/main.yml (94%) create mode 100644 tests/integration/targets/k8s_scale/aliases create mode 100644 tests/integration/targets/k8s_scale/defaults/main.yml rename {molecule/default => tests/integration/targets/k8s_scale}/files/deployment.yaml (100%) create mode 100644 tests/integration/targets/k8s_scale/meta/main.yml rename molecule/default/tasks/scale.yml => tests/integration/targets/k8s_scale/tasks/main.yml (94%) create mode 100644 tests/integration/targets/k8s_template/aliases create mode 100644 tests/integration/targets/k8s_template/defaults/main.yml create mode 100644 tests/integration/targets/k8s_template/meta/main.yml rename molecule/default/tasks/template.yml => tests/integration/targets/k8s_template/tasks/main.yml (86%) rename molecule/default/templates/pod_template_one.j2 => tests/integration/targets/k8s_template/templates/pod_one.j2 (100%) rename molecule/default/templates/pod_template_three.j2 => tests/integration/targets/k8s_template/templates/pod_three.j2 (100%) rename molecule/default/templates/pod_template_two.j2 => tests/integration/targets/k8s_template/templates/pod_two.j2 (100%) rename {molecule/default => tests/integration/targets/k8s_template}/templates/pod_with_bad_namespace.j2 (100%) create mode 100644 tests/integration/targets/k8s_validate/aliases create mode 100644 tests/integration/targets/k8s_validate/defaults/main.yml create mode 100644 tests/integration/targets/k8s_validate/meta/main.yml rename molecule/default/tasks/validate.yml => tests/integration/targets/k8s_validate/tasks/main.yml (96%) create mode 100644 tests/integration/targets/k8s_waiter/aliases create mode 100644 tests/integration/targets/k8s_waiter/defaults/main.yml create mode 100644 tests/integration/targets/k8s_waiter/meta/main.yml rename molecule/default/tasks/waiter.yml => tests/integration/targets/k8s_waiter/tasks/main.yml (94%) delete mode 100644 tests/integration/targets/kubernetes/README.md delete mode 100644 tests/integration/targets/kubernetes/aliases delete mode 100644 tests/integration/targets/kubernetes/files/kuard-extra-property.yml delete mode 100644 tests/integration/targets/kubernetes/files/kuard-invalid-type.yml delete mode 100644 tests/integration/targets/kubernetes/handlers/main.yml delete mode 100644 tests/integration/targets/kubernetes/library/README.md delete mode 100644 tests/integration/targets/kubernetes/library/test_tempfile.py delete mode 100644 tests/integration/targets/kubernetes/meta/main.yml delete mode 100644 tests/integration/targets/kubernetes/tasks/main.yml delete mode 100644 tests/integration/targets/kubernetes/tasks/new_config_getter.yml delete mode 100644 tests/integration/targets/kubernetes/tasks/setup_remote_tmp_dir.yml delete mode 100644 tests/integration/targets/kubernetes/tasks/validate_installed.yml delete mode 100644 tests/integration/targets/kubernetes/tasks/validate_not_installed.yml create mode 100644 tests/integration/targets/lookup_k8s/aliases create mode 100644 tests/integration/targets/lookup_k8s/defaults/main.yml create mode 100644 tests/integration/targets/lookup_k8s/meta/main.yml rename molecule/default/tasks/lookup_k8s.yml => tests/integration/targets/lookup_k8s/tasks/main.yml (88%) create mode 100644 tests/integration/targets/lookup_kustomize/aliases create mode 100644 tests/integration/targets/lookup_kustomize/defaults/main.yml create mode 100644 tests/integration/targets/lookup_kustomize/meta/main.yml rename molecule/default/tasks/lookup_kustomize.yml => tests/integration/targets/lookup_kustomize/tasks/main.yml (95%) create mode 100644 tests/integration/targets/remove_namespace/aliases create mode 100644 tests/integration/targets/remove_namespace/tasks/main.yml create mode 100644 tests/integration/targets/setup_namespace/aliases create mode 100644 tests/integration/targets/setup_namespace/defaults/main.yml create mode 100644 tests/integration/targets/setup_namespace/tasks/create.yml create mode 100644 tests/integration/targets/setup_namespace/tasks/main.yml diff --git a/.gitignore b/.gitignore index 079e9742..43300e50 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,8 @@ tests/integration/cloud-config-* .cache # Helm charts -molecule/default/*-chart-*.tgz +tests/integration/*-chart-*.tgz + +# ansible-test generated file +tests/integration/inventory +tests/integration/*-*.yml diff --git a/Makefile b/Makefile index 72d9fb29..dd71f9b3 100644 --- a/Makefile +++ b/Makefile @@ -22,10 +22,7 @@ test-sanity: ansible-test sanity --docker -v --color --python $(PYTHON_VERSION) $(?TEST_ARGS) test-integration: - ansible-test integration --docker -v --color --retry-on-error --python $(PYTHON_VERSION) --continue-on-error --diff --coverage $(?TEST_ARGS) - -test-molecule: - molecule test + ansible-test integration --diff --no-temp-workdir --color --skip-tags False --retry-on-error --continue-on-error --python $(PYTHON_VERSION) -v --coverage $(?TEST_ARGS) test-unit: ansible-test units --docker -v --color --python $(PYTHON_VERSION) $(?TEST_ARGS) diff --git a/changelogs/fragments/exception.yml b/changelogs/fragments/exception.yml new file mode 100644 index 00000000..1f6f1645 --- /dev/null +++ b/changelogs/fragments/exception.yml @@ -0,0 +1,3 @@ +--- +bugfixes: +- import exception from ``kubernetes.client.rest``. diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml deleted file mode 100644 index 694357c9..00000000 --- a/molecule/default/converge.yml +++ /dev/null @@ -1,292 +0,0 @@ ---- -- name: Converge - hosts: localhost - connection: local - - collections: - - kubernetes.core - - vars_files: - - vars/main.yml - - tasks: - - name: Verify cluster is working. - k8s_info: - namespace: kube-system - kind: Pod - register: pod_list - - - name: Verify cluster has more than 5 pods running. - assert: - that: (pod_list.resources | count) > 5 - - - name: Include access_review.yml - include_tasks: - file: tasks/access_review.yml - apply: - tags: [ access_review, k8s ] - tags: - - always - - name: Include append_hash.yml - include_tasks: - file: tasks/append_hash.yml - apply: - tags: [ append_hash, k8s ] - tags: - - always - - name: Include apply.yml - include_tasks: - file: tasks/apply.yml - apply: - tags: [ apply, k8s ] - tags: - - always - - name: Include cluster_info.yml - include_tasks: - file: tasks/cluster_info.yml - apply: - tags: [ cluster_info, k8s ] - tags: - - always - - name: Include crd.yml - include_tasks: - file: tasks/crd.yml - apply: - tags: [ crd, k8s ] - tags: - - always - - name: Include delete.yml - include_tasks: - file: tasks/delete.yml - apply: - tags: [ delete, k8s ] - tags: - - always - - name: Include exec.yml - include_tasks: - file: tasks/exec.yml - apply: - tags: [ exec, k8s ] - tags: - - always - - name: Include full.yml - include_tasks: - file: tasks/full.yml - apply: - tags: [ full, k8s ] - tags: - - always - - name: Include gc.yml - include_tasks: - file: tasks/gc.yml - apply: - tags: [ gc, k8s ] - tags: - - always - - name: Include info.yml - include_tasks: - file: tasks/info.yml - apply: - tags: [ info, k8s ] - tags: - - always - - name: Include json_patch.yml - include_tasks: - file: tasks/json_patch.yml - apply: - tags: [ json_patch, k8s ] - tags: - - always - - name: Include lists.yml - include_tasks: - file: tasks/lists.yml - apply: - tags: [ lists, k8s ] - tags: - - always - - name: Include log.yml - include_tasks: - file: tasks/log.yml - apply: - tags: [ log, k8s ] - tags: - - always - - name: Include rollback.yml - include_tasks: - file: tasks/rollback.yml - apply: - tags: [ rollback, k8s ] - tags: - - always - - name: Include scale.yml - include_tasks: - file: tasks/scale.yml - apply: - tags: [ scale, k8s ] - tags: - - always - - name: Include template.yml - include_tasks: - file: tasks/template.yml - apply: - tags: [ template, k8s ] - tags: - - always - - - name: Include validate.yml - include_tasks: - file: tasks/validate.yml - apply: - tags: [ validate, k8s ] - tags: - - always - - - name: Include waiter.yml - include_tasks: - file: tasks/waiter.yml - apply: - tags: [ waiter, k8s ] - tags: - - always - - - name: Include merge_type.yml - include_tasks: - file: tasks/merge_type.yml - apply: - tags: [ merge_type, k8s ] - tags: - - always - - - name: Include patched.yml - include_tasks: - file: tasks/patched.yml - apply: - tags: [ patched, k8s ] - tags: - - always - - - name: Include lookup_k8s.yml - include_tasks: - file: tasks/lookup_k8s.yml - apply: - tags: [ lookup_k8s, k8s ] - tags: - - always - - - name: Include label_selectors.yml - include_tasks: - file: tasks/label_selectors.yml - apply: - tags: [ label_selectors, k8s ] - tags: - - always - - - name: Include diff.yml - include_tasks: - file: tasks/diff.yml - apply: - tags: [ diff, k8s ] - tags: - - always - - - name: Include lookup_kustomize.yml - include_tasks: - file: tasks/lookup_kustomize.yml - apply: - tags: [ lookup_kustomize, k8s ] - tags: - - always - - roles: - - role: helm - tags: - - helm - - - role: k8scopy - tags: - - copy - - k8s - - post_tasks: - - name: Ensure namespace exists - k8s: - api_version: v1 - kind: Namespace - name: inventory - - - name: Add a deployment - k8s: - definition: - apiVersion: apps/v1 - kind: Deployment - metadata: - name: inventory - namespace: inventory - spec: - replicas: 1 - selector: - matchLabels: - app: "{{ k8s_pod_name }}" - template: "{{ k8s_pod_template }}" - wait: yes - wait_timeout: 120 - vars: - k8s_pod_name: inventory - k8s_pod_image: python - k8s_pod_command: - - python - - '-m' - - http.server - k8s_pod_env: - - name: TEST - value: test - - - meta: refresh_inventory - -- name: Verify inventory and connection plugins - hosts: namespace_inventory_pods - gather_facts: no - - vars: - file_content: | - Hello world - - tasks: - - name: End play if host not running (TODO should we not add these to the inventory?) - meta: end_host - when: pod_phase != "Running" - - - debug: var=hostvars - - setup: - - - debug: var=ansible_facts - - - name: Assert the TEST environment variable was retrieved - assert: - that: ansible_facts.env.TEST == 'test' - - - name: Copy a file into the host - copy: - content: '{{ file_content }}' - dest: /tmp/test_file - - - name: Retrieve the file from the host - slurp: - src: /tmp/test_file - register: slurped_file - - - name: Assert the file content matches expectations - assert: - that: (slurped_file.content|b64decode) == file_content - -- name: Delete inventory namespace - hosts: localhost - connection: local - gather_facts: no - tasks: - - name: Remove inventory namespace - k8s: - api_version: v1 - kind: Namespace - name: inventory - state: absent diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml deleted file mode 100644 index c037ff9c..00000000 --- a/molecule/default/molecule.yml +++ /dev/null @@ -1,35 +0,0 @@ ---- -driver: - name: delegated - options: - managed: false - login_cmd_template: 'docker exec -ti {instance} bash' - ansible_connection_options: - ansible_connection: docker -platforms: - - name: instance-kind -provisioner: - name: ansible - log: true - config_options: - inventory: - enable_plugins: kubernetes.core.k8s,yaml - lint: {} - inventory: - hosts: - plugin: kubernetes.core.k8s - host_vars: - localhost: - ansible_python_interpreter: '{{ ansible_playbook_python }}' - env: - ANSIBLE_FORCE_COLOR: 'true' - options: - vvv: True -scenario: - name: default - test_sequence: - - dependency - - syntax - - prepare - - converge - - verify diff --git a/molecule/default/prepare.yml b/molecule/default/prepare.yml deleted file mode 100644 index 5853460e..00000000 --- a/molecule/default/prepare.yml +++ /dev/null @@ -1,12 +0,0 @@ ---- -- name: Prepare - hosts: localhost - connection: local - - collections: - - kubernetes.core - - tasks: - - name: Include drain.yml - include_tasks: - file: tasks/drain.yml diff --git a/molecule/default/roles/k8scopy/library/kubectl_file_compare.py b/molecule/default/roles/k8scopy/library/kubectl_file_compare.py deleted file mode 100644 index 2487febd..00000000 --- a/molecule/default/roles/k8scopy/library/kubectl_file_compare.py +++ /dev/null @@ -1,217 +0,0 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- -# Copyright: (c) 2021, Aubin Bikouo <@abikouo> -# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) - -from __future__ import absolute_import, division, print_function - -__metaclass__ = type - - -DOCUMENTATION = r''' - -module: kubectl_file_compare - -short_description: Compare file and directory using kubectl - -author: - - Aubin Bikouo (@abikouo) - -description: - - This module is used to validate k8s_cp module. - - Compare the local file/directory with the remote pod version - -notes: - - This module authenticates on kubernetes cluster using default kubeconfig only. - -options: - namespace: - description: - - The pod namespace name - type: str - required: yes - pod: - description: - - The pod name - type: str - required: yes - container: - description: - - The container to retrieve files from. - type: str - remote_path: - description: - - Path of the file or directory on Pod. - type: path - required: yes - local_path: - description: - - Path of the local file or directory. - type: path - content: - description: - - local content to compare with remote file from pod. - - mutually exclusive with option I(local_path). - type: path - required: yes - args: - description: - - The file is considered to be an executable. - - The tool will be run locally and on pod and compare result from output and stderr. - type: list - kubectl_path: - description: - - Path to the kubectl executable, if not specified it will be download. - type: path -''' - -EXAMPLES = r''' -- name: compare local /tmp/foo with /tmp/bar in a remote pod - kubectl_file_compare: - namespace: some-namespace - pod: some-pod - remote_path: /tmp/bar - local_path: /tmp/foo - kubectl_path: /tmp/test/kubectl - -- name: Compare executable running help command - kubectl_file_compare: - namespace: some-namespace - pod: some-pod - remote_path: /tmp/test/kubectl - local_path: kubectl - kubectl_path: /tmp/test/kubectl - args: - - "--help" -''' - - -RETURN = r''' -''' - -import os -import filecmp - -from tempfile import NamedTemporaryFile, TemporaryDirectory -from ansible.module_utils.basic import AnsibleModule - - -def kubectl_get_content(module, dest_dir): - kubectl_path = module.params.get('kubectl_path') - if kubectl_path is None: - kubectl_path = module.get_bin_path('kubectl', required=True) - - namespace = module.params.get('namespace') - pod = module.params.get('pod') - file = module.params.get('remote_path') - - cmd = [ - kubectl_path, - 'cp', - "{0}/{1}:{2}".format(namespace, pod, file) - ] - container = module.params.get('container') - if container: - cmd += ['-c', container] - local_file = os.path.join(dest_dir, os.path.basename(module.params.get('remote_path'))) - cmd.append(local_file) - rc, out, err = module.run_command(cmd) - return local_file, err, rc, out - - -def kubectl_run_from_pod(module): - kubectl_path = module.params.get('kubectl_path') - if kubectl_path is None: - kubectl_path = module.get_bin_path('kubectl', required=True) - - cmd = [ - kubectl_path, - 'exec', - module.params.get('pod'), - '-n', - module.params.get('namespace') - ] - container = module.params.get('container') - if container: - cmd += ['-c', container] - cmd += ['--', module.params.get('remote_path')] - cmd += module.params.get('args') - return module.run_command(cmd) - - -def compare_directories(dir1, dir2): - test = filecmp.dircmp(dir1, dir2) - if any([len(test.left_only) > 0, len(test.right_only) > 0, len(test.funny_files) > 0]): - return False - (t, mismatch, errors) = filecmp.cmpfiles(dir1, dir2, test.common_files, shallow=False) - if len(mismatch) > 0 or len(errors) > 0: - return False - for common_dir in test.common_dirs: - new_dir1 = os.path.join(dir1, common_dir) - new_dir2 = os.path.join(dir2, common_dir) - if not compare_directories(new_dir1, new_dir2): - return False - return True - - -def execute_module(module): - - args = module.params.get('args') - local_path = module.params.get('local_path') - namespace = module.params.get('namespace') - pod = module.params.get('pod') - file = module.params.get('remote_path') - content = module.params.get('content') - if args: - pod_rc, pod_out, pod_err = kubectl_run_from_pod(module) - rc, out, err = module.run_command([module.params.get('local_path')] + args) - if rc == pod_rc and out == pod_out: - module.exit_json(msg="{0} and {1}/{2}:{3} are same.".format( - local_path, namespace, pod, file - ), rc=rc, stderr=err, stdout=out) - result = dict(local=dict(rc=rc, out=out, err=err), remote=dict(rc=pod_rc, out=pod_out, err=pod_err)) - module.fail_json(msg=f"{local_path} and {namespace}/{pod}:{file} are same.", **result) - else: - with TemporaryDirectory() as tmpdirname: - file_from_pod, err, rc, out = kubectl_get_content(module=module, dest_dir=tmpdirname) - if not os.path.exists(file_from_pod): - module.fail_json(msg="failed to copy content from pod", error=err, output=out) - - if content is not None: - with NamedTemporaryFile(mode="w") as tmp_file: - tmp_file.write(content) - tmp_file.flush() - if filecmp.cmp(file_from_pod, tmp_file.name): - module.exit_json(msg=f"defined content and {namespace}/{pod}:{file} are same.") - module.fail_json(msg=f"defined content and {namespace}/{pod}:{file} are same.") - - if os.path.isfile(local_path): - if filecmp.cmp(file_from_pod, local_path): - module.exit_json(msg=f"{local_path} and {namespace}/{pod}:{file} are same.") - module.fail_json(msg=f"{local_path} and {namespace}/{pod}:{file} are same.") - - if os.path.isdir(local_path): - if compare_directories(file_from_pod, local_path): - module.exit_json(msg=f"{local_path} and {namespace}/{pod}:{file} are same.") - module.fail_json(msg=f"{local_path} and {namespace}/{pod}:{file} are same.") - - -def main(): - argument_spec = {} - argument_spec['namespace'] = {'type': 'str', 'required': True} - argument_spec['pod'] = {'type': 'str', 'required': True} - argument_spec['container'] = {} - argument_spec['remote_path'] = {'type': 'path', 'required': True} - argument_spec['local_path'] = {'type': 'path'} - argument_spec['content'] = {'type': 'str'} - argument_spec['kubectl_path'] = {'type': 'path'} - argument_spec['args'] = {'type': 'list'} - module = AnsibleModule(argument_spec=argument_spec, - mutually_exclusive=[('local_path', 'content')], - required_one_of=[['local_path', 'content']]) - - execute_module(module) - - -if __name__ == '__main__': - main() diff --git a/plugins/modules/k8s_drain.py b/plugins/modules/k8s_drain.py index abde8469..f24754fb 100644 --- a/plugins/modules/k8s_drain.py +++ b/plugins/modules/k8s_drain.py @@ -119,20 +119,40 @@ result: type: str ''' import copy -from datetime import datetime import time -from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import AnsibleModule -from ansible_collections.kubernetes.core.plugins.module_utils.args_common import AUTH_ARG_SPEC +import traceback + +from datetime import datetime +from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import ( + AnsibleModule, +) +from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( + AUTH_ARG_SPEC, +) from ansible.module_utils._text import to_native try: from kubernetes.client.api import core_v1_api - from kubernetes.client.models import V1beta1Eviction, V1DeleteOptions + from kubernetes.client.models import V1DeleteOptions from kubernetes.client.exceptions import ApiException except ImportError: # ImportError are managed by the common module already. pass +HAS_EVICTION_API = True +k8s_import_exception = None +K8S_IMP_ERR = None + +try: + from kubernetes.client.models import V1beta1Eviction as v1_eviction +except ImportError: + try: + from kubernetes.client.models import V1Eviction as v1_eviction + except ImportError as e: + k8s_import_exception = e + K8S_IMP_ERR = traceback.format_exc() + HAS_EVICTION_API = False + def filter_pods(pods, force, ignore_daemonset): k8s_kind_mirror = "kubernetes.io/config.mirror" @@ -271,8 +291,10 @@ class K8sDrainAnsible(object): body = V1DeleteOptions(**definition) self._api_instance.delete_namespaced_pod(name=name, namespace=namespace, body=body) else: - body = V1beta1Eviction(**definition) - self._api_instance.create_namespaced_pod_eviction(name=name, namespace=namespace, body=body) + body = v1_eviction(**definition) + self._api_instance.create_namespaced_pod_eviction( + name=name, namespace=namespace, body=body + ) self._changed = True except ApiException as exc: if exc.reason != "Not Found": @@ -404,6 +426,13 @@ def argspec(): def main(): module = AnsibleModule(argument_spec=argspec()) + if not HAS_EVICTION_API: + module.fail_json( + msg="The kubernetes Python library missing with V1Eviction API", + exception=K8S_IMP_ERR, + error=to_native(k8s_import_exception), + ) + k8s_drain = K8sDrainAnsible(module) k8s_drain.execute_module() diff --git a/tests/integration/targets/helm/aliases b/tests/integration/targets/helm/aliases new file mode 100644 index 00000000..7bc4476e --- /dev/null +++ b/tests/integration/targets/helm/aliases @@ -0,0 +1,8 @@ +# slow - 11min +slow +time=313 +helm_info +helm_plugin +helm_plugin_info +helm_repository +helm_template diff --git a/molecule/default/roles/helm/defaults/main.yml b/tests/integration/targets/helm/defaults/main.yml similarity index 70% rename from molecule/default/roles/helm/defaults/main.yml rename to tests/integration/targets/helm/defaults/main.yml index 26c0a1e9..d96176de 100644 --- a/molecule/default/roles/helm/defaults/main.yml +++ b/tests/integration/targets/helm/defaults/main.yml @@ -1,10 +1,6 @@ --- helm_archive_name: "helm-{{ helm_version }}-{{ ansible_system | lower }}-amd64.tar.gz" helm_binary: "/tmp/helm/{{ ansible_system | lower }}-amd64/helm" -helm_namespace: helm - -tiller_namespace: tiller -tiller_cluster_role: cluster-admin chart_test: "ingress-nginx" chart_test_local_path: "nginx-ingress" @@ -17,3 +13,15 @@ chart_test_git_repo: "http://github.com/helm/charts.git" chart_test_values: revisionHistoryLimit: 0 myValue: "changed" + +test_namespace: + - "helm-diff" + - "helm-envvars" + - "helm-uninstall" + - "helm-not-installed" + - "helm-crd" + - "helm-url" + - "helm-repository" + - "helm-local-path-001" + - "helm-local-path-002" + - "helm-local-path-003" diff --git a/molecule/default/roles/helm/files/appversionless-chart-v2/Chart.yaml b/tests/integration/targets/helm/files/appversionless-chart-v2/Chart.yaml similarity index 100% rename from molecule/default/roles/helm/files/appversionless-chart-v2/Chart.yaml rename to tests/integration/targets/helm/files/appversionless-chart-v2/Chart.yaml diff --git a/molecule/default/roles/helm/files/appversionless-chart-v2/templates/configmap.yaml b/tests/integration/targets/helm/files/appversionless-chart-v2/templates/configmap.yaml similarity index 100% rename from molecule/default/roles/helm/files/appversionless-chart-v2/templates/configmap.yaml rename to tests/integration/targets/helm/files/appversionless-chart-v2/templates/configmap.yaml diff --git a/molecule/default/roles/helm/files/appversionless-chart/Chart.yaml b/tests/integration/targets/helm/files/appversionless-chart/Chart.yaml similarity index 100% rename from molecule/default/roles/helm/files/appversionless-chart/Chart.yaml rename to tests/integration/targets/helm/files/appversionless-chart/Chart.yaml diff --git a/molecule/default/roles/helm/files/appversionless-chart/templates/configmap.yaml b/tests/integration/targets/helm/files/appversionless-chart/templates/configmap.yaml similarity index 100% rename from molecule/default/roles/helm/files/appversionless-chart/templates/configmap.yaml rename to tests/integration/targets/helm/files/appversionless-chart/templates/configmap.yaml diff --git a/molecule/default/roles/helm/files/sample_plugin/plugin.yaml b/tests/integration/targets/helm/files/sample_plugin/plugin.yaml similarity index 100% rename from molecule/default/roles/helm/files/sample_plugin/plugin.yaml rename to tests/integration/targets/helm/files/sample_plugin/plugin.yaml diff --git a/molecule/default/roles/helm/files/test-chart-v2/Chart.yaml b/tests/integration/targets/helm/files/test-chart-v2/Chart.yaml similarity index 100% rename from molecule/default/roles/helm/files/test-chart-v2/Chart.yaml rename to tests/integration/targets/helm/files/test-chart-v2/Chart.yaml diff --git a/molecule/default/roles/helm/files/test-chart-v2/templates/configmap.yaml b/tests/integration/targets/helm/files/test-chart-v2/templates/configmap.yaml similarity index 100% rename from molecule/default/roles/helm/files/test-chart-v2/templates/configmap.yaml rename to tests/integration/targets/helm/files/test-chart-v2/templates/configmap.yaml diff --git a/molecule/default/roles/helm/files/test-chart/Chart.yaml b/tests/integration/targets/helm/files/test-chart/Chart.yaml similarity index 100% rename from molecule/default/roles/helm/files/test-chart/Chart.yaml rename to tests/integration/targets/helm/files/test-chart/Chart.yaml diff --git a/molecule/default/roles/helm/files/test-chart/templates/configmap.yaml b/tests/integration/targets/helm/files/test-chart/templates/configmap.yaml similarity index 100% rename from molecule/default/roles/helm/files/test-chart/templates/configmap.yaml rename to tests/integration/targets/helm/files/test-chart/templates/configmap.yaml diff --git a/molecule/default/roles/helm/files/test-crds/Chart.yaml b/tests/integration/targets/helm/files/test-crds/Chart.yaml similarity index 100% rename from molecule/default/roles/helm/files/test-crds/Chart.yaml rename to tests/integration/targets/helm/files/test-crds/Chart.yaml diff --git a/molecule/default/roles/helm/files/test-crds/crds/crd.yaml b/tests/integration/targets/helm/files/test-crds/crds/crd.yaml similarity index 88% rename from molecule/default/roles/helm/files/test-crds/crds/crd.yaml rename to tests/integration/targets/helm/files/test-crds/crds/crd.yaml index b63ddc9d..ac285313 100644 --- a/molecule/default/roles/helm/files/test-crds/crds/crd.yaml +++ b/tests/integration/targets/helm/files/test-crds/crds/crd.yaml @@ -1,9 +1,9 @@ apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: - name: foos.example.com + name: foos.ansible.com spec: - group: example.com + group: ansible.com versions: - name: v1 served: true diff --git a/molecule/default/roles/helm/files/values.yaml b/tests/integration/targets/helm/files/values.yaml similarity index 100% rename from molecule/default/roles/helm/files/values.yaml rename to tests/integration/targets/helm/files/values.yaml diff --git a/tests/integration/targets/helm/library/helm_test_version.py b/tests/integration/targets/helm/library/helm_test_version.py new file mode 100644 index 00000000..6b765778 --- /dev/null +++ b/tests/integration/targets/helm/library/helm_test_version.py @@ -0,0 +1,95 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# Copyright: (c) 2021, Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + + +DOCUMENTATION = r""" +--- +module: helm_test_version +short_description: check helm executable version +author: + - Aubin Bikouo (@abikouo) +requirements: + - "helm (https://github.com/helm/helm/releases)" +description: + - validate version of helm binary is lower than the specified version. +options: + binary_path: + description: + - The path of a helm binary to use. + required: false + type: path + version: + description: + - version to test against helm binary. + type: str + default: 3.7.0 +""" + +EXAMPLES = r""" +- name: validate helm binary version is lower than 3.5.0 + helm_test_version: + binary_path: path/to/helm + version: "3.5.0" +""" + +RETURN = r""" +message: + type: str + description: Text message describing the test result. + returned: always + sample: 'version installed: 3.4.5 is lower than version 3.5.0' +result: + type: bool + description: Test result. + returned: always + sample: 1 +""" + +import re +from ansible_collections.kubernetes.core.plugins.module_utils.version import ( + LooseVersion, +) + +from ansible.module_utils.basic import AnsibleModule + + +def main(): + module = AnsibleModule( + argument_spec=dict( + binary_path=dict(type="path"), version=dict(type="str", default="3.7.0"), + ), + ) + + bin_path = module.params.get("binary_path") + version = module.params.get("version") + + if bin_path is not None: + helm_cmd_common = bin_path + else: + helm_cmd_common = "helm" + + helm_cmd_common = module.get_bin_path(helm_cmd_common, required=True) + rc, out, err = module.run_command([helm_cmd_common, "version"]) + if rc != 0: + module.fail_json(msg="helm version failed.", err=err, out=out, rc=rc) + + m = re.match(r'version.BuildInfo{Version:"v([0-9\.]*)",', out) + installed_version = m.group(1) + + message = "version installed: %s" % installed_version + if LooseVersion(installed_version) < LooseVersion(version): + message += " is lower than version %s" % version + module.exit_json(changed=False, result=True, message=message) + else: + message += " is greater than version %s" % version + module.exit_json(changed=False, result=False, message=message) + + +if __name__ == "__main__": + main() diff --git a/molecule/default/roles/k8scopy/meta/main.yml b/tests/integration/targets/helm/meta/main.yml similarity index 51% rename from molecule/default/roles/k8scopy/meta/main.yml rename to tests/integration/targets/helm/meta/main.yml index d05af689..79869fd3 100644 --- a/molecule/default/roles/k8scopy/meta/main.yml +++ b/tests/integration/targets/helm/meta/main.yml @@ -1,3 +1,5 @@ --- collections: - kubernetes.core +dependencies: + - remove_namespace diff --git a/molecule/default/roles/helm/tasks/install.yml b/tests/integration/targets/helm/tasks/install.yml similarity index 100% rename from molecule/default/roles/helm/tasks/install.yml rename to tests/integration/targets/helm/tasks/install.yml diff --git a/molecule/default/roles/helm/tasks/main.yml b/tests/integration/targets/helm/tasks/main.yml similarity index 100% rename from molecule/default/roles/helm/tasks/main.yml rename to tests/integration/targets/helm/tasks/main.yml diff --git a/molecule/default/roles/helm/tasks/run_test.yml b/tests/integration/targets/helm/tasks/run_test.yml similarity index 100% rename from molecule/default/roles/helm/tasks/run_test.yml rename to tests/integration/targets/helm/tasks/run_test.yml diff --git a/molecule/default/roles/helm/tasks/test_crds.yml b/tests/integration/targets/helm/tasks/test_crds.yml similarity index 81% rename from molecule/default/roles/helm/tasks/test_crds.yml rename to tests/integration/targets/helm/tasks/test_crds.yml index f8370f4a..45637ce7 100644 --- a/molecule/default/roles/helm/tasks/test_crds.yml +++ b/tests/integration/targets/helm/tasks/test_crds.yml @@ -6,7 +6,7 @@ - name: Create namespace k8s: kind: Namespace - name: "{{ helm_namespace }}" + name: "{{ test_namespace[4] }}" - name: Copy test chart copy: @@ -17,7 +17,7 @@ helm: binary_path: "{{ helm_binary }}" chart_ref: "/tmp/helm_test_crds/{{ test_chart }}" - namespace: "{{ helm_namespace }}" + namespace: "{{ test_namespace[4] }}" name: test-crds skip_crds: true register: install @@ -30,10 +30,10 @@ - name: Fail to create custom resource k8s: definition: - apiVersion: example.com/v1 + apiVersion: ansible.com/v1 kind: Foo metadata: - namespace: "{{ helm_namespace }}" + namespace: "{{ test_namespace[4] }}" name: test-foo foobar: footest ignore_errors: true @@ -42,13 +42,13 @@ - assert: that: - result is failed - - "result.msg.startswith('Failed to find exact match for example.com/v1.Foo')" + - "result.msg.startswith('Failed to find exact match for ansible.com/v1.Foo')" # Helm won't install CRDs into an existing release, so we need to delete this, first - name: Uninstall chart helm: binary_path: "{{ helm_binary }}" - namespace: "{{ helm_namespace }}" + namespace: "{{ test_namespace[4] }}" name: test-crds state: absent @@ -56,16 +56,16 @@ helm: binary_path: "{{ helm_binary }}" chart_ref: "/tmp/helm_test_crds/{{ test_chart }}" - namespace: "{{ helm_namespace }}" + namespace: "{{ test_namespace[4] }}" name: test-crds - name: Create custom resource k8s: definition: - apiVersion: example.com/v1 + apiVersion: ansible.com/v1 kind: Foo metadata: - namespace: "{{ helm_namespace }}" + namespace: "{{ test_namespace[4] }}" name: test-foo foobar: footest register: result @@ -85,16 +85,14 @@ - name: Remove namespace k8s: kind: Namespace - name: "{{ helm_namespace }}" + name: "{{ test_namespace[4] }}" state: absent - wait: true - wait_timeout: 180 ignore_errors: true # CRDs aren't deleted with a namespace, so we need to manually delete it - name: Remove CRD k8s: kind: CustomResourceDefinition - name: foos.example.com + name: foos.ansible.com state: absent ignore_errors: true diff --git a/molecule/default/roles/helm/tasks/test_helm_not_installed.yml b/tests/integration/targets/helm/tasks/test_helm_not_installed.yml similarity index 89% rename from molecule/default/roles/helm/tasks/test_helm_not_installed.yml rename to tests/integration/targets/helm/tasks/test_helm_not_installed.yml index 0832dcb0..e683fc09 100644 --- a/molecule/default/roles/helm/tasks/test_helm_not_installed.yml +++ b/tests/integration/targets/helm/tasks/test_helm_not_installed.yml @@ -4,7 +4,7 @@ binary_path: "{{ helm_binary}}_fake" name: test chart_ref: "{{ chart_test }}" - namespace: "{{ helm_namespace }}" + namespace: "{{ test_namespace[3] }}" ignore_errors: yes register: helm_missing_binary diff --git a/molecule/default/roles/helm/tasks/test_read_envvars.yml b/tests/integration/targets/helm/tasks/test_read_envvars.yml similarity index 89% rename from molecule/default/roles/helm/tasks/test_read_envvars.yml rename to tests/integration/targets/helm/tasks/test_read_envvars.yml index b167d59e..379b28ed 100644 --- a/molecule/default/roles/helm/tasks/test_read_envvars.yml +++ b/tests/integration/targets/helm/tasks/test_read_envvars.yml @@ -3,7 +3,7 @@ binary_path: "{{ helm_binary }}" state: absent name: does-not-exist - namespace: "{{ helm_namespace }}" + namespace: "{{ test_namespace[1] }}" environment: K8S_AUTH_HOST: somewhere register: _helm_result diff --git a/molecule/default/roles/helm/tasks/tests_chart.yml b/tests/integration/targets/helm/tasks/tests_chart.yml similarity index 90% rename from molecule/default/roles/helm/tasks/tests_chart.yml rename to tests/integration/targets/helm/tasks/tests_chart.yml index 2199991c..ad0b9791 100644 --- a/molecule/default/roles/helm/tasks/tests_chart.yml +++ b/tests/integration/targets/helm/tasks/tests_chart.yml @@ -1,5 +1,8 @@ --- - name: Chart tests + vars: + chart_release_name: "test-{{ chart_name | default(source) }}" + chart_release_replaced_name: "test-{{ chart_name | default(source) }}-001" block: - name: Create temp directory tempfile: @@ -13,7 +16,7 @@ - name: Check helm_info empty helm_info: binary_path: "{{ helm_binary }}" - name: test + name: "{{ chart_release_name }}" namespace: "{{ helm_namespace }}" register: empty_info @@ -25,7 +28,7 @@ - name: "Install fail {{ chart_test }} from {{ source }}" helm: binary_path: "{{ helm_binary }}" - name: test + name: "{{ chart_release_name }}" chart_ref: "{{ chart_source }}" chart_version: "{{ chart_source_version | default(omit) }}" namespace: "{{ helm_namespace }}" @@ -41,7 +44,7 @@ - name: "Install {{ chart_test }} from {{ source }} in check mode" helm: binary_path: "{{ helm_binary }}" - name: test + name: "{{ chart_release_name }}" chart_ref: "{{ chart_source }}" chart_version: "{{ chart_source_version | default(omit) }}" namespace: "{{ helm_namespace }}" @@ -59,7 +62,7 @@ - name: "Install {{ chart_test }} from {{ source }}" helm: binary_path: "{{ helm_binary }}" - name: test + name: "{{ chart_release_name }}" chart_ref: "{{ chart_source }}" chart_version: "{{ chart_source_version | default(omit) }}" namespace: "{{ helm_namespace }}" @@ -76,7 +79,7 @@ - name: Check helm_info content helm_info: binary_path: "{{ helm_binary }}" - name: test + name: "{{ chart_release_name }}" namespace: "{{ helm_namespace }}" register: content_info @@ -89,7 +92,7 @@ - name: Check idempotency helm: binary_path: "{{ helm_binary }}" - name: test + name: "{{ chart_release_name }}" chart_ref: "{{ chart_source }}" chart_version: "{{ chart_source_version | default(omit) }}" namespace: "{{ helm_namespace }}" @@ -105,7 +108,7 @@ - name: "Add vars to {{ chart_test }} from {{ source }}" helm: binary_path: "{{ helm_binary }}" - name: test + name: "{{ chart_release_name }}" chart_ref: "{{ chart_source }}" chart_version: "{{ chart_source_version | default(omit) }}" namespace: "{{ helm_namespace }}" @@ -123,7 +126,7 @@ - name: Check idempotency after adding vars helm: binary_path: "{{ helm_binary }}" - name: test + name: "{{ chart_release_name }}" chart_ref: "{{ chart_source }}" chart_version: "{{ chart_source_version | default(omit) }}" namespace: "{{ helm_namespace }}" @@ -141,7 +144,7 @@ - name: "Remove Vars to {{ chart_test }} from {{ source }}" helm: binary_path: "{{ helm_binary }}" - name: test + name: "{{ chart_release_name }}" chart_ref: "{{ chart_source }}" chart_version: "{{ chart_source_version | default(omit) }}" namespace: "{{ helm_namespace }}" @@ -158,7 +161,7 @@ - name: Check idempotency after removing vars helm: binary_path: "{{ helm_binary }}" - name: test + name: "{{ chart_release_name }}" chart_ref: "{{ chart_source }}" chart_version: "{{ chart_source_version | default(omit) }}" namespace: "{{ helm_namespace }}" @@ -175,7 +178,7 @@ - name: "Upgrade {{ chart_test }} from {{ source }}" helm: binary_path: "{{ helm_binary }}" - name: test + name: "{{ chart_release_name }}" chart_ref: "{{ chart_source_upgrade | default(chart_source) }}" chart_version: "{{ chart_source_version_upgrade | default(omit) }}" namespace: "{{ helm_namespace }}" @@ -191,7 +194,7 @@ - name: Check idempotency after upgrade helm: binary_path: "{{ helm_binary }}" - name: test + name: "{{ chart_release_name }}" chart_ref: "{{ chart_source_upgrade | default(chart_source) }}" chart_version: "{{ chart_source_version_upgrade | default(omit) }}" namespace: "{{ helm_namespace }}" @@ -208,7 +211,7 @@ helm: binary_path: "{{ helm_binary }}" state: absent - name: test + name: "{{ chart_release_name }}" namespace: "{{ helm_namespace }}" register: install @@ -221,7 +224,7 @@ helm: binary_path: "{{ helm_binary }}" state: absent - name: test + name: "{{ chart_release_name }}" namespace: "{{ helm_namespace }}" register: install @@ -234,7 +237,7 @@ - name: Install chart for replace option helm: binary_path: "{{ helm_binary }}" - name: test-0001 + name: "{{ chart_release_replaced_name }}" chart_ref: "{{ chart_source }}" chart_version: "{{ chart_source_version | default(omit) }}" namespace: "{{ helm_namespace }}" @@ -245,11 +248,11 @@ that: - install is changed - - name: Remove {{ chart_test }} with --purge + - name: "Remove {{ chart_release_replaced_name }} with --purge" helm: binary_path: "{{ helm_binary }}" state: absent - name: test-0001 + name: "{{ chart_release_replaced_name }}" purge: False namespace: "{{ helm_namespace }}" register: install @@ -259,10 +262,10 @@ that: - install is changed - - name: Install chart again with same name test-0001 + - name: "Install chart again with same name {{ chart_release_replaced_name }}" helm: binary_path: "{{ helm_binary }}" - name: test-0001 + name: "{{ chart_release_replaced_name }}" chart_ref: "{{ chart_source }}" chart_version: "{{ chart_source_version | default(omit) }}" namespace: "{{ helm_namespace }}" @@ -278,7 +281,7 @@ helm: binary_path: "{{ helm_binary }}" state: absent - name: test-0001 + name: "{{ chart_release_replaced_name }}" namespace: "{{ helm_namespace }}" register: install @@ -290,7 +293,7 @@ - name: "Install {{ chart_test }} from {{ source }} with values_files" helm: binary_path: "{{ helm_binary }}" - name: test + name: "{{ chart_release_name }}" chart_ref: "{{ chart_source }}" chart_version: "{{ chart_source_version | default(omit) }}" namespace: "{{ helm_namespace }}" @@ -309,7 +312,7 @@ - name: "Install {{ chart_test }} from {{ source }} with values_files (again)" helm: binary_path: "{{ helm_binary }}" - name: test + name: "{{ chart_release_name }}" chart_ref: "{{ chart_source }}" chart_version: "{{ chart_source_version | default(omit) }}" namespace: "{{ helm_namespace }}" @@ -351,7 +354,7 @@ - name: Release using non-existent context helm: binary_path: "{{ helm_binary }}" - name: test + name: "{{ chart_release_name }}" chart_ref: "{{ chart_source }}" chart_version: "{{ chart_source_version | default(omit) }}" namespace: "{{ helm_namespace }}" @@ -379,5 +382,3 @@ kind: Namespace name: "{{ helm_namespace }}" state: absent - wait: true - wait_timeout: 180 diff --git a/molecule/default/roles/helm/tasks/tests_chart/from_local_path.yml b/tests/integration/targets/helm/tasks/tests_chart/from_local_path.yml similarity index 94% rename from molecule/default/roles/helm/tasks/tests_chart/from_local_path.yml rename to tests/integration/targets/helm/tasks/tests_chart/from_local_path.yml index 5f5216e7..f29b3e2c 100644 --- a/molecule/default/roles/helm/tasks/tests_chart/from_local_path.yml +++ b/tests/integration/targets/helm/tasks/tests_chart/from_local_path.yml @@ -22,6 +22,8 @@ chart_source_upgrade: "/tmp/helm_test_repo_upgrade/stable/{{ chart_test_local_path }}/" chart_test_version: "{{ chart_test_version_local_path }}" chart_test_version_upgrade: "{{ chart_test_version_upgrade_local_path }}" + chart_name: "local-path-001" + helm_namespace: "{{ test_namespace[7] }}" - name: Test appVersion idempotence vars: @@ -66,6 +68,8 @@ source: local_path chart_source: "/tmp/helm_test_appversion/test-chart/{{ chart_test }}-{{ chart_test_app_version }}-{{ chart_test_version }}.tgz" chart_source_upgrade: "/tmp/helm_test_appversion/test-chart/{{ chart_test }}-{{ chart_test_upgrade_app_version }}-{{ chart_test_version_upgrade }}.tgz" + chart_name: "local-path-002" + helm_namespace: "{{ test_namespace[8] }}" - name: Test appVersion handling when null vars: @@ -94,6 +98,8 @@ source: local_path chart_source: "/tmp/helm_test_appversion/test-null/{{ chart_test }}/" chart_source_upgrade: "{{ chart_test }}-{{ chart_test_version_upgrade }}.tgz" + chart_name: "local-path-003" + helm_namespace: "{{ test_namespace[9] }}" - name: Remove clone repos file: diff --git a/molecule/default/roles/helm/tasks/tests_chart/from_repository.yml b/tests/integration/targets/helm/tasks/tests_chart/from_repository.yml similarity index 92% rename from molecule/default/roles/helm/tasks/tests_chart/from_repository.yml rename to tests/integration/targets/helm/tasks/tests_chart/from_repository.yml index 566bfff9..7187bf28 100644 --- a/molecule/default/roles/helm/tasks/tests_chart/from_repository.yml +++ b/tests/integration/targets/helm/tasks/tests_chart/from_repository.yml @@ -12,6 +12,7 @@ chart_source: "test_helm/{{ chart_test }}" chart_source_version: "{{ chart_test_version }}" chart_source_version_upgrade: "{{ chart_test_version_upgrade }}" + helm_namespace: "{{ test_namespace[6] }}" - name: Add chart repo helm_repository: diff --git a/molecule/default/roles/helm/tasks/tests_chart/from_url.yml b/tests/integration/targets/helm/tasks/tests_chart/from_url.yml similarity index 90% rename from molecule/default/roles/helm/tasks/tests_chart/from_url.yml rename to tests/integration/targets/helm/tasks/tests_chart/from_url.yml index fe1ef4b6..1286b401 100644 --- a/molecule/default/roles/helm/tasks/tests_chart/from_url.yml +++ b/tests/integration/targets/helm/tasks/tests_chart/from_url.yml @@ -5,3 +5,4 @@ source: url chart_source: "https://github.com/kubernetes/ingress-nginx/releases/download/{{ chart_test }}-{{ chart_test_version }}/{{ chart_test }}-{{ chart_test_version }}.tgz" chart_source_upgrade: "https://github.com/kubernetes/ingress-nginx/releases/download/{{ chart_test }}-{{ chart_test_version_upgrade }}/{{ chart_test }}-{{ chart_test_version_upgrade }}.tgz" + helm_namespace: "{{ test_namespace[5] }}" diff --git a/molecule/default/roles/helm/tasks/tests_helm_diff.yml b/tests/integration/targets/helm/tasks/tests_helm_diff.yml similarity index 98% rename from molecule/default/roles/helm/tasks/tests_helm_diff.yml rename to tests/integration/targets/helm/tasks/tests_helm_diff.yml index 5b4ec579..d87006f6 100644 --- a/molecule/default/roles/helm/tasks/tests_helm_diff.yml +++ b/tests/integration/targets/helm/tasks/tests_helm_diff.yml @@ -4,6 +4,9 @@ test_chart_ref: "/tmp/test-chart" block: + - set_fact: + helm_namespace: "{{ test_namespace[0] }}" + - name: Install helm diff helm_plugin: binary_path: "{{ helm_binary }}" @@ -148,6 +151,4 @@ kind: Namespace name: "{{ helm_namespace }}" state: absent - wait: yes - wait_timeout: 180 ignore_errors: yes diff --git a/molecule/default/roles/helm/tasks/tests_helm_plugin.yml b/tests/integration/targets/helm/tasks/tests_helm_plugin.yml similarity index 100% rename from molecule/default/roles/helm/tasks/tests_helm_plugin.yml rename to tests/integration/targets/helm/tasks/tests_helm_plugin.yml diff --git a/molecule/default/roles/helm/tasks/tests_repository.yml b/tests/integration/targets/helm/tasks/tests_repository.yml similarity index 100% rename from molecule/default/roles/helm/tasks/tests_repository.yml rename to tests/integration/targets/helm/tasks/tests_repository.yml diff --git a/tests/integration/targets/inventory_k8s/aliases b/tests/integration/targets/inventory_k8s/aliases new file mode 100644 index 00000000..c023328e --- /dev/null +++ b/tests/integration/targets/inventory_k8s/aliases @@ -0,0 +1,3 @@ +context/target +time=42 +k8s diff --git a/tests/integration/targets/inventory_k8s/playbooks/play.yml b/tests/integration/targets/inventory_k8s/playbooks/play.yml new file mode 100644 index 00000000..c62ffaeb --- /dev/null +++ b/tests/integration/targets/inventory_k8s/playbooks/play.yml @@ -0,0 +1,102 @@ +--- +- name: Converge + hosts: localhost + connection: local + + collections: + - kubernetes.core + + vars_files: + - vars/main.yml + + tasks: + - name: Delete existing namespace + k8s: + api_version: v1 + kind: Namespace + name: inventory + wait: yes + state: absent + + - name: Ensure namespace exists + k8s: + api_version: v1 + kind: Namespace + name: inventory + + - name: Add a deployment + k8s: + definition: + apiVersion: apps/v1 + kind: Deployment + metadata: + name: inventory + namespace: inventory + spec: + replicas: 1 + selector: + matchLabels: + app: "{{ k8s_pod_name }}" + template: "{{ k8s_pod_template }}" + wait: yes + wait_timeout: 400 + vars: + k8s_pod_name: inventory + k8s_pod_image: python + k8s_pod_command: + - python + - '-m' + - http.server + k8s_pod_env: + - name: TEST + value: test + + - meta: refresh_inventory + +- name: Verify inventory and connection plugins + hosts: namespace_inventory_pods + gather_facts: no + + vars: + file_content: | + Hello world + + tasks: + - name: End play if host not running (TODO should we not add these to the inventory?) + meta: end_host + when: pod_phase != "Running" + + - debug: var=hostvars + - setup: + + - debug: var=ansible_facts + + - name: Assert the TEST environment variable was retrieved + assert: + that: ansible_facts.env.TEST == 'test' + + - name: Copy a file into the host + copy: + content: '{{ file_content }}' + dest: /tmp/test_file + + - name: Retrieve the file from the host + slurp: + src: /tmp/test_file + register: slurped_file + + - name: Assert the file content matches expectations + assert: + that: (slurped_file.content|b64decode) == file_content + +- name: Delete inventory namespace + hosts: localhost + connection: local + gather_facts: no + tasks: + - name: Remove inventory namespace + k8s: + api_version: v1 + kind: Namespace + name: inventory + state: absent diff --git a/tests/integration/targets/inventory_k8s/playbooks/test.inventory_k8s.yml b/tests/integration/targets/inventory_k8s/playbooks/test.inventory_k8s.yml new file mode 100644 index 00000000..cdbb9316 --- /dev/null +++ b/tests/integration/targets/inventory_k8s/playbooks/test.inventory_k8s.yml @@ -0,0 +1,2 @@ +--- +plugin: kubernetes.core.k8s diff --git a/tests/integration/targets/inventory_k8s/playbooks/vars/main.yml b/tests/integration/targets/inventory_k8s/playbooks/vars/main.yml new file mode 100644 index 00000000..5656784f --- /dev/null +++ b/tests/integration/targets/inventory_k8s/playbooks/vars/main.yml @@ -0,0 +1,38 @@ +--- +k8s_pod_metadata: + labels: + app: "{{ k8s_pod_name }}" + +k8s_pod_spec: + serviceAccount: "{{ k8s_pod_service_account }}" + containers: + - image: "{{ k8s_pod_image }}" + imagePullPolicy: Always + name: "{{ k8s_pod_name }}" + command: "{{ k8s_pod_command }}" + readinessProbe: + initialDelaySeconds: 15 + exec: + command: + - /bin/true + resources: "{{ k8s_pod_resources }}" + ports: "{{ k8s_pod_ports }}" + env: "{{ k8s_pod_env }}" + + +k8s_pod_service_account: default + +k8s_pod_resources: + limits: + cpu: "100m" + memory: "100Mi" + +k8s_pod_command: [] + +k8s_pod_ports: [] + +k8s_pod_env: [] + +k8s_pod_template: + metadata: "{{ k8s_pod_metadata }}" + spec: "{{ k8s_pod_spec }}" diff --git a/tests/integration/targets/inventory_k8s/runme.sh b/tests/integration/targets/inventory_k8s/runme.sh new file mode 100755 index 00000000..1ec40e26 --- /dev/null +++ b/tests/integration/targets/inventory_k8s/runme.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +set -eux + +export ANSIBLE_INVENTORY_ENABLED=kubernetes.core.k8s,yaml +export ANSIBLE_PYTHON_INTERPRETER=auto_silent + +ansible-playbook playbooks/play.yml -i playbooks/test.inventory_k8s.yml "$@" diff --git a/tests/integration/targets/k8s_access_review/aliases b/tests/integration/targets/k8s_access_review/aliases new file mode 100644 index 00000000..6193ad83 --- /dev/null +++ b/tests/integration/targets/k8s_access_review/aliases @@ -0,0 +1,2 @@ +time=7 +k8s diff --git a/molecule/default/tasks/access_review.yml b/tests/integration/targets/k8s_access_review/tasks/main.yml similarity index 100% rename from molecule/default/tasks/access_review.yml rename to tests/integration/targets/k8s_access_review/tasks/main.yml diff --git a/tests/integration/targets/k8s_append_hash/aliases b/tests/integration/targets/k8s_append_hash/aliases new file mode 100644 index 00000000..f78aec22 --- /dev/null +++ b/tests/integration/targets/k8s_append_hash/aliases @@ -0,0 +1,2 @@ +time=14 +k8s \ No newline at end of file diff --git a/tests/integration/targets/k8s_append_hash/defaults/main.yml b/tests/integration/targets/k8s_append_hash/defaults/main.yml new file mode 100644 index 00000000..9fc0c0b5 --- /dev/null +++ b/tests/integration/targets/k8s_append_hash/defaults/main.yml @@ -0,0 +1,2 @@ +--- +test_namespace: "append-hash" diff --git a/tests/integration/targets/k8s_append_hash/meta/main.yml b/tests/integration/targets/k8s_append_hash/meta/main.yml new file mode 100644 index 00000000..0cb0a524 --- /dev/null +++ b/tests/integration/targets/k8s_append_hash/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: + - setup_namespace diff --git a/molecule/default/tasks/append_hash.yml b/tests/integration/targets/k8s_append_hash/tasks/main.yml similarity index 91% rename from molecule/default/tasks/append_hash.yml rename to tests/integration/targets/k8s_append_hash/tasks/main.yml index 9c726a3d..3a6411e8 100644 --- a/molecule/default/tasks/append_hash.yml +++ b/tests/integration/targets/k8s_append_hash/tasks/main.yml @@ -3,14 +3,14 @@ - name: Ensure that append_hash namespace exists k8s: kind: Namespace - name: append-hash + name: "{{ test_namespace }}" - name: Create k8s_resource variable set_fact: k8s_resource: metadata: name: config-map-test - namespace: append-hash + namespace: "{{ test_namespace }}" apiVersion: v1 kind: ConfigMap data: @@ -46,7 +46,7 @@ definition: metadata: name: config-map-test - namespace: append-hash + namespace: "{{ test_namespace }}" apiVersion: v1 kind: ConfigMap data: @@ -65,5 +65,5 @@ - name: Ensure that namespace is removed k8s: kind: Namespace - name: append-hash + name: "{{ test_namespace }}" state: absent diff --git a/tests/integration/targets/k8s_apply/aliases b/tests/integration/targets/k8s_apply/aliases new file mode 100644 index 00000000..5406897b --- /dev/null +++ b/tests/integration/targets/k8s_apply/aliases @@ -0,0 +1,5 @@ +# duration 9min +slow +k8s_service +k8s +time=192 diff --git a/molecule/default/vars/main.yml b/tests/integration/targets/k8s_apply/defaults/main.yml similarity index 92% rename from molecule/default/vars/main.yml rename to tests/integration/targets/k8s_apply/defaults/main.yml index a478de97..c45a18d8 100644 --- a/molecule/default/vars/main.yml +++ b/tests/integration/targets/k8s_apply/defaults/main.yml @@ -37,4 +37,6 @@ k8s_pod_template: metadata: "{{ k8s_pod_metadata }}" spec: "{{ k8s_pod_spec }}" -kubernetes_role_path: ../../tests/integration/targets/kubernetes +test_namespace: "apply" + +k8s_wait_timeout: 240 diff --git a/tests/integration/targets/k8s_apply/meta/main.yml b/tests/integration/targets/k8s_apply/meta/main.yml new file mode 100644 index 00000000..0cb0a524 --- /dev/null +++ b/tests/integration/targets/k8s_apply/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: + - setup_namespace diff --git a/molecule/default/tasks/apply.yml b/tests/integration/targets/k8s_apply/tasks/main.yml similarity index 58% rename from molecule/default/tasks/apply.yml rename to tests/integration/targets/k8s_apply/tasks/main.yml index 18c60d83..b613b186 100644 --- a/molecule/default/tasks/apply.yml +++ b/tests/integration/targets/k8s_apply/tasks/main.yml @@ -1,20 +1,17 @@ --- - block: - - set_fact: - apply_namespace: apply - - name: Ensure namespace exists k8s: definition: apiVersion: v1 kind: Namespace metadata: - name: "{{ apply_namespace }}" + name: "{{ test_namespace }}" - name: Add a configmap k8s: name: "apply-configmap" - namespace: "{{ apply_namespace }}" + namespace: "{{ test_namespace }}" definition: kind: ConfigMap apiVersion: v1 @@ -38,7 +35,7 @@ apiVersion: v1 metadata: name: "apply-configmap" - namespace: "{{ apply_namespace }}" + namespace: "{{ test_namespace }}" data: one: "1" two: "2" @@ -58,7 +55,7 @@ apiVersion: v1 metadata: name: "apply-configmap" - namespace: "{{ apply_namespace }}" + namespace: "{{ test_namespace }}" data: one: "1" two: "2" @@ -75,7 +72,7 @@ - name: Add same configmap again but using name and namespace args k8s: name: "apply-configmap" - namespace: "{{ apply_namespace }}" + namespace: "{{ test_namespace }}" definition: kind: ConfigMap apiVersion: v1 @@ -98,7 +95,7 @@ apiVersion: v1 metadata: name: "apply-configmap" - namespace: "{{ apply_namespace }}" + namespace: "{{ test_namespace }}" data: one: "1" three: "3" @@ -120,7 +117,7 @@ kind: Service metadata: name: apply-svc - namespace: "{{ apply_namespace }}" + namespace: "{{ test_namespace }}" spec: selector: app: whatever @@ -138,7 +135,7 @@ kind: Service metadata: name: apply-svc - namespace: "{{ apply_namespace }}" + namespace: "{{ test_namespace }}" spec: selector: app: whatever @@ -161,7 +158,7 @@ kind: Service metadata: name: apply-svc - namespace: "{{ apply_namespace }}" + namespace: "{{ test_namespace }}" spec: selector: app: whatever @@ -185,7 +182,7 @@ kind: Service metadata: name: apply-svc - namespace: "{{ apply_namespace }}" + namespace: "{{ test_namespace }}" spec: selector: app: whatever @@ -210,7 +207,7 @@ kind: Service metadata: name: apply-svc - namespace: "{{ apply_namespace }}" + namespace: "{{ test_namespace }}" spec: selector: app: whatever @@ -239,7 +236,7 @@ kind: Service metadata: name: apply-svc - namespace: "{{ apply_namespace }}" + namespace: "{{ test_namespace }}" spec: selector: app: whatever @@ -265,7 +262,7 @@ kind: Service metadata: name: apply-svc - namespace: "{{ apply_namespace }}" + namespace: "{{ test_namespace }}" spec: selector: app: whatever @@ -290,7 +287,7 @@ kind: ServiceAccount metadata: name: apply-deploy - namespace: "{{ apply_namespace }}" + namespace: "{{ test_namespace }}" - name: Add a deployment k8s: @@ -299,7 +296,7 @@ kind: Deployment metadata: name: apply-deploy - namespace: "{{ apply_namespace }}" + namespace: "{{ test_namespace }}" spec: replicas: 1 selector: @@ -307,6 +304,7 @@ app: "{{ k8s_pod_name }}" template: "{{ k8s_pod_template }}" wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" apply: yes vars: k8s_pod_name: apply-deploy @@ -331,7 +329,7 @@ kind: Deployment metadata: name: apply-deploy - namespace: "{{ apply_namespace }}" + namespace: "{{ test_namespace }}" spec: replicas: 1 selector: @@ -339,6 +337,7 @@ app: "{{ k8s_pod_name }}" template: "{{ k8s_pod_template }}" wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" apply: yes check_mode: yes vars: @@ -370,7 +369,7 @@ kind: Deployment metadata: name: apply-deploy - namespace: "{{ apply_namespace }}" + namespace: "{{ test_namespace }}" spec: replicas: 1 selector: @@ -378,6 +377,7 @@ app: "{{ k8s_pod_name }}" template: "{{ k8s_pod_template }}" wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" apply: yes vars: k8s_pod_name: apply-deploy @@ -409,7 +409,7 @@ kind: ServiceAccount metadata: name: apply-deploy - namespace: "{{ apply_namespace }}" + namespace: "{{ test_namespace }}" - name: Apply deployment after service account removed k8s: @@ -418,7 +418,7 @@ kind: Deployment metadata: name: apply-deploy - namespace: "{{ apply_namespace }}" + namespace: "{{ test_namespace }}" spec: replicas: 1 selector: @@ -426,6 +426,7 @@ app: "{{ k8s_pod_name }}" template: "{{ k8s_pod_template }}" wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" apply: yes vars: k8s_pod_name: apply-deploy @@ -449,318 +450,6 @@ that: - deploy_after_serviceaccount_removal is failed - - name: Insert new service port - k8s: - definition: - apiVersion: v1 - kind: Service - metadata: - name: apply-svc - namespace: "{{ apply_namespace }}" - spec: - selector: - app: whatever - ports: - - name: mesh - port: 8080 - targetPort: 8080 - - name: http - port: 8081 - targetPort: 8081 - apply: yes - register: k8s_service_4 - - - name: Check ports are correct - assert: - that: - - k8s_service_4 is changed - - k8s_service_4.result.spec.ports | length == 2 - - k8s_service_4.result.spec.ports[0].port == 8080 - - k8s_service_4.result.spec.ports[1].port == 8081 - - - name: Remove new service port (check mode) - k8s: - definition: - apiVersion: v1 - kind: Service - metadata: - name: apply-svc - namespace: "{{ apply_namespace }}" - spec: - selector: - app: whatever - ports: - - name: http - port: 8081 - targetPort: 8081 - apply: yes - check_mode: yes - register: k8s_service_check - - - name: Check ports are correct - assert: - that: - - k8s_service_check is changed - - k8s_service_check.result.spec.ports | length == 1 - - k8s_service_check.result.spec.ports[0].port == 8081 - - - name: Remove new service port - k8s: - definition: - apiVersion: v1 - kind: Service - metadata: - name: apply-svc - namespace: "{{ apply_namespace }}" - spec: - selector: - app: whatever - ports: - - name: http - port: 8081 - targetPort: 8081 - apply: yes - register: k8s_service_5 - - - name: Check ports are correct - assert: - that: - - k8s_service_5 is changed - - k8s_service_5.result.spec.ports | length == 1 - - k8s_service_5.result.spec.ports[0].port == 8081 - - - name: Add a serviceaccount - k8s: - definition: - apiVersion: v1 - kind: ServiceAccount - metadata: - name: apply-deploy - namespace: "{{ apply_namespace }}" - - - name: Add a deployment - k8s: - definition: - apiVersion: apps/v1 - kind: Deployment - metadata: - name: apply-deploy - namespace: "{{ apply_namespace }}" - spec: - replicas: 1 - selector: - matchLabels: - app: "{{ k8s_pod_name }}" - template: "{{ k8s_pod_template }}" - wait: yes - apply: yes - vars: - k8s_pod_name: apply-deploy - k8s_pod_image: gcr.io/kuar-demo/kuard-amd64:v0.10.0-green - k8s_pod_service_account: apply-deploy - k8s_pod_ports: - - containerPort: 8080 - name: http - protocol: TCP - - - name: Remove the serviceaccount - k8s: - state: absent - definition: - apiVersion: v1 - kind: ServiceAccount - metadata: - name: apply-deploy - namespace: "{{ apply_namespace }}" - - - name: Update the earlier deployment - k8s: - definition: - apiVersion: apps/v1 - kind: Deployment - metadata: - name: apply-deploy - namespace: "{{ apply_namespace }}" - spec: - replicas: 2 - selector: - matchLabels: - app: "{{ k8s_pod_name }}" - template: "{{ k8s_pod_template }}" - wait: yes - apply: yes - vars: - k8s_pod_name: apply-deploy - k8s_pod_image: gcr.io/kuar-demo/kuard-amd64:v0.10.0-purple - k8s_pod_service_account: apply-deploy - k8s_pod_ports: - - containerPort: 8080 - name: http - protocol: TCP - register: deploy_after_serviceaccount_removal - ignore_errors: yes - - - name: Ensure that updating deployment after service account removal failed - assert: - that: - - deploy_after_serviceaccount_removal is failed - - - name: Insert new service port - k8s: - definition: - apiVersion: v1 - kind: Service - metadata: - name: apply-svc - namespace: "{{ apply_namespace }}" - spec: - selector: - app: whatever - ports: - - name: mesh - port: 8080 - targetPort: 8080 - - name: http - port: 8081 - targetPort: 8081 - apply: yes - register: k8s_service_4 - - - name: Check ports are correct - assert: - that: - - k8s_service_4 is changed - - k8s_service_4.result.spec.ports | length == 2 - - k8s_service_4.result.spec.ports[0].port == 8080 - - k8s_service_4.result.spec.ports[1].port == 8081 - - - name: Remove new service port (check mode) - k8s: - definition: - apiVersion: v1 - kind: Service - metadata: - name: apply-svc - namespace: "{{ apply_namespace }}" - spec: - selector: - app: whatever - ports: - - name: http - port: 8081 - targetPort: 8081 - apply: yes - check_mode: yes - register: k8s_service_check - - - name: Check ports are correct - assert: - that: - - k8s_service_check is changed - - k8s_service_check.result.spec.ports | length == 1 - - k8s_service_check.result.spec.ports[0].port == 8081 - - - name: Remove new service port - k8s: - definition: - apiVersion: v1 - kind: Service - metadata: - name: apply-svc - namespace: "{{ apply_namespace }}" - spec: - selector: - app: whatever - ports: - - name: http - port: 8081 - targetPort: 8081 - apply: yes - register: k8s_service_5 - - - name: Check ports are correct - assert: - that: - - k8s_service_5 is changed - - k8s_service_5.result.spec.ports | length == 1 - - k8s_service_5.result.spec.ports[0].port == 8081 - - - name: Add a serviceaccount - k8s: - definition: - apiVersion: v1 - kind: ServiceAccount - metadata: - name: apply-deploy - namespace: "{{ apply_namespace }}" - - - name: Add a deployment - k8s: - definition: - apiVersion: apps/v1 - kind: Deployment - metadata: - name: apply-deploy - namespace: "{{ apply_namespace }}" - spec: - replicas: 1 - selector: - matchLabels: - app: "{{ k8s_pod_name }}" - template: "{{ k8s_pod_template }}" - wait: yes - apply: yes - vars: - k8s_pod_name: apply-deploy - k8s_pod_image: gcr.io/kuar-demo/kuard-amd64:v0.10.0-green - k8s_pod_service_account: apply-deploy - k8s_pod_ports: - - containerPort: 8080 - name: http - protocol: TCP - - - name: Remove the serviceaccount - k8s: - state: absent - definition: - apiVersion: v1 - kind: ServiceAccount - metadata: - name: apply-deploy - namespace: "{{ apply_namespace }}" - - - name: Update the earlier deployment - k8s: - definition: - apiVersion: apps/v1 - kind: Deployment - metadata: - name: apply-deploy - namespace: "{{ apply_namespace }}" - spec: - replicas: 2 - selector: - matchLabels: - app: "{{ k8s_pod_name }}" - template: "{{ k8s_pod_template }}" - wait: yes - apply: yes - vars: - k8s_pod_name: apply-deploy - k8s_pod_image: gcr.io/kuar-demo/kuard-amd64:v0.10.0-purple - k8s_pod_service_account: apply-deploy - k8s_pod_ports: - - containerPort: 8080 - name: http - protocol: TCP - register: deploy_after_serviceaccount_removal - ignore_errors: yes - - - name: Ensure that updating deployment after service account removal failed - assert: - that: - - deploy_after_serviceaccount_removal is failed - - name: Add a secret k8s: definition: @@ -768,7 +457,7 @@ kind: Secret metadata: name: apply-secret - namespace: "{{ apply_namespace }}" + namespace: "{{ test_namespace }}" type: Opaque stringData: foo: bar @@ -787,7 +476,7 @@ kind: Secret metadata: name: apply-secret - namespace: "{{ apply_namespace }}" + namespace: "{{ test_namespace }}" type: Opaque stringData: foo: bar @@ -805,7 +494,7 @@ kind: Secret metadata: name: apply-secret - namespace: "{{ apply_namespace }}" + namespace: "{{ test_namespace }}" type: Opaque stringData: foo: bar @@ -824,7 +513,7 @@ kind: Secret metadata: name: apply-secret - namespace: "{{ apply_namespace }}" + namespace: "{{ test_namespace }}" type: Opaque data: foo: YmFy @@ -838,7 +527,7 @@ - name: Create network policy (egress array with empty dict) k8s: - namespace: "{{ apply_namespace }}" + namespace: "{{ test_namespace }}" apply: true definition: kind: NetworkPolicy @@ -865,7 +554,7 @@ - name: Apply network policy k8s: - namespace: "{{ apply_namespace }}" + namespace: "{{ test_namespace }}" definition: kind: NetworkPolicy apiVersion: networking.k8s.io/v1 @@ -900,5 +589,5 @@ - name: Remove namespace k8s: kind: Namespace - name: "{{ apply_namespace }}" + name: "{{ test_namespace }}" state: absent diff --git a/tests/integration/targets/k8s_cluster_info/aliases b/tests/integration/targets/k8s_cluster_info/aliases new file mode 100644 index 00000000..00696503 --- /dev/null +++ b/tests/integration/targets/k8s_cluster_info/aliases @@ -0,0 +1,2 @@ +k8s_cluster_info +time=9 diff --git a/molecule/default/tasks/cluster_info.yml b/tests/integration/targets/k8s_cluster_info/tasks/main.yml similarity index 100% rename from molecule/default/tasks/cluster_info.yml rename to tests/integration/targets/k8s_cluster_info/tasks/main.yml diff --git a/tests/integration/targets/k8s_copy/aliases b/tests/integration/targets/k8s_copy/aliases new file mode 100644 index 00000000..1e430360 --- /dev/null +++ b/tests/integration/targets/k8s_copy/aliases @@ -0,0 +1,4 @@ +k8s_exec +k8s_cp +k8s +time=101 diff --git a/molecule/default/roles/k8scopy/defaults/main.yml b/tests/integration/targets/k8s_copy/defaults/main.yml similarity index 91% rename from molecule/default/roles/k8scopy/defaults/main.yml rename to tests/integration/targets/k8s_copy/defaults/main.yml index 06080b06..34fc2a26 100644 --- a/molecule/default/roles/k8scopy/defaults/main.yml +++ b/tests/integration/targets/k8s_copy/defaults/main.yml @@ -1,6 +1,6 @@ --- # defaults file for k8copy -copy_namespace: copy +test_namespace: copy pod_with_one_container: name: pod-copy-0 diff --git a/molecule/default/roles/k8scopy/files/archive.tar b/tests/integration/targets/k8s_copy/files/archive.tar similarity index 100% rename from molecule/default/roles/k8scopy/files/archive.tar rename to tests/integration/targets/k8s_copy/files/archive.tar diff --git a/molecule/default/roles/k8scopy/files/data/ansible/collection.txt b/tests/integration/targets/k8s_copy/files/data/ansible/collection.txt similarity index 100% rename from molecule/default/roles/k8scopy/files/data/ansible/collection.txt rename to tests/integration/targets/k8s_copy/files/data/ansible/collection.txt diff --git a/molecule/default/roles/k8scopy/files/data/ansible/module.txt b/tests/integration/targets/k8s_copy/files/data/ansible/module.txt similarity index 100% rename from molecule/default/roles/k8scopy/files/data/ansible/module.txt rename to tests/integration/targets/k8s_copy/files/data/ansible/module.txt diff --git a/molecule/default/roles/k8scopy/files/data/file.txt b/tests/integration/targets/k8s_copy/files/data/file.txt similarity index 100% rename from molecule/default/roles/k8scopy/files/data/file.txt rename to tests/integration/targets/k8s_copy/files/data/file.txt diff --git a/molecule/default/roles/k8scopy/files/data/teams/ansible.txt b/tests/integration/targets/k8s_copy/files/data/teams/ansible.txt similarity index 100% rename from molecule/default/roles/k8scopy/files/data/teams/ansible.txt rename to tests/integration/targets/k8s_copy/files/data/teams/ansible.txt diff --git a/molecule/default/roles/k8scopy/files/simple_file.txt b/tests/integration/targets/k8s_copy/files/simple_file.txt similarity index 100% rename from molecule/default/roles/k8scopy/files/simple_file.txt rename to tests/integration/targets/k8s_copy/files/simple_file.txt diff --git a/molecule/default/roles/k8scopy/files/simple_zip_file.txt.gz b/tests/integration/targets/k8s_copy/files/simple_zip_file.txt.gz similarity index 100% rename from molecule/default/roles/k8scopy/files/simple_zip_file.txt.gz rename to tests/integration/targets/k8s_copy/files/simple_zip_file.txt.gz diff --git a/molecule/default/roles/k8scopy/library/k8s_create_file.py b/tests/integration/targets/k8s_copy/library/k8s_create_file.py similarity index 76% rename from molecule/default/roles/k8scopy/library/k8s_create_file.py rename to tests/integration/targets/k8s_copy/library/k8s_create_file.py index 7160e205..6898c36a 100644 --- a/molecule/default/roles/k8scopy/library/k8s_create_file.py +++ b/tests/integration/targets/k8s_copy/library/k8s_create_file.py @@ -8,9 +8,9 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type -DOCUMENTATION = r''' +DOCUMENTATION = r""" -module: k8s_diff +module: k8s_create_file short_description: Create large file with a defined size. @@ -36,18 +36,18 @@ options: - If this flag is set to yes, the generated file content binary data. type: bool default: False -''' +""" -EXAMPLES = r''' +EXAMPLES = r""" - name: create 150MB file k8s_diff: path: large_file.txt size: 150 -''' +""" -RETURN = r''' -''' +RETURN = r""" +""" import os @@ -57,17 +57,19 @@ from ansible.module_utils._text import to_native def execute_module(module): try: - size = module.params.get('size') * 1024 * 1024 - path = module.params.get('path') + size = module.params.get("size") * 1024 * 1024 + path = module.params.get("path") write_mode = "w" - if module.params.get('binary'): + if module.params.get("binary"): content = os.urandom(size) write_mode = "wb" else: content = "" count = 0 while len(content) < size: - content += "This file has been generated using ansible: {0}\n".format(count) + content += "This file has been generated using ansible: {0}\n".format( + count + ) count += 1 with open(path, write_mode) as f: @@ -79,13 +81,13 @@ def execute_module(module): def main(): argument_spec = {} - argument_spec['size'] = {'type': 'int', 'default': 400} - argument_spec['path'] = {'type': 'path', 'required': True} - argument_spec['binary'] = {'type': 'bool', 'default': False} + argument_spec["size"] = {"type": "int", "default": 400} + argument_spec["path"] = {"type": "path", "required": True} + argument_spec["binary"] = {"type": "bool", "default": False} module = AnsibleModule(argument_spec=argument_spec) execute_module(module) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/tests/integration/targets/k8s_copy/library/kubectl_file_compare.py b/tests/integration/targets/k8s_copy/library/kubectl_file_compare.py new file mode 100644 index 00000000..bcf09783 --- /dev/null +++ b/tests/integration/targets/k8s_copy/library/kubectl_file_compare.py @@ -0,0 +1,247 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# Copyright: (c) 2021, Aubin Bikouo <@abikouo> +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + + +DOCUMENTATION = r""" + +module: kubectl_file_compare + +short_description: Compare file and directory using kubectl + +author: + - Aubin Bikouo (@abikouo) + +description: + - This module is used to validate k8s_cp module. + - Compare the local file/directory with the remote pod version + +notes: + - This module authenticates on kubernetes cluster using default kubeconfig only. + +options: + namespace: + description: + - The pod namespace name + type: str + required: yes + pod: + description: + - The pod name + type: str + required: yes + container: + description: + - The container to retrieve files from. + type: str + remote_path: + description: + - Path of the file or directory on Pod. + type: path + required: yes + local_path: + description: + - Path of the local file or directory. + type: path + content: + description: + - local content to compare with remote file from pod. + - mutually exclusive with option I(local_path). + type: path + required: yes + args: + description: + - The file is considered to be an executable. + - The tool will be run locally and on pod and compare result from output and stderr. + type: list + kubectl_path: + description: + - Path to the kubectl executable, if not specified it will be download. + type: path +""" + +EXAMPLES = r""" +- name: compare local /tmp/foo with /tmp/bar in a remote pod + kubectl_file_compare: + namespace: some-namespace + pod: some-pod + remote_path: /tmp/bar + local_path: /tmp/foo + kubectl_path: /tmp/test/kubectl + +- name: Compare executable running help command + kubectl_file_compare: + namespace: some-namespace + pod: some-pod + remote_path: /tmp/test/kubectl + local_path: kubectl + kubectl_path: /tmp/test/kubectl + args: + - "--help" +""" + + +RETURN = r""" +""" + +import os +import filecmp + +from tempfile import NamedTemporaryFile, TemporaryDirectory +from ansible.module_utils.basic import AnsibleModule + + +def kubectl_get_content(module, dest_dir): + kubectl_path = module.params.get("kubectl_path") + if kubectl_path is None: + kubectl_path = module.get_bin_path("kubectl", required=True) + + namespace = module.params.get("namespace") + pod = module.params.get("pod") + file = module.params.get("remote_path") + + cmd = [kubectl_path, "cp", "{0}/{1}:{2}".format(namespace, pod, file)] + container = module.params.get("container") + if container: + cmd += ["-c", container] + local_file = os.path.join( + dest_dir, os.path.basename(module.params.get("remote_path")) + ) + cmd.append(local_file) + rc, out, err = module.run_command(cmd) + return local_file, err, rc, out + + +def kubectl_run_from_pod(module): + kubectl_path = module.params.get("kubectl_path") + if kubectl_path is None: + kubectl_path = module.get_bin_path("kubectl", required=True) + + cmd = [ + kubectl_path, + "exec", + module.params.get("pod"), + "-n", + module.params.get("namespace"), + ] + container = module.params.get("container") + if container: + cmd += ["-c", container] + cmd += ["--", module.params.get("remote_path")] + cmd += module.params.get("args") + return module.run_command(cmd) + + +def compare_directories(dir1, dir2): + test = filecmp.dircmp(dir1, dir2) + if any( + [len(test.left_only) > 0, len(test.right_only) > 0, len(test.funny_files) > 0] + ): + return False + (t, mismatch, errors) = filecmp.cmpfiles( + dir1, dir2, test.common_files, shallow=False + ) + if len(mismatch) > 0 or len(errors) > 0: + return False + for common_dir in test.common_dirs: + new_dir1 = os.path.join(dir1, common_dir) + new_dir2 = os.path.join(dir2, common_dir) + if not compare_directories(new_dir1, new_dir2): + return False + return True + + +def execute_module(module): + + args = module.params.get("args") + local_path = module.params.get("local_path") + namespace = module.params.get("namespace") + pod = module.params.get("pod") + file = module.params.get("remote_path") + content = module.params.get("content") + if args: + pod_rc, pod_out, pod_err = kubectl_run_from_pod(module) + rc, out, err = module.run_command([module.params.get("local_path")] + args) + if rc == pod_rc and out == pod_out: + module.exit_json( + msg="{0} and {1}/{2}:{3} are same.".format( + local_path, namespace, pod, file + ), + rc=rc, + stderr=err, + stdout=out, + ) + result = dict( + local=dict(rc=rc, out=out, err=err), + remote=dict(rc=pod_rc, out=pod_out, err=pod_err), + ) + module.fail_json( + msg=f"{local_path} and {namespace}/{pod}:{file} are same.", **result + ) + else: + with TemporaryDirectory() as tmpdirname: + file_from_pod, err, rc, out = kubectl_get_content( + module=module, dest_dir=tmpdirname + ) + if not os.path.exists(file_from_pod): + module.fail_json( + msg="failed to copy content from pod", error=err, output=out + ) + + if content is not None: + with NamedTemporaryFile(mode="w") as tmp_file: + tmp_file.write(content) + tmp_file.flush() + if filecmp.cmp(file_from_pod, tmp_file.name): + module.exit_json( + msg=f"defined content and {namespace}/{pod}:{file} are same." + ) + module.fail_json( + msg=f"defined content and {namespace}/{pod}:{file} are same." + ) + + if os.path.isfile(local_path): + if filecmp.cmp(file_from_pod, local_path): + module.exit_json( + msg=f"{local_path} and {namespace}/{pod}:{file} are same." + ) + module.fail_json( + msg=f"{local_path} and {namespace}/{pod}:{file} are same." + ) + + if os.path.isdir(local_path): + if compare_directories(file_from_pod, local_path): + module.exit_json( + msg=f"{local_path} and {namespace}/{pod}:{file} are same." + ) + module.fail_json( + msg=f"{local_path} and {namespace}/{pod}:{file} are same." + ) + + +def main(): + argument_spec = {} + argument_spec["namespace"] = {"type": "str", "required": True} + argument_spec["pod"] = {"type": "str", "required": True} + argument_spec["container"] = {} + argument_spec["remote_path"] = {"type": "path", "required": True} + argument_spec["local_path"] = {"type": "path"} + argument_spec["content"] = {"type": "str"} + argument_spec["kubectl_path"] = {"type": "path"} + argument_spec["args"] = {"type": "list"} + module = AnsibleModule( + argument_spec=argument_spec, + mutually_exclusive=[("local_path", "content")], + required_one_of=[["local_path", "content"]], + ) + + execute_module(module) + + +if __name__ == "__main__": + main() diff --git a/molecule/default/roles/helm/meta/main.yml b/tests/integration/targets/k8s_copy/meta/main.yml similarity index 52% rename from molecule/default/roles/helm/meta/main.yml rename to tests/integration/targets/k8s_copy/meta/main.yml index d05af689..4b952614 100644 --- a/molecule/default/roles/helm/meta/main.yml +++ b/tests/integration/targets/k8s_copy/meta/main.yml @@ -1,3 +1,5 @@ --- collections: - kubernetes.core +dependencies: + - setup_namespace diff --git a/molecule/default/roles/k8scopy/tasks/main.yml b/tests/integration/targets/k8s_copy/tasks/main.yml similarity index 95% rename from molecule/default/roles/k8scopy/tasks/main.yml rename to tests/integration/targets/k8s_copy/tasks/main.yml index dd823747..cef8bb80 100644 --- a/molecule/default/roles/k8scopy/tasks/main.yml +++ b/tests/integration/targets/k8s_copy/tasks/main.yml @@ -1,4 +1,7 @@ --- +- set_fact: + copy_namespace: "{{ test_namespace }}" + - block: - name: Download kubeclt executable used to compare results get_url: diff --git a/molecule/default/roles/k8scopy/tasks/test_copy_directory.yml b/tests/integration/targets/k8s_copy/tasks/test_copy_directory.yml similarity index 100% rename from molecule/default/roles/k8scopy/tasks/test_copy_directory.yml rename to tests/integration/targets/k8s_copy/tasks/test_copy_directory.yml diff --git a/molecule/default/roles/k8scopy/tasks/test_copy_errors.yml b/tests/integration/targets/k8s_copy/tasks/test_copy_errors.yml similarity index 100% rename from molecule/default/roles/k8scopy/tasks/test_copy_errors.yml rename to tests/integration/targets/k8s_copy/tasks/test_copy_errors.yml diff --git a/molecule/default/roles/k8scopy/tasks/test_copy_file.yml b/tests/integration/targets/k8s_copy/tasks/test_copy_file.yml similarity index 100% rename from molecule/default/roles/k8scopy/tasks/test_copy_file.yml rename to tests/integration/targets/k8s_copy/tasks/test_copy_file.yml diff --git a/molecule/default/roles/k8scopy/tasks/test_copy_large_file.yml b/tests/integration/targets/k8s_copy/tasks/test_copy_large_file.yml similarity index 100% rename from molecule/default/roles/k8scopy/tasks/test_copy_large_file.yml rename to tests/integration/targets/k8s_copy/tasks/test_copy_large_file.yml diff --git a/molecule/default/roles/k8scopy/tasks/test_multi_container_pod.yml b/tests/integration/targets/k8s_copy/tasks/test_multi_container_pod.yml similarity index 100% rename from molecule/default/roles/k8scopy/tasks/test_multi_container_pod.yml rename to tests/integration/targets/k8s_copy/tasks/test_multi_container_pod.yml diff --git a/molecule/default/roles/k8scopy/templates/pods_definition.j2 b/tests/integration/targets/k8s_copy/templates/pods_definition.j2 similarity index 100% rename from molecule/default/roles/k8scopy/templates/pods_definition.j2 rename to tests/integration/targets/k8s_copy/templates/pods_definition.j2 diff --git a/tests/integration/targets/k8s_crd/aliases b/tests/integration/targets/k8s_crd/aliases new file mode 100644 index 00000000..cd32372b --- /dev/null +++ b/tests/integration/targets/k8s_crd/aliases @@ -0,0 +1,2 @@ +time=22 +k8s diff --git a/tests/integration/targets/k8s_crd/defaults/main.yml b/tests/integration/targets/k8s_crd/defaults/main.yml new file mode 100644 index 00000000..9ccaec0b --- /dev/null +++ b/tests/integration/targets/k8s_crd/defaults/main.yml @@ -0,0 +1,2 @@ +--- +test_namespace: "crd" diff --git a/tests/integration/targets/kubernetes/files/crd-resource.yml b/tests/integration/targets/k8s_crd/files/crd-resource.yml similarity index 100% rename from tests/integration/targets/kubernetes/files/crd-resource.yml rename to tests/integration/targets/k8s_crd/files/crd-resource.yml diff --git a/tests/integration/targets/kubernetes/files/setup-crd.yml b/tests/integration/targets/k8s_crd/files/setup-crd.yml similarity index 100% rename from tests/integration/targets/kubernetes/files/setup-crd.yml rename to tests/integration/targets/k8s_crd/files/setup-crd.yml diff --git a/tests/integration/targets/k8s_crd/meta/main.yml b/tests/integration/targets/k8s_crd/meta/main.yml new file mode 100644 index 00000000..9963f67e --- /dev/null +++ b/tests/integration/targets/k8s_crd/meta/main.yml @@ -0,0 +1,3 @@ +--- +dependencies: + - setup_namespace diff --git a/molecule/default/tasks/crd.yml b/tests/integration/targets/k8s_crd/tasks/main.yml similarity index 58% rename from molecule/default/tasks/crd.yml rename to tests/integration/targets/k8s_crd/tasks/main.yml index 2a96610c..f26e3419 100644 --- a/molecule/default/tasks/crd.yml +++ b/tests/integration/targets/k8s_crd/tasks/main.yml @@ -1,13 +1,8 @@ --- - block: - - name: Create a namespace - k8s: - name: crd - kind: Namespace - - name: Install custom resource definitions k8s: - definition: "{{ lookup('file', kubernetes_role_path + '/files/setup-crd.yml') }}" + definition: "{{ lookup('file', 'setup-crd.yml') }}" - name: Pause 5 seconds to avoid race condition pause: @@ -15,15 +10,15 @@ - name: Create custom resource definition k8s: - definition: "{{ lookup('file', kubernetes_role_path + '/files/crd-resource.yml') }}" - namespace: crd + definition: "{{ lookup('file', 'crd-resource.yml') }}" + namespace: "{{ test_namespace }}" apply: "{{ create_crd_with_apply | default(omit) }}" register: create_crd - name: Patch custom resource definition k8s: - definition: "{{ lookup('file', kubernetes_role_path + '/files/crd-resource.yml') }}" - namespace: crd + definition: "{{ lookup('file', 'crd-resource.yml') }}" + namespace: "{{ test_namespace }}" register: recreate_crd ignore_errors: yes @@ -35,33 +30,32 @@ - block: - name: Recreate custom resource definition with merge_type k8s: - definition: "{{ lookup('file', kubernetes_role_path + '/files/crd-resource.yml') }}" + definition: "{{ lookup('file', 'crd-resource.yml') }}" merge_type: - merge - namespace: crd + namespace: "{{ test_namespace }}" register: recreate_crd_with_merge - name: Recreate custom resource definition with merge_type list k8s: - definition: "{{ lookup('file', kubernetes_role_path + '/files/crd-resource.yml') }}" + definition: "{{ lookup('file', 'crd-resource.yml') }}" merge_type: - strategic-merge - merge - namespace: crd + namespace: "{{ test_namespace }}" register: recreate_crd_with_merge_list when: recreate_crd is successful - name: Remove crd k8s: - definition: "{{ lookup('file', kubernetes_role_path + '/files/crd-resource.yml') }}" - namespace: crd + definition: "{{ lookup('file', 'crd-resource.yml') }}" + namespace: "{{ test_namespace }}" state: absent always: - name: Remove crd namespace k8s: kind: Namespace - name: crd + name: "{{ test_namespace }}" state: absent - ignore_errors: yes diff --git a/tests/integration/targets/k8s_delete/aliases b/tests/integration/targets/k8s_delete/aliases new file mode 100644 index 00000000..66de780e --- /dev/null +++ b/tests/integration/targets/k8s_delete/aliases @@ -0,0 +1,3 @@ +time=70 +k8s_info +k8s diff --git a/tests/integration/targets/k8s_delete/defaults/main.yml b/tests/integration/targets/k8s_delete/defaults/main.yml new file mode 100644 index 00000000..43db9196 --- /dev/null +++ b/tests/integration/targets/k8s_delete/defaults/main.yml @@ -0,0 +1,25 @@ +--- +k8s_pod_template: + metadata: + labels: + app: "{{ k8s_pod_name }}" + spec: + serviceAccount: "default" + containers: + - image: "{{ k8s_pod_image }}" + imagePullPolicy: Always + name: "{{ k8s_pod_name }}" + command: [] + readinessProbe: + initialDelaySeconds: 15 + exec: + command: + - /bin/true + resources: + limits: + cpu: "100m" + memory: "100Mi" + ports: [] + env: [] + +test_namespace: "delete" diff --git a/tests/integration/targets/k8s_delete/meta/main.yml b/tests/integration/targets/k8s_delete/meta/main.yml new file mode 100644 index 00000000..9963f67e --- /dev/null +++ b/tests/integration/targets/k8s_delete/meta/main.yml @@ -0,0 +1,3 @@ +--- +dependencies: + - setup_namespace diff --git a/molecule/default/tasks/delete.yml b/tests/integration/targets/k8s_delete/tasks/main.yml similarity index 77% rename from molecule/default/tasks/delete.yml rename to tests/integration/targets/k8s_delete/tasks/main.yml index e49ff221..f3dafd78 100644 --- a/molecule/default/tasks/delete.yml +++ b/tests/integration/targets/k8s_delete/tasks/main.yml @@ -1,16 +1,5 @@ --- - block: - - set_fact: - delete_namespace: delete - - - name: Ensure namespace exists - k8s: - definition: - apiVersion: v1 - kind: Namespace - metadata: - name: "{{ delete_namespace }}" - - name: Add a daemonset k8s: definition: @@ -18,14 +7,14 @@ kind: DaemonSet metadata: name: delete-daemonset - namespace: "{{ delete_namespace }}" + namespace: "{{ test_namespace }}" spec: selector: matchLabels: app: "{{ k8s_pod_name }}" template: "{{ k8s_pod_template }}" wait: yes - wait_timeout: 180 + wait_timeout: 400 vars: k8s_pod_name: delete-ds k8s_pod_image: gcr.io/kuar-demo/kuard-amd64:1 @@ -38,7 +27,7 @@ - name: Check if pods exist k8s_info: - namespace: "{{ delete_namespace }}" + namespace: "{{ test_namespace }}" kind: Pod label_selectors: - "app={{ k8s_pod_name }}" @@ -55,13 +44,13 @@ k8s: kind: DaemonSet name: delete-daemonset - namespace: "{{ delete_namespace }}" + namespace: "{{ test_namespace }}" state: absent wait: yes - name: Show status of pods k8s_info: - namespace: "{{ delete_namespace }}" + namespace: "{{ test_namespace }}" kind: Pod label_selectors: - "app={{ k8s_pod_name }}" @@ -74,7 +63,7 @@ - name: Check if pods still exist k8s_info: - namespace: "{{ delete_namespace }}" + namespace: "{{ test_namespace }}" kind: Pod label_selectors: - "app={{ k8s_pod_name }}" @@ -91,5 +80,5 @@ - name: Remove namespace k8s: kind: Namespace - name: "{{ delete_namespace }}" + name: "{{ test_namespace }}" state: absent diff --git a/tests/integration/targets/k8s_diff/aliases b/tests/integration/targets/k8s_diff/aliases new file mode 100644 index 00000000..05895d24 --- /dev/null +++ b/tests/integration/targets/k8s_diff/aliases @@ -0,0 +1,2 @@ +time=20 +k8s diff --git a/tests/integration/targets/k8s_diff/defaults/main.yml b/tests/integration/targets/k8s_diff/defaults/main.yml new file mode 100644 index 00000000..5e2db246 --- /dev/null +++ b/tests/integration/targets/k8s_diff/defaults/main.yml @@ -0,0 +1,3 @@ +--- +test_namespace: "diff" +diff_configmap: "diff-configmap" diff --git a/tests/integration/targets/k8s_diff/meta/main.yml b/tests/integration/targets/k8s_diff/meta/main.yml new file mode 100644 index 00000000..08362c78 --- /dev/null +++ b/tests/integration/targets/k8s_diff/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: +- setup_namespace diff --git a/molecule/default/tasks/diff.yml b/tests/integration/targets/k8s_diff/tasks/main.yml similarity index 90% rename from molecule/default/tasks/diff.yml rename to tests/integration/targets/k8s_diff/tasks/main.yml index 0e13067b..81e3dacc 100644 --- a/molecule/default/tasks/diff.yml +++ b/tests/integration/targets/k8s_diff/tasks/main.yml @@ -1,20 +1,14 @@ --- -- set_fact: - diff_namespace: "diff" - diff_configmap: "diff-configmap" - - block: - - name: Ensure namespace - k8s: - kind: Namespace - name: '{{ diff_namespace }}' + - set_fact: + diff_namespace: "{{ test_namespace }}" # Using option 'apply' set to 'yes' - name: Create Pod using apply and diff set to yes k8s: namespace: '{{ diff_namespace }}' apply: yes - template: "pod_diff.j2" + template: "pod.j2" diff: yes vars: pod_name: "pod-apply" @@ -31,7 +25,7 @@ k8s: namespace: '{{ diff_namespace }}' apply: yes - template: "pod_diff.j2" + template: "pod.j2" diff: no vars: pod_name: "pod-apply" @@ -49,7 +43,7 @@ k8s: namespace: '{{ diff_namespace }}' state: present - template: "pod_diff.j2" + template: "pod.j2" vars: pod_name: "pod-patch" pod_image: "busybox:1.32.0" @@ -59,7 +53,7 @@ k8s: namespace: '{{ diff_namespace }}' state: patched - template: "pod_diff.j2" + template: "pod.j2" diff: no vars: pod_name: "pod-patch" @@ -77,7 +71,7 @@ k8s: namespace: '{{ diff_namespace }}' state: patched - template: "pod_diff.j2" + template: "pod.j2" diff: yes vars: pod_name: "pod-patch" @@ -151,3 +145,4 @@ state: absent kind: Namespace name: '{{ diff_namespace }}' + ignore_errors: true diff --git a/molecule/default/templates/pod_diff.j2 b/tests/integration/targets/k8s_diff/templates/pod.j2 similarity index 100% rename from molecule/default/templates/pod_diff.j2 rename to tests/integration/targets/k8s_diff/templates/pod.j2 diff --git a/tests/integration/targets/k8s_drain/aliases b/tests/integration/targets/k8s_drain/aliases new file mode 100644 index 00000000..476fde2b --- /dev/null +++ b/tests/integration/targets/k8s_drain/aliases @@ -0,0 +1,4 @@ +k8s_drain +k8s +k8s_info +time=78 diff --git a/tests/integration/targets/k8s_drain/defaults/main.yml b/tests/integration/targets/k8s_drain/defaults/main.yml new file mode 100644 index 00000000..918c67d2 --- /dev/null +++ b/tests/integration/targets/k8s_drain/defaults/main.yml @@ -0,0 +1,3 @@ +--- +test_namespace: "drain" +k8s_wait_timeout: 400 diff --git a/tests/integration/targets/k8s_drain/meta/main.yml b/tests/integration/targets/k8s_drain/meta/main.yml new file mode 100644 index 00000000..9963f67e --- /dev/null +++ b/tests/integration/targets/k8s_drain/meta/main.yml @@ -0,0 +1,3 @@ +--- +dependencies: + - setup_namespace diff --git a/molecule/default/tasks/drain.yml b/tests/integration/targets/k8s_drain/tasks/main.yml similarity index 93% rename from molecule/default/tasks/drain.yml rename to tests/integration/targets/k8s_drain/tasks/main.yml index 6bc2428b..806869c1 100644 --- a/molecule/default/tasks/drain.yml +++ b/tests/integration/targets/k8s_drain/tasks/main.yml @@ -2,15 +2,9 @@ - block: - name: Set common facts set_fact: - drain_namespace: "drain" drain_daemonset_name: "promotheus-dset" drain_pod_name: "pod-drain" - - name: Create {{ drain_namespace }} namespace - k8s: - kind: Namespace - name: '{{ drain_namespace }}' - # It seems that the default ServiceAccount can take a bit to be created # right after a cluster is brought up. This can lead to the ServiceAccount # admission controller rejecting a Pod creation request because the @@ -19,7 +13,7 @@ k8s_info: kind: ServiceAccount name: default - namespace: "{{ drain_namespace }}" + namespace: "{{ test_namespace }}" wait: yes - name: list cluster nodes @@ -42,7 +36,7 @@ - name: Deploy daemonset on cluster k8s: - namespace: '{{ drain_namespace }}' + namespace: '{{ test_namespace }}' definition: apiVersion: apps/v1 kind: DaemonSet @@ -74,8 +68,9 @@ - name: Create Pods not managed by ReplicationController, ReplicaSet, Job, DaemonSet or StatefulSet. k8s: - namespace: '{{ drain_namespace }}' + namespace: '{{ test_namespace }}' wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" definition: apiVersion: v1 kind: Pod @@ -124,7 +119,7 @@ - name: Get pods k8s_info: kind: Pod - namespace: '{{ drain_namespace }}' + namespace: '{{ test_namespace }}' register: Pod - name: assert that pods are running on cordoned node @@ -186,7 +181,7 @@ - name: assert that unmanaged pod were deleted k8s_info: - namespace: '{{ drain_namespace }}' + namespace: '{{ test_namespace }}' kind: Pod name: '{{ drain_pod_name }}' register: _result @@ -209,7 +204,7 @@ - name: Get DaemonSet k8s_info: kind: DaemonSet - namespace: '{{ drain_namespace }}' + namespace: '{{ test_namespace }}' name: '{{ drain_daemonset_name }}' register: dset_result @@ -235,4 +230,4 @@ k8s: state: absent kind: namespace - name: '{{ drain_namespace }}' + name: '{{ test_namespace }}' diff --git a/tests/integration/targets/k8s_exec/aliases b/tests/integration/targets/k8s_exec/aliases new file mode 100644 index 00000000..334dc479 --- /dev/null +++ b/tests/integration/targets/k8s_exec/aliases @@ -0,0 +1,3 @@ +k8s_exec +k8s +time=23 diff --git a/tests/integration/targets/k8s_exec/defaults/main.yml b/tests/integration/targets/k8s_exec/defaults/main.yml new file mode 100644 index 00000000..d87b3e9f --- /dev/null +++ b/tests/integration/targets/k8s_exec/defaults/main.yml @@ -0,0 +1,2 @@ +--- +test_namespace: "k8s-exec" diff --git a/tests/integration/targets/k8s_exec/meta/main.yml b/tests/integration/targets/k8s_exec/meta/main.yml new file mode 100644 index 00000000..9963f67e --- /dev/null +++ b/tests/integration/targets/k8s_exec/meta/main.yml @@ -0,0 +1,3 @@ +--- +dependencies: + - setup_namespace diff --git a/molecule/default/tasks/exec.yml b/tests/integration/targets/k8s_exec/tasks/main.yml similarity index 76% rename from molecule/default/tasks/exec.yml rename to tests/integration/targets/k8s_exec/tasks/main.yml index 1878f91e..16668e45 100644 --- a/molecule/default/tasks/exec.yml +++ b/tests/integration/targets/k8s_exec/tasks/main.yml @@ -1,13 +1,13 @@ --- - vars: - exec_namespace: k8s-exec + k8s_wait_timeout: 400 pod: sleep-pod exec_pod_definition: apiVersion: v1 kind: Pod metadata: name: "{{ pod }}" - namespace: "{{ exec_namespace }}" + namespace: "{{ test_namespace }}" spec: containers: - name: sleeper @@ -15,22 +15,17 @@ command: ["sleep", "infinity"] block: - - name: "Ensure that {{ exec_namespace }} namespace exists" - k8s: - kind: Namespace - name: "{{ exec_namespace }}" - - name: "Create a pod" k8s: definition: "{{ exec_pod_definition }}" wait: yes wait_sleep: 1 - wait_timeout: 30 + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" - name: "Execute a command" k8s_exec: pod: "{{ pod }}" - namespace: "{{ exec_namespace }}" + namespace: "{{ test_namespace }}" command: cat /etc/resolv.conf register: output @@ -45,7 +40,7 @@ - name: Check if rc is returned for the given command k8s_exec: - namespace: "{{ exec_namespace }}" + namespace: "{{ test_namespace }}" pod: "{{ pod }}" command: 'false' register: command_status @@ -61,5 +56,5 @@ - name: "Cleanup namespace" k8s: kind: Namespace - name: "{{ exec_namespace }}" + name: "{{ test_namespace }}" state: absent diff --git a/tests/integration/targets/k8s_full/aliases b/tests/integration/targets/k8s_full/aliases new file mode 100644 index 00000000..c9ed608b --- /dev/null +++ b/tests/integration/targets/k8s_full/aliases @@ -0,0 +1,3 @@ +time=57 +k8s +k8s_info \ No newline at end of file diff --git a/tests/integration/targets/k8s_full/defaults/main.yml b/tests/integration/targets/k8s_full/defaults/main.yml new file mode 100644 index 00000000..3d0d3394 --- /dev/null +++ b/tests/integration/targets/k8s_full/defaults/main.yml @@ -0,0 +1,10 @@ +--- +test_namespace: + - testing + - testing1 + - testing2 + - testing3 + - testing4 + - testing5 + - testing6 + - test-namespace-module-defaults diff --git a/tests/integration/targets/k8s_full/meta/main.yml b/tests/integration/targets/k8s_full/meta/main.yml new file mode 100644 index 00000000..2e3ba2fa --- /dev/null +++ b/tests/integration/targets/k8s_full/meta/main.yml @@ -0,0 +1,3 @@ +--- +dependencies: + - remove_namespace diff --git a/molecule/default/tasks/full.yml b/tests/integration/targets/k8s_full/tasks/main.yml similarity index 100% rename from molecule/default/tasks/full.yml rename to tests/integration/targets/k8s_full/tasks/main.yml diff --git a/tests/integration/targets/k8s_gc/aliases b/tests/integration/targets/k8s_gc/aliases new file mode 100644 index 00000000..c7783971 --- /dev/null +++ b/tests/integration/targets/k8s_gc/aliases @@ -0,0 +1 @@ +time=142 diff --git a/tests/integration/targets/k8s_gc/defaults/main.yml b/tests/integration/targets/k8s_gc/defaults/main.yml new file mode 100644 index 00000000..d0c74f81 --- /dev/null +++ b/tests/integration/targets/k8s_gc/defaults/main.yml @@ -0,0 +1,3 @@ +--- +test_namespace: "garbage" +k8s_wait_timeout: 400 diff --git a/tests/integration/targets/k8s_gc/meta/main.yml b/tests/integration/targets/k8s_gc/meta/main.yml new file mode 100644 index 00000000..9963f67e --- /dev/null +++ b/tests/integration/targets/k8s_gc/meta/main.yml @@ -0,0 +1,3 @@ +--- +dependencies: + - setup_namespace diff --git a/molecule/default/tasks/gc.yml b/tests/integration/targets/k8s_gc/tasks/main.yml similarity index 91% rename from molecule/default/tasks/gc.yml rename to tests/integration/targets/k8s_gc/tasks/main.yml index 3d71ddcf..923ffc3f 100644 --- a/molecule/default/tasks/gc.yml +++ b/tests/integration/targets/k8s_gc/tasks/main.yml @@ -1,6 +1,6 @@ --- - vars: - gc_namespace: garbage + gc_namespace: "{{ test_namespace }}" gc_name: garbage-job # This is a job definition that runs for 10 minutes and won't gracefully # shutdown. It allows us to test foreground vs background deletion. @@ -25,14 +25,6 @@ restartPolicy: Never block: - - name: Ensure namespace exists - k8s: - definition: - apiVersion: v1 - kind: Namespace - metadata: - name: "{{ gc_namespace }}" - - name: Add a job k8s: definition: "{{ job_definition }}" @@ -54,6 +46,7 @@ namespace: "{{ gc_namespace }}" name: "{{ wait_job.resources[0].metadata.name }}" wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" register: job - name: Assert job's pod is running @@ -67,7 +60,7 @@ namespace: "{{ gc_namespace }}" state: absent wait: yes - wait_timeout: 100 + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" delete_options: propagationPolicy: Foreground @@ -104,6 +97,7 @@ namespace: "{{ gc_namespace }}" name: "{{ wait_job.resources[0].metadata.name }}" wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" register: job - name: Assert job's pod is running @@ -117,7 +111,7 @@ namespace: "{{ gc_namespace }}" state: absent wait: yes - wait_timeout: 100 + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" delete_options: propagationPolicy: "Background" @@ -155,6 +149,7 @@ namespace: "{{ gc_namespace }}" name: "{{ wait_job.resources[0].metadata.name }}" wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" register: job - name: Assert job's pod is running @@ -168,7 +163,7 @@ namespace: "{{ gc_namespace }}" state: absent wait: yes - wait_timeout: 100 + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" delete_options: propagationPolicy: "Orphan" @@ -219,7 +214,7 @@ preconditions: uid: "{{ job.result.metadata.uid }}" wait: yes - wait_timeout: 100 + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" - name: Check that job is deleted k8s_info: @@ -238,3 +233,4 @@ kind: Namespace name: "{{ gc_namespace }}" state: absent + ignore_errors: true diff --git a/tests/integration/targets/k8s_info/aliases b/tests/integration/targets/k8s_info/aliases new file mode 100644 index 00000000..00b93ecf --- /dev/null +++ b/tests/integration/targets/k8s_info/aliases @@ -0,0 +1,3 @@ +time=13 +k8s +k8s_info diff --git a/tests/integration/targets/k8s_info/defaults/main.yml b/tests/integration/targets/k8s_info/defaults/main.yml new file mode 100644 index 00000000..26b87c7f --- /dev/null +++ b/tests/integration/targets/k8s_info/defaults/main.yml @@ -0,0 +1,3 @@ +--- +test_namespace: "wait" +k8s_wait_timeout: 400 diff --git a/tests/integration/targets/k8s_info/meta/main.yml b/tests/integration/targets/k8s_info/meta/main.yml new file mode 100644 index 00000000..9963f67e --- /dev/null +++ b/tests/integration/targets/k8s_info/meta/main.yml @@ -0,0 +1,3 @@ +--- +dependencies: + - setup_namespace diff --git a/molecule/default/tasks/info.yml b/tests/integration/targets/k8s_info/tasks/main.yml similarity index 92% rename from molecule/default/tasks/info.yml rename to tests/integration/targets/k8s_info/tasks/main.yml index 9c1633d1..7a8efeec 100644 --- a/molecule/default/tasks/info.yml +++ b/tests/integration/targets/k8s_info/tasks/main.yml @@ -1,18 +1,10 @@ --- - block: - set_fact: - wait_namespace: wait + wait_namespace: "{{ test_namespace }}" multi_pod_one: multi-pod-1 multi_pod_two: multi-pod-2 - - name: Ensure namespace exists - k8s: - definition: - apiVersion: v1 - kind: Namespace - metadata: - name: "{{ wait_namespace }}" - - name: Add a simple pod with initContainer k8s: definition: @@ -38,7 +30,7 @@ namespace: "{{ wait_namespace }}" wait: yes wait_sleep: 5 - wait_timeout: 400 + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" register: wait_info - name: Assert that pod creation succeeded @@ -56,6 +48,7 @@ namespace: "{{ wait_namespace }}" state: absent wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" ignore_errors: yes register: short_wait_remove_pod @@ -111,7 +104,7 @@ namespace: "{{ wait_namespace }}" wait: yes wait_sleep: 5 - wait_timeout: 400 + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" label_selectors: - run == multi-box register: wait_info @@ -132,6 +125,7 @@ namespace: "{{ wait_namespace }}" state: absent wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" ignore_errors: yes register: multi_pod_one_remove @@ -149,6 +143,7 @@ namespace: "{{ wait_namespace }}" state: absent wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" ignore_errors: yes register: multi_pod_two_remove @@ -191,7 +186,7 @@ name: does-not-exist namespace: "{{ wait_namespace }}" wait: yes - wait_timeout: 45 + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" register: result - name: Check that module waited @@ -223,7 +218,7 @@ label_selectors: - thislabel=doesnotexist wait: yes - wait_timeout: 10 + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" register: result - name: Assert no pods were found @@ -240,3 +235,4 @@ kind: Namespace name: "{{ wait_namespace }}" state: absent + ignore_errors: true diff --git a/tests/integration/targets/k8s_json_patch/aliases b/tests/integration/targets/k8s_json_patch/aliases new file mode 100644 index 00000000..73984332 --- /dev/null +++ b/tests/integration/targets/k8s_json_patch/aliases @@ -0,0 +1,3 @@ +k8s_json_patch +k8s +time=33 diff --git a/tests/integration/targets/k8s_json_patch/defaults/main.yml b/tests/integration/targets/k8s_json_patch/defaults/main.yml new file mode 100644 index 00000000..a6c8adb7 --- /dev/null +++ b/tests/integration/targets/k8s_json_patch/defaults/main.yml @@ -0,0 +1,2 @@ +--- +test_namespace: "json-patch" diff --git a/tests/integration/targets/k8s_json_patch/meta/main.yml b/tests/integration/targets/k8s_json_patch/meta/main.yml new file mode 100644 index 00000000..9963f67e --- /dev/null +++ b/tests/integration/targets/k8s_json_patch/meta/main.yml @@ -0,0 +1,3 @@ +--- +dependencies: + - setup_namespace diff --git a/molecule/default/tasks/json_patch.yml b/tests/integration/targets/k8s_json_patch/tasks/main.yml similarity index 87% rename from molecule/default/tasks/json_patch.yml rename to tests/integration/targets/k8s_json_patch/tasks/main.yml index 4aa1bd0e..314f611b 100644 --- a/molecule/default/tasks/json_patch.yml +++ b/tests/integration/targets/k8s_json_patch/tasks/main.yml @@ -1,21 +1,16 @@ - vars: - namespace: json-patch pod: json-patch deployment: json-patch + k8s_wait_timeout: 400 block: - - name: Ensure namespace exists - kubernetes.core.k8s: - kind: namespace - name: "{{ namespace }}" - - name: Create a simple pod kubernetes.core.k8s: definition: apiVersion: v1 kind: Pod metadata: - namespace: "{{ namespace }}" + namespace: "{{ test_namespace }}" name: "{{ pod }}" labels: label1: foo @@ -28,11 +23,12 @@ - -c - while true; do echo $(date); sleep 10; done wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" - name: Add a label and replace the image in checkmode kubernetes.core.k8s_json_patch: kind: Pod - namespace: "{{ namespace }}" + namespace: "{{ test_namespace }}" name: "{{ pod }}" patch: - op: add @@ -57,7 +53,7 @@ kubernetes.core.k8s_info: kind: Pod name: "{{ pod }}" - namespace: "{{ namespace }}" + namespace: "{{ test_namespace }}" register: result - name: Assert pod has not changed @@ -69,7 +65,7 @@ - name: Add a label and replace the image kubernetes.core.k8s_json_patch: kind: Pod - namespace: "{{ namespace }}" + namespace: "{{ test_namespace }}" name: "{{ pod }}" patch: - op: add @@ -79,6 +75,7 @@ path: /spec/containers/0/image value: busybox:glibc register: result + diff: no - name: Assert patch was made assert: @@ -90,7 +87,7 @@ kubernetes.core.k8s_info: kind: Pod name: "{{ pod }}" - namespace: "{{ namespace }}" + namespace: "{{ test_namespace }}" register: result - name: Assert that both patch operations have been applied @@ -102,7 +99,7 @@ - name: Apply the same patch to the pod kubernetes.core.k8s_json_patch: kind: Pod - namespace: "{{ namespace }}" + namespace: "{{ test_namespace }}" name: "{{ pod }}" patch: - op: add @@ -120,12 +117,13 @@ - name: Create a simple deployment kubernetes.core.k8s: + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" wait: yes definition: apiVersion: apps/v1 kind: Deployment metadata: - namespace: "{{ namespace }}" + namespace: "{{ test_namespace }}" name: "{{ deployment }}" labels: name: "{{ deployment }}" @@ -150,13 +148,14 @@ - name: Apply patch and wait for deployment to be ready kubernetes.core.k8s_json_patch: kind: Deployment - namespace: "{{ namespace }}" + namespace: "{{ test_namespace }}" name: "{{ deployment }}" patch: - op: replace path: /spec/replicas value: 3 wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" register: result - name: Assert all replicas are available @@ -168,6 +167,6 @@ - name: Ensure namespace has been deleted kubernetes.core.k8s: kind: Namespace - name: "{{ namespace }}" + name: "{{ test_namespace }}" state: absent ignore_errors: yes diff --git a/tests/integration/targets/k8s_label_selectors/aliases b/tests/integration/targets/k8s_label_selectors/aliases new file mode 100644 index 00000000..1c5bbb1b --- /dev/null +++ b/tests/integration/targets/k8s_label_selectors/aliases @@ -0,0 +1,3 @@ +time=184 +k8s +k8s_info diff --git a/tests/integration/targets/k8s_label_selectors/defaults/main.yml b/tests/integration/targets/k8s_label_selectors/defaults/main.yml new file mode 100644 index 00000000..d50c8fe2 --- /dev/null +++ b/tests/integration/targets/k8s_label_selectors/defaults/main.yml @@ -0,0 +1,3 @@ +--- +test_namespace: "label-selectors" +k8s_wait_timeout: 400 diff --git a/tests/integration/targets/k8s_label_selectors/meta/main.yml b/tests/integration/targets/k8s_label_selectors/meta/main.yml new file mode 100644 index 00000000..9963f67e --- /dev/null +++ b/tests/integration/targets/k8s_label_selectors/meta/main.yml @@ -0,0 +1,3 @@ +--- +dependencies: + - setup_namespace diff --git a/molecule/default/tasks/label_selectors.yml b/tests/integration/targets/k8s_label_selectors/tasks/main.yml similarity index 98% rename from molecule/default/tasks/label_selectors.yml rename to tests/integration/targets/k8s_label_selectors/tasks/main.yml index 3d7e5591..af43e7ea 100644 --- a/molecule/default/tasks/label_selectors.yml +++ b/tests/integration/targets/k8s_label_selectors/tasks/main.yml @@ -1,7 +1,7 @@ --- - block: - set_fact: - selector_namespace: "selector" + selector_namespace: "{{ test_namespace }}" selector_pod_delete: "pod-selector-delete" selector_pod_apply: "pod-selector-apply" selector_pod_create: @@ -10,11 +10,6 @@ - "pod-selector-apply-02" - "pod-selector-apply-03" - - name: Ensure namespace selector - k8s: - kind: namespace - name: '{{ selector_namespace }}' - # Resource deletion using label selector (equality-based requirement) - name: Create simple pod k8s: @@ -45,7 +40,7 @@ label_selectors: - ansible.dev/team=cloud wait: yes - wait_timeout: 180 + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" - name: Ensure resources have been deleted k8s_info: @@ -87,7 +82,7 @@ label_selectors: - environment in (test, qa) wait: yes - wait_timeout: 180 + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" register: result - name: check that no resources were deleted @@ -115,7 +110,7 @@ label_selectors: - environment in (production) wait: yes - wait_timeout: 180 + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" register: result - name: check result is changed diff --git a/tests/integration/targets/k8s_lists/aliases b/tests/integration/targets/k8s_lists/aliases new file mode 100644 index 00000000..c0d2100e --- /dev/null +++ b/tests/integration/targets/k8s_lists/aliases @@ -0,0 +1,3 @@ +time=22 +k8s +k8s_info diff --git a/tests/integration/targets/k8s_lists/defaults/main.yml b/tests/integration/targets/k8s_lists/defaults/main.yml new file mode 100644 index 00000000..2ad3cd01 --- /dev/null +++ b/tests/integration/targets/k8s_lists/defaults/main.yml @@ -0,0 +1,2 @@ +--- +test_namespace: "lists" diff --git a/tests/integration/targets/k8s_lists/meta/main.yml b/tests/integration/targets/k8s_lists/meta/main.yml new file mode 100644 index 00000000..9963f67e --- /dev/null +++ b/tests/integration/targets/k8s_lists/meta/main.yml @@ -0,0 +1,3 @@ +--- +dependencies: + - setup_namespace diff --git a/molecule/default/tasks/lists.yml b/tests/integration/targets/k8s_lists/tasks/main.yml similarity index 86% rename from molecule/default/tasks/lists.yml rename to tests/integration/targets/k8s_lists/tasks/main.yml index 3ff939c2..ab9a134f 100644 --- a/molecule/default/tasks/lists.yml +++ b/tests/integration/targets/k8s_lists/tasks/main.yml @@ -1,15 +1,9 @@ --- - block: - - name: Ensure testing1 namespace exists - k8s: - api_version: v1 - kind: Namespace - name: testing1 - - block: - name: Create configmaps k8s: - namespace: testing1 + namespace: "{{ test_namespace }}" definition: apiVersion: v1 kind: ConfigMapList @@ -19,7 +13,7 @@ k8s_info: api_version: v1 kind: ConfigMap - namespace: testing1 + namespace: "{{ test_namespace }}" label_selectors: - app=test register: cms @@ -32,7 +26,7 @@ - name: Delete configmaps k8s: state: absent - namespace: testing1 + namespace: "{{ test_namespace }}" definition: apiVersion: v1 kind: ConfigMapList @@ -42,7 +36,7 @@ k8s_info: api_version: v1 kind: ConfigMap - namespace: testing1 + namespace: "{{ test_namespace }}" label_selectors: - app=test register: cms @@ -74,18 +68,18 @@ - block: - name: Create list of arbitrary resources k8s: - namespace: testing1 + namespace: "{{ test_namespace }}" definition: apiVersion: v1 kind: List - namespace: testing1 + namespace: "{{ test_namespace }}" items: '{{ resources }}' - name: Get the created resources k8s_info: api_version: '{{ item.apiVersion }}' kind: '{{ item.kind }}' - namespace: testing1 + namespace: "{{ test_namespace }}" name: '{{ item.metadata.name }}' register: list_resources with_items: '{{ resources }}' @@ -97,18 +91,18 @@ - name: Delete list of arbitrary resources k8s: state: absent - namespace: testing1 + namespace: "{{ test_namespace }}" definition: apiVersion: v1 kind: List - namespace: testing1 + namespace: "{{ test_namespace }}" items: '{{ resources }}' - name: Get the resources k8s_info: api_version: '{{ item.apiVersion }}' kind: '{{ item.kind }}' - namespace: testing1 + namespace: "{{ test_namespace }}" name: '{{ item.metadata.name }}' register: list_resources with_items: '{{ resources }}' @@ -139,9 +133,9 @@ name: port-8000-tcp port: 8000 always: - - name: Remove testing1 namespace + - name: Remove "{{ test_namespace }}" namespace k8s: kind: Namespace - name: testing1 + name: "{{ test_namespace }}" state: absent ignore_errors: yes diff --git a/tests/integration/targets/k8s_log/aliases b/tests/integration/targets/k8s_log/aliases new file mode 100644 index 00000000..38be79f8 --- /dev/null +++ b/tests/integration/targets/k8s_log/aliases @@ -0,0 +1,2 @@ +k8s_log +time=27 diff --git a/tests/integration/targets/k8s_log/defaults/main.yml b/tests/integration/targets/k8s_log/defaults/main.yml new file mode 100644 index 00000000..e5892521 --- /dev/null +++ b/tests/integration/targets/k8s_log/defaults/main.yml @@ -0,0 +1,3 @@ +--- +test_namespace: "k8s-log" +k8s_wait_timeout: 400 diff --git a/tests/integration/targets/k8s_log/meta/main.yml b/tests/integration/targets/k8s_log/meta/main.yml new file mode 100644 index 00000000..9963f67e --- /dev/null +++ b/tests/integration/targets/k8s_log/meta/main.yml @@ -0,0 +1,3 @@ +--- +dependencies: + - setup_namespace diff --git a/molecule/default/tasks/log.yml b/tests/integration/targets/k8s_log/tasks/main.yml similarity index 85% rename from molecule/default/tasks/log.yml rename to tests/integration/targets/k8s_log/tasks/main.yml index d3da05d3..299874f7 100644 --- a/molecule/default/tasks/log.yml +++ b/tests/integration/targets/k8s_log/tasks/main.yml @@ -1,19 +1,15 @@ --- - block: - - name: ensure that k8s-log namespace exists - k8s: - kind: Namespace - name: k8s-log - - name: create hello-world deployment k8s: wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" definition: apiVersion: apps/v1 kind: Deployment metadata: name: hello-world - namespace: k8s-log + namespace: "{{ test_namespace }}" spec: selector: matchLabels: @@ -34,7 +30,7 @@ k8s_log: api_version: apps/v1 kind: Deployment - namespace: k8s-log + namespace: "{{ test_namespace }}" name: hello-world register: deployment_log @@ -47,7 +43,7 @@ - name: retrieve the log with a label selector k8s_log: - namespace: k8s-log + namespace: "{{ test_namespace }}" label_selectors: - 'app=hello-world' register: label_selector_log @@ -62,14 +58,14 @@ - name: get the hello-world pod k8s_info: kind: Pod - namespace: k8s-log + namespace: "{{ test_namespace }}" label_selectors: - 'app=hello-world' register: k8s_log_pods - name: retrieve the log directly with the pod name k8s_log: - namespace: k8s-log + namespace: "{{ test_namespace }}" name: '{{ k8s_log_pods.resources.0.metadata.name }}' register: pod_log @@ -84,7 +80,7 @@ k8s: state: present wait: yes - wait_timeout: 120 + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" wait_condition: type: Complete status: 'True' @@ -93,7 +89,7 @@ kind: Job metadata: name: int-log - namespace: k8s-log + namespace: "{{ test_namespace }}" spec: template: spec: @@ -108,7 +104,7 @@ k8s_log: api_version: batch/v1 kind: Job - namespace: k8s-log + namespace: "{{ test_namespace }}" name: int-log register: job_logs @@ -120,5 +116,6 @@ - name: ensure that namespace is removed k8s: kind: Namespace - name: k8s-log + name: "{{ test_namespace }}" state: absent + ignore_errors: true diff --git a/tests/integration/targets/k8s_merge_type/aliases b/tests/integration/targets/k8s_merge_type/aliases new file mode 100644 index 00000000..b961a8b1 --- /dev/null +++ b/tests/integration/targets/k8s_merge_type/aliases @@ -0,0 +1,3 @@ +time=19 +k8s +k8s_info diff --git a/tests/integration/targets/k8s_merge_type/defaults/main.yml b/tests/integration/targets/k8s_merge_type/defaults/main.yml new file mode 100644 index 00000000..93c5a7b5 --- /dev/null +++ b/tests/integration/targets/k8s_merge_type/defaults/main.yml @@ -0,0 +1,2 @@ +--- +test_namespace: "merge-type" diff --git a/tests/integration/targets/k8s_merge_type/meta/main.yml b/tests/integration/targets/k8s_merge_type/meta/main.yml new file mode 100644 index 00000000..08362c78 --- /dev/null +++ b/tests/integration/targets/k8s_merge_type/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: +- setup_namespace diff --git a/molecule/default/tasks/merge_type.yml b/tests/integration/targets/k8s_merge_type/tasks/main.yml similarity index 95% rename from molecule/default/tasks/merge_type.yml rename to tests/integration/targets/k8s_merge_type/tasks/main.yml index fad238bd..32e41d8b 100644 --- a/molecule/default/tasks/merge_type.yml +++ b/tests/integration/targets/k8s_merge_type/tasks/main.yml @@ -1,17 +1,11 @@ - block: - name: Define common facts set_fact: - k8s_patch_namespace: "patch" + k8s_patch_namespace: "{{ test_namespace }}" k8s_strategic_merge: "strategic-merge" k8s_merge: "json-merge" k8s_json: "json-patch" - - name: Ensure the namespace exist - kubernetes.core.k8s: - kind: namespace - name: "{{ k8s_patch_namespace }}" - - # Strategic merge - name: create a simple nginx deployment kubernetes.core.k8s: diff --git a/tests/integration/targets/k8s_patched/aliases b/tests/integration/targets/k8s_patched/aliases new file mode 100644 index 00000000..106e8b44 --- /dev/null +++ b/tests/integration/targets/k8s_patched/aliases @@ -0,0 +1,3 @@ +time=20 +k8s +k8s_info \ No newline at end of file diff --git a/tests/integration/targets/k8s_patched/defaults/main.yml b/tests/integration/targets/k8s_patched/defaults/main.yml new file mode 100644 index 00000000..a2ecfe95 --- /dev/null +++ b/tests/integration/targets/k8s_patched/defaults/main.yml @@ -0,0 +1,4 @@ +--- +test_namespace: + - patched-namespace-1 + - patched-namespace-2 diff --git a/tests/integration/targets/k8s_patched/meta/main.yml b/tests/integration/targets/k8s_patched/meta/main.yml new file mode 100644 index 00000000..54561c97 --- /dev/null +++ b/tests/integration/targets/k8s_patched/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: +- remove_namespace diff --git a/molecule/default/tasks/patched.yml b/tests/integration/targets/k8s_patched/tasks/main.yml similarity index 71% rename from molecule/default/tasks/patched.yml rename to tests/integration/targets/k8s_patched/tasks/main.yml index 9c0f9077..6fe514d3 100644 --- a/molecule/default/tasks/patched.yml +++ b/tests/integration/targets/k8s_patched/tasks/main.yml @@ -1,27 +1,25 @@ --- - block: - set_fact: - patch_only_namespace: - first: patched-namespace-1 - second: patched-namespace-2 + patch_only_namespace: "{{ test_namespace }}" - - name: Ensure namespace {{ patch_only_namespace.first }} exist + - name: Ensure namespace {{ patch_only_namespace[0] }} exist kubernetes.core.k8s: definition: apiVersion: v1 kind: Namespace metadata: - name: "{{ patch_only_namespace.first }}" + name: "{{ patch_only_namespace[0] }}" labels: existingLabel: "labelValue" annotations: existingAnnotation: "annotationValue" wait: yes - - name: Ensure namespace {{ patch_only_namespace.second }} does not exist + - name: Ensure namespace {{ patch_only_namespace[1] }} does not exist kubernetes.core.k8s_info: kind: namespace - name: "{{ patch_only_namespace.second }}" + name: "{{ patch_only_namespace[1] }}" register: second_namespace - name: assert that second namespace does not exist @@ -38,14 +36,14 @@ apiVersion: v1 kind: Namespace metadata: - name: "{{ patch_only_namespace.first }}" + name: "{{ patch_only_namespace[0] }}" labels: ansible: patched --- apiVersion: v1 kind: Namespace metadata: - name: "{{ patch_only_namespace.second }}" + name: "{{ patch_only_namespace[1] }}" labels: ansible: patched register: patch_resource @@ -56,10 +54,10 @@ - patch_resource.changed - patch_resource.result.results | selectattr('warning', 'defined') | list | length == 1 - - name: Ensure namespace {{ patch_only_namespace.first }} was patched correctly + - name: Ensure namespace {{ patch_only_namespace[0] }} was patched correctly kubernetes.core.k8s_info: kind: namespace - name: "{{ patch_only_namespace.first }}" + name: "{{ patch_only_namespace[0] }}" register: first_namespace - name: assert labels are as expected @@ -68,10 +66,10 @@ - first_namespace.resources[0].metadata.labels.ansible == "patched" - first_namespace.resources[0].metadata.labels.existingLabel == "labelValue" - first_namespace.resources[0].metadata.annotations.existingAnnotation == "annotationValue" - - name: Ensure namespace {{ patch_only_namespace.second }} was not created + - name: Ensure namespace {{ patch_only_namespace[1] }} was not created kubernetes.core.k8s_info: kind: namespace - name: "{{ patch_only_namespace.second }}" + name: "{{ patch_only_namespace[1] }}" register: second_namespace - name: assert that second namespace does not exist @@ -87,23 +85,23 @@ apiVersion: v1 kind: Namespace metadata: - name: "{{ patch_only_namespace.first }}" + name: "{{ patch_only_namespace[0] }}" labels: patch: ansible --- apiVersion: v1 kind: Namespace metadata: - name: "{{ patch_only_namespace.second }}" + name: "{{ patch_only_namespace[1] }}" labels: patch: ansible wait: yes register: patch_resource - - name: Ensure namespace {{ patch_only_namespace.second }} was created + - name: Ensure namespace {{ patch_only_namespace[1] }} was created kubernetes.core.k8s_info: kind: namespace - name: "{{ patch_only_namespace.second }}" + name: "{{ patch_only_namespace[1] }}" register: second_namespace - name: assert that second namespace exist @@ -118,6 +116,6 @@ name: "{{ item }}" state: absent with_items: - - "{{ patch_only_namespace.first }}" - - "{{ patch_only_namespace.second }}" + - "{{ patch_only_namespace[0] }}" + - "{{ patch_only_namespace[1] }}" ignore_errors: true diff --git a/tests/integration/targets/k8s_rollback/aliases b/tests/integration/targets/k8s_rollback/aliases new file mode 100644 index 00000000..90770cd0 --- /dev/null +++ b/tests/integration/targets/k8s_rollback/aliases @@ -0,0 +1,4 @@ +k8s_rollback +k8s +k8s_info +time=187 diff --git a/tests/integration/targets/k8s_rollback/defaults/main.yml b/tests/integration/targets/k8s_rollback/defaults/main.yml new file mode 100644 index 00000000..58d8ae17 --- /dev/null +++ b/tests/integration/targets/k8s_rollback/defaults/main.yml @@ -0,0 +1,3 @@ +--- +test_namespace: "testingrollback" +k8s_wait_timeout: 400 diff --git a/tests/integration/targets/k8s_rollback/meta/main.yml b/tests/integration/targets/k8s_rollback/meta/main.yml new file mode 100644 index 00000000..08362c78 --- /dev/null +++ b/tests/integration/targets/k8s_rollback/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: +- setup_namespace diff --git a/molecule/default/tasks/rollback.yml b/tests/integration/targets/k8s_rollback/tasks/main.yml similarity index 94% rename from molecule/default/tasks/rollback.yml rename to tests/integration/targets/k8s_rollback/tasks/main.yml index 743ff53c..952e5608 100644 --- a/molecule/default/tasks/rollback.yml +++ b/tests/integration/targets/k8s_rollback/tasks/main.yml @@ -2,24 +2,13 @@ - block: - name: Set variables set_fact: - namespace: "testingrollback" - - - name: Create a namespace - k8s: - name: "{{ namespace }}" - kind: Namespace - api_version: v1 - apply: no - register: output - - - name: show output - debug: - var: output + namespace: "{{ test_namespace }}" - name: Create a deployment k8s: state: present wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" inline: &deploy apiVersion: apps/v1 kind: Deployment @@ -53,6 +42,7 @@ k8s: state: present wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" definition: apiVersion: apps/v1 kind: Deployment @@ -96,6 +86,7 @@ k8s: state: present wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" definition: apiVersion: apps/v1 kind: DaemonSet @@ -149,6 +140,7 @@ k8s: state: present wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" definition: apiVersion: apps/v1 kind: DaemonSet @@ -215,3 +207,4 @@ kind: Namespace api_version: v1 state: absent + ignore_errors: yes diff --git a/tests/integration/targets/k8s_scale/aliases b/tests/integration/targets/k8s_scale/aliases new file mode 100644 index 00000000..08e7cd5d --- /dev/null +++ b/tests/integration/targets/k8s_scale/aliases @@ -0,0 +1,4 @@ +k8s_scale +k8s +k8s_info +time=210 diff --git a/tests/integration/targets/k8s_scale/defaults/main.yml b/tests/integration/targets/k8s_scale/defaults/main.yml new file mode 100644 index 00000000..5089314b --- /dev/null +++ b/tests/integration/targets/k8s_scale/defaults/main.yml @@ -0,0 +1,42 @@ +--- +k8s_pod_metadata: + labels: + app: "{{ k8s_pod_name }}" + +k8s_pod_spec: + serviceAccount: "{{ k8s_pod_service_account }}" + containers: + - image: "{{ k8s_pod_image }}" + imagePullPolicy: Always + name: "{{ k8s_pod_name }}" + command: "{{ k8s_pod_command }}" + readinessProbe: + initialDelaySeconds: 15 + exec: + command: + - /bin/true + resources: "{{ k8s_pod_resources }}" + ports: "{{ k8s_pod_ports }}" + env: "{{ k8s_pod_env }}" + + +k8s_pod_service_account: default + +k8s_pod_resources: + limits: + cpu: "100m" + memory: "100Mi" + +k8s_pod_command: [] + +k8s_pod_ports: [] + +k8s_pod_env: [] + +k8s_pod_template: + metadata: "{{ k8s_pod_metadata }}" + spec: "{{ k8s_pod_spec }}" + +test_namespace: "scale" + +k8s_wait_timeout: 400 diff --git a/molecule/default/files/deployment.yaml b/tests/integration/targets/k8s_scale/files/deployment.yaml similarity index 100% rename from molecule/default/files/deployment.yaml rename to tests/integration/targets/k8s_scale/files/deployment.yaml diff --git a/tests/integration/targets/k8s_scale/meta/main.yml b/tests/integration/targets/k8s_scale/meta/main.yml new file mode 100644 index 00000000..08362c78 --- /dev/null +++ b/tests/integration/targets/k8s_scale/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: +- setup_namespace diff --git a/molecule/default/tasks/scale.yml b/tests/integration/targets/k8s_scale/tasks/main.yml similarity index 94% rename from molecule/default/tasks/scale.yml rename to tests/integration/targets/k8s_scale/tasks/main.yml index 7a846ae1..c446da37 100644 --- a/molecule/default/tasks/scale.yml +++ b/tests/integration/targets/k8s_scale/tasks/main.yml @@ -1,15 +1,7 @@ --- - block: - set_fact: - scale_namespace: scale - - - name: Ensure namespace exists - k8s: - definition: - apiVersion: v1 - kind: Namespace - metadata: - name: "{{ scale_namespace }}" + scale_namespace: "{{ test_namespace }}" - name: Add a deployment k8s: @@ -26,7 +18,7 @@ app: "{{ k8s_pod_name }}" template: "{{ k8s_pod_template }}" wait: yes - wait_timeout: 60 + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" apply: yes vars: k8s_pod_name: scale-deploy @@ -53,6 +45,7 @@ namespace: "{{ scale_namespace }}" replicas: 0 wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" register: scale_down diff: true @@ -91,7 +84,7 @@ app: "{{ k8s_pod_name }}" template: "{{ k8s_pod_template }}" wait: yes - wait_timeout: 60 + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" apply: yes vars: k8s_pod_name: scale-deploy @@ -126,8 +119,9 @@ namespace: "{{ scale_namespace }}" replicas: 2 wait: yes - wait_timeout: 60 + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" register: scale_up + diff: no - name: Get pods in scale-deploy k8s_info: @@ -156,6 +150,7 @@ replicas: 2 wait: yes register: scale_up_noop + diff: no - name: Get pods in scale-deploy k8s_info: @@ -259,6 +254,8 @@ namespace: "{{ scale_namespace }}" label_selectors: - app=nginx + wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" register: scale_out - assert: @@ -272,3 +269,4 @@ kind: Namespace name: "{{ scale_namespace }}" state: absent + ignore_errors: true diff --git a/tests/integration/targets/k8s_template/aliases b/tests/integration/targets/k8s_template/aliases new file mode 100644 index 00000000..7a847f3c --- /dev/null +++ b/tests/integration/targets/k8s_template/aliases @@ -0,0 +1,4 @@ +k8s_service +k8s +k8s_info +time=75 diff --git a/tests/integration/targets/k8s_template/defaults/main.yml b/tests/integration/targets/k8s_template/defaults/main.yml new file mode 100644 index 00000000..0d62f585 --- /dev/null +++ b/tests/integration/targets/k8s_template/defaults/main.yml @@ -0,0 +1,3 @@ +--- +test_namespace: "template-test" +k8s_wait_timeout: 400 diff --git a/tests/integration/targets/k8s_template/meta/main.yml b/tests/integration/targets/k8s_template/meta/main.yml new file mode 100644 index 00000000..08362c78 --- /dev/null +++ b/tests/integration/targets/k8s_template/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: +- setup_namespace diff --git a/molecule/default/tasks/template.yml b/tests/integration/targets/k8s_template/tasks/main.yml similarity index 86% rename from molecule/default/tasks/template.yml rename to tests/integration/targets/k8s_template/tasks/main.yml index 13246c37..c9c57252 100644 --- a/molecule/default/tasks/template.yml +++ b/tests/integration/targets/k8s_template/tasks/main.yml @@ -1,19 +1,11 @@ --- - block: - set_fact: - template_namespace: template-test - - - name: Ensure namespace exists - k8s: - definition: - apiVersion: v1 - kind: Namespace - metadata: - name: "{{ template_namespace }}" + template_namespace: "{{ test_namespace }}" - name: Check if k8s_service does not inherit parameter kubernetes.core.k8s_service: - template: "pod_template_one.j2" + template: "pod_one.j2" state: present ignore_errors: yes register: r @@ -27,7 +19,7 @@ - name: Specify both definition and template kubernetes.core.k8s: state: present - template: "pod_template_one.j2" + template: "pod_one.j2" definition: apiVersion: apps/v1 kind: Deployment @@ -54,8 +46,8 @@ - name: Specify both src and template kubernetes.core.k8s: state: present - src: "../templates/pod_template_one.j2" - template: "pod_template_one.j2" + src: "../templates/pod_one.j2" + template: "pod_one.j2" vars: k8s_pod_name_one: pod k8s_pod_namespace: "{{ template_namespace }}" @@ -70,8 +62,9 @@ - name: Create pod using template (direct specification) kubernetes.core.k8s: - template: "pod_template_one.j2" + template: "pod_one.j2" wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" vars: k8s_pod_name_one: pod-1 k8s_pod_namespace: "{{ template_namespace }}" @@ -87,6 +80,7 @@ template: - default wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" vars: k8s_pod_name_one: pod-2 k8s_pod_namespace: "{{ template_namespace }}" @@ -101,8 +95,9 @@ - name: Create pod using template (path parameter) kubernetes.core.k8s: template: - path: "pod_template_one.j2" + path: "pod_one.j2" wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" vars: k8s_pod_name_one: pod-3 k8s_pod_namespace: "{{ template_namespace }}" @@ -116,10 +111,11 @@ - name: Create pod using template (different variable string) kubernetes.core.k8s: template: - path: "pod_template_two.j2" + path: "pod_two.j2" variable_start_string: '[[' variable_end_string: ']]' wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" vars: k8s_pod_name_two: pod-4 k8s_pod_namespace: "[[ template_namespace ]]" @@ -134,8 +130,9 @@ - name: Create pods using multi-resource template kubernetes.core.k8s: template: - path: "pod_template_three.j2" + path: "pod_three.j2" wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" vars: k8s_pod_name_three_one: pod-5 k8s_pod_name_three_two: pod-6 @@ -150,12 +147,13 @@ - name: Create pods using list of template kubernetes.core.k8s: template: - - pod_template_one.j2 - - path: "pod_template_two.j2" + - pod_one.j2 + - path: "pod_two.j2" variable_start_string: '[[' variable_end_string: ']]' - - path: "pod_template_three.j2" + - path: "pod_three.j2" wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" vars: k8s_pod_name_one: pod-7 k8s_pod_name_two: pod-8 @@ -179,15 +177,16 @@ - name: delete pod if it exists kubernetes.core.k8s: - template: pod_template_one.j2 + template: pod_one.j2 wait: true + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" state: absent - name: create pod on bad namespace ( continue_on_error set to default(false) ) kubernetes.core.k8s: template: - pod_with_bad_namespace.j2 - - pod_template_one.j2 + - pod_one.j2 register: resource ignore_errors: true @@ -221,9 +220,10 @@ kubernetes.core.k8s: template: - pod_with_bad_namespace.j2 - - pod_template_one.j2 + - pod_one.j2 continue_on_error: true wait: true + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" register: resource ignore_errors: true @@ -256,3 +256,4 @@ kind: Namespace name: "{{ template_namespace }}" state: absent + ignore_errors: true diff --git a/molecule/default/templates/pod_template_one.j2 b/tests/integration/targets/k8s_template/templates/pod_one.j2 similarity index 100% rename from molecule/default/templates/pod_template_one.j2 rename to tests/integration/targets/k8s_template/templates/pod_one.j2 diff --git a/molecule/default/templates/pod_template_three.j2 b/tests/integration/targets/k8s_template/templates/pod_three.j2 similarity index 100% rename from molecule/default/templates/pod_template_three.j2 rename to tests/integration/targets/k8s_template/templates/pod_three.j2 diff --git a/molecule/default/templates/pod_template_two.j2 b/tests/integration/targets/k8s_template/templates/pod_two.j2 similarity index 100% rename from molecule/default/templates/pod_template_two.j2 rename to tests/integration/targets/k8s_template/templates/pod_two.j2 diff --git a/molecule/default/templates/pod_with_bad_namespace.j2 b/tests/integration/targets/k8s_template/templates/pod_with_bad_namespace.j2 similarity index 100% rename from molecule/default/templates/pod_with_bad_namespace.j2 rename to tests/integration/targets/k8s_template/templates/pod_with_bad_namespace.j2 diff --git a/tests/integration/targets/k8s_validate/aliases b/tests/integration/targets/k8s_validate/aliases new file mode 100644 index 00000000..64f66d0a --- /dev/null +++ b/tests/integration/targets/k8s_validate/aliases @@ -0,0 +1,3 @@ +time=59 +k8s +k8s_info diff --git a/tests/integration/targets/k8s_validate/defaults/main.yml b/tests/integration/targets/k8s_validate/defaults/main.yml new file mode 100644 index 00000000..f868ad2d --- /dev/null +++ b/tests/integration/targets/k8s_validate/defaults/main.yml @@ -0,0 +1,2 @@ +--- +test_namespace: "validate" diff --git a/tests/integration/targets/k8s_validate/meta/main.yml b/tests/integration/targets/k8s_validate/meta/main.yml new file mode 100644 index 00000000..08362c78 --- /dev/null +++ b/tests/integration/targets/k8s_validate/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: +- setup_namespace diff --git a/molecule/default/tasks/validate.yml b/tests/integration/targets/k8s_validate/tasks/main.yml similarity index 96% rename from molecule/default/tasks/validate.yml rename to tests/integration/targets/k8s_validate/tasks/main.yml index 73ea93e0..18587560 100644 --- a/molecule/default/tasks/validate.yml +++ b/tests/integration/targets/k8s_validate/tasks/main.yml @@ -1,12 +1,4 @@ - block: - - name: Create namespace - k8s: - definition: - apiVersion: v1 - kind: Namespace - metadata: - name: "{{ validate_namespace }}" - - name: Create temp directory tempfile: state: directory @@ -235,6 +227,6 @@ ignore_errors: true vars: - validate_namespace: validate + validate_namespace: "{{ test_namespace }}" environment: ENABLE_TURBO_MODE: false diff --git a/tests/integration/targets/k8s_waiter/aliases b/tests/integration/targets/k8s_waiter/aliases new file mode 100644 index 00000000..295a154c --- /dev/null +++ b/tests/integration/targets/k8s_waiter/aliases @@ -0,0 +1,5 @@ +# duration 10min +slow +time=504 +k8s +k8s_info diff --git a/tests/integration/targets/k8s_waiter/defaults/main.yml b/tests/integration/targets/k8s_waiter/defaults/main.yml new file mode 100644 index 00000000..04c873ce --- /dev/null +++ b/tests/integration/targets/k8s_waiter/defaults/main.yml @@ -0,0 +1,40 @@ +--- +k8s_pod_metadata: + labels: + app: "{{ k8s_pod_name }}" + +k8s_pod_spec: + serviceAccount: "{{ k8s_pod_service_account }}" + containers: + - image: "{{ k8s_pod_image }}" + imagePullPolicy: Always + name: "{{ k8s_pod_name }}" + command: "{{ k8s_pod_command }}" + readinessProbe: + initialDelaySeconds: 15 + exec: + command: + - /bin/true + resources: "{{ k8s_pod_resources }}" + ports: "{{ k8s_pod_ports }}" + env: "{{ k8s_pod_env }}" + + +k8s_pod_service_account: default + +k8s_pod_resources: + limits: + cpu: "100m" + memory: "100Mi" + +k8s_pod_command: [] + +k8s_pod_ports: [] + +k8s_pod_env: [] + +k8s_pod_template: + metadata: "{{ k8s_pod_metadata }}" + spec: "{{ k8s_pod_spec }}" + +test_namespace: "wait" diff --git a/tests/integration/targets/k8s_waiter/meta/main.yml b/tests/integration/targets/k8s_waiter/meta/main.yml new file mode 100644 index 00000000..08362c78 --- /dev/null +++ b/tests/integration/targets/k8s_waiter/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: +- setup_namespace diff --git a/molecule/default/tasks/waiter.yml b/tests/integration/targets/k8s_waiter/tasks/main.yml similarity index 94% rename from molecule/default/tasks/waiter.yml rename to tests/integration/targets/k8s_waiter/tasks/main.yml index a2e5c1e5..1cb7e4c4 100644 --- a/molecule/default/tasks/waiter.yml +++ b/tests/integration/targets/k8s_waiter/tasks/main.yml @@ -1,15 +1,8 @@ --- - block: - set_fact: - wait_namespace: wait - - - name: Ensure namespace exists - k8s: - definition: - apiVersion: v1 - kind: Namespace - metadata: - name: "{{ wait_namespace }}" + wait_namespace: "{{ test_namespace }}" + k8s_wait_timeout: 400 - name: Add a simple pod k8s: @@ -21,19 +14,13 @@ namespace: "{{ wait_namespace }}" spec: "{{ k8s_pod_spec }}" wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" vars: k8s_pod_name: wait-pod k8s_pod_image: alpine:3.8 k8s_pod_command: - sleep - "10000" - register: wait_pod - ignore_errors: yes - - - name: Assert that pod creation succeeded - assert: - that: - - wait_pod is successful - name: Add a daemonset k8s: @@ -50,7 +37,7 @@ template: "{{ k8s_pod_template }}" wait: yes wait_sleep: 5 - wait_timeout: 180 + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" vars: k8s_pod_name: wait-ds k8s_pod_image: gcr.io/kuar-demo/kuard-amd64:1 @@ -114,7 +101,7 @@ template: "{{ k8s_pod_template }}" wait: yes wait_sleep: 3 - wait_timeout: 180 + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" vars: k8s_pod_name: wait-ds k8s_pod_image: gcr.io/kuar-demo/kuard-amd64:3 @@ -155,7 +142,7 @@ template: "{{ k8s_pod_template }}" wait: yes wait_sleep: 5 - wait_timeout: 180 + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" vars: k8s_pod_name: wait-sts k8s_pod_image: gcr.io/kuar-demo/kuard-amd64:1 @@ -219,7 +206,7 @@ template: "{{ k8s_pod_template }}" wait: yes wait_sleep: 3 - wait_timeout: 180 + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" vars: k8s_pod_name: wait-sts k8s_pod_image: gcr.io/kuar-demo/kuard-amd64:3 @@ -308,6 +295,7 @@ app: "{{ k8s_pod_name }}" template: "{{ k8s_pod_template }}" wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" vars: k8s_pod_name: wait-deploy k8s_pod_image: gcr.io/kuar-demo/kuard-amd64:1 @@ -338,6 +326,7 @@ app: "{{ k8s_pod_name }}" template: "{{ k8s_pod_template }}" wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" vars: k8s_pod_name: wait-deploy k8s_pod_image: gcr.io/kuar-demo/kuard-amd64:2 @@ -384,6 +373,7 @@ paused: True apply: no wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" wait_condition: type: Progressing status: Unknown @@ -414,6 +404,7 @@ targetPort: 8080 protocol: TCP wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" vars: k8s_pod_name: wait-deploy register: service @@ -438,6 +429,7 @@ app: "{{ k8s_pod_name }}" template: "{{ k8s_pod_template }}" wait: yes + wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" vars: k8s_pod_name: wait-crash-deploy k8s_pod_image: alpine:3.8 @@ -475,3 +467,4 @@ kind: Namespace name: "{{ wait_namespace }}" state: absent + ignore_errors: yes diff --git a/tests/integration/targets/kubernetes/README.md b/tests/integration/targets/kubernetes/README.md deleted file mode 100644 index 2a09ca17..00000000 --- a/tests/integration/targets/kubernetes/README.md +++ /dev/null @@ -1,19 +0,0 @@ -Wait tests ----------- - -wait tests require at least one node, and don't work on the normal k8s -openshift-origin container as provided by ansible-test --docker -v k8s - -minikube, Kubernetes from Docker or any other Kubernetes service will -suffice. - -If kubectl is already using the right config file and context, you can -just do - -``` -cd tests/integration/targets/k8s -./runme.sh -vv -``` - -otherwise set one or both of `K8S_AUTH_KUBECONFIG` and `K8S_AUTH_CONTEXT` -and use the same command diff --git a/tests/integration/targets/kubernetes/aliases b/tests/integration/targets/kubernetes/aliases deleted file mode 100644 index 326e6910..00000000 --- a/tests/integration/targets/kubernetes/aliases +++ /dev/null @@ -1,2 +0,0 @@ -cloud/openshift -shippable/cloud/group1 diff --git a/tests/integration/targets/kubernetes/files/kuard-extra-property.yml b/tests/integration/targets/kubernetes/files/kuard-extra-property.yml deleted file mode 100644 index bed92bc7..00000000 --- a/tests/integration/targets/kubernetes/files/kuard-extra-property.yml +++ /dev/null @@ -1,22 +0,0 @@ ---- -apiVersion: extensions/v1beta1 -kind: Deployment -metadata: - labels: - app: kuard - name: kuard - namespace: default -spec: - replicas: 3 - selector: - matchLabels: - app: kuard - unwanted: value - template: - metadata: - labels: - app: kuard - spec: - containers: - - image: gcr.io/kuar-demo/kuard-amd64:1 - name: kuard diff --git a/tests/integration/targets/kubernetes/files/kuard-invalid-type.yml b/tests/integration/targets/kubernetes/files/kuard-invalid-type.yml deleted file mode 100644 index 72505f88..00000000 --- a/tests/integration/targets/kubernetes/files/kuard-invalid-type.yml +++ /dev/null @@ -1,21 +0,0 @@ ---- -apiVersion: extensions/v1beta1 -kind: Deployment -metadata: - labels: - app: kuard - name: kuard - namespace: default -spec: - replicas: hello - selector: - matchLabels: - app: kuard - template: - metadata: - labels: - app: kuard - spec: - containers: - - image: gcr.io/kuar-demo/kuard-amd64:1 - name: kuard diff --git a/tests/integration/targets/kubernetes/handlers/main.yml b/tests/integration/targets/kubernetes/handlers/main.yml deleted file mode 100644 index efb5408e..00000000 --- a/tests/integration/targets/kubernetes/handlers/main.yml +++ /dev/null @@ -1,6 +0,0 @@ ---- -- name: delete temporary directory - file: - path: "{{ remote_tmp_dir }}" - state: absent - no_log: yes diff --git a/tests/integration/targets/kubernetes/library/README.md b/tests/integration/targets/kubernetes/library/README.md deleted file mode 100644 index ac312297..00000000 --- a/tests/integration/targets/kubernetes/library/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# README - -The `test_tempfile.py` module added here is only used for the `setup_remote_tmp_dir.yml` temporary directory setup task. It is a clone of the `tempfile.py` community-supported Ansible module, and has to be included with the tests here because it is not available in the `ansible-base` distribution against which this collection is tested. diff --git a/tests/integration/targets/kubernetes/library/test_tempfile.py b/tests/integration/targets/kubernetes/library/test_tempfile.py deleted file mode 100644 index c89f5a31..00000000 --- a/tests/integration/targets/kubernetes/library/test_tempfile.py +++ /dev/null @@ -1,121 +0,0 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- - -# Copyright: (c) 2016, Krzysztof Magosa -# Copyright: (c) 2017, Ansible Project -# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) - -from __future__ import absolute_import, division, print_function -__metaclass__ = type - - -DOCUMENTATION = ''' ---- -module: test_tempfile - -short_description: Creates temporary files and directories - -description: - - The C(test_tempfile) module creates temporary files and directories. C(mktemp) command takes different parameters on various systems, this module helps - to avoid troubles related to that. Files/directories created by module are accessible only by creator. In case you need to make them world-accessible - you need to use M(ansible.builtin.file) module. - - For Windows targets, use the M(ansible.builtin.win_tempfile) module instead. - -options: - state: - description: - - Whether to create file or directory. - type: str - choices: [ directory, file ] - default: file - path: - description: - - Location where temporary file or directory should be created. - - If path is not specified, the default system temporary directory will be used. - type: path - prefix: - description: - - Prefix of file/directory name created by module. - type: str - default: ansible. - suffix: - description: - - Suffix of file/directory name created by module. - type: str - default: "" - -seealso: -- module: file -- module: win_tempfile - -author: - - Krzysztof Magosa (@krzysztof-magosa) -''' - -EXAMPLES = """ -- name: create temporary build directory - test_tempfile: - state: directory - suffix: build - -- name: create temporary file - test_tempfile: - state: file - suffix: temp - register: tempfile_1 - -- name: use the registered var and the file module to remove the temporary file - file: - path: "{{ tempfile_1.path }}" - state: absent - when: tempfile_1.path is defined -""" - -RETURN = ''' -path: - description: Path to created file or directory - returned: success - type: str - sample: "/tmp/ansible.bMlvdk" -''' - -from os import close -from tempfile import mkstemp, mkdtemp -from traceback import format_exc - -from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils._text import to_native - - -def main(): - module = AnsibleModule( - argument_spec=dict( - state=dict(type='str', default='file', choices=['file', 'directory']), - path=dict(type='path'), - prefix=dict(type='str', default='ansible.'), - suffix=dict(type='str', default=''), - ), - ) - - try: - if module.params['state'] == 'file': - handle, path = mkstemp( - prefix=module.params['prefix'], - suffix=module.params['suffix'], - dir=module.params['path'], - ) - close(handle) - elif module.params['state'] == 'directory': - path = mkdtemp( - prefix=module.params['prefix'], - suffix=module.params['suffix'], - dir=module.params['path'], - ) - - module.exit_json(changed=True, path=path) - except Exception as e: - module.fail_json(msg=to_native(e), exception=format_exc()) - - -if __name__ == '__main__': - main() diff --git a/tests/integration/targets/kubernetes/meta/main.yml b/tests/integration/targets/kubernetes/meta/main.yml deleted file mode 100644 index 23d65c7e..00000000 --- a/tests/integration/targets/kubernetes/meta/main.yml +++ /dev/null @@ -1,2 +0,0 @@ ---- -dependencies: [] diff --git a/tests/integration/targets/kubernetes/tasks/main.yml b/tests/integration/targets/kubernetes/tasks/main.yml deleted file mode 100644 index 52fae25f..00000000 --- a/tests/integration/targets/kubernetes/tasks/main.yml +++ /dev/null @@ -1,76 +0,0 @@ ---- -- include_tasks: setup_remote_tmp_dir.yml - -- set_fact: - virtualenv: "{{ remote_tmp_dir }}/virtualenv" - virtualenv_command: "{{ ansible_python_interpreter }} -m venv" - -- set_fact: - virtualenv_interpreter: "{{ virtualenv }}/bin/python" - -# Test graceful failure for missing kubernetes-validate - -- pip: - name: - - kubernetes>=12.0.0 - - coverage>=5.3 - virtualenv: "{{ virtualenv }}" - virtualenv_command: "{{ virtualenv_command }}" - virtualenv_site_packages: no - -- include_tasks: validate_not_installed.yml - vars: - ansible_python_interpreter: "{{ virtualenv_interpreter }}" - -- file: - path: "{{ virtualenv }}" - state: absent - no_log: yes - -# Test validate with kubernetes-validate - -- pip: - name: - - kubernetes-validate==1.12.0 - - kubernetes>=12.0.0 - - coverage>=5.3 - virtualenv: "{{ virtualenv }}" - virtualenv_command: "{{ virtualenv_command }}" - virtualenv_site_packages: no - -- include_tasks: validate_installed.yml - vars: - ansible_python_interpreter: "{{ virtualenv_interpreter }}" - playbook_namespace: ansible-test-k8s-validate - -- file: - path: "{{ virtualenv }}" - state: absent - no_log: yes - -# Test new config getter (kubernetes==12.0.0) - -- pip: - name: - - kubernetes==12.0.0 - - coverage>=5.3 - virtualenv: "{{ virtualenv }}" - virtualenv_command: "{{ virtualenv_command }}" - virtualenv_site_packages: no - -- pip: - name: - - kubernetes>=12.0.0 - virtualenv: "{{ virtualenv }}" - virtualenv_command: "{{ virtualenv_command }}" - virtualenv_site_packages: no - -- include_tasks: new_config_getter.yml - vars: - ansible_python_interpreter: "{{ virtualenv_interpreter }}" - playbook_namespace: ansible-test-k8s-config-getter - -- file: - path: "{{ virtualenv }}" - state: absent - no_log: yes diff --git a/tests/integration/targets/kubernetes/tasks/new_config_getter.yml b/tests/integration/targets/kubernetes/tasks/new_config_getter.yml deleted file mode 100644 index 7a534882..00000000 --- a/tests/integration/targets/kubernetes/tasks/new_config_getter.yml +++ /dev/null @@ -1,16 +0,0 @@ ---- -- block: - - name: Create a namespace - k8s: - name: "{{ playbook_namespace }}" - kind: Namespace - - - name: Delete namespace - k8s: - state: absent - definition: - - kind: Namespace - apiVersion: v1 - metadata: - name: "{{ playbook_namespace }}" - ignore_errors: yes diff --git a/tests/integration/targets/kubernetes/tasks/setup_remote_tmp_dir.yml b/tests/integration/targets/kubernetes/tasks/setup_remote_tmp_dir.yml deleted file mode 100644 index 8acdb49e..00000000 --- a/tests/integration/targets/kubernetes/tasks/setup_remote_tmp_dir.yml +++ /dev/null @@ -1,12 +0,0 @@ ---- -- name: create temporary directory - test_tempfile: - state: directory - suffix: .test - register: remote_tmp_dir - notify: - - delete temporary directory - -- name: record temporary directory - set_fact: - remote_tmp_dir: "{{ remote_tmp_dir.path }}" diff --git a/tests/integration/targets/kubernetes/tasks/validate_installed.yml b/tests/integration/targets/kubernetes/tasks/validate_installed.yml deleted file mode 100644 index e00c7c23..00000000 --- a/tests/integration/targets/kubernetes/tasks/validate_installed.yml +++ /dev/null @@ -1,126 +0,0 @@ ---- -- block: - - name: Create a namespace - k8s: - name: "{{ playbook_namespace }}" - kind: Namespace - - - copy: - src: files - dest: "{{ remote_tmp_dir }}" - - - name: incredibly simple ConfigMap - k8s: - definition: - apiVersion: v1 - kind: ConfigMap - metadata: - name: hello - namespace: "{{ playbook_namespace }}" - validate: - fail_on_error: yes - register: k8s_with_validate - - - name: assert that k8s_with_validate succeeds - assert: - that: - - k8s_with_validate is successful - - - name: extra property does not fail without strict - k8s: - src: "{{ remote_tmp_dir }}/files/kuard-extra-property.yml" - namespace: "{{ playbook_namespace }}" - validate: - fail_on_error: yes - strict: no - - - name: extra property fails with strict - k8s: - src: "{{ remote_tmp_dir }}/files/kuard-extra-property.yml" - namespace: "{{ playbook_namespace }}" - validate: - fail_on_error: yes - strict: yes - ignore_errors: yes - register: extra_property - - - name: check that extra property fails with strict - assert: - that: - - extra_property is failed - - - name: invalid type fails at validation stage - k8s: - src: "{{ remote_tmp_dir }}/files/kuard-invalid-type.yml" - namespace: "{{ playbook_namespace }}" - validate: - fail_on_error: yes - strict: no - ignore_errors: yes - register: invalid_type - - - name: check that invalid type fails - assert: - that: - - invalid_type is failed - - - name: invalid type fails with warnings when fail_on_error is False - k8s: - src: "{{ remote_tmp_dir }}/files/kuard-invalid-type.yml" - namespace: "{{ playbook_namespace }}" - validate: - fail_on_error: no - strict: no - ignore_errors: yes - register: invalid_type_no_fail - - - name: check that invalid type fails - assert: - that: - - invalid_type_no_fail is failed - - - name: setup custom resource definition - k8s: - src: "{{ remote_tmp_dir }}/files/setup-crd.yml" - - - name: wait a few seconds - pause: - seconds: 5 - - - name: add custom resource definition - k8s: - src: "{{ remote_tmp_dir }}/files/crd-resource.yml" - namespace: "{{ playbook_namespace }}" - validate: - fail_on_error: yes - strict: yes - register: unknown_kind - - - name: check that unknown kind warns - assert: - that: - - unknown_kind is successful - - "'warnings' in unknown_kind" - - always: - - name: remove custom resource - k8s: - definition: "{{ lookup('file', role_path + '/files/crd-resource.yml') }}" - namespace: "{{ playbook_namespace }}" - state: absent - ignore_errors: yes - - - name: remove custom resource definitions - k8s: - definition: "{{ lookup('file', role_path + '/files/setup-crd.yml') }}" - state: absent - - - name: Delete namespace - k8s: - state: absent - definition: - - kind: Namespace - apiVersion: v1 - metadata: - name: "{{ playbook_namespace }}" - ignore_errors: yes diff --git a/tests/integration/targets/kubernetes/tasks/validate_not_installed.yml b/tests/integration/targets/kubernetes/tasks/validate_not_installed.yml deleted file mode 100644 index aeda2522..00000000 --- a/tests/integration/targets/kubernetes/tasks/validate_not_installed.yml +++ /dev/null @@ -1,25 +0,0 @@ ---- -# TODO: Not available in ansible-base -# - python_requirements_info: -# dependencies: -# - openshift -# - kubernetes -# - kubernetes-validate - -- k8s: - definition: - apiVersion: v1 - kind: ConfigMap - metadata: - name: hello - namespace: default - validate: - fail_on_error: yes - ignore_errors: yes - register: k8s_no_validate - -- name: assert that k8s_no_validate fails gracefully - assert: - that: - - k8s_no_validate is failed - - "k8s_no_validate.msg == 'kubernetes-validate python library is required to validate resources'" diff --git a/tests/integration/targets/lookup_k8s/aliases b/tests/integration/targets/lookup_k8s/aliases new file mode 100644 index 00000000..db0ee6ed --- /dev/null +++ b/tests/integration/targets/lookup_k8s/aliases @@ -0,0 +1,3 @@ +context/target +time=16 +k8s diff --git a/tests/integration/targets/lookup_k8s/defaults/main.yml b/tests/integration/targets/lookup_k8s/defaults/main.yml new file mode 100644 index 00000000..13a3b3c4 --- /dev/null +++ b/tests/integration/targets/lookup_k8s/defaults/main.yml @@ -0,0 +1,4 @@ +--- +test_namespace: + - app-development-one + - app-development-two diff --git a/tests/integration/targets/lookup_k8s/meta/main.yml b/tests/integration/targets/lookup_k8s/meta/main.yml new file mode 100644 index 00000000..54561c97 --- /dev/null +++ b/tests/integration/targets/lookup_k8s/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: +- remove_namespace diff --git a/molecule/default/tasks/lookup_k8s.yml b/tests/integration/targets/lookup_k8s/tasks/main.yml similarity index 88% rename from molecule/default/tasks/lookup_k8s.yml rename to tests/integration/targets/lookup_k8s/tasks/main.yml index b5733c42..4ef7ce43 100644 --- a/molecule/default/tasks/lookup_k8s.yml +++ b/tests/integration/targets/lookup_k8s/tasks/main.yml @@ -2,9 +2,9 @@ - block: - set_fact: pre_test1: "{{ lookup('kubernetes.core.k8s', kind='Namespace', label_selector='namespace_label=app_development') }}" - pre_test2: "{{ lookup('kubernetes.core.k8s', kind='Namespace', resource_name='app-development-one') }}" + pre_test2: "{{ lookup('kubernetes.core.k8s', kind='Namespace', resource_name=test_namespace[0]) }}" pre_test3: "{{ query('kubernetes.core.k8s', kind='Namespace', label_selector='namespace_label=app_development') }}" - pre_test4: "{{ query('kubernetes.core.k8s', kind='Namespace', resource_name='app-development-one') }}" + pre_test4: "{{ query('kubernetes.core.k8s', kind='Namespace', resource_name=test_namespace[0]) }}" # https://github.com/ansible-collections/kubernetes.core/issues/147 - name: Create a namespace with label @@ -13,17 +13,17 @@ apiVersion: v1 kind: Namespace metadata: - name: "app-development-one" + name: "{{ test_namespace[0] }}" labels: namespace_label: "app_development" - set_fact: test1: "{{ lookup('kubernetes.core.k8s', kind='Namespace', label_selector='namespace_label=app_development', wantlist=True) }}" test2: "{{ query('kubernetes.core.k8s', kind='Namespace', label_selector='namespace_label=app_development') }}" - test3: "{{ lookup('kubernetes.core.k8s', kind='Namespace', resource_name='app-development-one', wantlist=True) }}" - test4: "{{ query('kubernetes.core.k8s', kind='Namespace', resource_name='app-development-one') }}" + test3: "{{ lookup('kubernetes.core.k8s', kind='Namespace', resource_name=test_namespace[0], wantlist=True) }}" + test4: "{{ query('kubernetes.core.k8s', kind='Namespace', resource_name=test_namespace[0]) }}" test5: "{{ lookup('kubernetes.core.k8s', kind='Namespace', label_selector='namespace_label=app_development') }}" - test6: "{{ lookup('kubernetes.core.k8s', kind='Namespace', resource_name='app-development-one') }}" + test6: "{{ lookup('kubernetes.core.k8s', kind='Namespace', resource_name=test_namespace[0]) }}" test7: "{{ lookup('kubernetes.core.k8s', kind='Ingress', api_version='networking.k8s.io/vINVALID', errors='ignore') }}" - set_fact: @@ -64,20 +64,20 @@ apiVersion: v1 kind: Namespace metadata: - name: "app-development-two" + name: "{{ test_namespace[1] }}" labels: namespace_label: "app_development" - set_fact: test1: "{{ lookup('kubernetes.core.k8s', kind='Namespace', label_selector='namespace_label=app_development', wantlist=True) }}" test2: "{{ query('kubernetes.core.k8s', kind='Namespace', label_selector='namespace_label=app_development') }}" - test3: "{{ lookup('kubernetes.core.k8s', kind='Namespace', resource_name='app-development-one', wantlist=True) }}" - test4: "{{ query('kubernetes.core.k8s', kind='Namespace', resource_name='app-development-one') }}" - test5: "{{ lookup('kubernetes.core.k8s', kind='Namespace', resource_name='app-development-two', wantlist=True) }}" - test6: "{{ query('kubernetes.core.k8s', kind='Namespace', resource_name='app-development-two') }}" + test3: "{{ lookup('kubernetes.core.k8s', kind='Namespace', resource_name=test_namespace[0], wantlist=True) }}" + test4: "{{ query('kubernetes.core.k8s', kind='Namespace', resource_name=test_namespace[0]) }}" + test5: "{{ lookup('kubernetes.core.k8s', kind='Namespace', resource_name=test_namespace[1], wantlist=True) }}" + test6: "{{ query('kubernetes.core.k8s', kind='Namespace', resource_name=test_namespace[1]) }}" test7: "{{ lookup('kubernetes.core.k8s', kind='Namespace', label_selector='namespace_label=app_development') }}" - test8: "{{ lookup('kubernetes.core.k8s', kind='Namespace', resource_name='app-development-one') }}" - test9: "{{ lookup('kubernetes.core.k8s', kind='Namespace', resource_name='app-development-two') }}" + test8: "{{ lookup('kubernetes.core.k8s', kind='Namespace', resource_name=test_namespace[0]) }}" + test9: "{{ lookup('kubernetes.core.k8s', kind='Namespace', resource_name=test_namespace[1]) }}" - name: Assert that every test is passed after creating second object assert: @@ -110,3 +110,4 @@ with_items: - one - two + ignore_errors: true diff --git a/tests/integration/targets/lookup_kustomize/aliases b/tests/integration/targets/lookup_kustomize/aliases new file mode 100644 index 00000000..6e6cbcf1 --- /dev/null +++ b/tests/integration/targets/lookup_kustomize/aliases @@ -0,0 +1,3 @@ +context/target +time=44 +k8s diff --git a/tests/integration/targets/lookup_kustomize/defaults/main.yml b/tests/integration/targets/lookup_kustomize/defaults/main.yml new file mode 100644 index 00000000..bc7e6fbe --- /dev/null +++ b/tests/integration/targets/lookup_kustomize/defaults/main.yml @@ -0,0 +1,2 @@ +--- +test_namespace: "kustomize" diff --git a/tests/integration/targets/lookup_kustomize/meta/main.yml b/tests/integration/targets/lookup_kustomize/meta/main.yml new file mode 100644 index 00000000..08362c78 --- /dev/null +++ b/tests/integration/targets/lookup_kustomize/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: +- setup_namespace diff --git a/molecule/default/tasks/lookup_kustomize.yml b/tests/integration/targets/lookup_kustomize/tasks/main.yml similarity index 95% rename from molecule/default/tasks/lookup_kustomize.yml rename to tests/integration/targets/lookup_kustomize/tasks/main.yml index 164bb828..4e963141 100644 --- a/molecule/default/tasks/lookup_kustomize.yml +++ b/tests/integration/targets/lookup_kustomize/tasks/main.yml @@ -1,14 +1,10 @@ --- - block: - set_fact: - kustomize_ns: "kustomize" + kustomize_ns: "{{ test_namespace }}" - name: create environment for test block: - - name: Ensure namespace - k8s: - kind: Namespace - name: "{{ kustomize_ns }}" - name: Create temp directory tempfile: diff --git a/tests/integration/targets/remove_namespace/aliases b/tests/integration/targets/remove_namespace/aliases new file mode 100644 index 00000000..7a68b11d --- /dev/null +++ b/tests/integration/targets/remove_namespace/aliases @@ -0,0 +1 @@ +disabled diff --git a/tests/integration/targets/remove_namespace/tasks/main.yml b/tests/integration/targets/remove_namespace/tasks/main.yml new file mode 100644 index 00000000..b715c383 --- /dev/null +++ b/tests/integration/targets/remove_namespace/tasks/main.yml @@ -0,0 +1,17 @@ +--- +- name: Delete existing namespace + kubernetes.core.k8s: + kind: Namespace + name: "{{ item }}" + state: absent + wait: yes + with_items: "{{ test_namespace }}" + when: test_namespace | type_debug == "list" + +- name: Delete existing namespace + kubernetes.core.k8s: + kind: Namespace + name: "{{ test_namespace }}" + state: absent + wait: yes + when: test_namespace | type_debug == "AnsibleUnicode" diff --git a/tests/integration/targets/setup_namespace/aliases b/tests/integration/targets/setup_namespace/aliases new file mode 100644 index 00000000..7a68b11d --- /dev/null +++ b/tests/integration/targets/setup_namespace/aliases @@ -0,0 +1 @@ +disabled diff --git a/tests/integration/targets/setup_namespace/defaults/main.yml b/tests/integration/targets/setup_namespace/defaults/main.yml new file mode 100644 index 00000000..ed97d539 --- /dev/null +++ b/tests/integration/targets/setup_namespace/defaults/main.yml @@ -0,0 +1 @@ +--- diff --git a/tests/integration/targets/setup_namespace/tasks/create.yml b/tests/integration/targets/setup_namespace/tasks/create.yml new file mode 100644 index 00000000..a25780ec --- /dev/null +++ b/tests/integration/targets/setup_namespace/tasks/create.yml @@ -0,0 +1,22 @@ +--- +- name: "Read namespace {{ namespace_to_create }}" + kubernetes.core.k8s_info: + kind: Namespace + name: "{{ namespace_to_create }}" + register: _namespace + +- name: Delete existing namespace + kubernetes.core.k8s: + kind: Namespace + name: "{{ namespace_to_create }}" + state: absent + wait: yes + +- name: "Ensure namespace {{ namespace_to_create }}" + kubernetes.core.k8s: + definition: + apiVersion: v1 + kind: Namespace + metadata: + name: "{{ namespace_to_create }}" + labels: "{{ namespace_labels | default(omit) }}" diff --git a/tests/integration/targets/setup_namespace/tasks/main.yml b/tests/integration/targets/setup_namespace/tasks/main.yml new file mode 100644 index 00000000..31e1b362 --- /dev/null +++ b/tests/integration/targets/setup_namespace/tasks/main.yml @@ -0,0 +1,13 @@ +--- +- include_tasks: tasks/create.yml + vars: + namespace_to_create: "{{ item.name }}" + namespace_labels: "{{ item.labels | default(omit) }}" + with_items: "{{ test_namespace }}" + when: test_namespace | type_debug == "list" + +- include_tasks: tasks/create.yml + vars: + namespace_to_create: "{{ test_namespace }}" + namespace_labels: "{{ test_namespace_labels | default(omit) }}" + when: test_namespace | type_debug == "AnsibleUnicode" diff --git a/tests/sanity/ignore-2.10.txt b/tests/sanity/ignore-2.10.txt index c543b5de..a33e9a65 100644 --- a/tests/sanity/ignore-2.10.txt +++ b/tests/sanity/ignore-2.10.txt @@ -1,8 +1,8 @@ -molecule/default/files/deployment.yaml yamllint!skip -molecule/default/roles/helm/files/appversionless-chart-v2/templates/configmap.yaml yamllint!skip -molecule/default/roles/helm/files/appversionless-chart/templates/configmap.yaml yamllint!skip -molecule/default/roles/helm/files/test-chart-v2/templates/configmap.yaml yamllint!skip -molecule/default/roles/helm/files/test-chart/templates/configmap.yaml yamllint!skip +tests/integration/targets/k8s_scale/files/deployment.yaml yamllint!skip +tests/integration/targets/helm/files/appversionless-chart-v2/templates/configmap.yaml yamllint!skip +tests/integration/targets/helm/files/appversionless-chart/templates/configmap.yaml yamllint!skip +tests/integration/targets/helm/files/test-chart-v2/templates/configmap.yaml yamllint!skip +tests/integration/targets/helm/files/test-chart/templates/configmap.yaml yamllint!skip plugins/module_utils/__init__.py compile-2.6!skip plugins/module_utils/__init__.py compile-2.7!skip plugins/module_utils/__init__.py compile-3.5!skip @@ -232,18 +232,18 @@ plugins/modules/k8s_service.py validate-modules:return-syntax-error tests/sanity/refresh_ignore_files shebang!skip tests/unit/module_utils/test_discoverer.py future-import-boilerplate!skip tests/unit/module_utils/test_discoverer.py metaclass-boilerplate!skip -molecule/default/roles/k8scopy/library/kubectl_file_compare.py shebang -molecule/default/roles/k8scopy/library/k8s_create_file.py shebang plugins/modules/k8s_cp.py compile-2.6!skip plugins/modules/k8s_cp.py compile-2.7!skip plugins/modules/k8s_cp.py import-2.6!skip plugins/modules/k8s_cp.py import-2.7!skip -molecule/default/roles/k8scopy/library/kubectl_file_compare.py compile-2.6!skip -molecule/default/roles/k8scopy/library/kubectl_file_compare.py compile-2.7!skip -molecule/default/roles/k8scopy/library/kubectl_file_compare.py compile-3.5!skip +tests/integration/targets/k8s_copy/library/kubectl_file_compare.py compile-2.6!skip +tests/integration/targets/k8s_copy/library/kubectl_file_compare.py compile-2.7!skip +tests/integration/targets/k8s_copy/library/kubectl_file_compare.py compile-3.5!skip tests/unit/module_utils/test_selector.py future-import-boilerplate!skip tests/unit/module_utils/test_selector.py metaclass-boilerplate!skip plugins/module_utils/selector.py future-import-boilerplate!skip plugins/module_utils/selector.py metaclass-boilerplate!skip plugins/lookup/kustomize.py future-import-boilerplate!skip -plugins/lookup/kustomize.py metaclass-boilerplate!skip \ No newline at end of file +plugins/lookup/kustomize.py metaclass-boilerplate!skip +tests/integration/targets/helm/library/helm_test_version.py metaclass-boilerplate!skip +tests/integration/targets/helm/library/helm_test_version.py future-import-boilerplate!skip diff --git a/tests/sanity/ignore-2.11.txt b/tests/sanity/ignore-2.11.txt index c543b5de..a33e9a65 100644 --- a/tests/sanity/ignore-2.11.txt +++ b/tests/sanity/ignore-2.11.txt @@ -1,8 +1,8 @@ -molecule/default/files/deployment.yaml yamllint!skip -molecule/default/roles/helm/files/appversionless-chart-v2/templates/configmap.yaml yamllint!skip -molecule/default/roles/helm/files/appversionless-chart/templates/configmap.yaml yamllint!skip -molecule/default/roles/helm/files/test-chart-v2/templates/configmap.yaml yamllint!skip -molecule/default/roles/helm/files/test-chart/templates/configmap.yaml yamllint!skip +tests/integration/targets/k8s_scale/files/deployment.yaml yamllint!skip +tests/integration/targets/helm/files/appversionless-chart-v2/templates/configmap.yaml yamllint!skip +tests/integration/targets/helm/files/appversionless-chart/templates/configmap.yaml yamllint!skip +tests/integration/targets/helm/files/test-chart-v2/templates/configmap.yaml yamllint!skip +tests/integration/targets/helm/files/test-chart/templates/configmap.yaml yamllint!skip plugins/module_utils/__init__.py compile-2.6!skip plugins/module_utils/__init__.py compile-2.7!skip plugins/module_utils/__init__.py compile-3.5!skip @@ -232,18 +232,18 @@ plugins/modules/k8s_service.py validate-modules:return-syntax-error tests/sanity/refresh_ignore_files shebang!skip tests/unit/module_utils/test_discoverer.py future-import-boilerplate!skip tests/unit/module_utils/test_discoverer.py metaclass-boilerplate!skip -molecule/default/roles/k8scopy/library/kubectl_file_compare.py shebang -molecule/default/roles/k8scopy/library/k8s_create_file.py shebang plugins/modules/k8s_cp.py compile-2.6!skip plugins/modules/k8s_cp.py compile-2.7!skip plugins/modules/k8s_cp.py import-2.6!skip plugins/modules/k8s_cp.py import-2.7!skip -molecule/default/roles/k8scopy/library/kubectl_file_compare.py compile-2.6!skip -molecule/default/roles/k8scopy/library/kubectl_file_compare.py compile-2.7!skip -molecule/default/roles/k8scopy/library/kubectl_file_compare.py compile-3.5!skip +tests/integration/targets/k8s_copy/library/kubectl_file_compare.py compile-2.6!skip +tests/integration/targets/k8s_copy/library/kubectl_file_compare.py compile-2.7!skip +tests/integration/targets/k8s_copy/library/kubectl_file_compare.py compile-3.5!skip tests/unit/module_utils/test_selector.py future-import-boilerplate!skip tests/unit/module_utils/test_selector.py metaclass-boilerplate!skip plugins/module_utils/selector.py future-import-boilerplate!skip plugins/module_utils/selector.py metaclass-boilerplate!skip plugins/lookup/kustomize.py future-import-boilerplate!skip -plugins/lookup/kustomize.py metaclass-boilerplate!skip \ No newline at end of file +plugins/lookup/kustomize.py metaclass-boilerplate!skip +tests/integration/targets/helm/library/helm_test_version.py metaclass-boilerplate!skip +tests/integration/targets/helm/library/helm_test_version.py future-import-boilerplate!skip diff --git a/tests/sanity/ignore-2.12.txt b/tests/sanity/ignore-2.12.txt index f9aee54e..2875d0d1 100644 --- a/tests/sanity/ignore-2.12.txt +++ b/tests/sanity/ignore-2.12.txt @@ -1,8 +1,8 @@ -molecule/default/files/deployment.yaml yamllint!skip -molecule/default/roles/helm/files/appversionless-chart-v2/templates/configmap.yaml yamllint!skip -molecule/default/roles/helm/files/appversionless-chart/templates/configmap.yaml yamllint!skip -molecule/default/roles/helm/files/test-chart-v2/templates/configmap.yaml yamllint!skip -molecule/default/roles/helm/files/test-chart/templates/configmap.yaml yamllint!skip +tests/integration/targets/k8s_scale/files/deployment.yaml yamllint!skip +tests/integration/targets/helm/files/appversionless-chart-v2/templates/configmap.yaml yamllint!skip +tests/integration/targets/helm/files/appversionless-chart/templates/configmap.yaml yamllint!skip +tests/integration/targets/helm/files/test-chart-v2/templates/configmap.yaml yamllint!skip +tests/integration/targets/helm/files/test-chart/templates/configmap.yaml yamllint!skip plugins/module_utils/__init__.py compile-2.6!skip plugins/module_utils/__init__.py compile-2.7!skip plugins/module_utils/__init__.py compile-3.5!skip @@ -45,8 +45,6 @@ plugins/module_utils/client/discovery.py import-3.8!skip plugins/module_utils/client/discovery.py import-3.9!skip plugins/module_utils/client/discovery.py import-3.10!skip plugins/module_utils/client/discovery.py metaclass-boilerplate!skip -molecule/default/roles/k8scopy/library/kubectl_file_compare.py shebang -molecule/default/roles/k8scopy/library/k8s_create_file.py shebang plugins/module_utils/client/resource.py import-2.6!skip plugins/module_utils/client/resource.py import-2.7!skip plugins/module_utils/client/resource.py import-3.5!skip @@ -240,4 +238,4 @@ plugins/modules/k8s_cp.py compile-2.7!skip plugins/modules/k8s_cp.py import-2.6!skip plugins/modules/k8s_cp.py import-2.7!skip plugins/module_utils/selector.py future-import-boilerplate!skip -plugins/module_utils/selector.py metaclass-boilerplate!skip \ No newline at end of file +plugins/module_utils/selector.py metaclass-boilerplate!skip diff --git a/tests/sanity/ignore-2.13.txt b/tests/sanity/ignore-2.13.txt index b611ae99..e62b64d3 100644 --- a/tests/sanity/ignore-2.13.txt +++ b/tests/sanity/ignore-2.13.txt @@ -1,8 +1,8 @@ -molecule/default/files/deployment.yaml yamllint!skip -molecule/default/roles/helm/files/appversionless-chart-v2/templates/configmap.yaml yamllint!skip -molecule/default/roles/helm/files/appversionless-chart/templates/configmap.yaml yamllint!skip -molecule/default/roles/helm/files/test-chart-v2/templates/configmap.yaml yamllint!skip -molecule/default/roles/helm/files/test-chart/templates/configmap.yaml yamllint!skip +tests/integration/targets/k8s_scale/files/deployment.yaml yamllint!skip +tests/integration/targets/helm/files/appversionless-chart-v2/templates/configmap.yaml yamllint!skip +tests/integration/targets/helm/files/appversionless-chart/templates/configmap.yaml yamllint!skip +tests/integration/targets/helm/files/test-chart-v2/templates/configmap.yaml yamllint!skip +tests/integration/targets/helm/files/test-chart/templates/configmap.yaml yamllint!skip plugins/module_utils/__init__.py compile-2.7!skip plugins/module_utils/__init__.py compile-3.5!skip plugins/module_utils/__init__.py future-import-boilerplate!skip @@ -36,8 +36,6 @@ plugins/module_utils/client/discovery.py import-3.8!skip plugins/module_utils/client/discovery.py import-3.9!skip plugins/module_utils/client/discovery.py import-3.10!skip plugins/module_utils/client/discovery.py metaclass-boilerplate!skip -molecule/default/roles/k8scopy/library/kubectl_file_compare.py shebang -molecule/default/roles/k8scopy/library/k8s_create_file.py shebang plugins/module_utils/client/resource.py import-2.7!skip plugins/module_utils/client/resource.py import-3.5!skip plugins/module_utils/client/resource.py import-3.6!skip diff --git a/tests/sanity/ignore-2.9.txt b/tests/sanity/ignore-2.9.txt index 56c60dff..1a68f5bf 100644 --- a/tests/sanity/ignore-2.9.txt +++ b/tests/sanity/ignore-2.9.txt @@ -1,8 +1,8 @@ -molecule/default/files/deployment.yaml yamllint!skip -molecule/default/roles/helm/files/appversionless-chart-v2/templates/configmap.yaml yamllint!skip -molecule/default/roles/helm/files/appversionless-chart/templates/configmap.yaml yamllint!skip -molecule/default/roles/helm/files/test-chart-v2/templates/configmap.yaml yamllint!skip -molecule/default/roles/helm/files/test-chart/templates/configmap.yaml yamllint!skip +tests/integration/targets/k8s_scale/files/deployment.yaml yamllint!skip +tests/integration/targets/helm/files/appversionless-chart-v2/templates/configmap.yaml yamllint!skip +tests/integration/targets/helm/files/appversionless-chart/templates/configmap.yaml yamllint!skip +tests/integration/targets/helm/files/test-chart-v2/templates/configmap.yaml yamllint!skip +tests/integration/targets/helm/files/test-chart/templates/configmap.yaml yamllint!skip plugins/module_utils/__init__.py compile-2.6!skip plugins/module_utils/__init__.py compile-2.7!skip plugins/module_utils/__init__.py compile-3.5!skip @@ -226,18 +226,18 @@ plugins/modules/k8s_service.py validate-modules:parameter-type-not-in-doc tests/sanity/refresh_ignore_files shebang!skip tests/unit/module_utils/test_discoverer.py future-import-boilerplate!skip tests/unit/module_utils/test_discoverer.py metaclass-boilerplate!skip -molecule/default/roles/k8scopy/library/kubectl_file_compare.py shebang -molecule/default/roles/k8scopy/library/k8s_create_file.py shebang plugins/modules/k8s_cp.py compile-2.6!skip plugins/modules/k8s_cp.py compile-2.7!skip plugins/modules/k8s_cp.py import-2.6!skip plugins/modules/k8s_cp.py import-2.7!skip -molecule/default/roles/k8scopy/library/kubectl_file_compare.py compile-2.6!skip -molecule/default/roles/k8scopy/library/kubectl_file_compare.py compile-2.7!skip -molecule/default/roles/k8scopy/library/kubectl_file_compare.py compile-3.5!skip +tests/integration/targets/k8s_copy/library/kubectl_file_compare.py compile-2.6!skip +tests/integration/targets/k8s_copy/library/kubectl_file_compare.py compile-2.7!skip +tests/integration/targets/k8s_copy/library/kubectl_file_compare.py compile-3.5!skip tests/unit/module_utils/test_selector.py future-import-boilerplate!skip tests/unit/module_utils/test_selector.py metaclass-boilerplate!skip plugins/module_utils/selector.py future-import-boilerplate!skip plugins/module_utils/selector.py metaclass-boilerplate!skip plugins/lookup/kustomize.py future-import-boilerplate!skip -plugins/lookup/kustomize.py metaclass-boilerplate!skip \ No newline at end of file +plugins/lookup/kustomize.py metaclass-boilerplate!skip +tests/integration/targets/helm/library/helm_test_version.py metaclass-boilerplate!skip +tests/integration/targets/helm/library/helm_test_version.py future-import-boilerplate!skip diff --git a/tox.ini b/tox.ini index 7d71a811..50273ef6 100644 --- a/tox.ini +++ b/tox.ini @@ -2,21 +2,6 @@ minversion = 1.4.2 skipsdist = True -[testenv:molecule] -install_command = pip install {opts} {packages} - -deps = ansible - kubernetes - molecule<3.3.0 - yamllint - flake8 - -passenv = - HOME - -commands= - {posargs} - [testenv:integration] install_command = pip install {opts} {packages} @@ -28,7 +13,7 @@ passenv = HOME commands= - ansible-test integration --docker -v --color --retry-on-error --diff --coverage --continue-on-error --python {posargs} + ansible-test integration -v --color --retry-on-error --diff --coverage --continue-on-error --python {posargs} [testenv:add_docs] deps = git+https://github.com/ansible-network/collection_prep