From 2ad8c0cf1b1b96b880bcc19ba9d89c0360a145f9 Mon Sep 17 00:00:00 2001 From: aubin bikouo Date: Wed, 15 Jan 2025 16:42:24 +0100 Subject: [PATCH 1/2] Fixing lint issues --- .../targets/k8s_validate/tasks/main.yml | 103 ++++++++------- .../targets/k8s_waiter/tasks/main.yml | 122 +++++++++--------- .../targets/lookup_k8s/tasks/main.yml | 70 +++++----- .../targets/lookup_kustomize/tasks/main.yml | 51 ++++---- .../push_to_helm_registry/tasks/main.yml | 10 +- .../setup_helm_registry/handlers/main.yml | 2 +- .../setup_helm_registry/tasks/main.yml | 6 +- .../tasks/remove_docker_container.yml | 6 +- .../tasks/teardown_registry.yml | 2 +- .../targets/setup_kubeconfig/tasks/main.yml | 29 +++-- .../targets/setup_namespace/tasks/main.yml | 6 +- 11 files changed, 218 insertions(+), 189 deletions(-) diff --git a/tests/integration/targets/k8s_validate/tasks/main.yml b/tests/integration/targets/k8s_validate/tasks/main.yml index b7b99c18a2..fb9fc199cc 100644 --- a/tests/integration/targets/k8s_validate/tasks/main.yml +++ b/tests/integration/targets/k8s_validate/tasks/main.yml @@ -1,21 +1,25 @@ - block: - name: Create temp directory - tempfile: + ansible.builtin.tempfile: state: directory suffix: .test register: remote_tmp_dir - - set_fact: + - name: Set remote_tmp_dir variable + ansible.builtin.set_fact: remote_tmp_dir: "{{ remote_tmp_dir.path }}" - - set_fact: + - name: Set virtualenv variables + ansible.builtin.set_fact: virtualenv: "{{ remote_tmp_dir }}/virtualenv" virtualenv_command: "virtualenv --python {{ ansible_python_interpreter }}" - - set_fact: + - name: Set virtualenv interpreter variable + ansible.builtin.set_fact: virtualenv_interpreter: "{{ virtualenv }}/bin/python" - - pip: + - name: Install python libraries + ansible.builtin.pip: name: - kubernetes virtualenv: "{{ virtualenv }}" @@ -23,7 +27,7 @@ virtualenv_site_packages: false - name: Validate should fail gracefully without kubernetes-validate - k8s: + kubernetes.core.k8s: definition: &cmap apiVersion: v1 kind: ConfigMap @@ -34,21 +38,24 @@ mykey: myval validate: fail_on_error: true - ignore_errors: true + failed_when: false register: k8s_no_validate vars: ansible_python_interpreter: "{{ virtualenv_interpreter }}" - - assert: + - name: Validate that module failed + ansible.builtin.assert: that: - k8s_no_validate is failed - "'Failed to import the required Python library (kubernetes-validate)' in k8s_no_validate.msg" - - file: + - name: Delete virtualenv path + ansible.builtin.file: path: "{{ virtualenv }}" state: absent - - pip: + - name: Install python libraries + ansible.builtin.pip: name: - kubernetes - kubernetes-validate @@ -57,15 +64,16 @@ virtualenv_command: "{{ virtualenv_command }}" virtualenv_site_packages: false - - block: + - name: Test with new virtualenv + block: - name: Simple ConfigMap should validate - k8s: + kubernetes.core.k8s: definition: *cmap validate: fail_on_error: true - name: ConfigMap with extra properties should validate without strict - k8s: + kubernetes.core.k8s: definition: <<: *cmap extra: stuff @@ -74,23 +82,24 @@ strict: false - name: ConfigMap with extra properties should not validate with strict - k8s: + kubernetes.core.k8s: definition: <<: *cmap extra: stuff validate: fail_on_error: true strict: true - ignore_errors: true + failed_when: false register: result - - assert: + - name: Ensure module failed + ansible.builtin.assert: that: - result is failed - "\"('extra' was unexpected)\" is in result.msg" - name: Property with invalid type should fail with strict - k8s: + kubernetes.core.k8s: definition: apiVersion: apps/v1 kind: Deployment @@ -115,17 +124,18 @@ validate: fail_on_error: true strict: false - ignore_errors: true + failed_when: false register: result - - assert: + - name: Ensure module failed + ansible.builtin.assert: that: - result is failed - result.status is not defined - "\"'lots' is not of type 'integer'\" in result.msg" - name: Create CRD - k8s: + kubernetes.core.k8s: definition: &crd apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition @@ -149,11 +159,12 @@ singular: foobar kind: Foobar - - pause: + - name: Pause for 5 seconds + ansible.builtin.pause: seconds: 5 - name: Adding CRD should succeed with warning - k8s: + kubernetes.core.k8s: definition: apiVersion: example.com/v1 kind: Foobar @@ -166,68 +177,72 @@ strict: true register: result - - assert: + - name: Validate module success + ansible.builtin.assert: that: - result is successful - "'warnings' in result" vars: ansible_python_interpreter: "{{ virtualenv_interpreter }}" - - name: stat default kube config - stat: + - name: Stat default kube config + ansible.builtin.stat: path: "~/.kube/config" register: _stat - - name: validate that in-memory kubeconfig authentication failed for kubernetes < 17.17.0 + - name: Validate that in-memory kubeconfig authentication failed for kubernetes < 17.17.0 + when: + - _stat.stat.exists + - _stat.stat.readable + - _stat.stat.isreg block: - - set_fact: + - name: Set virtualenv vars + ansible.builtin.set_fact: virtualenv_kubeconfig: "{{ remote_tmp_dir }}/kubeconfig" - - pip: + - name: Install python libraries + ansible.builtin.pip: name: - "kubernetes<17.17.0" virtualenv: "{{ virtualenv_kubeconfig }}" virtualenv_command: "{{ virtualenv_command }}" virtualenv_site_packages: false - - name: list namespace using in-memory kubeconfig - k8s_info: + - name: List namespace using in-memory kubeconfig + kubernetes.core.k8s_info: kind: Namespace kubeconfig: "{{ lookup('file', '~/.kube/config') | from_yaml }}" register: _result - ignore_errors: true + failed_when: false vars: ansible_python_interpreter: "{{ virtualenv_kubeconfig }}/bin/python" - - name: assert that task failed with proper message - assert: + - name: Assert that task failed with proper message + ansible.builtin.assert: that: - '"This is required to use in-memory config." in _result.msg' - when: - - _stat.stat.exists - - _stat.stat.readable - - _stat.stat.isreg + always: - name: Remove temp directory - file: + ansible.builtin.file: path: "{{ remote_tmp_dir }}" state: absent - ignore_errors: true + failed_when: false - name: Remove namespace - k8s: + kubernetes.core.k8s: kind: Namespace name: "{{ validate_namespace }}" state: absent - ignore_errors: true + failed_when: false - name: Remove CRD - k8s: + kubernetes.core.k8s: definition: *crd state: absent - ignore_errors: true + failed_when: false vars: validate_namespace: "{{ test_namespace }}" environment: - ENABLE_TURBO_MODE: false + ENABLE_TURBO_MODE: "false" diff --git a/tests/integration/targets/k8s_waiter/tasks/main.yml b/tests/integration/targets/k8s_waiter/tasks/main.yml index 3bfb668bb6..2869314f82 100644 --- a/tests/integration/targets/k8s_waiter/tasks/main.yml +++ b/tests/integration/targets/k8s_waiter/tasks/main.yml @@ -1,11 +1,13 @@ --- -- block: - - set_fact: +- name: Run k8s_waiter tests + block: + - name: Set test variables + ansible.builtin.set_fact: wait_namespace: "{{ test_namespace }}" k8s_wait_timeout: 400 - name: Add a simple pod - k8s: + kubernetes.core.k8s: definition: apiVersion: v1 kind: Pod @@ -23,7 +25,7 @@ - "10000" - name: Add a daemonset - k8s: + kubernetes.core.k8s: definition: apiVersion: apps/v1 kind: DaemonSet @@ -47,12 +49,12 @@ register: ds - name: Check that daemonset wait worked - assert: + ansible.builtin.assert: that: - ds.result.status.currentNumberScheduled == ds.result.status.desiredNumberScheduled - name: Update a daemonset in check_mode - k8s: + kubernetes.core.k8s: definition: apiVersion: apps/v1 kind: DaemonSet @@ -66,7 +68,7 @@ updateStrategy: type: RollingUpdate template: "{{ k8s_pod_template }}" - wait: yes + wait: true wait_sleep: 3 wait_timeout: 180 vars: @@ -76,16 +78,16 @@ - sleep - "600" register: update_ds_check_mode - check_mode: yes + check_mode: true - name: Check that check_mode result contains the changes - assert: + ansible.builtin.assert: that: - update_ds_check_mode is changed - "update_ds_check_mode.result.spec.template.spec.containers[0].image == 'gcr.io/kuar-demo/kuard-amd64:2'" - name: Update a daemonset - k8s: + kubernetes.core.k8s: definition: apiVersion: apps/v1 kind: DaemonSet @@ -99,7 +101,7 @@ updateStrategy: type: RollingUpdate template: "{{ k8s_pod_template }}" - wait: yes + wait: true wait_sleep: 3 wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" vars: @@ -111,7 +113,7 @@ register: ds - name: Get updated pods - k8s_info: + kubernetes.core.k8s_info: api_version: v1 kind: Pod namespace: "{{ wait_namespace }}" @@ -122,13 +124,13 @@ register: updated_ds_pods - name: Check that daemonset wait worked - assert: + ansible.builtin.assert: that: - ds.result.status.currentNumberScheduled == ds.result.status.desiredNumberScheduled - updated_ds_pods.resources[0].spec.containers[0].image.endswith(":3") - name: Create daemonset with nodeSelector and not existing label - k8s: + kubernetes.core.k8s: definition: apiVersion: apps/v1 kind: DaemonSet @@ -140,7 +142,7 @@ matchLabels: app: "{{ k8s_pod_name }}" template: "{{ k8s_pod_template }}" - wait: yes + wait: true wait_sleep: 5 wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" vars: @@ -154,7 +156,7 @@ register: ds_not_existing_label - name: Get updated pods - k8s_info: + kubernetes.core.k8s_info: api_version: v1 kind: Pod namespace: "{{ wait_namespace }}" @@ -163,14 +165,14 @@ register: updated_ds_pods_not_existing_label - name: Check that daemonset wait worked (when desired number is 0) - assert: + ansible.builtin.assert: that: - ds_not_existing_label.result.status.currentNumberScheduled == ds_not_existing_label.result.status.desiredNumberScheduled - ds_not_existing_label.result.status.desiredNumberScheduled == 0 - updated_ds_pods_not_existing_label.resources | length == 0 - name: Add a statefulset - k8s: + kubernetes.core.k8s: definition: apiVersion: apps/v1 kind: StatefulSet @@ -182,7 +184,7 @@ matchLabels: app: "{{ k8s_pod_name }}" template: "{{ k8s_pod_template }}" - wait: yes + wait: true wait_sleep: 5 wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" vars: @@ -194,12 +196,12 @@ register: sts - name: Check that statefulset wait worked - assert: + ansible.builtin.assert: that: - sts.result.spec.replicas == sts.result.status.readyReplicas - name: Update a statefulset in check_mode - k8s: + kubernetes.core.k8s: definition: apiVersion: apps/v1 kind: StatefulSet @@ -213,7 +215,7 @@ updateStrategy: type: RollingUpdate template: "{{ k8s_pod_template }}" - wait: yes + wait: true wait_sleep: 3 wait_timeout: 180 vars: @@ -223,16 +225,16 @@ - sleep - "600" register: update_sts_check_mode - check_mode: yes + check_mode: true - name: Check that check_mode result contains the changes - assert: + ansible.builtin.assert: that: - update_sts_check_mode is changed - "update_sts_check_mode.result.spec.template.spec.containers[0].image == 'gcr.io/kuar-demo/kuard-amd64:2'" - name: Update a statefulset - k8s: + kubernetes.core.k8s: definition: apiVersion: apps/v1 kind: StatefulSet @@ -246,7 +248,7 @@ updateStrategy: type: RollingUpdate template: "{{ k8s_pod_template }}" - wait: yes + wait: true wait_sleep: 3 wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" vars: @@ -258,7 +260,7 @@ register: sts - name: Get updated pods - k8s_info: + kubernetes.core.k8s_info: api_version: v1 kind: Pod namespace: "{{ wait_namespace }}" @@ -269,13 +271,13 @@ register: updated_sts_pods - name: Check that statefulset wait worked - assert: + ansible.builtin.assert: that: - sts.result.spec.replicas == sts.result.status.readyReplicas - updated_sts_pods.resources[0].spec.containers[0].image.endswith(":3") - name: Add a crashing pod - k8s: + kubernetes.core.k8s: definition: apiVersion: v1 kind: Pod @@ -283,7 +285,7 @@ name: "{{ k8s_pod_name }}" namespace: "{{ wait_namespace }}" spec: "{{ k8s_pod_spec }}" - wait: yes + wait: true wait_sleep: 1 wait_timeout: 30 vars: @@ -292,7 +294,7 @@ k8s_pod_command: - /bin/false register: crash_pod - ignore_errors: yes + failed_when: crash_pod is successful - name: Check that task failed assert: @@ -300,7 +302,7 @@ - crash_pod is failed - name: Use a non-existent image - k8s: + kubernetes.core.k8s: definition: apiVersion: v1 kind: Pod @@ -308,22 +310,22 @@ name: "{{ k8s_pod_name }}" namespace: "{{ wait_namespace }}" spec: "{{ k8s_pod_spec }}" - wait: yes + wait: true wait_sleep: 1 wait_timeout: 30 vars: k8s_pod_name: wait-no-image-pod k8s_pod_image: i_made_this_up:and_this_too register: no_image_pod - ignore_errors: yes + failed_when: no_image_pod is successful - name: Check that task failed - assert: + ansible.builtin.assert: that: - no_image_pod is failed - name: Add a deployment - k8s: + kubernetes.core.k8s: definition: apiVersion: apps/v1 kind: Deployment @@ -336,7 +338,7 @@ matchLabels: app: "{{ k8s_pod_name }}" template: "{{ k8s_pod_template }}" - wait: yes + wait: true wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" vars: k8s_pod_name: wait-deploy @@ -345,16 +347,15 @@ - containerPort: 8080 name: http protocol: TCP - register: deploy - name: Check that deployment wait worked - assert: + ansible.builtin.assert: that: - deploy.result.status.availableReplicas == deploy.result.status.replicas - name: Update a deployment - k8s: + kubernetes.core.k8s: definition: apiVersion: apps/v1 kind: Deployment @@ -367,7 +368,7 @@ matchLabels: app: "{{ k8s_pod_name }}" template: "{{ k8s_pod_template }}" - wait: yes + wait: true wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" vars: k8s_pod_name: wait-deploy @@ -381,11 +382,11 @@ # It looks like the Deployment is updated to have the desired state *before* the pods are terminated # Wait a couple of seconds to allow the old pods to at least get to Terminating state - name: Avoid race condition - pause: + ansible.builtin.pause: seconds: 2 - name: Get updated pods - k8s_info: + kubernetes.core.k8s_info: api_version: v1 kind: Pod namespace: "{{ wait_namespace }}" @@ -399,12 +400,12 @@ delay: 5 - name: Check that deployment wait worked - assert: + ansible.builtin.assert: that: - deploy.result.status.availableReplicas == deploy.result.status.replicas - name: Pause a deployment - k8s: + kubernetes.core.k8s: definition: apiVersion: apps/v1 kind: Deployment @@ -413,8 +414,8 @@ namespace: "{{ wait_namespace }}" spec: paused: True - apply: no - wait: yes + apply: false + wait: true wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" wait_condition: type: Progressing @@ -423,7 +424,7 @@ register: pause_deploy - name: Check that paused deployment wait worked - assert: + ansible.builtin.assert: that: - condition.reason == "DeploymentPaused" - condition.status == "Unknown" @@ -431,7 +432,7 @@ condition: '{{ pause_deploy.result.status.conditions[1] }}' - name: Add a service based on the deployment - k8s: + kubernetes.core.k8s: definition: apiVersion: v1 kind: Service @@ -445,19 +446,19 @@ - port: 8080 targetPort: 8080 protocol: TCP - wait: yes + wait: true wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" vars: k8s_pod_name: wait-deploy register: service - name: Assert that waiting for service works - assert: + ansible.builtin.assert: that: - service is successful - name: Add a crashing deployment - k8s: + kubernetes.core.k8s: definition: apiVersion: apps/v1 kind: Deployment @@ -470,7 +471,7 @@ matchLabels: app: "{{ k8s_pod_name }}" template: "{{ k8s_pod_template }}" - wait: yes + wait: true wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" vars: k8s_pod_name: wait-crash-deploy @@ -478,35 +479,34 @@ k8s_pod_command: - /bin/false register: wait_crash_deploy - ignore_errors: yes + failed_when: wait_crash_deploy is successful - name: Check that task failed - assert: + ansible.builtin.assert: that: - wait_crash_deploy is failed - name: Remove Pod with very short timeout - k8s: + kubernetes.core.k8s: api_version: v1 kind: Pod name: wait-pod namespace: "{{ wait_namespace }}" state: absent - wait: yes + wait: true wait_sleep: 2 wait_timeout: 5 - ignore_errors: yes + failed_when: short_wait_remove_pod is successful register: short_wait_remove_pod - name: Check that task failed - assert: + ansible.builtin.assert: that: - short_wait_remove_pod is failed always: - name: Remove namespace - k8s: + kubernetes.core.k8s: kind: Namespace name: "{{ wait_namespace }}" state: absent - ignore_errors: yes diff --git a/tests/integration/targets/lookup_k8s/tasks/main.yml b/tests/integration/targets/lookup_k8s/tasks/main.yml index 12911a0b59..8f698cb5e7 100644 --- a/tests/integration/targets/lookup_k8s/tasks/main.yml +++ b/tests/integration/targets/lookup_k8s/tasks/main.yml @@ -1,6 +1,8 @@ --- -- block: - - set_fact: +- name: Run lookup_k8s tests + block: + - name: Set common variables + ansible.builtin.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=test_namespace[0]) }}" pre_test3: "{{ query('kubernetes.core.k8s', kind='Namespace', label_selector='namespace_label=app_development') }}" @@ -19,7 +21,8 @@ labels: namespace_label: "app_development" - - set_fact: + - name: Set test variables + ansible.builtin.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=test_namespace[0], wantlist=True) }}" @@ -28,12 +31,13 @@ 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: + - name: Set test8 variable + ansible.builtin.set_fact: test8: "{{ lookup('kubernetes.core.k8s', kind='Ingress', api_version='networking.k8s.io/vINVALID') }}" ignore_errors: true - name: Assert that every test is passed - assert: + ansible.builtin.assert: that: # Before creating object - pre_test1 is sequence and pre_test1 is not string @@ -70,7 +74,8 @@ labels: namespace_label: "app_development" - - set_fact: + - name: Set test variables + ansible.builtin.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=test_namespace[0], wantlist=True) }}" @@ -82,7 +87,7 @@ test9: "{{ lookup('kubernetes.core.k8s', kind='Namespace', resource_name=test_namespace[1]) }}" - name: Assert that every test is passed after creating second object - assert: + ansible.builtin.assert: that: # After creating second object - test1 is sequence and test1 is not string @@ -104,11 +109,13 @@ - test9 is mapping # test using resource_definition - - k8s: + - name: Test using resource_definition + kubernetes.core.k8s: name: "{{ test_namespace[2] }}" kind: Namespace - - set_fact: + - name: Set configmap_def variable + ansible.builtin.set_fact: configmap_def: apiVersion: v1 kind: ConfigMap @@ -119,15 +126,15 @@ value: "{{ configmap_data }}" - name: Create simple configmap - k8s: + kubernetes.core.k8s: definition: "{{ configmap_def }}" - name: Retrieve configmap using resource_definition parameter - set_fact: + ansible.builtin.set_fact: result_configmap: "{{ lookup('kubernetes.core.k8s', resource_definition=configmap_def) }}" - name: Validate configmap result - assert: + ansible.builtin.assert: that: - result_configmap.apiVersion == 'v1' - result_configmap.metadata.name == configmap_name @@ -135,14 +142,15 @@ - result_configmap.data.value == configmap_data # test lookup plugin using src parameter - - block: + - name: Test lookup plugin using src parameter + block: - name: Create temporary file to store content - tempfile: + ansible.builtin.tempfile: suffix: ".yaml" register: tmpfile - name: Copy content into file - copy: + ansible.builtin.copy: content: | kind: ConfigMap apiVersion: v1 @@ -152,11 +160,11 @@ dest: "{{ tmpfile.path }}" - name: Retrieve configmap using src parameter - set_fact: + ansible.builtin.set_fact: src_configmap: "{{ lookup('kubernetes.core.k8s', src=tmpfile.path) }}" - name: Validate configmap result - assert: + ansible.builtin.assert: that: - src_configmap.apiVersion == 'v1' - src_configmap.metadata.name == configmap_name @@ -165,37 +173,38 @@ always: - name: Delete temporary file created - file: + ansible.builtin.file: state: absent path: "{{ tmpfile.path }}" - ignore_errors: true # test using aliases for user authentication - block: - name: Create temporary directory to save user credentials - tempfile: + ansible.builtin.tempfile: state: directory suffix: ".config" register: tmpdir - - include_role: + - name: Include role 'setup_kubeconfig' + ansible.builtin.include_role: name: setup_kubeconfig vars: user_credentials_dir: "{{ tmpdir.path }}" kubeconfig_operation: "save" - - set_fact: + - name: Set cluster variables + ansible.builtin.set_fact: cluster_host: "{{ lookup('file', tmpdir.path + '/host_data.txt') }}" user_cert_file: "{{ tmpdir.path }}/cert_file_data.txt" user_key_file: "{{ tmpdir.path }}/key_file_data.txt" ssl_ca_cert: "{{ tmpdir.path }}/ssl_ca_cert_data.txt" - name: Retrieve configmap using authentication aliases (validate_certs=false) - set_fact: + ansible.builtin.set_fact: configmap_no_ssl: "{{ lookup('kubernetes.core.k8s', host=cluster_host, cert_file=user_cert_file, key_file=user_key_file, verify_ssl=false, resource_definition=configmap_def) }}" - name: Validate configmap result - assert: + ansible.builtin.assert: that: - configmap_no_ssl.apiVersion == 'v1' - configmap_no_ssl.metadata.name == configmap_name @@ -203,11 +212,11 @@ - configmap_no_ssl.data.value == configmap_data - name: Retrieve configmap using authentication aliases (validate_certs=true) - set_fact: + ansible.builtin.set_fact: configmap_with_ssl: "{{ lookup('kubernetes.core.k8s', host=cluster_host, cert_file=user_cert_file, key_file=user_key_file, ssl_ca_cert=ssl_ca_cert, verify_ssl=true, resource_definition=configmap_def) }}" - name: Validate configmap result - assert: + ansible.builtin.assert: that: - configmap_with_ssl.apiVersion == 'v1' - configmap_with_ssl.metadata.name == configmap_name @@ -216,12 +225,12 @@ always: - name: Delete temporary directory - file: + ansible.builtin.file: state: absent path: "{{ tmpdir.path }}" - ignore_errors: true - - include_role: + - name: Include role 'setup_kubeconfig' + ansible.builtin.include_role: name: setup_kubeconfig ignore_errors: true vars: @@ -229,7 +238,7 @@ always: - name: Ensure that namespace is removed - k8s: + kubernetes.core.k8s: kind: Namespace name: "app-development-{{ item }}" state: absent @@ -237,4 +246,3 @@ - one - two - three - ignore_errors: true diff --git a/tests/integration/targets/lookup_kustomize/tasks/main.yml b/tests/integration/targets/lookup_kustomize/tasks/main.yml index dafdcdd0bd..e6735779ea 100644 --- a/tests/integration/targets/lookup_kustomize/tasks/main.yml +++ b/tests/integration/targets/lookup_kustomize/tasks/main.yml @@ -1,30 +1,33 @@ --- - block: - - set_fact: + - name: Set kustomize_ns variable + ansible.builtin.set_fact: kustomize_ns: "{{ test_namespace }}" - - name: create environment for test + - name: Create environment for test block: - name: Create temp directory - tempfile: + ansible.builtin.tempfile: state: directory suffix: .test register: _tmp_dir - - set_fact: + - name: Set tmp_dir_path variable + ansible.builtin.set_fact: tmp_dir_path: "{{ _tmp_dir.path }}" - - set_fact: + - name: Set kustomize_dir variable + ansible.builtin.set_fact: kustomize_dir: "{{ tmp_dir_path }}/kustomization" - - name: create kustomize directory - file: + - name: Create kustomize directory + ansible.builtin.file: path: "{{ kustomize_dir }}" state: directory - - name: create kustomization file - copy: + - name: Create kustomization file + ansible.builtin.copy: content: '{{ item.content }}' dest: '{{ item.dest }}' with_items: @@ -37,23 +40,24 @@ - content: "project=ansible" dest: "{{ kustomize_dir }}/data.properties" - - name: copy script to install kustomize - get_url: + - name: Copy script to install kustomize + ansible.builtin.get_url: url: https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh dest: "{{ tmp_dir_path }}" - - name: make script as executable - file: + - name: Make script as executable + ansible.builtin.file: path: "{{ tmp_dir_path }}/install_kustomize.sh" mode: 0755 - name: Install kustomize - command: "{{ tmp_dir_path }}/install_kustomize.sh" + ansible.builtin.command: "{{ tmp_dir_path }}/install_kustomize.sh" args: chdir: "{{ tmp_dir_path }}" register: _install - - set_fact: + - name: Set kustomize variables + ansible.builtin.set_fact: kustomize_binary: "{{ _install.stdout | regex_search('kustomize installed to (.*)', '\\1') | list | join('') }}" kubectl_release: "v1.22.0" kubectl_binary: "{{ tmp_dir_path }}/kubectl" @@ -77,32 +81,31 @@ - "{{ kubectl_binary }}" - name: Run lookup using kustomize binary - set_fact: + ansible.builtin.set_fact: resource_kustomize: "{{ lookup('kubernetes.core.kustomize', binary_path=kustomize_binary, dir=kustomize_dir) }}" - name: Run lookup using kubectl binary - set_fact: + ansible.builtin.set_fact: resource_kubectl: "{{ lookup('kubernetes.core.kustomize', binary_path=kubectl_binary, dir=kustomize_dir) }}" - - name: assert output are the same - assert: + - name: Assert output are the same + ansible.builtin.assert: that: - resource_kubectl == resource_kustomize - - name: create kubernetes resource using lookup plugin - k8s: + - name: Create kubernetes resource using lookup plugin + kubernetes.core.k8s: namespace: "{{ kustomize_ns }}" definition: "{{ lookup('kubernetes.core.kustomize', dir=kustomize_dir, opt_dirs=tmp_dir_path) }}" always: - name: Delete namespace - k8s: + kubernetes.core.k8s: kind: Namespace name: "{{ kustomize_ns }}" state: absent - ignore_errors: true - name: Delete temporary directory - file: + ansible.builtin.file: state: absent path: "{{ tmp_dir_path }}" diff --git a/tests/integration/targets/push_to_helm_registry/tasks/main.yml b/tests/integration/targets/push_to_helm_registry/tasks/main.yml index d3dfda18f8..800a68a200 100644 --- a/tests/integration/targets/push_to_helm_registry/tasks/main.yml +++ b/tests/integration/targets/push_to_helm_registry/tasks/main.yml @@ -1,6 +1,6 @@ --- - name: Ensure we can log into the helm registry - command: >- + ansible.builtin.command: >- {{ helm_binary_path }} registry login -u {{ chart_repo_username }} -p {{ chart_repo_password }} @@ -15,7 +15,7 @@ register: _tmpfile - name: Package helm chart - command: '{{ helm_binary_path }} package {{ chart_local_path }} --destination {{ _tmpfile.path }}' + ansible.builtin.command: '{{ helm_binary_path }} package {{ chart_local_path }} --destination {{ _tmpfile.path }}' - name: Locate helm chart package ansible.builtin.find: @@ -24,15 +24,13 @@ register: _files - name: Helm push chart to the registry - command: '{{ helm_binary_path }} push {{ _files.files.0.path }} oci://{{ chart_repo_url }}/{{ chart_repo_path }}' + ansible.builtin.command: '{{ helm_binary_path }} push {{ _files.files.0.path }} oci://{{ chart_repo_url }}/{{ chart_repo_path }}' always: - name: Logout from registry - command: '{{ helm_binary_path }} registry logout {{ chart_repo_url }}' - ignore_errors: true + ansible.builtin.command: '{{ helm_binary_path }} registry logout {{ chart_repo_url }}' - name: Delete temporary directory ansible.builtin.file: state: absent path: '{{ _tmpfile.path }}' - ignore_errors: true diff --git a/tests/integration/targets/setup_helm_registry/handlers/main.yml b/tests/integration/targets/setup_helm_registry/handlers/main.yml index 92bbe5978b..4eadc9e7b3 100644 --- a/tests/integration/targets/setup_helm_registry/handlers/main.yml +++ b/tests/integration/targets/setup_helm_registry/handlers/main.yml @@ -1,3 +1,3 @@ --- - name: Teardown registry - include_tasks: teardown_registry.yml + ansible.builtin.include_tasks: teardown_registry.yml diff --git a/tests/integration/targets/setup_helm_registry/tasks/main.yml b/tests/integration/targets/setup_helm_registry/tasks/main.yml index 4c84898680..926e44e27f 100644 --- a/tests/integration/targets/setup_helm_registry/tasks/main.yml +++ b/tests/integration/targets/setup_helm_registry/tasks/main.yml @@ -4,7 +4,7 @@ cmd: docker ps - name: Create temporary directory to store file in - tempfile: + ansible.builtin.tempfile: state: directory suffix: .helm_registry register: _tmpfile @@ -12,7 +12,7 @@ # - Teardown registry - name: Create authentication file - copy: + ansible.builtin.copy: content: "{{ registry_credentials }}" dest: "{{ _tmpfile.path }}/htpasswd" @@ -20,7 +20,7 @@ ansible.builtin.include_tasks: remove_docker_container.yml - name: Create registry container - command: >- + ansible.builtin.command: >- docker run -d -p {{ registry_port }}:5000 --restart=always diff --git a/tests/integration/targets/setup_helm_registry/tasks/remove_docker_container.yml b/tests/integration/targets/setup_helm_registry/tasks/remove_docker_container.yml index 77d33b61ac..4e60da589d 100644 --- a/tests/integration/targets/setup_helm_registry/tasks/remove_docker_container.yml +++ b/tests/integration/targets/setup_helm_registry/tasks/remove_docker_container.yml @@ -1,6 +1,6 @@ --- - name: Inspect docker container - command: docker container inspect {{ registry_name }} -f '{{ '{{' }} .State.Running {{ '}}' }}' + ansible.builtin.command: docker container inspect {{ registry_name }} -f '{{ '{{' }} .State.Running {{ '}}' }}' register: _inspect ignore_errors: true @@ -8,8 +8,8 @@ when: _inspect.rc == 0 block: - name: Stop running container - command: docker container stop {{ registry_name }} + ansible.builtin.command: docker container stop {{ registry_name }} when: _inspect.stdout == "true" - name: Remove container - command: docker container rm {{ registry_name }} + ansible.builtin.command: docker container rm {{ registry_name }} diff --git a/tests/integration/targets/setup_helm_registry/tasks/teardown_registry.yml b/tests/integration/targets/setup_helm_registry/tasks/teardown_registry.yml index 7382a8bb00..f858df48f7 100644 --- a/tests/integration/targets/setup_helm_registry/tasks/teardown_registry.yml +++ b/tests/integration/targets/setup_helm_registry/tasks/teardown_registry.yml @@ -3,7 +3,7 @@ ansible.builtin.include_tasks: remove_docker_container.yml - name: Delete temporary directory - file: + ansible.builtin.file: state: absent path: '{{ _tmpfile.path }}' ignore_errors: true diff --git a/tests/integration/targets/setup_kubeconfig/tasks/main.yml b/tests/integration/targets/setup_kubeconfig/tasks/main.yml index fcde7a72aa..2f284cacf5 100644 --- a/tests/integration/targets/setup_kubeconfig/tasks/main.yml +++ b/tests/integration/targets/setup_kubeconfig/tasks/main.yml @@ -1,46 +1,49 @@ --- -- fail: +- name: Fail when bad value for kubeconfig_operation + ansible.builtin.fail: msg: "kubeconfig_operation must be one of 'revert' or 'save'" when: kubeconfig_operation not in ["revert", "save"] -- set_fact: +- name: Set variables + ansible.builtin.set_fact: src_kubeconfig: "{{ (kubeconfig_operation == 'save') | ternary(kubeconfig_default_path, kubeconfig_custom_path) }}" dest_kubeconfig: "{{ (kubeconfig_operation == 'save') | ternary(kubeconfig_custom_path, kubeconfig_default_path) }}" -- name: check if source kubeconfig exists - stat: +- name: Check if source kubeconfig exists + ansible.builtin.stat: path: "{{ src_kubeconfig }}" register: _src -- name: check if destination kubeconfig exists - stat: +- name: Check if destination kubeconfig exists + ansible.builtin.stat: path: "{{ dest_kubeconfig }}" register: _dest -- fail: +- name: Fail if both variables are defined + ansible.builtin.fail: msg: "Both {{ src_kubeconfig }} and {{ dest_kubeconfig }} do not exist." when: - not _src.stat.exists - not _dest.stat.exists - name: Generate user cert_file, key_file, and hostname + when: user_credentials_dir is defined block: - name: Generate user credentials files test_inventory_read_credentials: kube_config: "{{ (_src.stat.exists) | ternary(src_kubeconfig, dest_kubeconfig) }}" dest_dir: "{{ user_credentials_dir }}" - when: user_credentials_dir is defined -- block: +- name: Copy kubeconfig into final path + when: _src.stat.exists + block: - name: "Copy {{ src_kubeconfig }} into {{ dest_kubeconfig }}" - copy: + ansible.builtin.copy: remote_src: true src: "{{ src_kubeconfig }}" dest: "{{ dest_kubeconfig }}" - name: "Delete {{ src_kubeconfig }}" - file: + ansible.builtin.file: state: absent path: "{{ src_kubeconfig }}" - - when: _src.stat.exists diff --git a/tests/integration/targets/setup_namespace/tasks/main.yml b/tests/integration/targets/setup_namespace/tasks/main.yml index 032444e104..5c3fefcbe8 100644 --- a/tests/integration/targets/setup_namespace/tasks/main.yml +++ b/tests/integration/targets/setup_namespace/tasks/main.yml @@ -1,12 +1,14 @@ --- -- include_tasks: tasks/create.yml +- name: Include 'tasks/create.yml' when test_namespace is a list + ansible.builtin.include_tasks: tasks/create.yml vars: namespace_to_create: "{{ item.name | default(item) }}" namespace_labels: "{{ item.labels | default(omit) }}" with_items: "{{ test_namespace }}" when: test_namespace | type_debug == "list" -- include_tasks: tasks/create.yml +- name: Include 'tasks/create.yml' when test_namespace is a single element + ansible.builtin.include_tasks: tasks/create.yml vars: namespace_to_create: "{{ test_namespace }}" namespace_labels: "{{ test_namespace_labels | default(omit) }}" From ec07bdd05ac8a6a2a6fdb79c65eee106fba1f99a Mon Sep 17 00:00:00 2001 From: aubin bikouo Date: Wed, 15 Jan 2025 18:28:41 +0100 Subject: [PATCH 2/2] some more linters --- .../targets/helm/tasks/install.yml | 6 +- tests/integration/targets/helm/tasks/main.yml | 2 +- .../targets/helm/tasks/run_test.yml | 26 +-- .../targets/helm/tasks/test_crds.yml | 37 ++-- .../helm/tasks/test_helm_not_installed.yml | 6 +- .../helm/tasks/test_helm_reuse_values.yml | 14 +- .../helm/tasks/test_helm_uninstall.yml | 42 ++-- .../test_helm_with_space_into_chart_name.yml | 10 +- .../targets/helm/tasks/test_read_envvars.yml | 2 +- .../targets/helm/tasks/test_up_dep.yml | 41 ++-- .../targets/helm/tasks/tests_chart.yml | 115 +++++----- .../tasks/tests_chart/from_local_path.yml | 32 +-- .../tasks/tests_chart/from_repository.yml | 6 +- .../helm/tasks/tests_chart/from_url.yml | 2 +- .../targets/helm_diff/tasks/main.yml | 92 ++++---- .../targets/helm_diff/tasks/reuse_values.yml | 14 +- .../tasks/from_in_memory_kubeconfig.yml | 5 +- .../tasks/from_kubeconfig_with_cacert.yml | 38 ++-- .../from_kubeconfig_with_validate_certs.yml | 42 ++-- .../targets/helm_kubeconfig/tasks/main.yml | 6 +- .../helm_kubeconfig/tasks/tests_helm_auth.yml | 69 +++--- .../targets/helm_plugin/tasks/main.yml | 83 +++---- .../targets/helm_pull/tasks/main.yml | 104 ++++----- .../helm_registry_auth/tasks/main.yaml | 16 +- .../targets/helm_repository/tasks/main.yml | 28 +-- .../targets/helm_set_values/tasks/main.yml | 26 +-- .../targets/install_helm/tasks/main.yml | 6 +- .../playbooks/create_resources.yml | 2 +- .../playbooks/delete_resources.yml | 6 +- .../targets/inventory_k8s/playbooks/play.yml | 29 +-- .../targets/k8s_access_review/tasks/main.yml | 5 +- .../targets/k8s_append_hash/tasks/main.yml | 18 +- .../targets/k8s_apply/tasks/main.yml | 205 +++++++++--------- .../k8s_apply/tasks/server_side_apply.yml | 7 +- .../k8s_check_mode/tasks/check_mode.yml | 10 +- .../targets/k8s_check_mode/tasks/main.yml | 13 +- .../targets/k8s_cluster_info/tasks/main.yml | 8 +- .../targets/k8s_copy/tasks/main.yml | 28 ++- .../k8s_copy/tasks/test_check_mode.yml | 54 ++--- .../k8s_copy/tasks/test_copy_directory.yml | 22 +- .../k8s_copy/tasks/test_copy_errors.yml | 30 +-- .../targets/k8s_copy/tasks/test_copy_file.yml | 55 ++--- .../test_copy_item_with_space_in_its_name.yml | 36 +-- .../k8s_copy/tasks/test_copy_large_file.yml | 21 +- .../tasks/test_multi_container_pod.yml | 15 +- .../targets/k8s_validate/tasks/main.yml | 2 +- 46 files changed, 749 insertions(+), 687 deletions(-) diff --git a/tests/integration/targets/helm/tasks/install.yml b/tests/integration/targets/helm/tasks/install.yml index 248925ee8b..b673b50f80 100644 --- a/tests/integration/targets/helm/tasks/install.yml +++ b/tests/integration/targets/helm/tasks/install.yml @@ -1,14 +1,14 @@ --- - name: Init Helm folders - file: + ansible.builtin.file: path: /tmp/helm/ state: directory - name: Unarchive Helm binary - unarchive: + ansible.builtin.unarchive: src: 'https://get.helm.sh/{{ helm_archive_name | default(helm_default_archive_name) }}' dest: /tmp/helm/ - remote_src: yes + remote_src: true retries: 10 delay: 5 register: result diff --git a/tests/integration/targets/helm/tasks/main.yml b/tests/integration/targets/helm/tasks/main.yml index 79c2832031..f50645eadd 100644 --- a/tests/integration/targets/helm/tasks/main.yml +++ b/tests/integration/targets/helm/tasks/main.yml @@ -1,6 +1,6 @@ --- - name: Run tests - include_tasks: run_test.yml + ansible.builtin.include_tasks: run_test.yml loop_control: loop_var: helm_version with_items: diff --git a/tests/integration/targets/helm/tasks/run_test.yml b/tests/integration/targets/helm/tasks/run_test.yml index 76d55b22ca..daae54325b 100644 --- a/tests/integration/targets/helm/tasks/run_test.yml +++ b/tests/integration/targets/helm/tasks/run_test.yml @@ -1,23 +1,23 @@ --- - name: Ensure helm is not installed - file: + ansible.builtin.file: path: "{{ item }}" state: absent with_items: - "/tmp/helm" - name: Check failed if helm is not installed - include_tasks: test_helm_not_installed.yml + ansible.builtin.include_tasks: test_helm_not_installed.yml - name: "Install {{ helm_version }}" - include_role: + ansible.builtin.include_role: name: install_helm - name: "Ensure we honor the environment variables" - include_tasks: test_read_envvars.yml + ansible.builtin.include_tasks: test_read_envvars.yml - name: Deploy charts - include_tasks: "tests_chart/{{ test_chart_type }}.yml" + ansible.builtin.include_tasks: "tests_chart/{{ test_chart_type }}.yml" loop_control: loop_var: test_chart_type with_items: @@ -25,24 +25,24 @@ - from_repository - from_url -- name: test helm upgrade with reuse_values - include_tasks: test_helm_reuse_values.yml +- name: Test helm upgrade with reuse_values + ansible.builtin.include_tasks: test_helm_reuse_values.yml -- name: test helm dependency update - include_tasks: test_up_dep.yml +- name: Test helm dependency update + ansible.builtin.include_tasks: test_up_dep.yml - name: Test helm uninstall - include_tasks: test_helm_uninstall.yml + ansible.builtin.include_tasks: test_helm_uninstall.yml - name: Test helm install with chart name containing space - include_tasks: test_helm_with_space_into_chart_name.yml + ansible.builtin.include_tasks: test_helm_with_space_into_chart_name.yml # https://github.com/ansible-collections/community.kubernetes/issues/296 - name: Test Skip CRDS feature in helm chart install - include_tasks: test_crds.yml + ansible.builtin.include_tasks: test_crds.yml - name: Clean helm install - file: + ansible.builtin.file: path: "{{ item }}" state: absent with_items: diff --git a/tests/integration/targets/helm/tasks/test_crds.yml b/tests/integration/targets/helm/tasks/test_crds.yml index 0534869be5..ed72e0b21c 100644 --- a/tests/integration/targets/helm/tasks/test_crds.yml +++ b/tests/integration/targets/helm/tasks/test_crds.yml @@ -5,17 +5,17 @@ helm_namespace: "{{ test_namespace[0] }}" block: - name: Create namespace - k8s: + kubernetes.core.k8s: kind: Namespace name: "{{ helm_namespace }}" - name: Copy test chart - copy: + ansible.builtin.copy: src: "{{ test_chart }}" dest: "/tmp/helm_test_crds/" - name: Install chart while skipping CRDs - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" chart_ref: "/tmp/helm_test_crds/{{ test_chart }}" namespace: "{{ helm_namespace }}" @@ -23,13 +23,14 @@ skip_crds: true register: install - - assert: + - name: Assert that installation succeed + ansible.builtin.assert: that: - install is changed - install.status.name == "test-crds" - name: Fail to create custom resource - k8s: + kubernetes.core.k8s: definition: apiVersion: ansible.com/v1 kind: Foo @@ -38,30 +39,31 @@ name: test-foo foobar: footest ignore_errors: true - register: result + failed_when: result is successful - - assert: + - name: Assert that module failed + ansible.builtin.assert: that: - result is failed - "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: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" namespace: "{{ helm_namespace }}" name: test-crds state: absent - name: Install chart with CRDs - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" chart_ref: "/tmp/helm_test_crds/{{ test_chart }}" namespace: "{{ helm_namespace }}" name: test-crds - name: Create custom resource - k8s: + kubernetes.core.k8s: definition: apiVersion: ansible.com/v1 kind: Foo @@ -71,29 +73,30 @@ foobar: footest register: result - - assert: + - name: Assert that module succeed + ansible.builtin.assert: that: - result is changed - result.result.foobar == "footest" always: - name: Remove chart - file: + ansible.builtin.file: path: "/tmp/helm_test_crds" state: absent - ignore_errors: true + failed_when: false - name: Remove namespace - k8s: + kubernetes.core.k8s: kind: Namespace name: "{{ helm_namespace }}" state: absent - ignore_errors: true + failed_when: false # CRDs aren't deleted with a namespace, so we need to manually delete it - name: Remove CRD - k8s: + kubernetes.core.k8s: kind: CustomResourceDefinition name: foos.ansible.com state: absent - ignore_errors: true + failed_when: false diff --git a/tests/integration/targets/helm/tasks/test_helm_not_installed.yml b/tests/integration/targets/helm/tasks/test_helm_not_installed.yml index 2f2fd27e27..967a553286 100644 --- a/tests/integration/targets/helm/tasks/test_helm_not_installed.yml +++ b/tests/integration/targets/helm/tasks/test_helm_not_installed.yml @@ -1,15 +1,15 @@ --- - name: Failed test when helm is not installed - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary}}_fake" name: test chart_ref: "{{ chart_test }}" namespace: "helm-test" - ignore_errors: yes + failed_when: helm_missing_binary is successful register: helm_missing_binary - name: Assert that helm is not installed - assert: + ansible.builtin.assert: that: - helm_missing_binary is failed - "'No such file or directory' in helm_missing_binary.msg" diff --git a/tests/integration/targets/helm/tasks/test_helm_reuse_values.yml b/tests/integration/targets/helm/tasks/test_helm_reuse_values.yml index 7801b485ea..3f445b2a0d 100644 --- a/tests/integration/targets/helm/tasks/test_helm_reuse_values.yml +++ b/tests/integration/targets/helm/tasks/test_helm_reuse_values.yml @@ -15,7 +15,7 @@ count: 3 block: - name: Initial chart installation - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" chart_ref: oci://registry-1.docker.io/bitnamicharts/redis release_name: test-redis @@ -25,21 +25,21 @@ register: install - name: Get value set as string - helm_info: + kubernetes.core.helm_info: binary_path: "{{ helm_binary }}" release_name: test-redis release_namespace: "{{ helm_namespace }}" register: release_value - name: Validate that chart values are as expected - assert: + ansible.builtin.assert: that: - install is changed - '"--reuse-values=True" not in install.command' - release_value["status"]["values"] == chart_release_values - name: Upgrade chart using reuse_values=true - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" chart_ref: oci://registry-1.docker.io/bitnamicharts/redis release_name: test-redis @@ -50,14 +50,14 @@ register: upgrade - name: Get value set as string - helm_info: + kubernetes.core.helm_info: binary_path: "{{ helm_binary }}" release_name: test-redis release_namespace: "{{ helm_namespace }}" register: release_value - name: Validate that chart values are as expected - assert: + ansible.builtin.assert: that: - upgrade is changed - '"--reuse-values=True" in upgrade.command' @@ -66,7 +66,7 @@ always: - name: Remove helm namespace - k8s: + kubernetes.core.k8s: api_version: v1 kind: Namespace name: "{{ helm_namespace }}" diff --git a/tests/integration/targets/helm/tasks/test_helm_uninstall.yml b/tests/integration/targets/helm/tasks/test_helm_uninstall.yml index 23e36fb0e9..876332f6e4 100644 --- a/tests/integration/targets/helm/tasks/test_helm_uninstall.yml +++ b/tests/integration/targets/helm/tasks/test_helm_uninstall.yml @@ -4,7 +4,9 @@ version: "3.7.0" register: test_version -- vars: +- name: Run test + when: test_version.result + vars: chart_source: "https://github.com/kubernetes/kube-state-metrics/releases/download/kube-state-metrics-helm-chart-2.13.3/kube-state-metrics-2.13.3.tgz" chart_name: "test-wait-uninstall" chart_name_pending: "test-uninstall-pending-release" @@ -12,7 +14,7 @@ block: - name: Install chart - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: "{{ chart_name }}" chart_ref: "{{ chart_source }}" @@ -20,36 +22,37 @@ create_namespace: true - name: Delete chart with wait - helm: + kubernetes.core.helm: state: absent binary_path: "{{ helm_binary }}" name: "{{ chart_name }}" namespace: "{{ helm_namespace }}" - wait: yes + wait: true register: uninstall - name: assert warning has been raised - assert: + ansible.builtin.assert: that: - uninstall.warnings - name: Create temp directory - tempfile: + ansible.builtin.tempfile: state: directory suffix: .test register: _result - - set_fact: + - name: Set helm_tmp_dir variable + ansible.builtin.set_fact: helm_tmp_dir: "{{ _result.path }}" - name: Unarchive Helm binary - unarchive: + ansible.builtin.unarchive: src: 'https://get.helm.sh/helm-v3.7.0-linux-amd64.tar.gz' dest: "{{ helm_tmp_dir }}" - remote_src: yes + remote_src: true - name: Install chart - helm: + kubernetes.core.helm: binary_path: "{{ helm_tmp_dir }}/linux-amd64/helm" name: "{{ chart_name }}" chart_ref: "{{ chart_source }}" @@ -57,12 +60,12 @@ create_namespace: true - name: uninstall chart again using recent version - helm: + kubernetes.core.helm: state: absent binary_path: "{{ helm_tmp_dir }}/linux-amd64/helm" name: "{{ chart_name }}" namespace: "{{ helm_namespace }}" - wait: yes + wait: true register: uninstall # Test uninstall chart release with 'pending-install' status @@ -74,7 +77,7 @@ chart_release_namespace: "{{ helm_namespace }}" - name: Uninstall chart release with 'pending-install' status - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" release_name: "{{ chart_name_pending }}" namespace: "{{ helm_namespace }}" @@ -82,30 +85,27 @@ register: _uninstall - name: Get Helm release details - helm_info: + kubernetes.core.helm_info: binary_path: "{{ helm_binary }}" release_namespace: "{{ helm_namespace }}" release_state: pending release_name: "{{ chart_name_pending }}" register: _info - - name: assert release does not exist anymore - assert: + - name: Assert release does not exist anymore + ansible.builtin.assert: that: - _uninstall is changed - _info.status is undefined always: - name: Delete temp directory - file: + ansible.builtin.file: path: "{{ helm_tmp_dir }}" state: absent - ignore_errors: true - name: Remove namespace - k8s: + kubernetes.core.k8s: kind: Namespace name: "{{ helm_namespace }}" state: absent - ignore_errors: true - when: test_version.result diff --git a/tests/integration/targets/helm/tasks/test_helm_with_space_into_chart_name.yml b/tests/integration/targets/helm/tasks/test_helm_with_space_into_chart_name.yml index fcf8676657..fd71f097a7 100644 --- a/tests/integration/targets/helm/tasks/test_helm_with_space_into_chart_name.yml +++ b/tests/integration/targets/helm/tasks/test_helm_with_space_into_chart_name.yml @@ -18,7 +18,7 @@ dest: "{{ helm_dir }}" - name: Install chart from local source with Space into name - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: "{{ chart_release_name }}" chart_ref: "{{ helm_dir }}/{{ helm_local_src | basename }}" @@ -27,20 +27,20 @@ register: install_chart - name: Assert that chart is installed - assert: + ansible.builtin.assert: that: - install_chart is changed - install_chart.status.status | lower == 'deployed' - name: Check helm_info content - helm_info: + kubernetes.core.helm_info: binary_path: "{{ helm_binary }}" name: "{{ chart_release_name }}" namespace: "{{ helm_namespace }}" register: chart_info - name: Assert that Chart is installed (using helm_info) - assert: + ansible.builtin.assert: that: - chart_info.status.status | lower == 'deployed' @@ -51,7 +51,7 @@ name: "{{ test_dir.path }}" - name: Remove helm namespace - k8s: + kubernetes.core.k8s: api_version: v1 kind: Namespace name: "{{ helm_namespace }}" diff --git a/tests/integration/targets/helm/tasks/test_read_envvars.yml b/tests/integration/targets/helm/tasks/test_read_envvars.yml index 09c0d3554a..a85c2e6205 100644 --- a/tests/integration/targets/helm/tasks/test_read_envvars.yml +++ b/tests/integration/targets/helm/tasks/test_read_envvars.yml @@ -1,5 +1,5 @@ - name: Pass a bogus server through the K8S_AUTH_HOST environment variable and ensure helm fails as expected - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" state: absent name: does-not-exist diff --git a/tests/integration/targets/helm/tasks/test_up_dep.yml b/tests/integration/targets/helm/tasks/test_up_dep.yml index 39a144607c..8e3352b452 100644 --- a/tests/integration/targets/helm/tasks/test_up_dep.yml +++ b/tests/integration/targets/helm/tasks/test_up_dep.yml @@ -4,7 +4,7 @@ helm_namespace: "{{ test_namespace[3] }}" block: - name: copy chart - copy: + ansible.builtin.copy: src: "{{ item }}" dest: /tmp loop: @@ -12,7 +12,7 @@ - dep-up - name: "Test chart with dependency_update false" - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: test chart_ref: "/tmp/test-chart" @@ -23,19 +23,19 @@ register: release - name: "Get stats of the subchart" - stat: + ansible.builtin.stat: path: "/tmp/test-chart/Chart.lock" register: stat_result - name: "Check if the subchart not exist in chart" - assert: + ansible.builtin.assert: that: - not stat_result.stat.exists success_msg: "subchart not exist in the chart directory" fail_msg: "subchart exist in the charts directory" - name: "Test chart without dependencies block and dependency_update true" - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: test chart_ref: "/tmp/test-chart" @@ -43,16 +43,17 @@ namespace: "{{ helm_namespace }}" create_namespace: yes dependency_update: true - ignore_errors: true + failed_when: false register: release - - assert: + - name: Validate module result + ansible.builtin.assert: that: - release.warnings[0] == "There is no dependencies block defined in Chart.yaml. Dependency update will not be performed. Please consider add dependencies block or disable dependency_update to remove this warning." success_msg: "warning when there is no dependencies block with dependency_update enabled" - name: "Test chart with dependencies block and dependency_update true" - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: test chart_ref: "/tmp/dep-up" @@ -63,26 +64,26 @@ register: release - name: "Get stats of the subchart" - stat: + ansible.builtin.stat: path: "/tmp/dep-up/Chart.lock" register: stat_result - name: "Check if the subchart exists in chart" - assert: + ansible.builtin.assert: that: - stat_result.stat.exists success_msg: "subchart exist in the chart directory" fail_msg: "subchart not exist in the charts directory" always: - name: Remove helm namespace - k8s: + kubernetes.core.k8s: api_version: v1 kind: Namespace name: "{{ helm_namespace }}" state: absent - name: "Remove charts" - file: + ansible.builtin.file: state: absent path: "/tmp/{{ item }}" loop: @@ -93,7 +94,7 @@ - name: "Test dependency update for helm_template module" block: - name: copy chart - copy: + ansible.builtin.copy: src: "{{ item }}" dest: /tmp loop: @@ -101,7 +102,7 @@ - dep-up - name: Test Helm dependency update true - helm_template: + kubernetes.core.helm_template: binary_path: "{{ helm_binary }}" chart_ref: "/tmp/dep-up" chart_version: "{{ chart_source_version | default(omit) }}" @@ -110,7 +111,7 @@ register: result - name: "Get stats of the subchart" - stat: + ansible.builtin.stat: path: "{{ item }}" register: stat_result loop: @@ -118,7 +119,7 @@ - /tmp/dep_up/charts/test-chart/templates/configmap.yaml - name: "Check if the subchart exist in chart" - assert: + ansible.builtin.assert: that: - stat_result.results[0].stat.exists - stat_result.results[1].stat.exists @@ -126,7 +127,7 @@ fail_msg: "There is no Subchart pulled" - name: Test Helm subchart not pulled when dependency_update false for helm_template - helm_template: + kubernetes.core.helm_template: binary_path: "{{ helm_binary }}" chart_ref: "/tmp/test-chart" chart_version: "{{ chart_source_version | default(omit) }}" @@ -135,7 +136,7 @@ register: result - name: "Get stats of the subchart" - stat: + ansible.builtin.stat: path: "{{ item }}" register: stat_result loop: @@ -143,7 +144,7 @@ - /tmp/test-chart/templates/configmap.yaml - name: "Check if the subchart not exist in chart" - assert: + ansible.builtin.assert: that: - not stat_result.results[0].stat.exists - stat_result.results[1].stat.exists @@ -152,7 +153,7 @@ always: - name: "Remove charts" - file: + ansible.builtin.file: state: absent path: "/tmp/{{ item }}" loop: diff --git a/tests/integration/targets/helm/tasks/tests_chart.yml b/tests/integration/targets/helm/tasks/tests_chart.yml index fda5bf85c2..e4054df44e 100644 --- a/tests/integration/targets/helm/tasks/tests_chart.yml +++ b/tests/integration/targets/helm/tasks/tests_chart.yml @@ -5,44 +5,44 @@ chart_release_replaced_name: "test-{{ chart_name | default(source) }}-001" block: - name: Create temp directory - tempfile: + ansible.builtin.tempfile: state: directory register: tmpdir - name: Set temp directory fact - set_fact: + ansible.builtin.set_fact: temp_dir: "{{ tmpdir.path }}" - name: Check helm_info empty - helm_info: + kubernetes.core.helm_info: binary_path: "{{ helm_binary }}" name: "{{ chart_release_name }}" namespace: "{{ helm_namespace }}" register: empty_info - name: "Assert that no charts are installed with helm_info" - assert: + ansible.builtin.assert: that: - empty_info.status is undefined - name: "Install fail {{ chart_test }} from {{ source }}" - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: "{{ chart_release_name }}" chart_ref: "{{ chart_source }}" chart_version: "{{ chart_source_version | default(omit) }}" namespace: "{{ helm_namespace }}" - ignore_errors: yes + failed_when: install_fail is successful register: install_fail - name: "Assert that Install fail {{ chart_test }} from {{ source }}" - assert: + ansible.builtin.assert: that: - install_fail is failed - "'Error: create: failed to create: namespaces \"' + helm_namespace + '\" not found' in install_fail.stderr" - name: "Install {{ chart_test }} from {{ source }} in check mode" - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: "{{ chart_release_name }}" chart_ref: "{{ chart_source }}" @@ -53,14 +53,14 @@ check_mode: true - name: "Assert that {{ chart_test }} chart is installed from {{ source }} in check mode" - assert: + ansible.builtin.assert: that: - install_check_mode is changed - install_check_mode.status is defined - install_check_mode.status.values is defined - name: "Install {{ chart_test }} from {{ source }}" - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: "{{ chart_release_name }}" chart_ref: "{{ chart_source }}" @@ -70,21 +70,21 @@ register: install - name: "Assert that {{ chart_test }} chart is installed from {{ source }}" - assert: + ansible.builtin.assert: that: - install is changed - install.status.chart == chart_test+"-"+chart_test_version - install.status.status | lower == 'deployed' - name: Check helm_info content - helm_info: + kubernetes.core.helm_info: binary_path: "{{ helm_binary }}" name: "{{ chart_release_name }}" namespace: "{{ helm_namespace }}" register: content_info - name: Check helm_info content using release_state - helm_info: + kubernetes.core.helm_info: binary_path: "{{ helm_binary }}" name: "{{ chart_release_name }}" namespace: "{{ helm_namespace }}" @@ -93,14 +93,14 @@ register: release_state_content_info - name: "Assert that {{ chart_test }} is installed from {{ source }} with helm_info" - assert: + ansible.builtin.assert: that: - content_info.status.chart == chart_test+"-"+chart_test_version - content_info.status.status | lower == 'deployed' - release_state_content_info.status.status | lower == 'deployed' - name: Check idempotency - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: "{{ chart_release_name }}" chart_ref: "{{ chart_source }}" @@ -109,14 +109,14 @@ register: install - name: Assert idempotency - assert: + ansible.builtin.assert: that: - install is not changed - install.status.chart == chart_test+"-"+chart_test_version - install.status.status | lower == 'deployed' - name: "Add vars to {{ chart_test }} from {{ source }}" - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: "{{ chart_release_name }}" chart_ref: "{{ chart_source }}" @@ -126,7 +126,7 @@ register: install - name: "Assert that {{ chart_test }} chart is upgraded with new var from {{ source }}" - assert: + ansible.builtin.assert: that: - install is changed - install.status.status | lower == 'deployed' @@ -134,7 +134,7 @@ - "install.status['values'].revisionHistoryLimit == 0" - name: Check idempotency after adding vars - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: "{{ chart_release_name }}" chart_ref: "{{ chart_source }}" @@ -144,7 +144,7 @@ register: install - name: Assert idempotency after add vars - assert: + ansible.builtin.assert: that: - install is not changed - install.status.status | lower == 'deployed' @@ -152,7 +152,7 @@ - "install.status['values'].revisionHistoryLimit == 0" - name: "Remove Vars to {{ chart_test }} from {{ source }}" - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: "{{ chart_release_name }}" chart_ref: "{{ chart_source }}" @@ -161,7 +161,7 @@ register: install - name: "Assert that {{ chart_test }} chart is upgraded with new var from {{ source }}" - assert: + ansible.builtin.assert: that: - install is changed - install.status.status | lower == 'deployed' @@ -169,7 +169,7 @@ - install.status['values'] == {} - name: Check idempotency after removing vars - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: "{{ chart_release_name }}" chart_ref: "{{ chart_source }}" @@ -178,7 +178,7 @@ register: install - name: Assert idempotency after removing vars - assert: + ansible.builtin.assert: that: - install is not changed - install.status.status | lower == 'deployed' @@ -186,7 +186,7 @@ - install.status['values'] == {} - name: "Upgrade {{ chart_test }} from {{ source }}" - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: "{{ chart_release_name }}" chart_ref: "{{ chart_source_upgrade | default(chart_source) }}" @@ -195,14 +195,14 @@ register: install - name: "Assert that {{ chart_test }} chart is upgraded with new version from {{ source }}" - assert: + ansible.builtin.assert: that: - install is changed - install.status.status | lower == 'deployed' - install.status.chart == chart_test+"-"+chart_test_version_upgrade - name: Check idempotency after upgrade - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: "{{ chart_release_name }}" chart_ref: "{{ chart_source_upgrade | default(chart_source) }}" @@ -211,14 +211,14 @@ register: install - name: Assert idempotency after upgrade - assert: + ansible.builtin.assert: that: - install is not changed - install.status.status | lower == 'deployed' - install.status.chart == chart_test+"-"+chart_test_version_upgrade - name: "Remove {{ chart_test }} from {{ source }}" - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" state: absent name: "{{ chart_release_name }}" @@ -226,12 +226,12 @@ register: install - name: "Assert that {{ chart_test }} chart is removed from {{ source }}" - assert: + ansible.builtin.assert: that: - install is changed - name: Check idempotency after remove - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" state: absent name: "{{ chart_release_name }}" @@ -245,7 +245,7 @@ # Test --replace - name: Install chart for replace option - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: "{{ chart_release_replaced_name }}" chart_ref: "{{ chart_source }}" @@ -254,12 +254,12 @@ register: install - name: "Assert that {{ chart_test }} chart is installed from {{ source }}" - assert: + ansible.builtin.assert: that: - install is changed - name: "Remove {{ chart_release_replaced_name }} with --purge" - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" state: absent name: "{{ chart_release_replaced_name }}" @@ -268,12 +268,12 @@ register: install - name: Check if chart is removed - assert: + ansible.builtin.assert: that: - install is changed - name: "Install chart again with same name {{ chart_release_replaced_name }}" - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: "{{ chart_release_replaced_name }}" chart_ref: "{{ chart_source }}" @@ -283,12 +283,12 @@ register: install - name: "Assert that {{ chart_test }} chart is installed from {{ source }}" - assert: + ansible.builtin.assert: that: - install is changed - name: Remove {{ chart_test }} (cleanup) - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" state: absent name: "{{ chart_release_replaced_name }}" @@ -296,12 +296,12 @@ register: install - name: Check if chart is removed - assert: + ansible.builtin.assert: that: - install is changed - name: "Install {{ chart_test }} from {{ source }} with values_files" - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: "{{ chart_release_name }}" chart_ref: "{{ chart_source }}" @@ -312,7 +312,7 @@ register: install - name: "Assert that {{ chart_test }} chart has var from {{ source }}" - assert: + ansible.builtin.assert: that: - install is changed - install.status.status | lower == 'deployed' @@ -320,7 +320,7 @@ - "install.status['values'].revisionHistoryLimit == 0" - name: "Install {{ chart_test }} from {{ source }} with values_files (again)" - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: "{{ chart_release_name }}" chart_ref: "{{ chart_source }}" @@ -331,19 +331,19 @@ register: install - name: "Assert the result is consistent" - assert: + ansible.builtin.assert: that: - not (install is changed) - name: "Remove {{ chart_release_name }} release" - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: "{{ chart_release_name }}" namespace: "{{ helm_namespace }}" state: absent - name: Render templates - helm_template: + kubernetes.core.helm_template: binary_path: "{{ helm_binary }}" chart_ref: "{{ chart_source }}" chart_version: "{{ chart_source_version | default(omit) }}" @@ -352,7 +352,8 @@ - "{{ role_path }}/files/values.yaml" register: result - - assert: + - name: Validate that result is changed + ansible.builtin.assert: that: - result is changed - result is not failed @@ -360,20 +361,21 @@ - result.command is match(helm_binary+" template "+chart_source) - name: Check templates created - stat: + ansible.builtin.stat: path: "{{ temp_dir }}/{{ chart_test }}/templates" register: result - - assert: + - name: Validate that stat exists + ansible.builtin.assert: that: result.stat.exists - name: Render single template from chart to result - helm_template: + kubernetes.core.helm_template: binary_path: "{{ helm_binary }}" chart_ref: "{{ chart_source }}" chart_version: "{{ chart_source_version | default(omit) }}" - disable_hook: True + disable_hook: true release_name: "myrelease" release_namespace: "myreleasenamespace" show_only: @@ -383,7 +385,8 @@ register: result when: chart_source is search("test-chart") - - assert: + - name: Validate module result + ansible.builtin.assert: that: - result is changed - result is not failed @@ -394,7 +397,7 @@ # limit assertion of test result to controlled (local) chart_source - name: Release using non-existent context - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: "{{ chart_release_name }}" chart_ref: "{{ chart_source }}" @@ -402,24 +405,24 @@ namespace: "{{ helm_namespace }}" create_namespace: true context: does-not-exist - ignore_errors: yes + failed_when: result is successful register: result - name: Assert that release fails with non-existent context - assert: + ansible.builtin.assert: that: - result is failed - "'context \"does-not-exist\" does not exist' in result.stderr" always: - name: Clean up temp dir - file: + ansible.builtin.file: state: absent path: "{{ temp_dir }}" ignore_errors: true - name: Remove helm namespace - k8s: + kubernetes.core.k8s: api_version: v1 kind: Namespace name: "{{ helm_namespace }}" diff --git a/tests/integration/targets/helm/tasks/tests_chart/from_local_path.yml b/tests/integration/targets/helm/tasks/tests_chart/from_local_path.yml index db3058e21b..6e9cae0339 100644 --- a/tests/integration/targets/helm/tasks/tests_chart/from_local_path.yml +++ b/tests/integration/targets/helm/tasks/tests_chart/from_local_path.yml @@ -1,20 +1,20 @@ --- - name: Git clone stable repo - git: + ansible.builtin.git: repo: "{{ chart_test_git_repo }}" dest: /tmp/helm_test_repo version: 631eb8413f6728962439488f48d7d6fbb954a6db depth: 1 - name: Git clone stable repo upgrade - git: + ansible.builtin.git: repo: "{{ chart_test_git_repo }}" dest: /tmp/helm_test_repo_upgrade version: d37b5025ffc8be49699898369fbb59661e2a8ffb depth: 1 - name: Install Chart from local path - include_tasks: "../tests_chart.yml" + ansible.builtin.include_tasks: "../tests_chart.yml" vars: source: local_path chart_test: "{{ chart_test_local_path }}" @@ -35,35 +35,37 @@ chart_test_upgrade_app_version: "v2" block: - name: Copy test chart - copy: + ansible.builtin.copy: src: "{{ chart_test }}" dest: "/tmp/helm_test_appversion/test-chart/" - name: Copy test chart v2 - copy: + ansible.builtin.copy: src: "{{ chart_test_upgrade }}" dest: "/tmp/helm_test_appversion/test-chart/" # create package with appVersion v1 - name: "Package chart into archive with appVersion {{ chart_test_app_version }}" - command: "{{ helm_binary }} package --app-version {{ chart_test_app_version }} /tmp/helm_test_appversion/test-chart/{{ chart_test }}" + ansible.builtin.command: "{{ helm_binary }} package --app-version {{ chart_test_app_version }} /tmp/helm_test_appversion/test-chart/{{ chart_test }}" + - name: "Move appVersion {{ chart_test_app_version }} chart archive" - copy: + ansible.builtin.copy: remote_src: true src: "test-chart-{{ chart_test_version }}.tgz" dest: "/tmp/helm_test_appversion/test-chart/{{ chart_test }}-{{ chart_test_app_version }}-{{ chart_test_version }}.tgz" # create package with appVersion v2 - name: "Package chart into archive with appVersion {{ chart_test_upgrade_app_version }}" - command: "{{ helm_binary }} package --app-version {{ chart_test_upgrade_app_version }} /tmp/helm_test_appversion/test-chart/{{ chart_test_upgrade }}" + ansible.builtin.command: "{{ helm_binary }} package --app-version {{ chart_test_upgrade_app_version }} /tmp/helm_test_appversion/test-chart/{{ chart_test_upgrade }}" + - name: "Move appVersion {{ chart_test_upgrade_app_version }} chart archive" - copy: + ansible.builtin.copy: remote_src: true src: "test-chart-{{ chart_test_version_upgrade }}.tgz" dest: "/tmp/helm_test_appversion/test-chart/{{ chart_test }}-{{ chart_test_upgrade_app_version }}-{{ chart_test_version_upgrade }}.tgz" - name: Install Chart from local path - include_tasks: "../tests_chart.yml" + ansible.builtin.include_tasks: "../tests_chart.yml" vars: source: local_path chart_source: "/tmp/helm_test_appversion/test-chart/{{ chart_test }}-{{ chart_test_app_version }}-{{ chart_test_version }}.tgz" @@ -79,21 +81,21 @@ chart_test_version_upgrade: "0.2.0" block: - name: Copy test chart - copy: + ansible.builtin.copy: src: "{{ chart_test }}" dest: "/tmp/helm_test_appversion/test-null/" - name: Copy test chart v2 - copy: + ansible.builtin.copy: src: "{{ chart_test_upgrade }}" dest: "/tmp/helm_test_appversion/test-null/" # create package with appVersion v1 - name: "Package chart into archive with appVersion v1" - command: "{{ helm_binary }} package --app-version v1 /tmp/helm_test_appversion/test-null/{{ chart_test_upgrade }}" + ansible.builtin.command: "{{ helm_binary }} package --app-version v1 /tmp/helm_test_appversion/test-null/{{ chart_test_upgrade }}" - name: Install Chart from local path - include_tasks: "../tests_chart.yml" + ansible.builtin.include_tasks: "../tests_chart.yml" vars: source: local_path chart_source: "/tmp/helm_test_appversion/test-null/{{ chart_test }}/" @@ -102,7 +104,7 @@ helm_namespace: "{{ test_namespace[6] }}" - name: Remove clone repos - file: + ansible.builtin.file: path: "{{ item }}" state: absent with_items: diff --git a/tests/integration/targets/helm/tasks/tests_chart/from_repository.yml b/tests/integration/targets/helm/tasks/tests_chart/from_repository.yml index 0d8bae4f6a..d9ef5b6961 100644 --- a/tests/integration/targets/helm/tasks/tests_chart/from_repository.yml +++ b/tests/integration/targets/helm/tasks/tests_chart/from_repository.yml @@ -1,12 +1,12 @@ --- - name: Add chart repo - helm_repository: + kubernetes.core.helm_repository: binary_path: "{{ helm_binary }}" name: test_helm repo_url: "{{ chart_test_repo }}" - name: Install Chart from repository - include_tasks: "../tests_chart.yml" + ansible.builtin.include_tasks: "../tests_chart.yml" vars: source: repository chart_source: "test_helm/{{ chart_test }}" @@ -15,7 +15,7 @@ helm_namespace: "{{ test_namespace[7] }}" - name: Remove chart repo - helm_repository: + kubernetes.core.helm_repository: binary_path: "{{ helm_binary }}" name: test_helm repo_url: "{{ chart_test_repo }}" diff --git a/tests/integration/targets/helm/tasks/tests_chart/from_url.yml b/tests/integration/targets/helm/tasks/tests_chart/from_url.yml index fdd839d376..0c9fa93cb7 100644 --- a/tests/integration/targets/helm/tasks/tests_chart/from_url.yml +++ b/tests/integration/targets/helm/tasks/tests_chart/from_url.yml @@ -1,6 +1,6 @@ --- - name: Install Chart from URL - include_tasks: "../tests_chart.yml" + ansible.builtin.include_tasks: "../tests_chart.yml" vars: source: url chart_source: "https://github.com/kubernetes/ingress-nginx/releases/download/helm-chart-{{ chart_test_version }}/{{ chart_test }}-{{ chart_test_version }}.tgz" diff --git a/tests/integration/targets/helm_diff/tasks/main.yml b/tests/integration/targets/helm_diff/tasks/main.yml index c2228e9afd..477087f6e3 100644 --- a/tests/integration/targets/helm_diff/tasks/main.yml +++ b/tests/integration/targets/helm_diff/tasks/main.yml @@ -6,19 +6,19 @@ block: - name: Install helm diff - helm_plugin: + kubernetes.core.helm_plugin: binary_path: "{{ helm_binary }}" state: present plugin_path: https://github.com/databus23/helm-diff plugin_version: 3.4.0 - name: Copy test chart - copy: + ansible.builtin.copy: src: "test-chart/" dest: "{{ test_chart_ref }}" - name: Install local chart - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: test-chart namespace: "{{ helm_namespace }}" @@ -26,12 +26,13 @@ create_namespace: true register: install - - assert: + - name: Assert that module reported change + ansible.builtin.assert: that: - install is changed - name: Modify local chart - blockinfile: + ansible.builtin.blockinfile: create: yes path: "{{ test_chart_ref }}/templates/anothermap.yaml" block: !unsafe | @@ -43,7 +44,7 @@ foo: {{ .Values.foo | default "bar" }} - name: Test helm diff in check mode - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: test-chart namespace: "{{ helm_namespace }}" @@ -55,51 +56,53 @@ - name: Check if helm diff check is correct vars: foo_bar_value: "+ foo: bar" - assert: + ansible.builtin.assert: that: - foo_bar_value in diff_result.diff.prepared - name: Upgrade local chart with modifications - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: test-chart namespace: "{{ helm_namespace }}" chart_ref: "{{ test_chart_ref }}" register: install - - assert: + - name: Assert that module reported change + ansible.builtin.assert: that: - install is changed - name: No diff in check mode when no change - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: test-chart namespace: "{{ helm_namespace }}" chart_ref: "{{ test_chart_ref }}" - check_mode: yes + check_mode: true diff: true register: diff_result - name: Check if no diff in check mode when no change - assert: + ansible.builtin.assert: that: - '"diff" not in diff_result' - name: Upgrade modified local chart idempotency check - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: test-chart namespace: "{{ helm_namespace }}" chart_ref: "{{ test_chart_ref }}" register: install - - assert: + - name: Assert that module did not reported change + ansible.builtin.assert: that: - install is not changed - name: Modify values - blockinfile: + ansible.builtin.blockinfile: create: true path: "{{ test_chart_ref }}/values.yml" block: | @@ -107,7 +110,7 @@ foo: baz - name: Upgrade with values file - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: test-chart namespace: "{{ helm_namespace }}" @@ -116,12 +119,13 @@ - "{{ test_chart_ref }}/values.yml" register: install - - assert: + - name: Assert that module did reported change + ansible.builtin.assert: that: - install is changed - name: Upgrade with values file idempotency check - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: test-chart namespace: "{{ helm_namespace }}" @@ -130,12 +134,13 @@ - "{{ test_chart_ref }}/values.yml" register: install - - assert: + - name: Assert that module did not reported change + ansible.builtin.assert: that: - install is not changed - name: Upgrade with values - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: test-chart namespace: "{{ helm_namespace }}" @@ -146,12 +151,13 @@ - "{{ test_chart_ref }}/values.yml" register: install - - assert: + - name: Assert that module reported change + ansible.builtin.assert: that: - install is changed - name: Upgrade with values idempotency check - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: test-chart namespace: "{{ helm_namespace }}" @@ -162,12 +168,13 @@ - "{{ test_chart_ref }}/values.yml" register: install - - assert: + - name: Assert that module did not reported change + ansible.builtin.assert: that: - install is not changed - name: Upgrade with set_values - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: test-chart namespace: "{{ helm_namespace }}" @@ -181,12 +188,13 @@ value_type: string register: install - - assert: + - name: Assert that module reported change + ansible.builtin.assert: that: - install is changed - name: Upgrade with set_values idempotency check - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: test-chart namespace: "{{ helm_namespace }}" @@ -200,20 +208,21 @@ value_type: string register: install - - assert: + - name: Assert that module did not reported change + ansible.builtin.assert: that: - install is not changed # Test helm diff with chart_repo_url - name: Uninstall helm diff - helm_plugin: + kubernetes.core.helm_plugin: binary_path: "{{ helm_binary }}" state: absent plugin_name: diff ignore_errors: true - name: Define chart variables - set_fact: + ansible.builtin.set_fact: test_chart_values: myValue: 'Some ConfigMap data value' myConfigmapName: 'ansible-config-from-url' @@ -249,7 +258,7 @@ ansible.builtin.command: "{{ helm_binary }} registry login -u testuser -p 'pass123!' localhost:6035" - name: Install chart from remote URL - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" chart_ref: "{{ test_chart_ref_url }}" chart_version: 0.1.0 @@ -258,7 +267,7 @@ release_values: "{{ test_chart_values }}" - name: Upgrade chart - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" chart_ref: "{{ test_chart_ref_url }}" chart_version: 0.1.0 @@ -269,7 +278,7 @@ register: _upgrade - name: Assert that module raised a warning - assert: + ansible.builtin.assert: that: - not _upgrade.changed - _upgrade.warnings is defined @@ -277,14 +286,14 @@ - _upgrade.warnings[0] == "The default idempotency check can fail to report changes in certain cases. Install helm diff >= 3.4.1 for better results." - name: Install helm diff (version=3.4.1) - helm_plugin: + kubernetes.core.helm_plugin: binary_path: "{{ helm_binary }}" state: present plugin_path: https://github.com/databus23/helm-diff plugin_version: 3.4.1 - name: Upgrade chart once again - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" chart_ref: "{{ test_chart_ref_url }}" chart_version: 0.1.0 @@ -295,24 +304,24 @@ register: _upgrade_2 - name: Assert that module raised a warning - assert: + ansible.builtin.assert: that: - _upgrade_2.changed - _upgrade_2.warnings is not defined always: - name: Remove chart directory - file: + ansible.builtin.file: path: "{{ test_chart_ref }}" state: absent - ignore_errors: true + failed_when: false - name: Uninstall helm diff - helm_plugin: + kubernetes.core.helm_plugin: binary_path: "{{ helm_binary }}" state: absent plugin_name: diff - ignore_errors: true + failed_when: false - name: Remove helm namespace k8s: @@ -321,6 +330,7 @@ name: "{{ helm_namespace }}" state: absent wait: true - ignore_errors: true + failed_when: false -- include_tasks: reuse_values.yml +- name: Include file 'reuse_values.yml' + ansible.builtin.include_tasks: reuse_values.yml diff --git a/tests/integration/targets/helm_diff/tasks/reuse_values.yml b/tests/integration/targets/helm_diff/tasks/reuse_values.yml index f2d80a8f4c..0aab7cfac7 100644 --- a/tests/integration/targets/helm_diff/tasks/reuse_values.yml +++ b/tests/integration/targets/helm_diff/tasks/reuse_values.yml @@ -1,6 +1,6 @@ --- - name: Create temporary directory for helm chart - tempfile: + ansible.builtin.tempfile: suffix: .helm state: directory register: helm_dir @@ -50,7 +50,7 @@ register: helm_upgrade - name: Ensure task did not reported change - assert: + ansible.builtin.assert: that: - helm_upgrade is not changed @@ -66,23 +66,23 @@ register: helm_upgrade - name: Ensure task reported change - assert: + ansible.builtin.assert: that: - helm_upgrade is changed always: - name: Remove temporary directory - file: + ansible.builtin.file: path: "{{ helm_dir.path }}" state: absent - ignore_errors: true + failed_when: false - name: Uninstall helm diff kubernetes.core.helm_plugin: binary_path: "{{ helm_binary }}" state: absent plugin_name: diff - ignore_errors: true + failed_when: false - name: Remove helm namespace kubernetes.core.k8s: @@ -90,4 +90,4 @@ kind: Namespace name: "{{ helm_namespace }}" state: absent - ignore_errors: true + failed_when: false diff --git a/tests/integration/targets/helm_kubeconfig/tasks/from_in_memory_kubeconfig.yml b/tests/integration/targets/helm_kubeconfig/tasks/from_in_memory_kubeconfig.yml index aebd69c4ac..a08fd86d47 100644 --- a/tests/integration/targets/helm_kubeconfig/tasks/from_in_memory_kubeconfig.yml +++ b/tests/integration/targets/helm_kubeconfig/tasks/from_in_memory_kubeconfig.yml @@ -1,9 +1,10 @@ --- -- set_fact: +- name: Set custom_config variable + ansible.builtin.set_fact: custom_config: "{{ lookup('file', default_kubeconfig_path | expanduser) | from_yaml }}" - name: Test helm modules using in-memory kubeconfig - include_tasks: "tests_helm_auth.yml" + ansible.builtin.include_tasks: "tests_helm_auth.yml" vars: test_kubeconfig: "{{ custom_config }}" helm_namespace: "{{ test_namespace[0] }}" diff --git a/tests/integration/targets/helm_kubeconfig/tasks/from_kubeconfig_with_cacert.yml b/tests/integration/targets/helm_kubeconfig/tasks/from_kubeconfig_with_cacert.yml index 0af0030a57..38646bf725 100644 --- a/tests/integration/targets/helm_kubeconfig/tasks/from_kubeconfig_with_cacert.yml +++ b/tests/integration/targets/helm_kubeconfig/tasks/from_kubeconfig_with_cacert.yml @@ -1,42 +1,49 @@ --- -- set_fact: +- name: Initialize variables + ansible.builtin.set_fact: content: "{{ lookup('file', default_kubeconfig_path) | from_yaml }}" custom_content: {} clusters: [] -- set_fact: +- name: Set custom_content variable + ansible.builtin.set_fact: custom_content: "{{ custom_content | combine({item.key: item.value}) }}" when: "{{ item.key not in ['clusters'] }}" with_dict: "{{ content }}" -- set_fact: +- name: Set clusters variable + ansible.builtin.set_fact: clusters: "{{ clusters + [item | combine({'cluster': {'certificate-authority-data': omit}}, recursive=true)] }}" with_items: "{{ content.clusters }}" -- set_fact: +- name: Set custom_content variable + ansible.builtin.set_fact: custom_content: "{{ custom_content | combine({'clusters': clusters}) }}" - name: create temporary file for ca_cert - tempfile: + ansible.builtin.tempfile: suffix: .cacert register: ca_file - name: copy content into certificate file - copy: + ansible.builtin.copy: content: "{{ content.clusters.0.cluster['certificate-authority-data'] | b64decode }}" dest: "{{ ca_file.path }}" - name: create temporary file to save config in - tempfile: + ansible.builtin.tempfile: suffix: .config register: tfile - name: create custom config - copy: + ansible.builtin.copy: content: "{{ custom_content | to_yaml }}" dest: "{{ tfile.path }}" -- block: +- name: Run tests + vars: + helm_namespace: "{{ test_namespace[1] }}" + block: - name: Install Redis chart without ca_cert (should fail) helm: binary_path: "{{ helm_binary }}" @@ -50,27 +57,24 @@ architecture: standalone release_state: present kubeconfig: "{{ tfile.path }}" - ignore_errors: true + failed_when: _install is successful register: _install - name: assert task failed - assert: + ansible.builtin.assert: that: - _install is failed - '"Error: Kubernetes cluster unreachable" in _install.msg' - name: Test helm modules using in-memory kubeconfig - include_tasks: "tests_helm_auth.yml" + ansible.builtin.include_tasks: "tests_helm_auth.yml" vars: test_kubeconfig: "{{ tfile.path }}" test_ca_cert: "{{ ca_file.path }}" - vars: - helm_namespace: "{{ test_namespace[1] }}" - always: - name: Delete temporary file - file: + ansible.builtin.file: state: absent path: "{{ tfile.path }}" - ignore_errors: true + failed_when: false diff --git a/tests/integration/targets/helm_kubeconfig/tasks/from_kubeconfig_with_validate_certs.yml b/tests/integration/targets/helm_kubeconfig/tasks/from_kubeconfig_with_validate_certs.yml index 73c02a272a..98c3d18bca 100644 --- a/tests/integration/targets/helm_kubeconfig/tasks/from_kubeconfig_with_validate_certs.yml +++ b/tests/integration/targets/helm_kubeconfig/tasks/from_kubeconfig_with_validate_certs.yml @@ -1,34 +1,41 @@ --- -- set_fact: +- name: Initialize variables + ansible.builtin.set_fact: content: "{{ lookup('file', default_kubeconfig_path) | from_yaml }}" custom_content: {} clusters: [] -- set_fact: +- name: Set custom_content variable + ansible.builtin.set_fact: custom_content: "{{ custom_content | combine({item.key: item.value}) }}" when: "{{ item.key not in ['clusters'] }}" with_dict: "{{ content }}" -- set_fact: +- name: Set clusters variable + ansible.builtin.set_fact: clusters: "{{ clusters + [item | combine({'cluster': {'certificate-authority-data': omit}}, recursive=true)] }}" with_items: "{{ content.clusters }}" -- set_fact: +- name: Set custom_content variable + ansible.builtin.set_fact: custom_content: "{{ custom_content | combine({'clusters': clusters}) }}" -- name: create temporary file to save config in - tempfile: +- name: Create temporary file to save config in + ansible.builtin.tempfile: suffix: .config register: tfile -- name: create custom config - copy: +- name: Create custom config + ansible.builtin.copy: content: "{{ custom_content | to_yaml }}" dest: "{{ tfile.path }}" -- block: +- name: Run tests + vars: + helm_namespace: "{{ test_namespace[2] }}" + block: - name: Install Redis chart without validate_certs=false (should fail) - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" chart_repo_url: https://charts.bitnami.com/bitnami chart_ref: redis @@ -41,27 +48,24 @@ release_state: present kubeconfig: "{{ tfile.path }}" validate_certs: true - ignore_errors: true + failed_when: _install is successful register: _install - - name: assert task failed - assert: + - name: Assert task failed + ansible.builtin.assert: that: - _install is failed - '"Error: Kubernetes cluster unreachable" in _install.msg' - name: Test helm modules using in-memory kubeconfig - include_tasks: "tests_helm_auth.yml" + ansible.builtin.include_tasks: "tests_helm_auth.yml" vars: test_kubeconfig: "{{ tfile.path }}" test_validate_certs: false - vars: - helm_namespace: "{{ test_namespace[2] }}" - always: - name: Delete temporary file - file: + ansible.builtin.file: state: absent path: "{{ tfile.path }}" - ignore_errors: true + failed_when: false diff --git a/tests/integration/targets/helm_kubeconfig/tasks/main.yml b/tests/integration/targets/helm_kubeconfig/tasks/main.yml index dc64c1172d..192bf85848 100644 --- a/tests/integration/targets/helm_kubeconfig/tasks/main.yml +++ b/tests/integration/targets/helm_kubeconfig/tasks/main.yml @@ -1,20 +1,20 @@ --- - name: Test helm with in-memory kubeconfig - include_tasks: "from_in_memory_kubeconfig.yml" + ansible.builtin.include_tasks: "from_in_memory_kubeconfig.yml" loop_control: loop_var: test_helm_version with_items: - "v3.10.3" - name: Test helm with custom kubeconfig and validate_certs=false - include_tasks: "from_kubeconfig_with_validate_certs.yml" + ansible.builtin.include_tasks: "from_kubeconfig_with_validate_certs.yml" loop_control: loop_var: test_helm_version with_items: - "v3.10.3" - name: Test helm with custom kubeconfig and ca_cert - include_tasks: "from_kubeconfig_with_cacert.yml" + ansible.builtin.include_tasks: "from_kubeconfig_with_cacert.yml" loop_control: loop_var: test_helm_version with_items: diff --git a/tests/integration/targets/helm_kubeconfig/tasks/tests_helm_auth.yml b/tests/integration/targets/helm_kubeconfig/tasks/tests_helm_auth.yml index 5b66660914..7b42f13fb9 100644 --- a/tests/integration/targets/helm_kubeconfig/tasks/tests_helm_auth.yml +++ b/tests/integration/targets/helm_kubeconfig/tasks/tests_helm_auth.yml @@ -1,11 +1,12 @@ --- -- name: create temporary directory - tempfile: +- name: Create temporary directory + ansible.builtin.tempfile: state: directory suffix: .helm register: _dir - name: Install helm binary + when: test_helm_version is defined block: - name: "Install {{ test_helm_version }}" include_role: @@ -13,31 +14,31 @@ vars: helm_version: "{{ test_helm_version }}" - when: test_helm_version is defined - -- set_fact: +- name: Set saved_kubeconfig_path variable + ansible.builtin.set_fact: saved_kubeconfig_path: "{{ _dir.path }}/config" -- vars: +- name: Run tests + vars: helm_repo_name: autoscaler helm_repo_url: "https://kubernetes.github.io/autoscaler" helm_release_name: "autoscaler" helm_chart_name: "cluster-autoscaler" block: - name: Copy default kubeconfig - copy: + ansible.builtin.copy: remote_src: true src: "{{ default_kubeconfig_path }}" dest: "{{ saved_kubeconfig_path }}" - name: Delete default kubeconfig - file: + ansible.builtin.file: path: "{{ default_kubeconfig_path }}" state: absent # helm_plugin and helm_plugin_info - name: Install subenv plugin - helm_plugin: + kubernetes.core.helm_plugin: binary_path: "{{ helm_binary }}" kubeconfig: "{{ test_kubeconfig | default(omit) }}" validate_certs: "{{ test_validate_certs | default(omit) }}" @@ -46,26 +47,28 @@ plugin_path: https://github.com/hydeenoble/helm-subenv register: plugin - - assert: + - name: Ensure module reported change + ansible.builtin.assert: that: - plugin is changed - name: Gather info about all plugin - helm_plugin_info: + kubernetes.core.helm_plugin_info: binary_path: "{{ helm_binary }}" kubeconfig: "{{ test_kubeconfig | default(omit) }}" validate_certs: "{{ test_validate_certs | default(omit) }}" ca_cert: "{{ test_ca_cert | default(omit) }}" register: plugin_info - - assert: + - name: Assert that plugin list is not empty + ansible.builtin.assert: that: - '"plugin_list" in plugin_info' - plugin_info.plugin_list != [] # helm_repository, helm, helm_info - name: 'Add "{{ helm_repo_name }}" chart repository' - helm_repository: + kubernetes.core.helm_repository: binary_path: "{{ helm_binary }}" name: "{{ helm_repo_name }}" kubeconfig: "{{ test_kubeconfig | default(omit) }}" @@ -75,12 +78,12 @@ register: repository - name: Assert that repository was added - assert: + ansible.builtin.assert: that: - repository is changed - name: Install chart from repository added before - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: "{{ helm_release_name }}" chart_ref: "{{ helm_repo_name }}/{{ helm_chart_name }}" @@ -93,12 +96,12 @@ register: deploy - name: Assert chart was successfully deployed - assert: + ansible.builtin.assert: that: - deploy is changed - name: Get chart content - helm_info: + kubernetes.core.helm_info: binary_path: "{{ helm_binary }}" kubeconfig: "{{ test_kubeconfig | default(omit) }}" validate_certs: "{{ test_validate_certs | default(omit) }}" @@ -108,14 +111,14 @@ register: chart_info - name: Assert chart was successfully deployed - assert: + ansible.builtin.assert: that: - '"status" in chart_info' - chart_info.status.status is defined - chart_info.status.status == "deployed" - name: Remove chart - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" name: "{{ helm_release_name }}" namespace: "{{ helm_namespace }}" @@ -126,12 +129,12 @@ register: remove_chart - name: Assert chart was successfully removed - assert: + ansible.builtin.assert: that: - remove_chart is changed - name: Get chart content - helm_info: + kubernetes.core.helm_info: binary_path: "{{ helm_binary }}" kubeconfig: "{{ test_kubeconfig | default(omit) }}" validate_certs: "{{ test_validate_certs | default(omit) }}" @@ -141,12 +144,12 @@ register: chart_info - name: Assert chart was successfully deployed - assert: + ansible.builtin.assert: that: - '"status" not in chart_info' - name: Remove chart repository - helm_repository: + kubernetes.core.helm_repository: binary_path: "{{ helm_binary }}" name: "{{ helm_repo_name }}" kubeconfig: "{{ test_kubeconfig | default(omit) }}" @@ -156,29 +159,29 @@ register: remove - name: Assert that repository was removed - assert: + ansible.builtin.assert: that: - remove is changed always: - name: Return kubeconfig - copy: + ansible.builtin.copy: remote_src: true src: "{{ saved_kubeconfig_path }}" dest: "{{ default_kubeconfig_path }}" - ignore_errors: true + failed_when: false - name: Delete temporary directory - file: + ansible.builtin.file: path: "{{ _dir.path }}" state: absent - ignore_errors: true + failed_when: false - name: Delete temporary directory for helm install - file: + ansible.builtin.file: path: "{{ _helm_install.path }}" state: absent - ignore_errors: true + failed_when: false when: _helm_install is defined - name: Remove subenv plugin @@ -186,17 +189,17 @@ binary_path: "{{ helm_binary }}" plugin_name: subenv state: absent - ignore_errors: true + failed_when: false - name: Delete namespace k8s: kind: Namespace name: "{{ helm_namespace }}" - ignore_errors: true + failed_when: false - name: Delete helm repository helm_repository: binary_path: "{{ helm_binary }}" name: "{{ helm_repo_name }}" state: absent - ignore_errors: true + failed_when: false diff --git a/tests/integration/targets/helm_plugin/tasks/main.yml b/tests/integration/targets/helm_plugin/tasks/main.yml index 72f0d7a3a8..33427109dd 100644 --- a/tests/integration/targets/helm_plugin/tasks/main.yml +++ b/tests/integration/targets/helm_plugin/tasks/main.yml @@ -1,24 +1,26 @@ --- - name: Install env plugin in check mode - helm_plugin: + kubernetes.core.helm_plugin: binary_path: "{{ helm_binary }}" state: present plugin_path: https://github.com/adamreese/helm-env register: check_install_env check_mode: true -- assert: +- name: Ensure check_mode returned change + ansible.builtin.assert: that: - check_install_env.changed - name: Install env plugin - helm_plugin: + kubernetes.core.helm_plugin: binary_path: "{{ helm_binary }}" state: present plugin_path: https://github.com/adamreese/helm-env register: install_env -- assert: +- name: Ensure module reported change + ansible.builtin.assert: that: - install_env.changed @@ -27,139 +29,146 @@ binary_path: "{{ helm_binary }}" register: plugin_info -- assert: +- name: Ensure plugin_info has plugin_list attribute + ansible.builtin.assert: that: - plugin_info.plugin_list is defined - name: Install env plugin again - helm_plugin: + kubernetes.core.helm_plugin: binary_path: "{{ helm_binary }}" state: present plugin_path: https://github.com/adamreese/helm-env register: install_env -- assert: +- name: Ensure module did not reported change + ansible.builtin.assert: that: - not install_env.changed - name: Uninstall env plugin in check mode - helm_plugin: + kubernetes.core.helm_plugin: binary_path: "{{ helm_binary }}" state: absent plugin_name: env register: check_uninstall_env check_mode: true -- assert: +- name: Ensure module reported change in check_mode + ansible.builtin.assert: that: - check_uninstall_env.changed - name: Uninstall env plugin - helm_plugin: + kubernetes.core.helm_plugin: binary_path: "{{ helm_binary }}" state: absent plugin_name: env register: uninstall_env -- assert: +- name: Ensure module reported change + ansible.builtin.assert: that: - uninstall_env.changed - name: Uninstall env plugin again - helm_plugin: + kubernetes.core.helm_plugin: binary_path: "{{ helm_binary }}" state: absent plugin_name: env register: uninstall_env -- assert: +- name: Ensure uninstall idempotency + ansible.builtin.assert: that: - not uninstall_env.changed # https://github.com/ansible-collections/community.kubernetes/issues/399 -- block: +- name: Run tests with local chart + block: - name: Copy required plugin files - copy: + ansible.builtin.copy: src: "files/sample_plugin" dest: "/tmp/helm_plugin_test/" - name: Install sample_plugin from the directory - helm_plugin: + kubernetes.core.helm_plugin: binary_path: "{{ helm_binary }}" state: present plugin_path: "/tmp/helm_plugin_test/sample_plugin" register: sample_plugin_output - name: Assert that sample_plugin is installed or not - assert: + ansible.builtin.assert: that: - sample_plugin_output.changed - name: Gather Helm plugin info - helm_plugin_info: + kubernetes.core.helm_plugin_info: binary_path: "{{ helm_binary }}" - register: r + register: result - name: Set sample_plugin version - set_fact: - plugin_version: "{{ ( r.plugin_list | selectattr('name', 'equalto', plugin_name) | list )[0].version }}" + ansible.builtin.set_fact: + plugin_version: "{{ ( result.plugin_list | selectattr('name', 'equalto', plugin_name) | list )[0].version }}" vars: plugin_name: "sample_plugin" - name: Assert if sample_plugin with multiline comment is installed - assert: + ansible.builtin.assert: that: - plugin_version == "0.0.1" always: - name: Uninstall sample_plugin - helm_plugin: + kubernetes.core.helm_plugin: binary_path: "{{ helm_binary }}" state: absent plugin_name: sample_plugin - ignore_errors: yes + failed_when: false -- block: - - name: uninstall helm plugin secrets - helm_plugin: +- name: Run tests + block: + - name: Uninstall helm plugin secrets + kubernetes.core.helm_plugin: binary_path: "{{ helm_binary }}" plugin_name: secrets state: absent - - name: install helm-secrets on a specific version - helm_plugin: + - name: Install helm-secrets on a specific version + kubernetes.core.helm_plugin: binary_path: "{{ helm_binary }}" plugin_path: https://github.com/jkroepke/helm-secrets plugin_version: 3.4.1 state: present - - name: list helm plugin + - name: List helm plugin helm_plugin_info: plugin_name: secrets binary_path: "{{ helm_binary }}" register: plugin_list - - name: assert that secrets has been installed with specified version - assert: + - name: Assert that secrets has been installed with specified version + ansible.builtin.assert: that: - plugin_list.plugin_list[0].version == "3.4.1" - name: Update helm plugin version to latest - helm_plugin: + kubernetes.core.helm_plugin: binary_path: "{{ helm_binary }}" plugin_name: secrets state: latest register: _update - - name: assert update was performed - assert: + - name: Assert update was performed + ansible.builtin.assert: that: - _update.changed - '"Updated plugin: secrets" in _update.stdout' always: - name: Uninstall sample_plugin - helm_plugin: + kubernetes.core.helm_plugin: binary_path: "{{ helm_binary }}" state: absent plugin_name: secrets - ignore_errors: yes + failed_when: false diff --git a/tests/integration/targets/helm_pull/tasks/main.yml b/tests/integration/targets/helm_pull/tasks/main.yml index 1faf2be35c..3915b2d34b 100644 --- a/tests/integration/targets/helm_pull/tasks/main.yml +++ b/tests/integration/targets/helm_pull/tasks/main.yml @@ -1,61 +1,64 @@ --- -- name: Define helm versions to test - set_fact: +- name: Run tests + vars: helm_versions: - 3.8.0 - 3.1.0 - 3.0.0 - 2.3.0 - -- block: + block: - name: Create temp directory for helm tests - tempfile: + ansible.builtin.tempfile: state: directory register: tmpdir - name: Set temp directory fact - set_fact: + ansible.builtin.set_fact: temp_dir: "{{ tmpdir.path }}" - - set_fact: + - name: Set destination variable + ansible.builtin.set_fact: destination: "{{ temp_dir }}" - name: Create Helm directories - file: + ansible.builtin.file: state: directory path: "{{ temp_dir }}/{{ item }}" with_items: "{{ helm_versions }}" - name: Unarchive Helm binary - unarchive: + ansible.builtin.unarchive: src: "https://get.helm.sh/helm-v{{ item }}-linux-amd64.tar.gz" dest: "{{ temp_dir }}/{{ item }}" - remote_src: yes + remote_src: true with_items: "{{ helm_versions }}" # Testing helm pull with helm version == 2.3.0 - - block: + - name: 'Testing helm pull with helm version == 2.3.0' + vars: + helm_path: "{{ temp_dir }}/2.3.0/linux-amd64/helm" + block: - name: Assert that helm pull failed with helm <= 3.0.0 - helm_pull: + kubernetes.core.helm_pull: binary_path: "{{ helm_path }}" chart_ref: https://github.com/grafana/helm-charts/releases/download/grafana-5.6.0/grafana-5.6.0.tgz destination: "{{ destination }}" ignore_errors: true register: _result - - name: assert that module failed with proper message - assert: + - name: Assert that module failed with proper message + ansible.builtin.ansible.builtin.assert: that: - _result is failed - _result.msg == "This module requires helm >= 3.0.0, current version is 2.3.0" - vars: - helm_path: "{{ temp_dir }}/2.3.0/linux-amd64/helm" - # Testing helm pull with helm version == 3.0.0 - - block: + - name: 'Testing helm pull with helm version == 3.0.0' + vars: + helm_path: "{{ temp_dir }}/3.0.0/linux-amd64/helm" + block: - name: Download chart using chart_ssl_cert_file - helm_pull: + kubernetes.core.helm_pull: binary_path: "{{ helm_path }}" chart_ref: https://github.com/grafana/helm-charts/releases/download/grafana-5.6.0/grafana-5.6.0.tgz destination: "{{ destination }}" @@ -65,13 +68,13 @@ register: _result - name: assert that module failed with proper message - assert: + ansible.builtin.assert: that: - _result is failed - _result.msg == "Parameter chart_ssl_cert_file requires helm >= 3.1.0, current version is 3.0.0" - name: Download chart using chart_ssl_key_file - helm_pull: + kubernetes.core.helm_pull: binary_path: "{{ helm_path }}" chart_ref: https://github.com/grafana/helm-charts/releases/download/grafana-5.6.0/grafana-5.6.0.tgz destination: "{{ destination }}" @@ -80,14 +83,14 @@ check_mode: true register: _result - - name: assert that module failed with proper message - assert: + - name: Assert that module failed with proper message + ansible.builtin.ansible.builtin.assert: that: - _result is failed - _result.msg == "Parameter chart_ssl_key_file requires helm >= 3.1.0, current version is 3.0.0" - name: Download chart using chart_ca_cert - helm_pull: + kubernetes.core.helm_pull: binary_path: "{{ helm_path }}" chart_ref: https://github.com/grafana/helm-charts/releases/download/grafana-5.6.0/grafana-5.6.0.tgz destination: "{{ destination }}" @@ -96,19 +99,19 @@ check_mode: true register: _result - - name: assert that module failed with proper message - assert: + - name: Assert that module failed with proper message + ansible.builtin.ansible.builtin.assert: that: - _result is failed - _result.msg == "Parameter chart_ca_cert requires helm >= 3.1.0, current version is 3.0.0" - vars: - helm_path: "{{ temp_dir }}/3.0.0/linux-amd64/helm" - # Testing helm pull with helm version == 3.1.0 - - block: + - name: 'Testing helm pull with helm version == 3.1.0' + vars: + helm_path: "{{ temp_dir }}/3.1.0/linux-amd64/helm" + block: - name: Download chart using chart_ssl_cert_file, chart_ca_cert, chart_ssl_key_file - helm_pull: + kubernetes.core.helm_pull: binary_path: "{{ helm_path }}" chart_ref: https://github.com/grafana/helm-charts/releases/download/grafana-5.6.0/grafana-5.6.0.tgz destination: "{{ destination }}" @@ -118,8 +121,8 @@ check_mode: true register: _result - - name: assert that module failed with proper message - assert: + - name: Assert that module failed with proper message + ansible.builtin.assert: that: - _result is changed - '"--ca-file ca_cert_file" in _result.command' @@ -127,7 +130,7 @@ - '"--key-file ssl_key_file" in _result.command' - name: Download chart using skip_tls_certs_check - helm_pull: + kubernetes.core.helm_pull: binary_path: "{{ helm_path }}" chart_ref: https://github.com/grafana/helm-charts/releases/download/grafana-5.6.0/grafana-5.6.0.tgz destination: "{{ destination }}" @@ -136,21 +139,21 @@ check_mode: true register: _result - - name: assert that module failed with proper message - assert: + - name: Assert that module failed with proper message + ansible.builtin.assert: that: - _result is failed - _result.msg == "Parameter skip_tls_certs_check requires helm >= 3.3.0, current version is 3.1.0" - vars: - helm_path: "{{ temp_dir }}/3.1.0/linux-amd64/helm" - # Testing helm pull with helm version == 3.8.0 - - block: + - name: 'Testing helm pull with helm version == 3.8.0' + vars: + helm_path: "{{ temp_dir }}/3.8.0/linux-amd64/helm" + block: # Test options chart_version, verify, pass-credentials, provenance, untar_chart # skip_tls_certs_check, repo_url, repo_username, repo_password - name: Testing chart version - helm_pull: + kubernetes.core.helm_pull: binary_path: "{{ helm_path }}" chart_ref: redis destination: "{{ destination }}" @@ -167,7 +170,8 @@ check_mode: true register: _result - - assert: + - name: Validate module execution result + ansible.builtin.assert: that: - _result is changed - '"--version 0.2.1" in _result.command' @@ -182,7 +186,7 @@ - '"--keyring pubring.gpg" in _result.command' - name: Download chart using chart_ref - helm_pull: + kubernetes.core.helm_pull: binary_path: "{{ helm_path }}" chart_ref: https://github.com/grafana/helm-charts/releases/download/grafana-5.6.0/grafana-5.6.0.tgz destination: "{{ destination }}" @@ -194,14 +198,14 @@ register: _chart - name: Validate that chart was downloaded - assert: + ansible.builtin.assert: that: - _result is changed - _chart.stat.exists - _chart.stat.isreg - name: Download chart using untar_chart - helm_pull: + kubernetes.core.helm_pull: binary_path: "{{ helm_path }}" chart_ref: "oci://registry-1.docker.io/bitnamicharts/redis" destination: "{{ destination }}" @@ -209,23 +213,19 @@ register: _result - name: Check chart on local filesystem - stat: + ansible.builtin.stat: path: "{{ destination }}/redis" register: _chart - name: Validate that chart was downloaded - assert: + ansible.builtin.assert: that: - _result is changed - _chart.stat.exists - _chart.stat.isdir - vars: - helm_path: "{{ temp_dir }}/3.8.0/linux-amd64/helm" - - always: - name: Delete temp directory - file: + ansible.builtin.file: state: absent path: "{{ temp_dir }}" diff --git a/tests/integration/targets/helm_registry_auth/tasks/main.yaml b/tests/integration/targets/helm_registry_auth/tasks/main.yaml index 096f4e31d7..ebe103cd54 100644 --- a/tests/integration/targets/helm_registry_auth/tasks/main.yaml +++ b/tests/integration/targets/helm_registry_auth/tasks/main.yaml @@ -69,7 +69,7 @@ status_code: 200 - name: Test module helm_registry_auth with correct credentials - helm_registry_auth: + kubernetes.core.helm_registry_auth: username: "{{ username }}" password: "{{ password }}" host: localhost:{{ registry_port }} @@ -93,23 +93,23 @@ - name: Assert that the chart is saved # Helm binary prints the message to stderr, refence: https://github.com/helm/helm/issues/13464 - assert: + ansible.builtin.assert: that: "'Pushed: localhost:{{ registry_port }}/test/k8s-monitoring' in _save_chart.stderr" - name: Test logout - helm_registry_auth: + kubernetes.core.helm_registry_auth: host: localhost:{{ registry_port }} state: absent register: _helm_registry_auth_logout - name: Assert logout # Helm binary prints the message to stderr - assert: + ansible.builtin.assert: that: "'Removing login credentials' in _helm_registry_auth_logout.stderr" - name: Test logout idempotency - helm_registry_auth: + kubernetes.core.helm_registry_auth: host: localhost:{{ registry_port }} state: absent register: _helm_registry_auth_logout_idempotency @@ -139,7 +139,7 @@ - "'localhost:{{ registry_port }}' not in _config_json.content | b64decode" - name: Test module helm_registry_auth with wrong credentials - helm_registry_auth: + kubernetes.core.helm_registry_auth: username: "{{ username }}" password: "{{ wrong_password }}" host: localhost:{{ registry_port }} @@ -165,7 +165,7 @@ always: - name: Stop and remove the registry ansible.builtin.command: docker stop {{ registry_name }} - ignore_errors: true + failed_when: false - name: Remove the tmpfile ansible.builtin.file: @@ -175,4 +175,4 @@ loop: - "{{ _tmpfile.path }}" - "{{ _destination.path }}" - ignore_errors: true + failed_when: false diff --git a/tests/integration/targets/helm_repository/tasks/main.yml b/tests/integration/targets/helm_repository/tasks/main.yml index dfd649fe84..7a7f6992cd 100644 --- a/tests/integration/targets/helm_repository/tasks/main.yml +++ b/tests/integration/targets/helm_repository/tasks/main.yml @@ -1,49 +1,49 @@ --- - name: "Ensure test_helm_repo doesn't exist" - helm_repository: + kubernetes.core.helm_repository: binary_path: "{{ helm_binary }}" name: test_helm_repo state: absent - name: Add test_helm_repo chart repository - helm_repository: + kubernetes.core.helm_repository: binary_path: "{{ helm_binary }}" name: test_helm_repo repo_url: "{{ chart_test_repo }}" register: repository - name: Assert that test_helm_repo repository is added - assert: + ansible.builtin.assert: that: - repository is changed - name: Check idempotency - helm_repository: + kubernetes.core.helm_repository: binary_path: "{{ helm_binary }}" name: test_helm_repo repo_url: "{{ chart_test_repo }}" register: repository - name: Assert idempotency - assert: + ansible.builtin.assert: that: - repository is not changed - name: Failed to add repository with the same name - helm_repository: + kubernetes.core.helm_repository: binary_path: "{{ helm_binary }}" name: test_helm_repo repo_url: "https://other-charts.url" register: repository_errors - ignore_errors: yes + failed_when: repository_errors is successful - name: Assert that adding repository with the same name failed - assert: + ansible.builtin.assert: that: - repository_errors is failed - name: Succesfully add repository with the same name when forcing - helm_repository: + kubernetes.core.helm_repository: binary_path: "{{ helm_binary }}" name: test_helm_repo repo_url: "{{ chart_test_repo }}" @@ -51,30 +51,30 @@ register: repository - name: Assert that test_helm_repo repository is changed - assert: + ansible.builtin.assert: that: - repository is changed - name: Remove test_helm_repo chart repository - helm_repository: + kubernetes.core.helm_repository: binary_path: "{{ helm_binary }}" name: test_helm_repo state: absent register: repository - name: Assert that test_helm_repo repository is removed - assert: + ansible.builtin.assert: that: - repository is changed - name: Check idempotency after remove - helm_repository: + kubernetes.core.helm_repository: binary_path: "{{ helm_binary }}" name: test_helm_repo state: absent register: repository - name: Assert idempotency - assert: + ansible.builtin.assert: that: - repository is not changed diff --git a/tests/integration/targets/helm_set_values/tasks/main.yml b/tests/integration/targets/helm_set_values/tasks/main.yml index f18428180e..d9658c92af 100644 --- a/tests/integration/targets/helm_set_values/tasks/main.yml +++ b/tests/integration/targets/helm_set_values/tasks/main.yml @@ -1,5 +1,5 @@ - name: Install helm using set_values parameters - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" chart_ref: oci://registry-1.docker.io/bitnamicharts/mariadb release_name: test-mariadb @@ -11,14 +11,14 @@ - value: versioned=false - name: Get value set as string - helm_info: + kubernetes.core.helm_info: binary_path: "{{ helm_binary }}" release_name: test-mariadb release_namespace: "{{ helm_namespace }}" register: user_values - name: Assert that release was created with user-defined variables - assert: + ansible.builtin.assert: that: - '"phase" in user_values.status["values"]' - '"versioned" in user_values.status["values"]' @@ -27,13 +27,13 @@ # install chart using set_values and release_values - name: Install helm binary (> 3.10.0) requires to use set-json - include_role: + ansible.builtin.include_role: name: install_helm vars: helm_version: "v3.10.3" - name: Install helm using set_values parameters - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" chart_ref: oci://registry-1.docker.io/bitnamicharts/apache release_name: test-apache @@ -46,14 +46,14 @@ replicaCount: 3 - name: Get release info - helm_info: + kubernetes.core.helm_info: binary_path: "{{ helm_binary }}" release_name: test-apache release_namespace: "{{ helm_namespace }}" register: values - name: Assert that release was created with user-defined variables - assert: + ansible.builtin.assert: that: - values.status["values"].replicaCount == 3 - values.status["values"].master.image.registry == "docker.io" @@ -62,20 +62,20 @@ # install chart using set_values and values_files - name: create temporary file to save values in - tempfile: + ansible.builtin.tempfile: suffix: .yml register: ymlfile - block: - name: copy content into values file - copy: + ansible.builtin.copy: content: | --- mode: distributed dest: "{{ ymlfile.path }}" - name: Install helm using set_values parameters - helm: + kubernetes.core.helm: binary_path: "{{ helm_binary }}" chart_ref: oci://registry-1.docker.io/bitnamicharts/minio release_name: test-minio @@ -87,20 +87,20 @@ - "{{ ymlfile.path }}" - name: Get release info - helm_info: + kubernetes.core.helm_info: binary_path: "{{ helm_binary }}" release_name: test-minio release_namespace: "{{ helm_namespace }}" register: values - name: Assert that release was created with user-defined variables - assert: + ansible.builtin.assert: that: - values.status["values"].mode == "distributed" - values.status["values"].disableWebUI is true always: - name: Delete temporary file - file: + ansible.builtin.file: state: absent path: "{{ ymlfile.path }}" diff --git a/tests/integration/targets/install_helm/tasks/main.yml b/tests/integration/targets/install_helm/tasks/main.yml index 49e36a4608..8dd3e55ab8 100644 --- a/tests/integration/targets/install_helm/tasks/main.yml +++ b/tests/integration/targets/install_helm/tasks/main.yml @@ -1,14 +1,14 @@ --- - name: Init Helm folders - file: + ansible.builtin.file: path: "{{ helm_install_path }}" state: directory - name: Unarchive Helm binary - unarchive: + ansible.builtin.unarchive: src: "https://get.helm.sh/{{ helm_archive_name | default(helm_default_archive_name) }}" dest: "{{ helm_install_path }}" - remote_src: yes + remote_src: true retries: 10 delay: 5 register: result diff --git a/tests/integration/targets/inventory_k8s/playbooks/create_resources.yml b/tests/integration/targets/inventory_k8s/playbooks/create_resources.yml index 7e16f94271..08e1ccf03f 100644 --- a/tests/integration/targets/inventory_k8s/playbooks/create_resources.yml +++ b/tests/integration/targets/inventory_k8s/playbooks/create_resources.yml @@ -12,7 +12,7 @@ tasks: - name: Create inventory files - copy: + ansible.builtin.copy: content: "{{ item.content }}" dest: "{{ item.path }}" vars: diff --git a/tests/integration/targets/inventory_k8s/playbooks/delete_resources.yml b/tests/integration/targets/inventory_k8s/playbooks/delete_resources.yml index ec09ec5cf2..677adaaf80 100644 --- a/tests/integration/targets/inventory_k8s/playbooks/delete_resources.yml +++ b/tests/integration/targets/inventory_k8s/playbooks/delete_resources.yml @@ -10,10 +10,10 @@ tasks: - name: Delete temporary files - file: + ansible.builtin.file: state: absent path: "{{ user_credentials_dir ~ '/' ~ item }}" - ignore_errors: true + failed_when: false with_items: - test_inventory_aliases_with_ssl_k8s.yml - test_inventory_aliases_no_ssl_k8s.yml @@ -23,7 +23,7 @@ - host_data.txt - name: Remove inventory namespace - k8s: + kubernetes.core.k8s: api_version: v1 kind: Namespace name: inventory diff --git a/tests/integration/targets/inventory_k8s/playbooks/play.yml b/tests/integration/targets/inventory_k8s/playbooks/play.yml index 07baf1a3ab..01decdfd30 100644 --- a/tests/integration/targets/inventory_k8s/playbooks/play.yml +++ b/tests/integration/targets/inventory_k8s/playbooks/play.yml @@ -11,21 +11,21 @@ tasks: - name: Delete existing namespace - k8s: + kubernetes.core.k8s: api_version: v1 kind: Namespace name: inventory - wait: yes + wait: true state: absent - name: Ensure namespace exists - k8s: + kubernetes.core.k8s: api_version: v1 kind: Namespace name: inventory - name: Add a deployment - k8s: + kubernetes.core.k8s: definition: apiVersion: apps/v1 kind: Deployment @@ -38,7 +38,7 @@ matchLabels: app: "{{ k8s_pod_name }}" template: "{{ k8s_pod_template }}" - wait: yes + wait: true wait_timeout: 400 vars: k8s_pod_name: inventory @@ -55,7 +55,7 @@ - name: Verify inventory and connection plugins hosts: namespace_inventory_pods - gather_facts: no + gather_facts: false vars: file_content: | @@ -66,25 +66,28 @@ meta: end_host when: pod_phase != "Running" - - debug: var=hostvars - - setup: + - name: Display hostvars + ansible.builtin.debug: var=hostvars + - name: Setup + ansible.builtin.setup: - - debug: var=ansible_facts + - name: Display ansible_facts + ansible.builtin.debug: var=ansible_facts - name: Assert the TEST environment variable was retrieved - assert: + ansible.builtin.assert: that: ansible_facts.env.TEST == 'test' - name: Copy a file into the host - copy: + ansible.builtin.copy: content: '{{ file_content }}' dest: /tmp/test_file - name: Retrieve the file from the host - slurp: + ansible.builtin.slurp: src: /tmp/test_file register: slurped_file - name: Assert the file content matches expectations - assert: + ansible.builtin.assert: that: (slurped_file.content|b64decode) == file_content diff --git a/tests/integration/targets/k8s_access_review/tasks/main.yml b/tests/integration/targets/k8s_access_review/tasks/main.yml index 78d6d56752..7a50cfc583 100644 --- a/tests/integration/targets/k8s_access_review/tasks/main.yml +++ b/tests/integration/targets/k8s_access_review/tasks/main.yml @@ -1,7 +1,5 @@ --- - name: Create a SelfSubjectAccessReview resource - register: can_i_create_namespaces - ignore_errors: yes k8s: state: present definition: @@ -12,9 +10,10 @@ group: v1 resource: Namespace verb: create + register: can_i_create_namespaces - name: Assert that the SelfSubjectAccessReview request succeded - assert: + ansible.builtin.assert: that: - can_i_create_namespaces is successful - can_i_create_namespaces.result.status is defined diff --git a/tests/integration/targets/k8s_append_hash/tasks/main.yml b/tests/integration/targets/k8s_append_hash/tasks/main.yml index 3a6411e8ac..fa847bb9f7 100644 --- a/tests/integration/targets/k8s_append_hash/tasks/main.yml +++ b/tests/integration/targets/k8s_append_hash/tasks/main.yml @@ -1,12 +1,12 @@ --- - block: - name: Ensure that append_hash namespace exists - k8s: + kubernetes.core.k8s: kind: Namespace name: "{{ test_namespace }}" - name: Create k8s_resource variable - set_fact: + ansible.legacy.set_fact: k8s_resource: metadata: name: config-map-test @@ -17,32 +17,32 @@ hello: world - name: Create config map - k8s: + kubernetes.core.k8s: definition: "{{ k8s_resource }}" append_hash: yes register: k8s_configmap1 - name: Check configmap is created with a hash - assert: + ansible.builtin.assert: that: - k8s_configmap1 is changed - k8s_configmap1.result.metadata.name != 'config-map-test' - k8s_configmap1.result.metadata.name[:-10] == 'config-map-test-' - name: Recreate same config map - k8s: + kubernetes.core.k8s: definition: "{{ k8s_resource }}" append_hash: yes register: k8s_configmap2 - name: Check configmaps are different - assert: + ansible.builtin.assert: that: - k8s_configmap2 is not changed - k8s_configmap1.result.metadata.name == k8s_configmap2.result.metadata.name - name: Add key to config map - k8s: + kubernetes.core.k8s: definition: metadata: name: config-map-test @@ -56,14 +56,14 @@ register: k8s_configmap3 - name: Check configmaps are different - assert: + ansible.builtin.assert: that: - k8s_configmap3 is changed - k8s_configmap1.result.metadata.name != k8s_configmap3.result.metadata.name always: - name: Ensure that namespace is removed - k8s: + kubernetes.core.k8s: kind: Namespace name: "{{ test_namespace }}" state: absent diff --git a/tests/integration/targets/k8s_apply/tasks/main.yml b/tests/integration/targets/k8s_apply/tasks/main.yml index c759d7bcf0..2385decd93 100644 --- a/tests/integration/targets/k8s_apply/tasks/main.yml +++ b/tests/integration/targets/k8s_apply/tasks/main.yml @@ -1,7 +1,8 @@ --- -- block: +- name: Run tests + block: - name: Ensure namespace exists - k8s: + kubernetes.core.k8s: definition: apiVersion: v1 kind: Namespace @@ -9,7 +10,7 @@ name: "{{ test_namespace }}" - name: Add a configmap - k8s: + kubernetes.core.k8s: name: "apply-configmap" namespace: "{{ test_namespace }}" definition: @@ -19,17 +20,17 @@ one: "1" two: "2" three: "3" - apply: yes + apply: true register: k8s_configmap - name: Check configmap was created - assert: + ansible.builtin.assert: that: - k8s_configmap is changed - k8s_configmap.result.metadata.annotations|default(False) - name: Add same configmap again - k8s: + kubernetes.core.k8s: definition: kind: ConfigMap apiVersion: v1 @@ -40,16 +41,16 @@ one: "1" two: "2" three: "3" - apply: yes + apply: true register: k8s_configmap_2 - name: Check nothing changed - assert: + ansible.builtin.assert: that: - k8s_configmap_2 is not changed - name: Add same configmap again with check mode on - k8s: + kubernetes.core.k8s: definition: kind: ConfigMap apiVersion: v1 @@ -60,17 +61,17 @@ one: "1" two: "2" three: "3" - apply: yes - check_mode: yes + apply: true + check_mode: true register: k8s_configmap_check - name: Check nothing changed - assert: + ansible.builtin.assert: that: - k8s_configmap_check is not changed - name: Add same configmap again but using name and namespace args - k8s: + kubernetes.core.k8s: name: "apply-configmap" namespace: "{{ test_namespace }}" definition: @@ -80,16 +81,16 @@ one: "1" two: "2" three: "3" - apply: yes + apply: true register: k8s_configmap_2a - name: Check nothing changed - assert: + ansible.builtin.assert: that: - k8s_configmap_2a is not changed - name: Update configmap - k8s: + kubernetes.core.k8s: definition: kind: ConfigMap apiVersion: v1 @@ -100,18 +101,18 @@ one: "1" three: "3" four: "4" - apply: yes + apply: true register: k8s_configmap_3 - name: Ensure that configmap has been correctly updated - assert: + ansible.builtin.assert: that: - k8s_configmap_3 is changed - "'four' in k8s_configmap_3.result.data" - "'two' not in k8s_configmap_3.result.data" - name: Add a service - k8s: + kubernetes.core.k8s: definition: apiVersion: v1 kind: Service @@ -125,11 +126,11 @@ - name: http port: 8080 targetPort: 8080 - apply: yes + apply: true register: k8s_service - name: Add exactly same service - k8s: + kubernetes.core.k8s: definition: apiVersion: v1 kind: Service @@ -143,16 +144,16 @@ - name: http port: 8080 targetPort: 8080 - apply: yes + apply: true register: k8s_service_2 - name: Check nothing changed - assert: + ansible.builtin.assert: that: - k8s_service_2 is not changed - name: Add exactly same service in check mode - k8s: + kubernetes.core.k8s: definition: apiVersion: v1 kind: Service @@ -166,17 +167,17 @@ - name: http port: 8080 targetPort: 8080 - apply: yes + apply: true register: k8s_service_3 - check_mode: yes + check_mode: true - name: Check nothing changed - assert: + ansible.builtin.assert: that: - k8s_service_3 is not changed - name: Change service ports - k8s: + kubernetes.core.k8s: definition: apiVersion: v1 kind: Service @@ -190,18 +191,18 @@ - name: http port: 8081 targetPort: 8081 - apply: yes + apply: true register: k8s_service_4 - name: Check ports are correct - assert: + ansible.builtin.assert: that: - k8s_service_4 is changed - k8s_service_4.result.spec.ports | length == 1 - k8s_service_4.result.spec.ports[0].port == 8081 - name: Insert new service port - k8s: + kubernetes.core.k8s: definition: apiVersion: v1 kind: Service @@ -218,11 +219,11 @@ - name: http port: 8081 targetPort: 8081 - apply: yes + apply: true register: k8s_service_4 - name: Check ports are correct - assert: + ansible.builtin.assert: that: - k8s_service_4 is changed - k8s_service_4.result.spec.ports | length == 2 @@ -230,7 +231,7 @@ - k8s_service_4.result.spec.ports[1].port == 8081 - name: Remove new service port (check mode) - k8s: + kubernetes.core.k8s: definition: apiVersion: v1 kind: Service @@ -244,19 +245,19 @@ - name: http port: 8081 targetPort: 8081 - apply: yes - check_mode: yes + apply: true + check_mode: true register: k8s_service_check - name: Check ports are correct - assert: + ansible.builtin.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: + kubernetes.core.k8s: definition: apiVersion: v1 kind: Service @@ -270,18 +271,18 @@ - name: http port: 8081 targetPort: 8081 - apply: yes + apply: true register: k8s_service_5 - name: Check ports are correct - assert: + ansible.builtin.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: + kubernetes.core.k8s: definition: apiVersion: v1 kind: ServiceAccount @@ -290,7 +291,7 @@ namespace: "{{ test_namespace }}" - name: Add a deployment - k8s: + kubernetes.core.k8s: definition: apiVersion: apps/v1 kind: Deployment @@ -303,9 +304,9 @@ matchLabels: app: "{{ k8s_pod_name }}" template: "{{ k8s_pod_template }}" - wait: yes + wait: true wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" - apply: yes + apply: true vars: k8s_pod_name: apply-deploy k8s_pod_image: gcr.io/kuar-demo/kuard-amd64:v0.10.0-green @@ -323,7 +324,7 @@ memory: 100Mi - name: Update the earlier deployment in check mode - k8s: + kubernetes.core.k8s: definition: apiVersion: apps/v1 kind: Deployment @@ -336,10 +337,10 @@ matchLabels: app: "{{ k8s_pod_name }}" template: "{{ k8s_pod_template }}" - wait: yes + wait: true wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" - apply: yes - check_mode: yes + apply: true + check_mode: true vars: k8s_pod_name: apply-deploy k8s_pod_image: gcr.io/kuar-demo/kuard-amd64:v0.10.0-purple @@ -357,13 +358,13 @@ register: update_deploy_check_mode - name: Ensure check mode change took - assert: + ansible.builtin.assert: that: - update_deploy_check_mode is changed - "update_deploy_check_mode.result.spec.template.spec.containers[0].image == 'gcr.io/kuar-demo/kuard-amd64:v0.10.0-purple'" - name: Update the earlier deployment - k8s: + kubernetes.core.k8s: definition: apiVersion: apps/v1 kind: Deployment @@ -376,9 +377,9 @@ matchLabels: app: "{{ k8s_pod_name }}" template: "{{ k8s_pod_template }}" - wait: yes + wait: true wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" - apply: yes + apply: true vars: k8s_pod_name: apply-deploy k8s_pod_image: gcr.io/kuar-demo/kuard-amd64:v0.10.0-purple @@ -396,13 +397,13 @@ register: update_deploy_for_real - name: Ensure change took - assert: + ansible.builtin.assert: that: - update_deploy_for_real is changed - "update_deploy_for_real.result.spec.template.spec.containers[0].image == 'gcr.io/kuar-demo/kuard-amd64:v0.10.0-purple'" - name: Remove the serviceaccount - k8s: + kubernetes.core.k8s: state: absent definition: apiVersion: v1 @@ -412,7 +413,7 @@ namespace: "{{ test_namespace }}" - name: Apply deployment after service account removed - k8s: + kubernetes.core.k8s: definition: apiVersion: apps/v1 kind: Deployment @@ -425,9 +426,9 @@ matchLabels: app: "{{ k8s_pod_name }}" template: "{{ k8s_pod_template }}" - wait: yes + wait: true wait_timeout: "{{ k8s_wait_timeout | default(omit) }}" - apply: yes + apply: true vars: k8s_pod_name: apply-deploy k8s_pod_image: gcr.io/kuar-demo/kuard-amd64:v0.10.0-green @@ -443,15 +444,15 @@ cpu: 50m memory: 50Mi register: deploy_after_serviceaccount_removal - ignore_errors: yes + failed_when: deploy_after_serviceaccount_removal is failed - name: Ensure that updating deployment after service account removal failed - assert: + ansible.builtin.assert: that: - deploy_after_serviceaccount_removal is failed - name: Add a secret - k8s: + kubernetes.core.k8s: definition: apiVersion: v1 kind: Secret @@ -464,13 +465,13 @@ register: k8s_secret - name: Check secret was created - assert: + ansible.builtin.assert: that: - k8s_secret is changed - k8s_secret.result.data.foo - name: Add same secret - k8s: + kubernetes.core.k8s: definition: apiVersion: v1 kind: Secret @@ -483,12 +484,12 @@ register: k8s_secret - name: Check nothing changed - assert: + ansible.builtin.assert: that: - k8s_secret is not changed - name: Add same secret with check mode on - k8s: + kubernetes.core.k8s: definition: apiVersion: v1 kind: Secret @@ -498,16 +499,16 @@ type: Opaque stringData: foo: bar - check_mode: yes + check_mode: true register: k8s_secret - name: Check nothing changed - assert: + ansible.builtin.assert: that: - k8s_secret is not changed - name: Add same secret with check mode on using data - k8s: + kubernetes.core.k8s: definition: apiVersion: v1 kind: Secret @@ -517,16 +518,16 @@ type: Opaque data: foo: YmFy - check_mode: yes + check_mode: true register: k8s_secret - name: Check nothing changed - assert: + ansible.builtin.assert: that: - k8s_secret is not changed - name: Create network policy (egress array with empty dict) - k8s: + kubernetes.core.k8s: namespace: "{{ test_namespace }}" apply: true definition: @@ -553,7 +554,7 @@ - {} - name: Apply network policy - k8s: + kubernetes.core.k8s: namespace: "{{ test_namespace }}" definition: kind: NetworkPolicy @@ -581,13 +582,13 @@ register: k8s_networkpolicy - name: Check that nothing changed - assert: + ansible.builtin.assert: that: - k8s_networkpolicy is not changed # Server Side Apply - name: Create Configmap using server side apply - field_manager not specified - k8s: + kubernetes.core.k8s: namespace: "{{ test_namespace }}" definition: apiVersion: v1 @@ -596,20 +597,20 @@ name: server-side-cm data: key: value-0 - apply: yes + apply: true server_side_apply: force_conflicts: false register: result ignore_errors: true - name: Check that configmap creation failed - assert: + ansible.builtin.assert: that: - result is failed - '"field_manager" in result.msg' - name: Create Configmap using server side apply - k8s: + kubernetes.core.k8s: namespace: "{{ test_namespace }}" definition: apiVersion: v1 @@ -618,20 +619,20 @@ name: server-side-cm data: key: value-0 - apply: yes + apply: true server_side_apply: field_manager: "manager-00" register: result - name: Check configmap was created with expected manager - assert: + ansible.builtin.assert: that: - result is changed - result.result.metadata.managedFields | length == 1 - result.result.metadata.managedFields[0].manager == 'manager-00' - name: Apply ConfigMap using same parameters - k8s: + kubernetes.core.k8s: namespace: "{{ test_namespace }}" definition: apiVersion: v1 @@ -640,18 +641,18 @@ name: server-side-cm data: key: value-0 - apply: yes + apply: true server_side_apply: field_manager: "manager-00" register: result - name: Assert that nothing change using check_mode - assert: + ansible.builtin.assert: that: - result is not changed - name: Apply ConfigMap adding new manager - k8s: + kubernetes.core.k8s: namespace: "{{ test_namespace }}" definition: apiVersion: v1 @@ -660,19 +661,19 @@ name: server-side-cm data: key: value-0 - apply: yes + apply: true server_side_apply: field_manager: "manager-01" register: result - name: Assert that number of manager has increased - assert: + ansible.builtin.assert: that: - result is changed - result.result.metadata.managedFields | length == 2 - name: Apply changes to Configmap using new field_manager - k8s: + kubernetes.core.k8s: namespace: "{{ test_namespace }}" definition: apiVersion: v1 @@ -681,20 +682,20 @@ name: server-side-cm data: key: value-1 - apply: yes + apply: true server_side_apply: field_manager: "manager-02" register: result ignore_errors: true - name: assert that operation failed with conflicts - assert: + ansible.builtin.assert: that: - result is failed - result.reason == 'Conflict' - name: Apply changes to Configmap using new field_manager and force_conflicts - k8s: + kubernetes.core.k8s: namespace: "{{ test_namespace }}" definition: apiVersion: v1 @@ -703,14 +704,14 @@ name: server-side-cm data: key: value-1 - apply: yes + apply: true server_side_apply: field_manager: "manager-02" force_conflicts: true register: result - - name: assert that operation failed with conflicts - assert: + - name: Assert that operation failed with conflicts + ansible.builtin.assert: that: - result is changed - result.result.metadata.managedFields | length == 1 @@ -719,14 +720,14 @@ # check_mode with server side apply - name: Ensure namespace does not exist - k8s: + kubernetes.core.k8s: state: absent kind: Namespace name: testing wait: true - name: Create namespace using server_side_apply=true and check_mode=true - k8s: + kubernetes.core.k8s: apply: true server_side_apply: field_manager: ansible @@ -739,28 +740,30 @@ register: _create - name: Ensure namespace was not created - k8s_info: + kubernetes.core.k8s_info: kind: Namespace name: testing register: _info - name: Validate that check_mode reported change even if namespace was not created - assert: + ansible.builtin.assert: that: - _create is changed - not _info.resources # server side apply over kubernetes client releases - name: Create temporary directory - tempfile: + ansible.builtin.tempfile: state: directory suffix: .server register: path - - set_fact: + - name: Set virtualenv_src variable + ansible.builtin.set_fact: virtualenv_src: "{{ path.path }}" - - include_tasks: tasks/server_side_apply.yml + - name: Include server_side_apply.yml + ansible.builtin.include_tasks: tasks/server_side_apply.yml with_items: - '24.2.0' - '25.2.0a1' @@ -770,14 +773,14 @@ always: - name: Remove namespace - k8s: + kubernetes.core.k8s: kind: Namespace name: "{{ test_namespace }}" state: absent - name: Delete temporary directory - file: + ansible.builtin.file: path: "{{ virtualenv_src }}" state: absent - ignore_errors: true + failed_when: false when: virtualenv_src is defined diff --git a/tests/integration/targets/k8s_apply/tasks/server_side_apply.yml b/tests/integration/targets/k8s_apply/tasks/server_side_apply.yml index d50bf14790..94501f2b26 100644 --- a/tests/integration/targets/k8s_apply/tasks/server_side_apply.yml +++ b/tests/integration/targets/k8s_apply/tasks/server_side_apply.yml @@ -1,16 +1,17 @@ -- set_fact: +- name: Define test variables + ansible.builtin.set_fact: kubernetes_version: "kubernetes=={{ item }}" virtualenv_path: "{{ virtualenv_src }}/venv{{ item | replace('.', '') }}" - name: Install kubernetes version - pip: + ansible.builtin.pip: name: - '{{ kubernetes_version }}' virtualenv_command: "virtualenv --python {{ ansible_python_interpreter }}" virtualenv: "{{ virtualenv_path }}" - name: Update namespace using server side apply - k8s: + kubernetes.core.k8s: apply: true server_side_apply: field_manager: "ansible{{ item | replace('.', '') }}" diff --git a/tests/integration/targets/k8s_check_mode/tasks/check_mode.yml b/tests/integration/targets/k8s_check_mode/tasks/check_mode.yml index 5d9e2caa76..cd55143b12 100644 --- a/tests/integration/targets/k8s_check_mode/tasks/check_mode.yml +++ b/tests/integration/targets/k8s_check_mode/tasks/check_mode.yml @@ -1,32 +1,32 @@ - name: Create virtualenv with kubernetes release - pip: + ansible.builtin.pip: name: - '{{ item.k8s_release }}' virtualenv_command: "virtualenv --python {{ ansible_python_interpreter }}" virtualenv: "{{ tmpdir }}/{{ item.virtualenv }}" - name: Create resource using check mode - k8s: + kubernetes.core.k8s: kind: Namespace name: '{{ test_namespace }}' check_mode: true register: _create - name: Ensure namespace was not created - k8s_info: + kubernetes.core.k8s_info: kind: Namespace name: '{{ test_namespace }}' register: info failed_when: info.resources | length > 0 - name: Ensure server side dry_run has being used - assert: + ansible.builtin.assert: that: - '"creationTimestamp" in _create.result.metadata' when: item.dry_run - name: Ensure server side dry_run was not used - assert: + ansible.builtin.assert: that: - '"creationTimestamp" in _create.result.metadata' when: not item.dry_run diff --git a/tests/integration/targets/k8s_check_mode/tasks/main.yml b/tests/integration/targets/k8s_check_mode/tasks/main.yml index 60af294e5f..6fe4721cc4 100644 --- a/tests/integration/targets/k8s_check_mode/tasks/main.yml +++ b/tests/integration/targets/k8s_check_mode/tasks/main.yml @@ -1,19 +1,20 @@ - name: create temporary directory for tests - tempfile: + ansible.builtin.tempfile: suffix: .k8s state: directory register: _path -- block: - - include_tasks: tasks/check_mode.yml - with_items: '{{ test_config }}' - +- name: Run tests vars: tmpdir: '{{ _path.path }}' + block: + - name: Include 'tasks/check_mode.yml' + ansible.builtin.include_tasks: tasks/check_mode.yml + with_items: '{{ test_config }}' always: - name: Delete temporaray directory - file: + ansible.builtin.file: state: absent path: '{{ tmpdir }}' ignore_errors: true diff --git a/tests/integration/targets/k8s_cluster_info/tasks/main.yml b/tests/integration/targets/k8s_cluster_info/tasks/main.yml index 939cedf697..a643d6f646 100644 --- a/tests/integration/targets/k8s_cluster_info/tasks/main.yml +++ b/tests/integration/targets/k8s_cluster_info/tasks/main.yml @@ -1,20 +1,20 @@ --- - name: Get Information about All APIs - k8s_cluster_info: + kubernetes.core.k8s_cluster_info: register: api_details - name: Print all APIs for debugging - debug: + ansible.builtin.debug: msg: "{{ api_details.apis }}" - name: Get core API version - set_fact: + ansible.builtin.set_fact: crd: "{{ api_details.apis['apiextensions.k8s.io/v1'] }}" host: "{{ api_details.connection['host'] }}" client_version: "{{ api_details.version['client'] }}" - name: Check if all APIs are present - assert: + ansible.builtin.assert: that: - api_details.apis is defined - api_details.apis.v1.Secret is defined diff --git a/tests/integration/targets/k8s_copy/tasks/main.yml b/tests/integration/targets/k8s_copy/tasks/main.yml index 519be8f6f9..5c89234930 100644 --- a/tests/integration/targets/k8s_copy/tasks/main.yml +++ b/tests/integration/targets/k8s_copy/tasks/main.yml @@ -1,11 +1,12 @@ --- -- set_fact: +- name: Set copy_namespace variable + ansible.builtin.set_fact: copy_namespace: "{{ test_namespace }}" - block: # Ensure namespace and create pod to perform tests on - name: Ensure namespace exists - k8s: + kubernetes.core.k8s: definition: apiVersion: v1 kind: Namespace @@ -13,24 +14,27 @@ name: "{{ copy_namespace }}" - name: Create Pods - k8s: + kubernetes.core.k8s: namespace: '{{ copy_namespace }}' wait: yes template: pods_definition.j2 - - include_tasks: test_copy_errors.yml - - include_tasks: test_check_mode.yml - - include_tasks: test_copy_file.yml - - include_tasks: test_multi_container_pod.yml - - include_tasks: test_copy_directory.yml - - include_tasks: test_copy_large_file.yml - - include_tasks: test_copy_item_with_space_in_its_name.yml + - name: Run tests from tasks file + ansible.builtin.include_tasks: "{{ item }}" + with_items: + - test_copy_errors.yml + - test_check_mode.yml + - test_copy_file.yml + - test_multi_container_pod.yml + - test_copy_directory.yml + - test_copy_large_file.yml + - test_copy_item_with_space_in_its_name.yml always: - name: Remove namespace - k8s: + kubernetes.core.k8s: kind: Namespace name: "{{ copy_namespace }}" state: absent - ignore_errors: true + failed_when: false diff --git a/tests/integration/targets/k8s_copy/tasks/test_check_mode.yml b/tests/integration/targets/k8s_copy/tasks/test_check_mode.yml index b246047ef7..4ac8db44de 100644 --- a/tests/integration/targets/k8s_copy/tasks/test_check_mode.yml +++ b/tests/integration/targets/k8s_copy/tasks/test_check_mode.yml @@ -1,26 +1,35 @@ --- - name: Create temporary directory for testing - tempfile: + ansible.builtin.tempfile: state: directory suffix: ansible-k8s-copy register: tmpdir -- block: +- name: Run tests + vars: + local_dir_path: "{{ tmpdir.path }}" + pod_dir_path: "/tmp/test_check_mode" + test_files: + - content: "collection = kubernetes.core" + dest: collection.txt + - content: "modules = k8s_cp and k8s_exec" + dest: modules.txt + block: # setup - name: Create local files for testing - copy: + ansible.builtin.copy: content: "{{ item.content }}" dest: "{{ local_dir_path }}/{{ item.dest }}" with_items: "{{ test_files }}" - name: Create directory into Pod - k8s_exec: + kubernetes.core.k8s_exec: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' command: "mkdir {{ pod_dir_path }}" - name: Create files into Pod - k8s_cp: + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' remote_path: "{{ pod_dir_path }}/{{ item.dest }}" @@ -30,7 +39,7 @@ # Test copy into Pod using check_mode=true - name: Copy text file into remote pod - k8s_cp: + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' remote_path: "{{ pod_dir_path }}/ansible.txt" @@ -40,12 +49,12 @@ register: copy_file - name: Ensure task is changed - assert: + ansible.builtin.assert: that: - copy_file is changed - name: Ensure file does not exists into Pod - k8s_exec: + kubernetes.core.k8s_exec: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' command: "test -e {{ pod_dir_path }}/ansible.txt" @@ -53,7 +62,7 @@ failed_when: test_file.return_code == 0 - name: Copy directory into Pod - k8s_cp: + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' remote_path: "{{ pod_dir_path }}/mydir" @@ -63,12 +72,12 @@ register: copy_dir - name: Ensure task is changed - assert: + ansible.builtin.assert: that: - copy_dir is changed - name: Ensure file does not exists into Pod - k8s_exec: + kubernetes.core.k8s_exec: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' command: "test -e {{ pod_dir_path }}/mydir" @@ -77,7 +86,7 @@ # Test copy from pod using check_mode=true - name: Copy file from Pod into local file system - k8s_cp: + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' remote_path: "{{ pod_dir_path }}/{{ test_files[0].dest }}" @@ -87,18 +96,18 @@ register: copy_file - name: Ensure task is changed - assert: + ansible.builtin.assert: that: - copy_file is changed - name: Ensure file does not exists into local file system - stat: + ansible.builtin.stat: path: "{{ local_dir_path }}/ansible.txt" register: testfile failed_when: testfile.stat.exists - name: Copy directory from Pod into local file system - k8s_cp: + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' remote_path: "{{ pod_dir_path }}" @@ -108,25 +117,16 @@ register: _dir - name: Ensure task is changed - assert: + ansible.builtin.assert: that: - _dir is changed - name: Ensure directory does not exist into local file system - stat: + ansible.builtin.stat: path: "{{ local_dir_path }}/mydir" register: testdir failed_when: testdir.stat.exists - vars: - local_dir_path: "{{ tmpdir.path }}" - pod_dir_path: "/tmp/test_check_mode" - test_files: - - content: "collection = kubernetes.core" - dest: collection.txt - - content: "modules = k8s_cp and k8s_exec" - dest: modules.txt - always: - name: Delete temporary directory file: @@ -135,7 +135,7 @@ ignore_errors: true - name: Delete temporary directory created into Pod - k8s_exec: + kubernetes.core.k8s_exec: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' command: 'rm -r {{ pod_dir_path }}' diff --git a/tests/integration/targets/k8s_copy/tasks/test_copy_directory.yml b/tests/integration/targets/k8s_copy/tasks/test_copy_directory.yml index ed2ad85d8e..97d71a5019 100644 --- a/tests/integration/targets/k8s_copy/tasks/test_copy_directory.yml +++ b/tests/integration/targets/k8s_copy/tasks/test_copy_directory.yml @@ -1,7 +1,7 @@ --- - block: - name: copy directory into remote Pod (create new directory) - k8s_cp: + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' remote_path: /dest_data @@ -16,7 +16,7 @@ local_path: '{{ role_path }}/files/data' - name: copy directory into remote Pod (existing directory) - k8s_cp: + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' remote_path: /tmp @@ -31,7 +31,7 @@ local_path: '{{ role_path }}/files/data' - name: copy directory from Pod into local filesystem (new directory to create) - k8s_cp: + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' remote_path: /tmp/data @@ -46,7 +46,7 @@ local_path: /tmp/test - name: copy directory from Pod into local filesystem (existing directory) - k8s_cp: + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' remote_path: /tmp/data @@ -62,7 +62,7 @@ # Test copy from Pod where the executable 'find' is missing - name: Ensure 'find' is missing from Pod - k8s_exec: + kubernetes.core.k8s_exec: namespace: '{{ copy_namespace }}' pod: '{{ pod_without_executable_find.name }}' command: 'find' @@ -70,13 +70,13 @@ register: _result - name: Validate that 'find' executable is missing from Pod - assert: + ansible.builtin.assert: that: - _result is failed fail_msg: "Pod contains 'find' executable, therefore we cannot run the next tasks." - name: Copy files into container - k8s_cp: + kubernetes.core.k8s_cp: namespace: "{{ copy_namespace }}" pod: '{{ pod_without_executable_find.name }}' remote_path: '{{ item.path }}' @@ -93,13 +93,13 @@ content: this hidden file is located at the root of the sub directory - name: Delete existing directory - file: + ansible.builtin.file: path: /tmp/openjdk-files state: absent ignore_errors: true - name: copy directory from Pod into local filesystem (new directory to create) - k8s_cp: + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_without_executable_find.name }}' remote_path: /ansible @@ -115,7 +115,7 @@ always: - name: Remove directories created into remote Pod - k8s_exec: + kubernetes.core.k8s_exec: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' command: 'rm -rf {{ item }}' @@ -125,7 +125,7 @@ - /tmp/data - name: Remove local directories - file: + ansible.builtin.file: path: '{{ item }}' state: absent ignore_errors: true diff --git a/tests/integration/targets/k8s_copy/tasks/test_copy_errors.yml b/tests/integration/targets/k8s_copy/tasks/test_copy_errors.yml index b1e799bfce..b04090e66c 100644 --- a/tests/integration/targets/k8s_copy/tasks/test_copy_errors.yml +++ b/tests/integration/targets/k8s_copy/tasks/test_copy_errors.yml @@ -1,69 +1,69 @@ --- # copy non-existent local file should fail - name: copy non-existent file into remote Pod - k8s_cp: + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' remote_path: /tmp local_path: this_file_does_not_exist state: to_pod - ignore_errors: true + failed_when: copy_non_existent is successful register: copy_non_existent - name: check that error message is as expected - assert: + ansible.builtin.assert: that: - copy_non_existent is failed - copy_non_existent.msg == "this_file_does_not_exist does not exist in local filesystem" # copy non-existent pod file should fail - name: copy of non-existent file from remote pod should fail - k8s_cp: + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' remote_path: /this_file_does_not_exist local_path: /tmp state: from_pod - ignore_errors: true + failed_when: copy_non_existent is successful register: copy_non_existent - name: check that error message is as expected - assert: + ansible.builtin.assert: that: - copy_non_existent is failed - copy_non_existent.msg == "/this_file_does_not_exist does not exist in remote pod filesystem" # copy file into multiple container pod without specifying the container should fail - name: copy file into multiple container pod - k8s_cp: + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_two_container.name }}' remote_path: /tmp local_path: files/simple_file.txt state: to_pod - ignore_errors: true + failed_when: copy_multi_container is successful register: copy_multi_container -- name: check that error message is as expected - assert: +- name: Check that error message is as expected + ansible.builtin.assert: that: - copy_multi_container is failed - copy_multi_container.msg == "Pod contains more than 1 container, option 'container' should be set" # copy using non-existent container from pod should failed -- name: copy file into multiple container pod - k8s_cp: +- name: Copy file into multiple container pod + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_two_container.name }}' remote_path: /tmp local_path: files/simple_file.txt state: to_pod container: this_is_a_fake_container - ignore_errors: true + failed_when: copy_fake_container is successful register: copy_fake_container -- name: check that error message is as expected - assert: +- name: Check that error message is as expected + ansible.builtin.assert: that: - copy_fake_container is failed - copy_fake_container.msg == "Pod has no container this_is_a_fake_container" diff --git a/tests/integration/targets/k8s_copy/tasks/test_copy_file.yml b/tests/integration/targets/k8s_copy/tasks/test_copy_file.yml index 51d177c07e..4a09f2a3dd 100644 --- a/tests/integration/targets/k8s_copy/tasks/test_copy_file.yml +++ b/tests/integration/targets/k8s_copy/tasks/test_copy_file.yml @@ -1,8 +1,9 @@ --- -- block: +- name: Run tests + block: # Text file - - name: copy text file into remote pod - k8s_cp: + - name: Copy text file into remote pod + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' remote_path: /tmp @@ -17,7 +18,7 @@ content: "{{ lookup('file', 'simple_file.txt')}}" - name: Copy simple text file from Pod - k8s_cp: + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' remote_path: /tmp/simple_file.txt @@ -34,15 +35,15 @@ # Binary file - name: Create temp binary file - tempfile: + ansible.builtin.tempfile: state: file register: binfile - name: Generate random binary content - command: dd if=/dev/urandom of={{ binfile.path }} bs=1M count=1 + ansible.builtin.command: dd if=/dev/urandom of={{ binfile.path }} bs=1M count=1 - name: Copy executable into Pod - k8s_cp: + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' remote_path: /tmp/hello.exe @@ -57,20 +58,21 @@ register: remote_hash - name: Get local hash - command: sha256sum -b {{ binfile.path }} + ansible.builtin.command: sha256sum -b {{ binfile.path }} register: local_hash - - assert: + - name: Validate command result + ansible.builtin.assert: that: - remote_hash.stdout.split()[0] == local_hash.stdout.split()[0] - name: Generate tempfile - tempfile: + ansible.builtin.tempfile: state: file register: binfile - name: Copy executable from Pod - k8s_cp: + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' remote_path: /tmp/hello.exe @@ -85,16 +87,17 @@ register: remote_hash - name: Get local hash - command: sha256sum -b {{ binfile.path }} + ansible.builtin.command: sha256sum -b {{ binfile.path }} register: local_hash - - assert: + - name: Validate command result + ansible.builtin.assert: that: - remote_hash.stdout.split()[0] == local_hash.stdout.split()[0] # zip files - name: copy zip file into remote pod - k8s_cp: + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' remote_path: /tmp @@ -109,7 +112,7 @@ local_path: '{{ role_path }}/files/simple_zip_file.txt.gz' - name: copy zip file from pod into local filesystem - k8s_cp: + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' remote_path: /tmp/simple_zip_file.txt.gz @@ -125,7 +128,7 @@ # tar files - name: copy archive into remote pod - k8s_cp: + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' remote_path: /tmp @@ -139,15 +142,15 @@ remote_path: /tmp/archive.tar local_path: '{{ role_path }}/files/archive.tar' - - name: copy archive from remote pod into local filesystem - k8s_cp: + - name: Copy archive from remote pod into local filesystem + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' remote_path: /tmp/archive.tar local_path: /tmp/local_archive.tar state: from_pod - - name: compare archive + - name: Compare archive kubectl_file_compare: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' @@ -155,12 +158,12 @@ local_path: /tmp/local_archive.tar # Copy into Pod using content option - - name: set content to be copied into Pod - set_fact: + - name: Set content to be copied into Pod + ansible.builtin.set_fact: pod_content: "{{ lookup('password', '/dev/null chars=ascii_lowercase,digits,punctuation length=128') }}" - - name: copy archive into remote pod - k8s_cp: + - name: Copy archive into remote pod + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' remote_path: /this_content.txt @@ -176,11 +179,11 @@ always: - name: Delete file created on Pod - k8s_exec: + kubernetes.core.k8s_exec: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' command: 'rm {{ item }}' - ignore_errors: true + failed_when: false with_items: - /tmp/simple_file.txt - /tmp/hello.exe @@ -189,7 +192,7 @@ - /this_content.txt - name: Delete file created locally - file: + ansible.builtin.file: path: '{{ item }}' state: absent with_items: diff --git a/tests/integration/targets/k8s_copy/tasks/test_copy_item_with_space_in_its_name.yml b/tests/integration/targets/k8s_copy/tasks/test_copy_item_with_space_in_its_name.yml index df3efd6bd4..f60294d9ea 100644 --- a/tests/integration/targets/k8s_copy/tasks/test_copy_item_with_space_in_its_name.yml +++ b/tests/integration/targets/k8s_copy/tasks/test_copy_item_with_space_in_its_name.yml @@ -1,12 +1,14 @@ --- -- name: create temporary directory for testing - tempfile: +- name: Create temporary directory for testing + ansible.builtin.tempfile: state: directory suffix: .space register: _tmpdir -- block: - - set_fact: +- name: Run tests + block: + - name: Set some_file_content variable + ansible.builtin.set_fact: some_file_content: 'this content will be stored into a file in the remote pod which has space in its name' - name: create file with space in the name on remote pod @@ -17,7 +19,8 @@ content: '{{ some_file_content }}' state: to_pod - - set_fact: + - name: Set local_file_path variable + ansible.builtin.set_fact: local_file_path: '{{ _tmpdir.path }}/file_from_pod.txt' - name: copy file (with space in its name) from pod to local filesystem @@ -29,11 +32,12 @@ state: from_pod - name: Ensure file was successfully copied - assert: + ansible.builtin.assert: that: - lookup('file', local_file_path) == some_file_content - - set_fact: + - name: Set dir_config variable + ansible.builtin.set_fact: dir_config: - 'test\ dir\ 01/file1.txt' - 'test\ dir\ 01/file with space in its name.txt' @@ -42,17 +46,18 @@ - 'test\ dir\ 03/file3.txt' - 'test\ dir\ 03/a third file with space in its name' - - set_fact: + - name: Set escape_char variable + ansible.builtin.set_fact: escape_char: \ - - name: create directories on Pod + - name: Create directories on Pod kubernetes.core.k8s_exec: namespace: "{{ copy_namespace }}" pod: "{{ pod_without_executable_find.name }}" command: "mkdir -p /ansible_testing/{{ item | dirname }}" with_items: '{{ dir_config }}' - - name: create files on remote pod + - name: Create files on remote pod kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_without_executable_find.name }}' @@ -61,12 +66,13 @@ state: to_pod with_items: '{{ dir_config }}' - - set_fact: + - name: Set copy variables + ansible.builtin.set_fact: local_copy: '{{ _tmpdir.path }}/local_partial_copy' full_copy: '{{ _tmpdir.path }}/local_full_copy' - - name: create local directories - file: + - name: Create local directories + ansible.builtin.file: state: directory path: '{{ item }}' with_items: @@ -113,8 +119,8 @@ always: - name: Delete temporary directory - file: + ansible.builtin.file: state: absent path: '{{ _tmpdir.path }}' - ignore_errors: true + failed_when: false when: _tmpdir is defined diff --git a/tests/integration/targets/k8s_copy/tasks/test_copy_large_file.yml b/tests/integration/targets/k8s_copy/tasks/test_copy_large_file.yml index 27d4050ac2..3db220b844 100644 --- a/tests/integration/targets/k8s_copy/tasks/test_copy_large_file.yml +++ b/tests/integration/targets/k8s_copy/tasks/test_copy_large_file.yml @@ -1,11 +1,12 @@ --- -- name: test copy of large binary and text files +- name: Test copy of large binary and text files block: - - set_fact: + - name: Set test_directory variable + ansible.builtin.set_fact: test_directory: "/tmp/test_k8scp_large_files" no_log: true - - name: create temporary directory for local files + - name: Create temporary directory for local files ansible.builtin.file: path: "{{ test_directory }}" state: directory @@ -20,7 +21,7 @@ # Copy large text file from/to local filesystem to Pod - name: copy large file into remote Pod - k8s_cp: + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' remote_path: /large_text_file.txt @@ -34,8 +35,8 @@ remote_path: /large_text_file.txt local_path: "{{ test_directory }}/large_text_file.txt" - - name: copy large file from Pod into local filesystem - k8s_cp: + - name: Copy large file from Pod into local filesystem + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' remote_path: /large_text_file.txt @@ -51,7 +52,7 @@ # Copy large binary file from/to local filesystem to Pod - name: copy large file into remote Pod - k8s_cp: + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' remote_path: /large_bin_file.bin @@ -66,7 +67,7 @@ local_path: "{{ test_directory }}/large_bin_file.bin" - name: copy executable from pod into local filesystem - k8s_cp: + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' remote_path: /large_bin_file.bin @@ -85,14 +86,14 @@ ansible.builtin.file: path: "{{ test_directory }}" state: absent - ignore_errors: true + failed_when: false - name: Delete file created on Pod k8s_exec: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_one_container.name }}' command: 'rm {{ item }}' - ignore_errors: true + failed_when: false with_items: - /large_text_file.txt - /large_bin_file.bin diff --git a/tests/integration/targets/k8s_copy/tasks/test_multi_container_pod.yml b/tests/integration/targets/k8s_copy/tasks/test_multi_container_pod.yml index d278855bec..4ee15a5207 100644 --- a/tests/integration/targets/k8s_copy/tasks/test_multi_container_pod.yml +++ b/tests/integration/targets/k8s_copy/tasks/test_multi_container_pod.yml @@ -1,9 +1,10 @@ --- -- set_fact: +- name: Set random_content variable + ansible.builtin.set_fact: random_content: "{{ lookup('password', '/dev/null chars=ascii_lowercase,digits,punctuation length=128') }}" - name: Copy content into first pod's container - k8s_cp: + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_two_container.name }}' remote_path: /file_from_localhost.txt @@ -29,13 +30,13 @@ register: diff ignore_errors: true -- name: check that diff failed - assert: +- name: Check that diff failed + ansible.builtin.assert: that: - diff is failed - name: Copy content into second's pod container - k8s_cp: + kubernetes.core.k8s_cp: namespace: '{{ copy_namespace }}' pod: '{{ pod_with_two_container.name }}' remote_path: /file_from_localhost_01.txt @@ -53,8 +54,8 @@ ignore_errors: true register: diff_1 -- name: check that diff failed - assert: +- name: Check that diff failed + ansible.builtin.assert: that: - diff_1 is failed diff --git a/tests/integration/targets/k8s_validate/tasks/main.yml b/tests/integration/targets/k8s_validate/tasks/main.yml index fb9fc199cc..cf6ed9d37d 100644 --- a/tests/integration/targets/k8s_validate/tasks/main.yml +++ b/tests/integration/targets/k8s_validate/tasks/main.yml @@ -221,7 +221,7 @@ ansible.builtin.assert: that: - '"This is required to use in-memory config." in _result.msg' - + always: - name: Remove temp directory ansible.builtin.file: