Skip to content

Commit

Permalink
Tox tests for consul maintenance cooldown
Browse files Browse the repository at this point in the history
  • Loading branch information
ahogui12 committed Apr 16, 2024
1 parent 8e0909c commit e8c1f9e
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 3 deletions.
35 changes: 35 additions & 0 deletions molecule/cooldown_maintenance/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
- name: Converge
hosts: molecule_consul_service
become: yes

vars:
consul_node_role: bootstrap
consul_group_name: molecule_consul_service
consul_service_cooldown_maintenance: true
consul_service_name: nginx
consul_service_url4: test.local
consul_service_tags:
- testtag
- published

pre_tasks:
- name: Update apt cache
apt:
update_cache: yes
when: ansible_pkg_mgr == 'apt'
tags: molecule-idempotence-notest

- name: Check Nginx configuration directory
stat:
path: /etc/nginx
register: consul_service_nginx_configuration_directory
changed_when: False

- name: Install nginx so default consul service health check is fine
include_role:
name: nginxinc.nginx
when: not consul_service_nginx_configuration_directory.stat.exists

roles:
- role: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') | basename }}"
52 changes: 52 additions & 0 deletions molecule/cooldown_maintenance/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
dependency:
name: galaxy
options:
force: False
driver:
name: docker
lint: |
set -e
yamllint .
ansible-lint .
flake8
platforms:
- name: molecule-consul-service-debian11
image: jrei/systemd-debian:11
privileged: True
volumes:
- "/sys/fs/cgroup:/sys/fs/cgroup:rw"
cgroupns_mode: host
command: /lib/systemd/systemd
groups:
- molecule_consul_service
provisioner:
name: ansible
log: true
# https://molecule.readthedocs.io/en/latest/configuration.html#molecule.scenario.Scenario
scenario:
converge_sequence:
- dependency
- create
- prepare
- converge
- converge # add a 2nd converge step so we can check if there's 2 maintenances on/off in Consul log
test_sequence:
- dependency
- lint
- cleanup
- destroy
- syntax
- create
- prepare
- converge
- idempotence
- converge # add a 2nd converge step so we can check if there's 2 maintenances on/off in Consul log
- side_effect
- verify
- cleanup
- destroy
verifier:
name: testinfra
options:
tb: short
7 changes: 7 additions & 0 deletions molecule/cooldown_maintenance/requirements.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- src: https://github.com/wazo-platform/ansible-role-metadata.git
scm: git
name: wazo.metadata

- role: brianshumate.consul
- role: nginxinc.nginx
55 changes: 55 additions & 0 deletions molecule/cooldown_maintenance/tests/test_consul-service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import os
import pytest
import testinfra.utils.ansible_runner

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ["MOLECULE_INVENTORY_FILE"]
).get_hosts("all")


@pytest.mark.parametrize(
"name", ["procps"],
)
def test_packages(host, name):
item = host.package(name)
assert item.is_installed


def test_user(host):
u = host.user("consul")
assert u.exists


@pytest.mark.parametrize(
"path",
[
"/etc/consul/.consul_bootstrapped",
"/etc/consul/consul.d/nginx.service.json",
"/etc/systemd/system/consul-maint-before-shutdown.service",
],
)
def test_files(host, path):
with host.sudo():
item = host.file(path)
assert item.exists


@pytest.mark.parametrize("name", ["consul"])
def test_services(host, name):
item = host.service(name)
assert item.is_running
assert item.is_enabled


def test_service_file_content(host):
with host.sudo():
service = host.file(
"/etc/consul/consul.d/nginx.service.json"
).content_string
assert "testtag" in service
assert "published" in service
assert '"URL"' in service
assert '"URL2"' not in service
assert '"URL3"' not in service
assert '"URL4"' in service
assert '"URL5"' not in service
6 changes: 3 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ deps =
flake8
molecule
molecule-plugins[docker]
molecule-plugins[vagrant]
# molecule-plugins[vagrant]
netaddr
requests
requests==2.28.1
pytest<8.0.0 # see https://github.com/pytest-dev/pytest/issues/11904
pytest-testinfra
yamllint
Expand All @@ -20,4 +20,4 @@ passenv =
SSH_AUTH_SOCK
TERM
TTY
commands = molecule {posargs:test}
commands = molecule {posargs:test --all}

0 comments on commit e8c1f9e

Please sign in to comment.