Skip to content

Commit

Permalink
Add github-actions and molecule tests (#23)
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Bednar <[email protected]>
  • Loading branch information
lukas-bednar authored Oct 16, 2023
1 parent 9d0bf75 commit ac5367d
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 42 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
25 changes: 25 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions meta/main.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 8 additions & 0 deletions molecule/default/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
- name: Converge
hosts: molecule
# gather_facts: false
vars:
seal_run_containerized: yes
roles:
- role: rhevm-qe-automation.ansible-role-seal
80 changes: 80 additions & 0 deletions molecule/default/create.yml
Original file line number Diff line number Diff line change
@@ -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 }}"
20 changes: 20 additions & 0 deletions molecule/default/destroy.yml
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions molecule/default/molecule.yml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions molecule/default/tasks/create-fail.yml
Original file line number Diff line number Diff line change
@@ -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 }}"

21 changes: 13 additions & 8 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
file:
path: '/.unconfigured'
state: touch
tags:
- molecule-idempotence-notest

- name: Fetch SSH keys to be removed
find:
Expand Down Expand Up @@ -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"

Expand All @@ -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
Expand All @@ -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
1 change: 0 additions & 1 deletion tests/inventory

This file was deleted.

4 changes: 2 additions & 2 deletions tests/requirements.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
---
- src: chrismeyersfsu.provision_docker
name: provision_docker
collections:
- community.docker
31 changes: 0 additions & 31 deletions tests/test.yml

This file was deleted.

0 comments on commit ac5367d

Please sign in to comment.