From a4b7d43ebb2163d570639dc6b35186ba3df5f21a Mon Sep 17 00:00:00 2001 From: Bruno Goncalves Date: Jan 12 2018 08:42:08 +0000 Subject: created standard-test-basic role --- diff --git a/roles/standard-test-basic/README.md b/roles/standard-test-basic/README.md new file mode 100644 index 0000000..ad17c20 --- /dev/null +++ b/roles/standard-test-basic/README.md @@ -0,0 +1,19 @@ +# Ansible role for tests using basic scripts + +Put this role in your test_local.yml playbook. The playbook +will first install package dependencies listed on playbook on +your localhost, then it will proceed to run testing. +You can redefine the following variables in +roles/standard-test-basic/vars/main.yml: + + * tests: A list of test cases + * 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 + tests. Please note that for Atomic Host, additional + packages will be installed using the rpm-ostree command which + is affecting the test subject (it's similar as rebuilding an + rpm package to be tested) so this should be used with caution + and only when necessary. diff --git a/roles/standard-test-basic/defaults/main.yml b/roles/standard-test-basic/defaults/main.yml new file mode 100644 index 0000000..1650168 --- /dev/null +++ b/roles/standard-test-basic/defaults/main.yml @@ -0,0 +1,2 @@ +--- +artifacts: "{{ lookup('env', 'TEST_ARTIFACTS')|default('./artifacts', true) }}" diff --git a/roles/standard-test-basic/tasks/main.yml b/roles/standard-test-basic/tasks/main.yml new file mode 100644 index 0000000..933ee9a --- /dev/null +++ b/roles/standard-test-basic/tasks/main.yml @@ -0,0 +1,151 @@ +--- +- name: Add executor host + add_host: + name: executor + ansible_connection: local + ansible_ssh_host: 127.0.0.1 + ansible_ssh_connection: local + +- name: Packages to be present on the system. + set_fact: + # Separate debuginfo and ordinary packages. There is an issue on GitHub: + # https://github.com/ansible/ansible/issues/31579. The easiest way to + # install debuginfo for package is to use 'debuginfo-install'. This + # program is present on RHEL/CentOS/Fedora and it automatically enables + # debuginfo repos. + pkgs_ordinary_req: > + {{ + required_packages | + reject('match', '.*-debuginfo$') | + list + }} + pkgs_debuginfo_req: > + {{ + required_packages | + select('match', '.*-debuginfo$') | + list | + regex_replace('-debuginfo') + }} + +- name: Target OS identification + block: + - name: Test if system is Atomic Host + lineinfile: + name: /etc/os-release + regexp: .*Atomic Host.* + state: absent + check_mode: yes + changed_when: False + register: os_release_atomic + - name: Set Ansible fact 'is_atomic_host' + set_fact: + is_atomic_host: "{{ os_release_atomic.found > 0 }}" + +- block: + - name: Install the basic requirements on target + package: name={{item}} state=latest + with_items: + - rsync # needed to copy tests to target + + - name: Install any test-specific package requirements + # Note, this method cannot install -debuginfo packages. + package: name={{item}} state=latest + with_items: + - "{{ pkgs_ordinary_req }}" + + - block: + - name: Install yum-utils + package: name=yum-utils state=latest + when: ansible_pkg_mgr == 'yum' + + - name: Install dnf-utils + # System can have installed yum-utils. Which conflicts wih dnf-utils. + # Therefore, remove yum-utils and install dnf-utils. + shell: dnf --assumeyes --allowerasing install dnf-utils + when: ansible_pkg_mgr == 'dnf' + + - name: Care about debuginfo packages for DNF/YUM systems + shell: | + debuginfo-install --assumeyes {{item}} + with_items: + - "{{ pkgs_debuginfo_req }}" + when: + - "[ansible_pkg_mgr] | intersect(['dnf', 'yum'])" + - pkgs_debuginfo_req + + # Only manually install packages on non atomic hosts + when: ansible_pkg_mgr != 'unknown' + +- block: + - name: Check presence of required packages for Atomic Host + shell: rpm -q {{ pkgs_ordinary_req|join(" ") }} + register: package_check + changed_when: False + failed_when: False + args: { warn: no } + - name: Install required packages at Atomic Host + shell: + rpm-ostree install {{ pkgs_ordinary_req|join(" ") }} + && rpm-ostree ex livefs + when: package_check.rc != 0 + when: + - is_atomic_host + - pkgs_ordinary_req + +- 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: Execute tests + shell: | + logfile={{ remote_artifacts }}/test.$(echo $TEST | sed -e 's/\//-/g').log + exec 2>>$logfile 1>>$logfile + cd $TEST_DIR + #make script executable + chmod 0775 $(echo $TEST_CMD | awk '{print $1;}') + #execute the test + eval $TEST_CMD + if [ $? -eq 0 ]; then + echo "PASS $TEST" >> {{ remote_artifacts }}/test.log + else + echo "FAIL $TEST" >> {{ remote_artifacts }}/test.log + fi + environment: + #Allow to use tests as a simple string which means the test is expected + #to be in a directory with same name and test script `runtest.sh` + #It is also possible to define the test as dictionary, in that case it is possible + #to change test directory and test command line parameters while test name is item key + TEST: + "{{item if item.keys is not defined else item.keys()[0]}}" + TEST_DIR: + "/usr/local/bin/{{item if item.keys is not defined else item[item.keys()[0]]['dir']|default(item.keys()[0])}}" + TEST_CMD: + "{{'./runtest.sh' if item.keys is not defined else item[item.keys()[0]]['run']|default('./runtest.sh')}}" + with_items: + - "{{ tests }}" + + - 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-basic/vars/main.yml b/roles/standard-test-basic/vars/main.yml new file mode 100644 index 0000000..b9860b2 --- /dev/null +++ b/roles/standard-test-basic/vars/main.yml @@ -0,0 +1,4 @@ +--- +remote_artifacts: /tmp/artifacts +tests: [] +required_packages: []