Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable basic molecule tests for agent role #323

Merged
merged 11 commits into from
May 4, 2023
66 changes: 66 additions & 0 deletions .github/workflows/molecule-role-agent.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# https://github.com/ansible-collections/collection_template/blob/main/.github/workflows/ansible-test.yml
# README FIRST
# 1. replace "NAMESPACE" and "COLLECTION_NAME" with the correct name in the env section (e.g. with 'community' and 'mycollection')
# 2. If you don't have unit tests remove that section
# 3. If your collection depends on other collections ensure they are installed, see "Install collection dependencies"
# If you need help please ask in #ansible-community on the Libera.chat IRC network

name: Molecule Tests for Agent Role
on:
workflow_dispatch:
pull_request:
branches:
- main
- devel
paths:
- 'roles/agent/**'
push:
branches:
- main
- devel
paths:
- 'roles/agent/**'

env:
NAMESPACE: tribe29
COLLECTION_NAME: checkmk

jobs:
build:
runs-on: ubuntu-20.04
name: Checkmk ${{ matrix.checkmk }} + py${{ matrix.python }}
strategy:
fail-fast: false
matrix:
python:
- '3.8'
- '3.9'
- '3.10'
- '3.11'
checkmk:
- '2.0.0'
- '2.1.0'
- '2.2.0'

steps:
- name: Check out code
uses: actions/checkout@v3
with:
path: ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}

- name: Set up Python ${{ matrix.python }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python }}

- name: Install dependencies
run: |
python3 -m pip install --upgrade pip
python3 -m pip install -r requirements.txt
working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}

- name: Test with molecule
run: |
cd roles/agent/
molecule test -s ${{ matrix.checkmk }}
working-directory: ./ansible_collections/${{env.NAMESPACE}}/${{env.COLLECTION_NAME}}
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ antsibull-changelog >= 0.16.0
antsibull-docs >= 1.5.0
ansible-lint >= 6.0.0
jinja2 >= 3.0.0
molecule >= 3.5.0
molecule >= 4.0.4
molecule-docker >= 1.1.0
# molecule-plugins[docker] >= 23.0.0 # This will be needed with Molecule 5.
yamllint >=1.0.0
37 changes: 37 additions & 0 deletions roles/agent/molecule/2.0.0/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
- name: Converge
hosts: all

pre_tasks:

- name: Update apt cache.
ansible.builtin.apt:
update_cache: true
cache_valid_time: 600
when: ansible_os_family == 'Debian'
- name: Install prerequisites.
ansible.builtin.apt:
name: "{{ item }}"
state: present
loop:
- apt-utils
- man
when: ansible_os_family == 'Debian'
- name: Create '/usr/share/man/man8/' on Ubuntu.
ansible.builtin.file:
path: /usr/share/man/man8/
state: directory
mode: "0755"
owner: root
group: root
when: ansible_distribution == 'Ubuntu'

tasks:

- name: "Run server role."
ansible.builtin.include_role:
name: server

- name: "Run agent role."
ansible.builtin.include_role:
name: agent
60 changes: 60 additions & 0 deletions roles/agent/molecule/2.0.0/group_vars/all.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
# General
checkmk_version: "2.0.0p35"
checkmk_edition: "cre"
checkmk_site: "my_site"
automation_user: "cmkadmin"
automation_secret: "d7589df1-01db-4eda-9858-dbcff8d0c361"

# Agent Role
checkmk_agent_version: "{{ checkmk_version }}"
checkmk_agent_edition: "{{ checkmk_edition }}"
checkmk_agent_protocol: http
checkmk_agent_server: localhost
checkmk_agent_site: "{{ checkmk_site }}"
checkmk_agent_registration_server: "{{ checkmk_agent_server }}"
checkmk_agent_registration_site: "{{ checkmk_agent_site }}"
checkmk_agent_server_validate_certs: 'false'
checkmk_agent_port: "{% if checkmk_agent_protocol == 'https' %}443{% else %}80{% endif %}"
checkmk_agent_user: "{{ automation_user | default('automation') }}"

# Depending on which user you will be using, set the password or secret:
# checkmk_agent_pass: "{{ automation_secret }}"
checkmk_agent_secret: "{{ automation_secret }}"

checkmk_agent_auto_activate: 'false'
checkmk_agent_add_host: 'false'
checkmk_agent_discover: 'false'
checkmk_agent_update: 'false'
checkmk_agent_tls: 'false'
checkmk_agent_configure_firewall: 'false'
checkmk_agent_force_install: 'false'
checkmk_agent_prep_legacy: 'false'
checkmk_agent_delegate_api_calls: localhost
checkmk_agent_delegate_download: "{{ inventory_hostname }}"
checkmk_agent_host_name: "{{ inventory_hostname }}"
checkmk_agent_folder: "{{ checkmk_folder_path | default('/') }}"
checkmk_agent_force_foreign_changes: 'false'
checkmk_agent_host_attributes:
ipaddress: 127.0.0.1

# If you trust your local hostnames, you could also use the following
# to use the local hostname instead of the inventory hostname:
# checkmk_agent_host_name: "{{ hostvars[inventory_hostname]['ansible_fqdn'] }}"
# Checkmk does not need an IP address, as long as the hostname is DNS-resolvable

# If that does not apply to your environment, you can configure the IP address below:
# checkmk_agent_host_ip: "{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}"

# Server Role

checkmk_server_edition: "{{ checkmk_edition }}"
checkmk_server_version: "{{ checkmk_version }}"
checkmk_server_verify_setup: 'true'

checkmk_server_sites:
- name: "{{ checkmk_site }}"
version: "{{ checkmk_version }}"
state: started
admin_pw: cmk
update_conflict_resolution: install
87 changes: 87 additions & 0 deletions roles/agent/molecule/2.0.0/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
dependency:
name: galaxy
driver:
name: docker
platforms:
- name: ubuntu1804
image: geerlingguy/docker-ubuntu1804-ansible
command: ${MOLECULE_DOCKER_COMMAND:-""}
tmpfs:
- /run
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
privileged: true
pre_build_image: true
- name: ubuntu2004
image: geerlingguy/docker-ubuntu2004-ansible
command: ${MOLECULE_DOCKER_COMMAND:-""}
tmpfs:
- /run
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
privileged: true
pre_build_image: true
- name: ubuntu2204
image: geerlingguy/docker-ubuntu2204-ansible
command: ${MOLECULE_DOCKER_COMMAND:-""}
tmpfs:
- /run
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
privileged: true
pre_build_image: true
- name: debian10
image: geerlingguy/docker-debian10-ansible
command: ${MOLECULE_DOCKER_COMMAND:-""}
tmpfs:
- /run
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
privileged: true
pre_build_image: true
- name: debian11
image: geerlingguy/docker-debian11-ansible
command: ${MOLECULE_DOCKER_COMMAND:-""}
tmpfs:
- /run
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
privileged: true
pre_build_image: true
- name: centos7
image: geerlingguy/docker-centos7-ansible
command: ${MOLECULE_DOCKER_COMMAND:-""}
tmpfs:
- /run
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
privileged: true
pre_build_image: true
- name: centos8
image: geerlingguy/docker-centos8-ansible
command: ${MOLECULE_DOCKER_COMMAND:-""}
tmpfs:
- /run
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
privileged: true
pre_build_image: true
provisioner:
name: ansible
verifier:
name: ansible
scenario:
test_sequence:
- dependency
- cleanup
- destroy
- syntax
- create
- prepare
- converge
# - idempotence # disable for now, as we have non-idempotent tasks
- side_effect
- verify
- cleanup
- destroy
31 changes: 31 additions & 0 deletions roles/agent/molecule/2.0.0/verify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
- name: Verify
hosts: all
gather_facts: false
tasks:

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

- name: "Test Agent is present."
ansible.builtin.stat:
path: "/usr/bin/check_mk_agent"
register: checkmk_agent_file

- name: "Verify Agent is present."
ansible.builtin.assert:
that: checkmk_agent_file.stat.exists | bool

- name: "Test Agent Service is running."
ansible.builtin.assert:
that: "'check_mk-async.service' in ansible_facts.services"

- name: "Test that the Agent is listening on the default Port."
ansible.builtin.wait_for:
port: 6556
connect_timeout: 30
register: checkmk_agent_port

- name: "Test Agent Service is listening on port 6556."
ansible.builtin.assert:
that: "(checkmk_agent_port.port == 6556) and (checkmk_agent_port.state == 'started')"
37 changes: 37 additions & 0 deletions roles/agent/molecule/2.1.0/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
- name: Converge
hosts: all

pre_tasks:

- name: Update apt cache.
ansible.builtin.apt:
update_cache: true
cache_valid_time: 600
when: ansible_os_family == 'Debian'
- name: Install prerequisites.
ansible.builtin.apt:
name: "{{ item }}"
state: present
loop:
- apt-utils
- man
when: ansible_os_family == 'Debian'
- name: Create '/usr/share/man/man8/' on Ubuntu.
ansible.builtin.file:
path: /usr/share/man/man8/
state: directory
mode: "0755"
owner: root
group: root
when: ansible_distribution == 'Ubuntu'

tasks:

- name: "Run server role."
ansible.builtin.include_role:
name: server

- name: "Run agent role."
ansible.builtin.include_role:
name: agent
60 changes: 60 additions & 0 deletions roles/agent/molecule/2.1.0/group_vars/all.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
# General
checkmk_version: "2.1.0p26"
checkmk_edition: "cre"
checkmk_site: "my_site"
automation_user: "cmkadmin"
automation_secret: "d7589df1-01db-4eda-9858-dbcff8d0c361"

# Agent Role
checkmk_agent_version: "{{ checkmk_version }}"
checkmk_agent_edition: "{{ checkmk_edition }}"
checkmk_agent_protocol: http
checkmk_agent_server: localhost
checkmk_agent_site: "{{ checkmk_site }}"
checkmk_agent_registration_server: "{{ checkmk_agent_server }}"
checkmk_agent_registration_site: "{{ checkmk_agent_site }}"
checkmk_agent_server_validate_certs: 'false'
checkmk_agent_port: "{% if checkmk_agent_protocol == 'https' %}443{% else %}80{% endif %}"
checkmk_agent_user: "{{ automation_user | default('automation') }}"

# Depending on which user you will be using, set the password or secret:
# checkmk_agent_pass: "{{ automation_secret }}"
checkmk_agent_secret: "{{ automation_secret }}"

checkmk_agent_auto_activate: 'false'
checkmk_agent_add_host: 'false'
checkmk_agent_discover: 'false'
checkmk_agent_update: 'false'
checkmk_agent_tls: 'false'
checkmk_agent_configure_firewall: 'false'
checkmk_agent_force_install: 'false'
checkmk_agent_prep_legacy: 'false'
checkmk_agent_delegate_api_calls: localhost
checkmk_agent_delegate_download: "{{ inventory_hostname }}"
checkmk_agent_host_name: "{{ inventory_hostname }}"
checkmk_agent_folder: "{{ checkmk_folder_path | default('/') }}"
checkmk_agent_force_foreign_changes: 'false'
checkmk_agent_host_attributes:
ipaddress: 127.0.0.1

# If you trust your local hostnames, you could also use the following
# to use the local hostname instead of the inventory hostname:
# checkmk_agent_host_name: "{{ hostvars[inventory_hostname]['ansible_fqdn'] }}"
# Checkmk does not need an IP address, as long as the hostname is DNS-resolvable

# If that does not apply to your environment, you can configure the IP address below:
# checkmk_agent_host_ip: "{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}"

# Server Role

checkmk_server_edition: "{{ checkmk_edition }}"
checkmk_server_version: "{{ checkmk_version }}"
checkmk_server_verify_setup: 'true'

checkmk_server_sites:
- name: "{{ checkmk_site }}"
version: "{{ checkmk_version }}"
state: started
admin_pw: cmk
update_conflict_resolution: install
Loading