#118 Basic role with str-common
Merged 6 years ago by astepano. Opened 6 years ago by astepano.

@@ -1,21 +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:

+ Put this role in your `tests.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.

+  * **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.

  

- Check glib2 package tests to find examples for using this role.

+ Check `glib2` package tests to find examples for using this role.

@@ -1,2 +0,0 @@ 

- ---

- artifacts: "{{ lookup('env', 'TEST_ARTIFACTS')|default('./artifacts', true) }}"

@@ -0,0 +1,4 @@ 

+ ---

+ 

+ dependencies:

+   - role: str-common

@@ -1,105 +1,5 @@ 

  ---

  

- - 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: |
@@ -123,23 +23,19 @@ 

        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])}}"

+         "{{ tenv_workdir }}/{{ 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

+ 

+   - include_role:

+       name: str-common

+       tasks_from: end.yml

@@ -1,4 +0,0 @@ 

- ---

- remote_artifacts: /tmp/artifacts

- tests: []

- required_packages: []

@@ -1,43 +1,41 @@ 

  # 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:

+ Put this role in your `tests.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. 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.

-  * repositories: A list of dictionaries defining git repos with

-    tests to be fetched. Available options are consistent with the

-    ansible git module syntax:

+  * **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. 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.

+  * **repositories**: A list of dictionaries defining git repos with tests to be

+    fetched. Available options are consistent with the ansible git module

+    syntax:

       url ... repository url (required)

       dest ... destination directory (required)

       version ... desired branch (optional, defaults to master)

  

  Example usage:

  

- - hosts: localhost

-   roles:

-   - role: standard-test-beakerlib

-     tags:

-     - classic

-     repositories:

-     - repo: "https://src.fedoraproject.org/tests/shell.git"

-       dest: "shell"

-     tests:

-     - shell/func

-     - shell/login

-     - shell/smoke

-     required_packages:

-     - expect

-     - which

+     - hosts: localhost

+       roles:

+       - role: standard-test-beakerlib

+         tags:

+         - classic

+         repositories:

+         - repo: "https://src.fedoraproject.org/tests/shell.git"

+           dest: "shell"

+         tests:

+         - shell/func

+         - shell/login

+         - shell/smoke

+         required_packages:

+         - expect

+         - which

@@ -1,2 +0,0 @@ 

- ---

- artifacts: "{{ lookup('env', 'TEST_ARTIFACTS')|default('./artifacts', true) }}"

@@ -5,14 +5,14 @@ 

    - name: Add restraint repo for restraint-rhts on Fedora hosts

      shell: dnf copr enable -y bpeck/restraint

      ignore_errors: True

-     when: hostvars[test_runner_inventory_name]['ansible_distribution'] == 'Fedora'

+     when: "hostvars[test_runner_inventory_name]['is_dnf_os']"

  

    - name: Add restraint repo for restraint-rhts on RHEL/CentOS hosts

      block:

        - package: name=yum-plugin-copr

        - shell: yum copr -y enable bpeck/restraint

          ignore_errors: True

-     when: "[hostvars[test_runner_inventory_name]['ansible_distribution'] ] | intersect(['RedHat', 'CentOS'])"

+     when: "hostvars[test_runner_inventory_name]['is_yum_os']"

  

    - name: Install the beakerlib requirements

      package: name={{item}} state=present
@@ -109,17 +109,13 @@ 

      with_items:

      - "{{ tests }}"

  

-   - name: Pull out the logs from test environment

-     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

+ 

+   - include_role:

+       name: str-common

+       tasks_from: end.yml

@@ -5,3 +5,4 @@ 

  tenv_workdir: /var/str/

  remote_artifacts: /tmp/artifacts/

  test_runner_inventory_name: test-runner

+ artifacts: "{{ lookup('env', 'TEST_ARTIFACTS')|default('./artifacts', true) }}"

@@ -0,0 +1,9 @@ 

+ ---

+ 

+ - name: Pull out the logs from test environment to test runner

+   synchronize:

+     dest: "{{ artifacts }}/"

+     src: "{{ remote_artifacts }}/"

+     mode: pull

+     ssh_args: "-o UserKnownHostsFile=/dev/null"

+   when: artifacts|default("") != ""

@@ -8,8 +8,15 @@ 

        changed_when: False

        register: os_release_atomic

      - name: Set fact 'is_atomic'

+       delegate_facts: True

        set_fact:

          is_atomic : "{{ os_release_atomic.found > 0 }}"

-     - debug:

-         msg: "System is Atomic Host: {{ is_atomic }}"

-         verbosity: 1

+ 

+ - name: Set facts about system

+   delegate_facts: True

+   set_fact:

+     is_yum_os: >

+       {{ ansible_pkg_mgr == 'yum' }}

+     is_dnf_os: >

+       {{ ansible_pkg_mgr == 'dnf' }}

+ 

@@ -1,7 +1,26 @@ 

  - import_tasks: inspect.yml

+ 

+ - debug:

+     msg: "System uses yum: {{ is_yum_os }}"

+     verbosity: 1

+ 

+ - debug:

+     msg: "System uses dnf: {{ is_dnf_os }}"

+     verbosity: 1

+ 

+ - debug:

+     msg: "System is Atomic Host: {{ is_atomic }}"

+     verbosity: 1

+ 

  - import_tasks: trunner.yml

  - import_tasks: pkgs.yml

  

- 

  - name: Make artifacts directory

    file: path={{ remote_artifacts }} state=directory owner=root mode=755 recurse=yes

+ 

+ # Next task requires rsync on test environment

+ - name: Copy tests to test environment

+   synchronize:

+     src: "{{ playbook_dir }}/"

+     dest: "{{ tenv_workdir }}"

+     ssh_args: "-o UserKnownHostsFile=/dev/null"

@@ -4,6 +4,11 @@ 

    with_items:

      - "{{ pkgs_ordinary_req }}"

  

+ - name: Install the common requirements on target

+   package: name={{item}} state=present

+   with_items:

+     - rsync # needed to copy tests to target

+ 

  - block:

    - name: Install dnf-utils

      # System can have installed yum-utils. Which conflicts wih dnf-utils.

@@ -1,11 +1,11 @@ 

  - block:

-   - name: Check presence of required packages for Atomic Host

+   - name: Check presence of test-specific package requirements

      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

+   - name: Install test-specific package requirements

      shell:

        rpm-ostree install {{ pkgs_ordinary_req|join(" ") }}

        && rpm-ostree ex livefs

@@ -4,6 +4,11 @@ 

    with_items:

      - "{{ pkgs_ordinary_req }}"

  

+ - name: Install the common requirements on target

+   package: name={{item}} state=present

+   with_items:

+     - rsync # needed to copy tests to target

+ 

  - block:

    - name: Install yum-utils

      package: name=yum-utils state=latest

@@ -9,11 +9,7 @@ 

      setup:

      delegate_facts: True

  

-   - debug:

-       msg:

-         "{{ test_runner_inventory_name }} is

-         {{ hostvars[test_runner_inventory_name]['ansible_distribution'] }}"

-       verbosity: 1

+   - import_tasks: inspect.yml

  

    - name: Fetch tests from remote repositories

      git:
@@ -23,3 +19,21 @@ 

      with_items:

        "{{ repositories }}"

      when: repositories is defined

+ 

+ - debug:

+     msg: >

+       {{ test_runner_inventory_name }} is:

+       {{ hostvars[test_runner_inventory_name]['ansible_distribution'] }}

+     verbosity: 1

+ 

+ - debug:

+     msg: >

+       {{ test_runner_inventory_name }} uses yum:

+       {{ hostvars[test_runner_inventory_name]['is_yum_os'] }}

+     verbosity: 1

+ 

+ - debug:

+     msg: >

+       {{ test_runner_inventory_name }} uses dnf:

+       {{ hostvars[test_runner_inventory_name]['is_dnf_os'] }}

+     verbosity: 1