From 54ea54c26b777709fd636b376cce1d5078ae287c Mon Sep 17 00:00:00 2001 From: Emilien Macchi Date: Fri, 29 May 2020 17:25:21 -0400 Subject: [PATCH] Molecule testing for TripleO Strategy Plugins For both tripleo_free and tripleo_linear, add molecule tests that will execute a simple task (for now) and test the max_fail_percentage value that is defined in the Ansible inventory. We simulate a failure on one compute node, which shouldn't make the playbook failing since we tolerate 50% of failure (we have 6 nodes and one failure). Change-Id: I97a0171f9e7ba08f020ac7cf224a9ff9f6a34d01 --- ansible-test-env.rc | 1 + .../tripleo_strategies/ansible-test-env.rc | 1 + .../molecule/tripleo_strategies/converge.yml | 59 +++++++++++++++++++ .../tripleo_strategies/inventory-fail | 16 +++++ .../tripleo_strategies/inventory-success | 16 +++++ .../molecule/tripleo_strategies/molecule.yml | 38 ++++++++++++ .../tripleo_strategies/tripleo-free.yml | 40 +++++++++++++ .../tripleo_strategies/tripleo-linear.yml | 40 +++++++++++++ 8 files changed, 211 insertions(+) create mode 120000 tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/ansible-test-env.rc create mode 100644 tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/converge.yml create mode 100644 tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/inventory-fail create mode 100644 tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/inventory-success create mode 100644 tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/molecule.yml create mode 100644 tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/tripleo-free.yml create mode 100644 tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/tripleo-linear.yml diff --git a/ansible-test-env.rc b/ansible-test-env.rc index 4db655f97..8b088cf17 100644 --- a/ansible-test-env.rc +++ b/ansible-test-env.rc @@ -2,6 +2,7 @@ export TRIPLEO_ANSIBLE_WORKPATH="$(dirname $(readlink -f ${BASH_SOURCE[0]}))" export ANSIBLE_STDOUT_CALLBACK=debug export ANSIBLE_ACTION_PLUGINS="${TRIPLEO_ANSIBLE_WORKPATH}/tripleo_ansible/roles.galaxy/config_template/action:${TRIPLEO_ANSIBLE_WORKPATH}/tripleo_ansible/ansible_plugins/action" export ANSIBLE_CALLBACK_PLUGINS="${TRIPLEO_ANSIBLE_WORKPATH}/tripleo_ansible/ansible_plugins/callback" +export ANSIBLE_STRATEGY_PLUGINS="${TRIPLEO_ANSIBLE_WORKPATH}/tripleo_ansible/ansible_plugins/strategy" export ANSIBLE_FILTER_PLUGINS="${TRIPLEO_ANSIBLE_WORKPATH}/tripleo_ansible/ansible_plugins/filter" export ANSIBLE_LIBRARY="${TRIPLEO_ANSIBLE_WORKPATH}/tripleo_ansible/roles.galaxy/config_template/library:${TRIPLEO_ANSIBLE_WORKPATH}/tripleo_ansible/ansible_plugins/modules" export ANSIBLE_MODULE_UTILS="${TRIPLEO_ANSIBLE_WORKPATH}/tripleo_ansible/ansible_plugins/module_utils" diff --git a/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/ansible-test-env.rc b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/ansible-test-env.rc new file mode 120000 index 000000000..d889a544f --- /dev/null +++ b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/ansible-test-env.rc @@ -0,0 +1 @@ +../../../../../ansible-test-env.rc \ No newline at end of file diff --git a/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/converge.yml b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/converge.yml new file mode 100644 index 000000000..08b5bd91e --- /dev/null +++ b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/converge.yml @@ -0,0 +1,59 @@ +--- +# Copyright 2020 Red Hat, Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +- name: Run strategy tests + hosts: localhost + connection: local + gather_facts: false + environment: + ANSIBLE_ROLES_PATH: "${ANSIBLE_ROLES_PATH:-/usr/share/ansible/roles}:${HOME}/zuul-jobs/roles" + ANSIBLE_LIBRARY: "${ANSIBLE_LIBRARY:-/usr/share/ansible/plugins/modules}" + ANSIBLE_FILTER_PLUGINS: "${ANSIBLE_FILTER_PLUGINS:-/usr/share/ansible/plugins/filter}" + ANSIBLE_STRATEGY_PLUGINS: "${ANSIBLE_STRATEGY_PLUGINS:-/usr/share/ansible/plugins/strategy}" + tasks: + - name: Run TripleO linear success + shell: |- + . {{ playbook_dir }}/ansible-test-env.rc + ansible-playbook {{ playbook_dir }}/tripleo-linear.yml -i {{ playbook_dir }}/inventory-success | grep "second" + args: + executable: /bin/bash + register: linear_success + + - name: Run TripleO linear fail + shell: |- + . {{ playbook_dir }}/ansible-test-env.rc + ansible-playbook {{ playbook_dir }}/tripleo-linear.yml -i {{ playbook_dir }}/inventory-fail | grep "second" + args: + executable: /bin/bash + register: linear_fail + failed_when: linear_fail.rc == 0 + + - name: Run TripleO free success + shell: |- + . {{ playbook_dir }}/ansible-test-env.rc + ansible-playbook {{ playbook_dir }}/tripleo-free.yml -i {{ playbook_dir }}/inventory-success | grep "second" + args: + executable: /bin/bash + register: free_success + + - name: Run TripleO free fail + shell: |- + . {{ playbook_dir }}/ansible-test-env.rc + ansible-playbook {{ playbook_dir }}/tripleo-free.yml -i {{ playbook_dir }}/inventory-fail | grep "second" + args: + executable: /bin/bash + register: free_fail + failed_when: free_fail.rc == 0 diff --git a/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/inventory-fail b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/inventory-fail new file mode 100644 index 000000000..803dbd47b --- /dev/null +++ b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/inventory-fail @@ -0,0 +1,16 @@ +[Controller] +instance1 ansible_connection=local +instance2 ansible_connection=local +instance3 ansible_connection=local + +[Compute] +instance4 ansible_connection=local +instance5 ansible_connection=local + +[Controller:vars] +max_fail_percentage=0 +tripleo_role_name=Controller + +[Compute:vars] +max_fail_percentage=10 +tripleo_role_name=Compute diff --git a/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/inventory-success b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/inventory-success new file mode 100644 index 000000000..55a847809 --- /dev/null +++ b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/inventory-success @@ -0,0 +1,16 @@ +[Controller] +instance1 ansible_connection=local +instance2 ansible_connection=local +instance3 ansible_connection=local + +[Compute] +instance4 ansible_connection=local +instance5 ansible_connection=local + +[Controller:vars] +max_fail_percentage=0 +tripleo_role_name=Controller + +[Compute:vars] +max_fail_percentage=100 +tripleo_role_name=Compute diff --git a/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/molecule.yml b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/molecule.yml new file mode 100644 index 000000000..bd84541d6 --- /dev/null +++ b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/molecule.yml @@ -0,0 +1,38 @@ +--- +driver: + name: delegated + options: + managed: false + login_cmd_template: >- + ssh + -o UserKnownHostsFile=/dev/null + -o StrictHostKeyChecking=no + -o Compression=no + -o TCPKeepAlive=yes + -o VerifyHostKeyDNS=no + -o ForwardX11=no + -o ForwardAgent=no + {instance} + ansible_connection_options: + ansible_connection: ssh + +log: true + +platforms: + - name: instance1 + +provisioner: + name: ansible + config_options: + defaults: + fact_caching: jsonfile + fact_caching_connection: /tmp/molecule/facts + log: true + +scenario: + test_sequence: + - converge + - verify + +verifier: + name: testinfra diff --git a/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/tripleo-free.yml b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/tripleo-free.yml new file mode 100644 index 000000000..05c1559b0 --- /dev/null +++ b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/tripleo-free.yml @@ -0,0 +1,40 @@ +--- +# Copyright 2020 Red Hat, Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +- name: Run a first play with tests for the tripleo_linear strategy + hosts: all + any_errors_fatal: true + gather_facts: false + strategy: tripleo_free + tasks: + - name: Run a task that will fail on one host + debug: + msg: "Task running with tripleo_linear strategy with max_fail_percentage set to {{ max_fail_percentage|default('undefined') }}" + failed_when: + - ansible_host in ['instance5'] + - name: Run a task after a potential failure on a node + debug: + msg: "Task running on {{ ansible_host }} after a potential failure" + +- name: Run a second play with tests for the tripleo_linear strategy + hosts: all + any_errors_fatal: true + gather_facts: false + strategy: tripleo_free + tasks: + - name: Run a task on a node + debug: + msg: "Task running on {{ ansible_host }}" diff --git a/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/tripleo-linear.yml b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/tripleo-linear.yml new file mode 100644 index 000000000..a4a1e0572 --- /dev/null +++ b/tripleo_ansible/ansible_plugins/tests/molecule/tripleo_strategies/tripleo-linear.yml @@ -0,0 +1,40 @@ +--- +# Copyright 2020 Red Hat, Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +- name: Run a first play with tests for the tripleo_linear strategy + hosts: all + any_errors_fatal: true + gather_facts: false + strategy: tripleo_linear + tasks: + - name: Run a task that will fail on one host + debug: + msg: "Task running with tripleo_linear strategy with max_fail_percentage set to {{ max_fail_percentage|default('undefined') }}" + failed_when: + - ansible_host in ['instance5'] + - name: Run a task after a potential failure on a node + debug: + msg: "Task running on {{ ansible_host }} after a potential failure" + +- name: Run a second play with tests for the tripleo_linear strategy + hosts: all + any_errors_fatal: true + gather_facts: false + strategy: tripleo_linear + tasks: + - name: Run a task on a node + debug: + msg: "Task running on {{ ansible_host }}"