From c8cc84cb2be01ded87f7b01664d610284adceb8a Mon Sep 17 00:00:00 2001 From: esaka Date: Dec 08 2017 09:43:18 +0000 Subject: [PATCH 1/4] Adding standard-test-behave-role for running behave tests --- diff --git a/roles/standard-test-behave/README.md b/roles/standard-test-behave/README.md new file mode 100644 index 0000000..3c9ff94 --- /dev/null +++ b/roles/standard-test-behave/README.md @@ -0,0 +1,16 @@ +# Ansible role for Beakerlib 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-beakerlib/vars/main.yml: + + * tests: A list of beakerlib test files + * artifacts: An artifacts directory on localhost to store logs + * remote_artifacts: The directory on the system under test + where the logs are stored. Note: if this variable is left + undefined, it will default to /tmp/artifacts + * required_packages: A list of prerequisite packages required by + beakerlib tests. Note: will be disregarded on Atomic Host, etc. + where additional packages can't be installed diff --git a/roles/standard-test-behave/defaults/main.yml b/roles/standard-test-behave/defaults/main.yml new file mode 100644 index 0000000..1650168 --- /dev/null +++ b/roles/standard-test-behave/defaults/main.yml @@ -0,0 +1,2 @@ +--- +artifacts: "{{ lookup('env', 'TEST_ARTIFACTS')|default('./artifacts', true) }}" diff --git a/roles/standard-test-behave/files/rpm.py b/roles/standard-test-behave/files/rpm.py new file mode 100644 index 0000000..83093be --- /dev/null +++ b/roles/standard-test-behave/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/roles/standard-test-behave/tasks/main.yml b/roles/standard-test-behave/tasks/main.yml new file mode 100644 index 0000000..8b9d1f8 --- /dev/null +++ b/roles/standard-test-behave/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/roles/standard-test-behave/vars/main.yml b/roles/standard-test-behave/vars/main.yml new file mode 100644 index 0000000..7c086e9 --- /dev/null +++ b/roles/standard-test-behave/vars/main.yml @@ -0,0 +1,6 @@ +--- +remote_artifacts: /tmp/artifacts +tests_folder: '.' +features: [] +required_packages: [] +run_in_background: false From be7b224e9b29b407d4e03c40f798fee1469c9cd4 Mon Sep 17 00:00:00 2001 From: esakaiev Date: Dec 08 2017 15:17:42 +0000 Subject: [PATCH 2/4] Improving standard-test-behave tasks --- diff --git a/roles/standard-test-behave/tasks/main.yml b/roles/standard-test-behave/tasks/main.yml index 8b9d1f8..efc5c04 100644 --- a/roles/standard-test-behave/tasks/main.yml +++ b/roles/standard-test-behave/tasks/main.yml @@ -80,38 +80,54 @@ - block: - name: Running behave tests shell: | + check_cpu_usage() + { + echo $(grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage}') + } export OUTPUTFILE=/dev/stdout TEST={{ item }} - exec 2>>$logfile 1>>$logfile + logfile={{ remote_artifacts }}/test.$(echo {{ item }} | sed -e 's/\//-/g').log cd /usr/local/bin cd {{ tests_folder }} + cpu_usage=$( check_cpu_usage ) + while [ $cpu_usage \> 80 ] + do + sleep 1 + cpu_usage=$( check_cpu_usage ) + done if [ -f runtest.sh ]; then - /bin/sh -e ./runtest.sh {{ item }} & + /bin/sh -e ./runtest.sh {{ item }} & + echo $! elif [ -f Makefile ]; then make ARGS="{{item}}" run & + echo $! else echo "FAIL don't know how to run test {{ item }}" fi + + register: proc_id with_items: - "{{ features }}" when: run_in_background + + - name: Waiting to finish of the behave background processes shell: | - echo {{ item }} - while docker ps | grep {{ item }} + while ps -p {{ item.stdout }}| grep {{ item.stdout }} do sleep 2 done when: run_in_background with_items: - - "{{ features }}" + - "{{ proc_id.results }}" + - 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 cat $logfile > {{ remote_artifacts }}/$logfile sudo rm $logfile when: run_in_background with_items: From 8f69fd58e8f48d7609515a1ad129321efbecf6c3 Mon Sep 17 00:00:00 2001 From: esakaiev Date: Jan 23 2018 15:06:49 +0000 Subject: [PATCH 3/4] Adding changes for standard-test-behave role in standart-test-roles, removed rpm and default folders --- diff --git a/roles/standard-test-behave/README.md b/roles/standard-test-behave/README.md index 3c9ff94..4a7be15 100644 --- a/roles/standard-test-behave/README.md +++ b/roles/standard-test-behave/README.md @@ -4,13 +4,16 @@ 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-beakerlib/vars/main.yml: +roles/standard-test-behave/vars/main.yml: - * tests: A list of beakerlib test files + * run_in_background: A list of beakerlib test files * artifacts: An artifacts directory on localhost to store logs * remote_artifacts: The directory on the system under test where the logs are stored. Note: if this variable is left undefined, it will default to /tmp/artifacts * required_packages: A list of prerequisite packages required by - beakerlib tests. Note: will be disregarded on Atomic Host, etc. + behave tests. Note: will be disregarded on Atomic Host, etc. where additional packages can't be installed + * tests_folder - ("." by default) - path folder, where need to execute tests + * run_in_background - (true/false) option for behave tests, if need to runall + tests in parallel as daemon process diff --git a/roles/standard-test-behave/defaults/main.yml b/roles/standard-test-behave/defaults/main.yml deleted file mode 100644 index 1650168..0000000 --- a/roles/standard-test-behave/defaults/main.yml +++ /dev/null @@ -1,2 +0,0 @@ ---- -artifacts: "{{ lookup('env', 'TEST_ARTIFACTS')|default('./artifacts', true) }}" diff --git a/roles/standard-test-behave/files/rpm.py b/roles/standard-test-behave/files/rpm.py deleted file mode 100644 index 83093be..0000000 --- a/roles/standard-test-behave/files/rpm.py +++ /dev/null @@ -1,15 +0,0 @@ -#!/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/roles/standard-test-behave/tasks/main.yml b/roles/standard-test-behave/tasks/main.yml index efc5c04..e1a62da 100644 --- a/roles/standard-test-behave/tasks/main.yml +++ b/roles/standard-test-behave/tasks/main.yml @@ -11,18 +11,6 @@ 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: @@ -31,7 +19,7 @@ - block: - name: Install the behave requirements on target - package: name={{item}} state=latest + package: name={{item}} state=present with_items: - python2-behave @@ -60,18 +48,8 @@ - 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 + cd /usr/local/bin/{{ tests_folder }} + ./dnf-testing.sh run {{ item }} > test.{{ item }}.log 2>&1 with_items: - "{{ features }}" when: not run_in_background @@ -80,37 +58,14 @@ - block: - name: Running behave tests shell: | - check_cpu_usage() - { - echo $(grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage}') - } - export OUTPUTFILE=/dev/stdout TEST={{ item }} - logfile={{ remote_artifacts }}/test.$(echo {{ item }} | sed -e 's/\//-/g').log - cd /usr/local/bin - cd {{ tests_folder }} - cpu_usage=$( check_cpu_usage ) - while [ $cpu_usage \> 80 ] - do - sleep 1 - cpu_usage=$( check_cpu_usage ) - done - if [ -f runtest.sh ]; then - /bin/sh -e ./runtest.sh {{ item }} & - echo $! - elif [ -f Makefile ]; then - make ARGS="{{item}}" run & - echo $! - else - echo "FAIL don't know how to run test {{ item }}" - fi - + cd /usr/local/bin/{{ tests_folder }} + ./dnf-testing.sh run {{ item }} > test.{{ item }}.log 2>&1 & + echo $! register: proc_id with_items: - "{{ features }}" when: run_in_background - - - name: Waiting to finish of the behave background processes shell: | while ps -p {{ item.stdout }}| grep {{ item.stdout }} @@ -121,28 +76,30 @@ with_items: - "{{ proc_id.results }}" - - name: Move logs to the remote_artifacts shell: | logfile=test.$(echo {{ item }} | sed -e 's/\//-/g').log - cd /usr/local/bin - cd {{ tests_folder }} + cd /usr/local/bin/{{ 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 + echo FAIL else echo "PASS {{ item }}" >> {{ remote_artifacts }}/test.log + echo PASS fi + register: test_results + failed_when: "'FAIL' in test_results.stdout" with_items: - "{{ features }}" @@ -153,10 +110,3 @@ 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 From abe6b92f0c3353c9eccf34a39550115ceb4d8917 Mon Sep 17 00:00:00 2001 From: esakaiev Date: Jan 23 2018 15:23:35 +0000 Subject: [PATCH 4/4] Modified README.md for standard-behave-test --- diff --git a/roles/standard-test-behave/README.md b/roles/standard-test-behave/README.md index 4a7be15..456a98f 100644 --- a/roles/standard-test-behave/README.md +++ b/roles/standard-test-behave/README.md @@ -1,19 +1,16 @@ -# Ansible role for Beakerlib tests +# Ansible role for 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. +Put this role in your test_local.yml playbook. You can redefine the following variables in roles/standard-test-behave/vars/main.yml: - * run_in_background: A list of beakerlib test files + * features: A list of behave test files * artifacts: An artifacts directory on localhost to store logs * remote_artifacts: The directory on the system under test where the logs are stored. Note: if this variable is left undefined, it will default to /tmp/artifacts * required_packages: A list of prerequisite packages required by - behave tests. Note: will be disregarded on Atomic Host, etc. - where additional packages can't be installed + behave tests. * tests_folder - ("." by default) - path folder, where need to execute tests * run_in_background - (true/false) option for behave tests, if need to runall tests in parallel as daemon process