From 867ea89324c47916ad1b6a31d5d14c393be569de Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Sep 25 2017 17:11:19 +0000 Subject: standard-test-scripts: Ansible role for executing test scripts Put this role in your tests.yml playbook and specify a number of test scripts to execute as tests. The results of each script will be piped into log file in the artifacts directory. If any script exits with a non-zero exit code the role will fail. In the case of test subjects such a host or container, these test scripts and related files will be copied to the target into /var/tmp/tests before execution. You should define the following variables: * tests: An array of scripts to run * files: A list of files or directories needed by the scripts --- diff --git a/roles/standard-test-scripts/README.md b/roles/standard-test-scripts/README.md new file mode 100644 index 0000000..d407bcc --- /dev/null +++ b/roles/standard-test-scripts/README.md @@ -0,0 +1,17 @@ +# Ansible role for executing test scripts + +Put this role in your tests.yml playbook and specify a +number of test scripts to execute as tests. The results +of each script will be piped into log file in the artifacts +directory. If any script exits with a non-zero exit code +the role will fail. + +In the case of test subjects such a host or container, these +test scripts and related files will be copied to the target into +/var/tmp/tests before execution. + +You should define the following variables: + + * tests: An array of scripts to run + * files: A list of files or directories needed by the scripts + diff --git a/roles/standard-test-scripts/defaults/main.yml b/roles/standard-test-scripts/defaults/main.yml new file mode 100644 index 0000000..ceb88f4 --- /dev/null +++ b/roles/standard-test-scripts/defaults/main.yml @@ -0,0 +1,2 @@ +--- +artifacts: "{{ lookup('env', 'TEST_ARTIFACTS') | default('./artifacts', true) }}" diff --git a/roles/standard-test-scripts/tasks/main.yml b/roles/standard-test-scripts/tasks/main.yml new file mode 100644 index 0000000..bddd620 --- /dev/null +++ b/roles/standard-test-scripts/tasks/main.yml @@ -0,0 +1,48 @@ +--- +- name: Install testing requirements + package: name={{ item }} state=present + with_items: + - rsync + when: ansible_pkg_mgr != 'unknown' + +- name: Copy test scripts to target + synchronize: + src: "{{ item }}" + dest: /var/tmp/tests/ + ssh_args: "-o UserKnownHostsFile=/dev/null" + with_items: + - "{{ tests }}" + - "{{ files }}" + +- block: + - name: Execute test scripts + shell: | + set -e + mkdir -p /var/tmp/artifacts + logfile=/var/tmp/artifacts/test.$(echo {{ item }} | sed -e 's/\//-/g').log + exec 2>>$logfile 1>>$logfile + cd /var/tmp/tests + if [ -x {{ item }} ]; then + ./$(basename {{ item }}) && result="PASS" || result="FAIL" + else + /bin/sh -e ./$(basename {{ item }}) && result="PASS" || result="FAIL" + fi + echo "$result {{ item }}" >> /var/tmp/artifacts/test.log + with_items: + - "{{ tests }}" + + always: + - name: Pull out the logs + synchronize: + dest: "{{ artifacts }}" + src: "/var/tmp/artifacts/./" + mode: pull + ssh_args: "-o UserKnownHostsFile=/dev/null" + when: artifacts|default("") != "" + + # Can't go in block. See + # https://github.com/ansible/ansible/issues/20736 + - name: Check the results + shell: grep "^FAIL" /var/tmp/artifacts/test.log + register: test_fails + failed_when: test_fails.stdout or test_fails.stderr diff --git a/roles/standard-test-scripts/vars/main.yml b/roles/standard-test-scripts/vars/main.yml new file mode 100644 index 0000000..81108ab --- /dev/null +++ b/roles/standard-test-scripts/vars/main.yml @@ -0,0 +1,3 @@ +--- +tests: [] +files: []