From fae934c186d614202d2d168463e79bb6825a83c7 Mon Sep 17 00:00:00 2001 From: esaka Date: Dec 06 2017 11:33:56 +0000 Subject: Adding standard behave test role to the standard_test_behave branch --- diff --git a/README.md b/README.md new file mode 100644 index 0000000..8cf114e --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# Behave role for Running behave tests + +Put this role in your test_local.yml playbook. The playbook +will first install beakerlib and restraint binaries on +your localhost, then it will proceed to run testing. +You can redefine the following variables in +roles/standard-test-behave/vars/main.yml: + diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..1650168 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,2 @@ +--- +artifacts: "{{ lookup('env', 'TEST_ARTIFACTS')|default('./artifacts', true) }}" diff --git a/files/rpm.py b/files/rpm.py new file mode 100644 index 0000000..83093be --- /dev/null +++ b/files/rpm.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python + +# The purpose of this file is to satisfy /usr/bin/beakerlib-journalling use of "import rpm", +# particularly on Atomic Host which doesn't have python2-rpm package. beakerlib-journalling +# does basically nothing interesting with the data, and just gets the version of the RPM. + +# Although tests that have been looked at so far work just fine with TransactionSet.dbMatch() +# returning empty results, it may be the case this needs to be extended to add further shim code. + +class TransactionSet: + def dbMatch(self, key, value): + return [] + +def ts(): + return TransactionSet() diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..8b9d1f8 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,146 @@ +--- +- name: Add executor host + add_host: + name: executor + ansible_connection: local + ansible_ssh_host: 127.0.0.1 + ansible_ssh_connection: local + +- block: + - name: Gather facts + setup: + delegate_facts: True + + - name: Add restraint repo for restraint-rhts on Fedora hosts + get_url: + url: https://copr.fedorainfracloud.org/coprs/bpeck/restraint/repo/fedora-{{ansible_distribution_major_version}}/bpeck-restraint-fedora-{{ansible_distribution_major_version}}.repo + dest: /etc/yum.repos.d/restraint.repo + when: ansible_distribution == 'Fedora' + + - name: Add restraint repo for restraint-rhts on RHEL/CentOS hosts + get_url: + url: https://copr.fedorainfracloud.org/coprs/bpeck/restraint/repo/epel-7/bpeck-restraint-epel-7.repo + dest: /etc/yum.repos.d/restraint.repo + when: ansible_distribution == 'RedHat' or ansible_distribution == 'CentOS' + + - name: Install the behave requirements + package: name={{item}} state=latest + with_items: + - python2-behave + delegate_to: executor + +- block: + - name: Install the behave requirements on target + package: name={{item}} state=latest + with_items: + - python2-behave + + - name: Install any test-specific package requirements + package: name={{item}} state=latest + with_items: + - "{{ required_packages }}" + + # Only manually install packages on non atomic hosts + when: ansible_pkg_mgr != 'unknown' + +- name: Define remote_artifacts if it is not already defined + set_fact: + remote_artifacts: /tmp/artifacts + when: remote_artifacts is not defined + +- name: Copy tests to target + synchronize: + src: "{{ playbook_dir }}/" + dest: /usr/local/bin/ + ssh_args: "-o UserKnownHostsFile=/dev/null" + +- name: Make artifacts directory + file: path={{ remote_artifacts }} state=directory owner=root mode=755 recurse=yes + +- block: + - name: Running behave tests + shell: | + export OUTPUTFILE=/dev/stdout TEST={{ item }} + logfile={{ remote_artifacts }}/test.$(echo {{ item }} | sed -e 's/\//-/g').log + exec 2>>$logfile 1>>$logfile + cd /usr/local/bin + cd {{ tests_folder }} + if [ -f runtest.sh ]; then + /bin/sh -e ./runtest.sh {{ item }} + elif [ -f Makefile ]; then + make ARGS="{{item}}" run + else + echo "FAIL don't know how to run test {{ item }}" + fi + with_items: + - "{{ features }}" + when: not run_in_background + + +- block: + - name: Running behave tests + shell: | + export OUTPUTFILE=/dev/stdout TEST={{ item }} + exec 2>>$logfile 1>>$logfile + cd /usr/local/bin + cd {{ tests_folder }} + if [ -f runtest.sh ]; then + /bin/sh -e ./runtest.sh {{ item }} & + elif [ -f Makefile ]; then + make ARGS="{{item}}" run & + else + echo "FAIL don't know how to run test {{ item }}" + fi + with_items: + - "{{ features }}" + when: run_in_background + + - name: Waiting to finish of the behave background processes + shell: | + echo {{ item }} + while docker ps | grep {{ item }} + do + sleep 2 + done + when: run_in_background + with_items: + - "{{ features }}" + + - name: Move logs to the remote_artifacts + shell: | + logfile=test.$(echo {{ item }} | sed -e 's/\//-/g').log + cd /usr/local/bin + cd {{ tests_folder }} + sudo cat $logfile >> {{ remote_artifacts }}/$logfile + sudo rm $logfile + when: run_in_background + with_items: + - "{{ features }}" + + + always: + - name: Make the master test summary log artifact + shell: | + logfile={{ remote_artifacts }}/test.$(echo {{ item }} | sed -e 's/\//-/g').log + if grep -P ', ([1-9]*) (failed)' "$logfile"; then + echo "FAIL {{ item }}" >> {{ remote_artifacts }}/test.log + else + echo "PASS {{ item }}" >> {{ remote_artifacts }}/test.log + fi + with_items: + - "{{ features }}" + + - name: Pull out the logs + synchronize: + dest: "{{ artifacts }}/" + src: "{{ remote_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" {{ remote_artifacts }}/test.log + register: test_fails + failed_when: test_fails.stdout or test_fails.stderr diff --git a/vars/main.yml b/vars/main.yml new file mode 100644 index 0000000..7c086e9 --- /dev/null +++ b/vars/main.yml @@ -0,0 +1,6 @@ +--- +remote_artifacts: /tmp/artifacts +tests_folder: '.' +features: [] +required_packages: [] +run_in_background: false