From ac5367dd74a0e67e63ad0da7be60ff204ced090d Mon Sep 17 00:00:00 2001 From: Lukas Bednar Date: Mon, 16 Oct 2023 15:44:48 +0200 Subject: [PATCH] Add github-actions and molecule tests (#23) Signed-off-by: Lukas Bednar --- .github/workflows/publish.yaml | 27 +++++++++ .github/workflows/test.yaml | 25 ++++++++ meta/main.yml | 2 + molecule/default/converge.yml | 8 +++ molecule/default/create.yml | 80 ++++++++++++++++++++++++++ molecule/default/destroy.yml | 20 +++++++ molecule/default/molecule.yml | 13 +++++ molecule/default/tasks/create-fail.yml | 14 +++++ tasks/main.yml | 21 ++++--- tests/inventory | 1 - tests/requirements.yml | 4 +- tests/test.yml | 31 ---------- 12 files changed, 204 insertions(+), 42 deletions(-) create mode 100644 .github/workflows/publish.yaml create mode 100644 .github/workflows/test.yaml create mode 100644 molecule/default/converge.yml create mode 100644 molecule/default/create.yml create mode 100644 molecule/default/destroy.yml create mode 100644 molecule/default/molecule.yml create mode 100644 molecule/default/tasks/create-fail.yml delete mode 100644 tests/inventory delete mode 100644 tests/test.yml diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml new file mode 100644 index 0000000..d5cc546 --- /dev/null +++ b/.github/workflows/publish.yaml @@ -0,0 +1,27 @@ +name: Publish to Ansible Galaxy + +on: + release: + types: + - created +jobs: + publish: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: 3.x + + - name: Install dependencies + run: pip install ansible + + - name: Publish to Ansible Galaxy + run: | + ansible-galaxy role import rhevm-qe-automation ansible-role-seal --token ${{ secrets.ANSIBLE_GALAXY_TOKEN }} + env: + ANSIBLE_GALAXY_TOKEN: ${{ secrets.ANSIBLE_GALAXY_TOKEN }} diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..0c33d08 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,25 @@ +name: Test Ansible Role + +on: + pull_request: + branches: + - master + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: 3.x + + - name: Install dependencies + run: pip install ansible molecule-docker + + - name: Test Ansible Role + run: molecule test diff --git a/meta/main.yml b/meta/main.yml index 78afabe..628b309 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -1,4 +1,6 @@ galaxy_info: + role_name: ansible-role-seal + namespace: rhevm-qe-automation author: Katerina Koukiou description: Role to seal Linux machine company: RedHat diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml new file mode 100644 index 0000000..5236d65 --- /dev/null +++ b/molecule/default/converge.yml @@ -0,0 +1,8 @@ +--- +- name: Converge + hosts: molecule +# gather_facts: false + vars: + seal_run_containerized: yes + roles: + - role: rhevm-qe-automation.ansible-role-seal diff --git a/molecule/default/create.yml b/molecule/default/create.yml new file mode 100644 index 0000000..744fe41 --- /dev/null +++ b/molecule/default/create.yml @@ -0,0 +1,80 @@ +--- +- name: Create + hosts: localhost + gather_facts: false + vars: + molecule_inventory: + all: + hosts: {} + molecule: {} + tasks: + - name: Create a container + community.docker.docker_container: + name: "{{ item.name }}" + image: "{{ item.image }}" + state: started + command: sleep 1d + log_driver: json-file + register: result + loop: "{{ molecule_yml.platforms }}" + + - name: Print some info + ansible.builtin.debug: + msg: "{{ result.results }}" + + - name: Fail if container is not running + when: > + item.container.State.ExitCode != 0 or + not item.container.State.Running + ansible.builtin.include_tasks: + file: tasks/create-fail.yml + loop: "{{ result.results }}" + loop_control: + label: "{{ item.container.Name }}" + + - name: Add container to molecule_inventory + vars: + inventory_partial_yaml: | + all: + children: + molecule: + hosts: + "{{ item.name }}": + ansible_connection: community.docker.docker + ansible.builtin.set_fact: + molecule_inventory: > + {{ molecule_inventory | combine(inventory_partial_yaml | from_yaml) }} + loop: "{{ molecule_yml.platforms }}" + loop_control: + label: "{{ item.name }}" + + - name: Dump molecule_inventory + ansible.builtin.copy: + content: | + {{ molecule_inventory | to_yaml }} + dest: "{{ molecule_ephemeral_directory }}/inventory/molecule_inventory.yml" + mode: 0600 + + - name: Force inventory refresh + ansible.builtin.meta: refresh_inventory + + - name: Fail if molecule group is missing + ansible.builtin.assert: + that: "'molecule' in groups" + fail_msg: | + molecule group was not found inside inventory groups: {{ groups }} + run_once: true # noqa: run-once[task] + +# we want to avoid errors like "Failed to create temporary directory" +- name: Validate that inventory was refreshed + hosts: molecule + gather_facts: false + tasks: + - name: Check uname + ansible.builtin.raw: uname -a + register: result + changed_when: false + + - name: Display uname info + ansible.builtin.debug: + msg: "{{ result.stdout }}" diff --git a/molecule/default/destroy.yml b/molecule/default/destroy.yml new file mode 100644 index 0000000..2214991 --- /dev/null +++ b/molecule/default/destroy.yml @@ -0,0 +1,20 @@ +--- +- name: Destroy molecule containers + hosts: molecule + gather_facts: false + tasks: + - name: Stop and remove container + delegate_to: localhost + community.docker.docker_container: + name: "{{ inventory_hostname }}" + state: absent + auto_remove: true + +- name: Remove dynamic molecule inventory + hosts: localhost + gather_facts: false + tasks: + - name: Remove dynamic inventory file + ansible.builtin.file: + path: "{{ molecule_ephemeral_directory }}/inventory/molecule_inventory.yml" + state: absent diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml new file mode 100644 index 0000000..087ca9c --- /dev/null +++ b/molecule/default/molecule.yml @@ -0,0 +1,13 @@ +--- +role_name_check: 1 +dependency: + name: galaxy + options: + requirements-file: tests/requirements.yml +lint: | + set -e + yamllint . + ansible-lint +platforms: + - name: centos8 + image: quay.io/centos/centos:stream8 diff --git a/molecule/default/tasks/create-fail.yml b/molecule/default/tasks/create-fail.yml new file mode 100644 index 0000000..2bb7923 --- /dev/null +++ b/molecule/default/tasks/create-fail.yml @@ -0,0 +1,14 @@ +- name: Retrieve container log + ansible.builtin.command: + cmd: >- + {% raw %} + docker logs + {% endraw %} + {{ item.stdout_lines[0] }} + changed_when: false + register: logfile_cmd + +- name: Display container log + ansible.builtin.fail: + msg: "{{ logfile_cmd.stderr }}" + diff --git a/tasks/main.yml b/tasks/main.yml index 0701b16..2a90703 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -10,6 +10,8 @@ file: path: '/.unconfigured' state: touch + tags: + - molecule-idempotence-notest - name: Fetch SSH keys to be removed find: @@ -88,9 +90,9 @@ state: absent - name: remove yum cache on RHEL/Centos - shell: "yum clean all; rm -rf /var/cache/yum" - args: - warn: no + ansible.builtin.shell: "yum clean all; rm -rf /var/cache/yum" + tags: + - molecule-idempotence-notest when: ansible_os_family == "RedHat" @@ -102,9 +104,7 @@ when: ansible_os_family == 'Debian' - name: remove apt cache on Ubuntu/Debian - shell: "apt-get clean" - args: - warn: no + ansible.builtin.shell: "apt-get clean" when: ansible_os_family == 'Debian' - name: Fetch logs to be removed @@ -124,12 +124,17 @@ with_items: "{{ log_files.files }}" tags: - delete_logs + - molecule-idempotence-notest - name: remove content from /etc/machine-id file - shell: "> /etc/machine-id" + ansible.builtin.shell: "> /etc/machine-id" + tags: + - molecule-idempotence-notest - name: Poweroff host - shell: "shutdown" # scheduling shutdown in 1m + ansible.builtin.shell: "shutdown" # scheduling shutdown in 1m ignore_errors: yes tags: - poweroff + tags: + - molecule-idempotence-notest diff --git a/tests/inventory b/tests/inventory deleted file mode 100644 index 42de3a1..0000000 --- a/tests/inventory +++ /dev/null @@ -1 +0,0 @@ -localhost ansible_connection=local ansible_python_interpreter="/usr/bin/env python" diff --git a/tests/requirements.yml b/tests/requirements.yml index 562771a..d1a4685 100644 --- a/tests/requirements.yml +++ b/tests/requirements.yml @@ -1,3 +1,3 @@ --- -- src: chrismeyersfsu.provision_docker - name: provision_docker +collections: + - community.docker diff --git a/tests/test.yml b/tests/test.yml deleted file mode 100644 index 7f8f263..0000000 --- a/tests/test.yml +++ /dev/null @@ -1,31 +0,0 @@ ---- -- name: Bring up docker containers - hosts: localhost - gather_facts: false - vars: - inventory: - - name: seal_centos6 - image: "chrismeyers/centos6" - - name: seal_centos7 - image: "chrismeyers/centos7" - - name: seal_rhel6 - image: "registry.access.redhat.com/rhel6:latest" - - name: seal_rhel7 - image: "registry.access.redhat.com/rhel:latest" - - name: seal_ubuntu12 - image: "chrismeyers/ubuntu12.04" - - name: seal_ubuntu14 - image: "nimmis/ubuntu:14.04" - - name: seal_ubuntu16 - image: "nimmis/ubuntu:16.04" - roles: - - role: provision_docker - provision_docker_inventory: "{{ inventory }}" - - -- name: Run seal tests on containerized environments - hosts: docker_containers - vars: - seal_run_containerized: yes - roles: - - role: seal