From 0f33aafca72108410f77ed3b36263450b8035184 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Thu, 1 Oct 2020 19:26:40 +0530 Subject: [PATCH] Add testcase for multi resource template (#245) User can combine multiple resources in the given template file, which can be consumed by k8s module to create multiple resources at once. Fixes: #243 Signed-off-by: Abhijeet Kasurde --- changelogs/fragments/243_template.yml | 2 ++ molecule/default/tasks/template.yml | 24 +++++++++---- .../default/templates/pod_template_three.j2 | 35 +++++++++++++++++++ 3 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 changelogs/fragments/243_template.yml create mode 100644 molecule/default/templates/pod_template_three.j2 diff --git a/changelogs/fragments/243_template.yml b/changelogs/fragments/243_template.yml new file mode 100644 index 00000000..9a1630eb --- /dev/null +++ b/changelogs/fragments/243_template.yml @@ -0,0 +1,2 @@ +minor_changes: +- k8s - add testcase for adding multiple resources using template parameter (https://github.com/ansible-collections/community.kubernetes/issues/243). diff --git a/molecule/default/tasks/template.yml b/molecule/default/tasks/template.yml index ea9344b3..4d76d799 100644 --- a/molecule/default/tasks/template.yml +++ b/molecule/default/tasks/template.yml @@ -132,20 +132,32 @@ that: - r is successful + - name: Create pods using multi-resource template + community.kubernetes.k8s: + template: + path: "pod_template_three.j2" + wait: yes + vars: + k8s_pod_name_one: pod-5 + k8s_pod_name_two: pod-6 + k8s_pod_namespace: "{{ template_namespace }}" + register: r + + - name: Assert that pod creation succeeded using template + assert: + that: + - r is successful + - name: Remove Pod (Cleanup) k8s: api_version: v1 kind: Pod - name: "{{ item }}" + name: "pod-{{ item }}" namespace: "{{ template_namespace }}" state: absent wait: yes ignore_errors: yes - with_items: - - pod-1 - - pod-2 - - pod-3 - - pod-4 + loop: "{{ range(1, 7) | list }}" always: - name: Remove namespace (Cleanup) diff --git a/molecule/default/templates/pod_template_three.j2 b/molecule/default/templates/pod_template_three.j2 new file mode 100644 index 00000000..06e4686e --- /dev/null +++ b/molecule/default/templates/pod_template_three.j2 @@ -0,0 +1,35 @@ +--- +apiVersion: v1 +kind: Pod +metadata: + labels: + app: "{{ k8s_pod_name_one }}" + name: '{{ k8s_pod_name_one }}' + namespace: '{{ k8s_pod_namespace }}' +spec: + containers: + - args: + - /bin/sh + - -c + - while true; do echo $(date); sleep 10; done + image: python:3.7-alpine + imagePullPolicy: Always + name: '{{ k8s_pod_name_one }}' + +--- +apiVersion: v1 +kind: Pod +metadata: + labels: + app: "{{ k8s_pod_name_two }}" + name: '{{ k8s_pod_name_two }}' + namespace: '{{ k8s_pod_namespace }}' +spec: + containers: + - args: + - /bin/sh + - -c + - while true; do echo $(date); sleep 10; done + image: python:3.7-alpine + imagePullPolicy: Always + name: '{{ k8s_pod_name_two }}'