Skip to content

Commit

Permalink
Add package hold tests
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyphreak committed Nov 25, 2023
1 parent 8a9f09b commit e71a7f9
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 49 deletions.
36 changes: 0 additions & 36 deletions molecule/default/verify.yml

This file was deleted.

73 changes: 73 additions & 0 deletions molecule/hold/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
- name: Deploy docker packages
hosts: all
become: true
roles:
- nephelaiio.docker
pre_tasks:
- name: Query Debian package versions
ansible.builtin.shell:
cmd: "apt-cache madison {{ item }} | awk -F'|' '{ print $2 }'"
loop: "{{ docker_packages }}"
register: _debian_package_query
when: ansible_os_family == "Debian"
changed_when: false

- name: Query RedHat package versions
ansible.builtin.shell:
cmd: "yum list {{ item }} --showduplicates | grep '^{{ item }}' | awk '{ print $1,$2 }' | sort -r"
loop: "{{ docker_packages }}"
register: _redhat_package_query
when: ansible_os_family == "RedHat"
changed_when: false

- name: Set package version facts
ansible.builtin.set_fact:
_docker_package_query: "{{ _redhat_packages + _debian_packages }}"
vars:
_redhat_packages: "{{ [] if _redhat_package_query.skipped else _redhat_package_query.results }}"
_debian_packages: "{{ [] if _debian_package_query.skipped else _debian_package_query.results }}"

- name: Verify target package versions
ansible.builtin.fail:
msg: "{{ _package }} has no installation candidate: {{ item }}"
vars:
_stdout: "{{ item.stdout_lines }}"
_package: "{{ item.item }}"
loop_control:
label: "{{ item.item }}"
loop: "{{ _docker_package_query }}"
when: _stdout | length == 0

- name: Initialize package versions
ansible.builtin.set_fact:
docker_packages: []

- name: Set target Debian package versions
ansible.builtin.set_fact:
docker_packages: "{{ docker_packages + [_package] }}"
vars:
_stdout: "{{ item.stdout_lines }}"
_multiple: "{{ _stdout | length > 1 }}"
_version: "{{ _stdout[0] if not _multiple else _stdout[1] }}"
_package: "{{ item.item }}={{ _version | trim }}"
loop_control:
label: "{{ item.item }}"
loop: "{{ _docker_package_query }}"
when: ansible_os_family == "Debian"

- name: Set target RedHat package versions
ansible.builtin.set_fact:
docker_packages: "{{ docker_packages + [_package] }}"
vars:
_stdout: "{{ item.stdout_lines }}"
_multiple: "{{ _stdout | length > 1 }}"
_target: "{{ (_stdout[0] if not _multiple else _stdout[1]).split(' ') }}"
_arch: "{{ _target[0].split('.')[1] }}"
_name: "{{ _target[0].split('.')[0] }}"
_version: "{{ _target[1] }}"
_package: "{{ _name }}-{{ _version }}.{{ _arch }}"
loop_control:
label: "{{ item.item }}"
loop: "{{ _docker_package_query }}"
when: ansible_os_family == "RedHat"
4 changes: 0 additions & 4 deletions molecule/default/molecule.yml → molecule/hold/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,3 @@ provisioner:
name: ansible
verifier:
name: ansible
lint: |
set -e
run yamllint .
run ansible-lint .
14 changes: 14 additions & 0 deletions molecule/hold/side_effect.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
- name: Update OS
hosts: all
become: true
roles:
- robertdebock.update
serial: 1
tasks:
- name: Wait for MongoDB to be up
community.mongodb.mongodb_shell:
eval: "db.runCommand({ping: 1})"
mongo_cmd: "{{ mongodb_shell | default(omit) }}"
retries: 6
delay: 10
89 changes: 89 additions & 0 deletions molecule/hold/verify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
- name: Verify docker packages
hosts: all
become: true
tasks:
- name: Query Debian package versions
ansible.builtin.shell:
cmd: "apt-cache madison {{ item }} | awk -F'|' '{ print $2 }'"
loop: "{{ docker_packages }}"
register: _debian_package_query
when: ansible_os_family == 'Debian'
changed_when: false

- name: Query RedHat package versions
ansible.builtin.shell:
cmd: "yum list {{ item }} --showduplicates | grep '^{{ item }}' | awk '{ print $1,$2 }' | sort -r"
loop: "{{ docker_packages }}"
register: _redhat_package_query
when: ansible_os_family == "RedHat"
changed_when: false

- name: Set package version facts
ansible.builtin.set_fact:
_docker_package_query: "{{ _redhat_packages + _debian_packages }}"
vars:
_redhat_packages: "{{ [] if _redhat_package_query.skipped else _redhat_package_query.results }}"
_debian_packages: "{{ [] if _debian_package_query.skipped else _debian_package_query.results }}"

- name: Initialize package versions
ansible.builtin.set_fact:
docker_package_list: []

- name: Set target Debian package versions
ansible.builtin.set_fact:
docker_package_list: "{{ docker_package_list + [_package] }}"
vars:
_stdout: "{{ item.stdout_lines }}"
_multiple: "{{ _stdout | length > 1 }}"
_version: "{{ _stdout[0] if not _multiple else _stdout[1] }}"
_package: "{{ item.item }}={{ _version | trim }}"
loop_control:
label: "{{ item.item }}"
loop: "{{ _docker_package_query }}"
when: ansible_os_family == "Debian"

- name: Set target RedHat package versions
ansible.builtin.set_fact:
docker_package_list: "{{ docker_package_list + [_package] }}"
vars:
_stdout: "{{ item.stdout_lines }}"
_multiple: "{{ _stdout | length > 1 }}"
_target: "{{ (_stdout[0] if not _multiple else _stdout[1]).split(' ') }}"
_arch: "{{ _target[0].split('.')[1] }}"
_name: "{{ _target[0].split('.')[0] }}"
_version: "{{ _target[1] }}"
_package: "{{ _name }}-{{ _version }}.{{ _arch }}"
loop_control:
label: "{{ item.item }}"
loop: "{{ _docker_package_query }}"
when: ansible_os_family == "RedHat"

- name: Gather package facts
ansible.builtin.package_facts:

- name: Verify package versions
ansible.builtin.fail:
msg: "Expected installed version for package {{ _package }} to be {{ _version }}, found {{ _installed }}"
vars:
_package: "{{ item.split('=') | first }}"
_version: "{{ item.split('=') | last }}"
_installed: "{{ (packages[_package] | default([{'version': 'None'}]))[0].version }}"
loop: "{{ docker_package_list }}"
when:
- ansible_os_family == "Debian"
- _package not in packages or _version != _installed

- name: Verify package versions
ansible.builtin.fail:
msg: "Expected installed version for package {{ _package }} to be {{ _version }}, found {{ _installed }}"
vars:
_prefix: "^{{ _package }}-"
_suffix: "-.*$"
_package: "{{ item.0 }}"
_version: "{{ item.1 | regex_replace(_prefix, '') | regex_replace(_suffix, '') }}"
_installed: "{{ (packages[_package] | default([{'version': 'None'}]))[0].version }}"
loop: "{{ docker_packages | zip(docker_package_list) }}"
when:
- ansible_os_family == "RedHat"
- _package not in packages or _version != _installed
10 changes: 2 additions & 8 deletions molecule/default/converge.yml → molecule/install/converge.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
---
- name: Converge

hosts: all

roles:

- nephelaiio.docker

pre_tasks:

- name: update apt package cache
- name: Update apt package cache
apt:
package: gpg-agent
update_cache: true
when: ansible_os_family == 'Debian'

post_tasks:

- name: install python3 binary redirection
- name: Install python3 binary redirection
apt:
name: python-is-python3
when: ansible_os_family == 'Debian'
21 changes: 21 additions & 0 deletions molecule/install/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
dependency:
name: galaxy
driver:
name: docker
platforms:
- name: instance
image: geerlingguy/docker-${MOLECULE_DISTRO:-ubuntu2204}-ansible
pre_build_image: true
command: ${MOLECULE_COMMAND:-"/lib/systemd/systemd"}
cgroupns_mode: host
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:rw
privileged: true
tmpfs:
- /tmp
- /opt
provisioner:
name: ansible
verifier:
name: ansible
29 changes: 29 additions & 0 deletions molecule/install/verify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
- name: Verify
hosts: all
vars:
docker_service: docker.service
pre_tasks:
- name: Verify docker cli is installed
ansible.builtin.command: "docker --version"

- name: Verify docker pip is installed
ansible.builtin.command: "python3 -c import docker"

- name: Gather service facts
ansible.builtin.service_facts:

- name: Check service definition
ansible.builtin.assert:
that: docker service is available
when: _service not in ansible_facts.services

- name: Check service running state
ansible.builtin.assert:
that: docker service is running
when: ansible_facts.services[_service].state != "running"

- name: Check service startup state
ansible.builtin.assert:
that: docker service is running
when: ansible_facts.services[_service].status != "enabled"
3 changes: 2 additions & 1 deletion requirements.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
roles:
- nephelaiio.plugins
- name: robertdebock.update
- nephelaiio.pip
collections:
- community.docker
- nephelaiio.plugins

0 comments on commit e71a7f9

Please sign in to comment.