From 57b63d4503d7c8abe53c5ab6f345f01a038bea79 Mon Sep 17 00:00:00 2001 From: Matthew Stobbs Date: Thu, 29 Jan 2026 11:53:09 -0700 Subject: [PATCH] moving to molecule --- molecule.yml | 0 molecule/fedora/converge.yml | 10 ++++++++ molecule/fedora/create.yml | 35 ++++++++++++++++++++++++++ molecule/fedora/destroy.yml | 24 ++++++++++++++++++ molecule/fedora/inventory.yml | 10 ++++++++ molecule/fedora/molecule.yml | 47 +++++++++++++++++++++++++++++++++++ molecule/fedora/verify.yml | 10 ++++++++ molecule/requirements.yml | 4 +++ tests/Containerfile.debian | 18 -------------- tests/Containerfile.el9 | 17 ------------- tests/Containerfile.fedora | 17 ------------- tests/Containerfile.ubuntu | 18 -------------- tests/cleanup.sh | 20 --------------- tests/inventory.yml | 7 ------ tests/lib.sh | 10 -------- tests/pubkey | 1 - tests/setup.sh | 26 ------------------- tests/test.sh | 11 -------- tests/test.yml | 12 --------- 19 files changed, 140 insertions(+), 157 deletions(-) create mode 100644 molecule.yml create mode 100644 molecule/fedora/converge.yml create mode 100644 molecule/fedora/create.yml create mode 100644 molecule/fedora/destroy.yml create mode 100644 molecule/fedora/inventory.yml create mode 100644 molecule/fedora/molecule.yml create mode 100644 molecule/fedora/verify.yml create mode 100644 molecule/requirements.yml delete mode 100644 tests/Containerfile.debian delete mode 100644 tests/Containerfile.el9 delete mode 100644 tests/Containerfile.fedora delete mode 100644 tests/Containerfile.ubuntu delete mode 100755 tests/cleanup.sh delete mode 100644 tests/inventory.yml delete mode 100644 tests/lib.sh delete mode 100644 tests/pubkey delete mode 100755 tests/setup.sh delete mode 100755 tests/test.sh delete mode 100644 tests/test.yml diff --git a/molecule.yml b/molecule.yml new file mode 100644 index 0000000..e69de29 diff --git a/molecule/fedora/converge.yml b/molecule/fedora/converge.yml new file mode 100644 index 0000000..d3a2052 --- /dev/null +++ b/molecule/fedora/converge.yml @@ -0,0 +1,10 @@ +--- +# Purpose: bring the instance to the desired state by running the role under test. +# Molecule calls this playbook with `molecule converge`. +- name: Converge + hosts: all + gather_facts: true # Disable if your role does not rely on facts + tasks: + - name: Apply role under test + ansible.builtin.include_role: + name: yournamespace.yourcollection.yourrole diff --git a/molecule/fedora/create.yml b/molecule/fedora/create.yml new file mode 100644 index 0000000..fcd6b92 --- /dev/null +++ b/molecule/fedora/create.yml @@ -0,0 +1,35 @@ +--- +- name: Create + hosts: localhost + connection: local + gather_facts: false + # no_log: "{{ molecule_no_log }}" + tasks: + # TODO: Developer must implement and populate 'server' variable + + - name: Create instance config + when: server.changed | default(false) | bool # noqa no-handler + block: + - name: Populate instance config dict # noqa jinja + ansible.builtin.set_fact: + instance_conf_dict: {} + # instance': "{{ }}", + # address': "{{ }}", + # user': "{{ }}", + # port': "{{ }}", + # 'identity_file': "{{ }}", } + with_items: "{{ server.results }}" + register: instance_config_dict + + - name: Convert instance config dict to a list + ansible.builtin.set_fact: + instance_conf: "{{ instance_config_dict.results | map(attribute='ansible_facts.instance_conf_dict') | list }}" + + - name: Dump instance config + ansible.builtin.copy: + content: | + # Molecule managed + + {{ instance_conf | to_json | from_json | to_yaml }} + dest: "{{ molecule_instance_config }}" + mode: "0600" diff --git a/molecule/fedora/destroy.yml b/molecule/fedora/destroy.yml new file mode 100644 index 0000000..59d6a8e --- /dev/null +++ b/molecule/fedora/destroy.yml @@ -0,0 +1,24 @@ +--- +- name: Destroy + hosts: localhost + connection: local + gather_facts: false + # no_log: "{{ molecule_no_log }}" + tasks: + # Developer must implement. + + # Mandatory configuration for Molecule to function. + + - name: Populate instance config + ansible.builtin.set_fact: + instance_conf: {} + + - name: Dump instance config + ansible.builtin.copy: + content: | + # Molecule managed + + {{ instance_conf | to_json | from_json | to_yaml }} + dest: "{{ molecule_instance_config }}" + mode: "0600" + when: server.changed | default(false) | bool # noqa no-handler diff --git a/molecule/fedora/inventory.yml b/molecule/fedora/inventory.yml new file mode 100644 index 0000000..8608676 --- /dev/null +++ b/molecule/fedora/inventory.yml @@ -0,0 +1,10 @@ +--- +all: + children: + builders: + hosts: + fedora-test: + ansible_host: fedora-test + container_image: registry.fedoraproject.org/fedora:latest + container_command: /sbin/init + container_privileged: true diff --git a/molecule/fedora/molecule.yml b/molecule/fedora/molecule.yml new file mode 100644 index 0000000..4219132 --- /dev/null +++ b/molecule/fedora/molecule.yml @@ -0,0 +1,47 @@ +--- +# Dependency management (download roles/collections) +dependency: + name: galaxy + options: + requirements-file: ../requirements.yml + + +ansible: + cfg: + defaults: + host_key_checking: false + verbosity: 1 + + executor: + backend: ansible-playbook + args: + ansible_playbook: + - --diff + - --force-handlers + - --inventory=/path/to/inventory.yml + ansible_navigator: + - --mode stdout + - --pull-policy missing + - --execution-environment-image ghcr.io/ansible/community-ansible-dev-tools:latest + + playbooks: + create: create.yml + converge: converge.yml + destroy: destroy.yml + cleanup: cleanup.yml + prepare: prepare.yml + side_effect: side_effect.yml + verify: verify.yml + +scenario: + name: fedora + test_sequence: + - dependency + - syntax + - create + - prepare + - converge + - idempotence + - verify + - cleanup + - destroy diff --git a/molecule/fedora/verify.yml b/molecule/fedora/verify.yml new file mode 100644 index 0000000..469b98c --- /dev/null +++ b/molecule/fedora/verify.yml @@ -0,0 +1,10 @@ +--- +# Purpose: assert that the instance really ended up in the expected state. +# Molecule calls this playbook with `molecule verify`. +- name: Verify + hosts: instance + gather_facts: false # Quicker, if you do not need facts + tasks: + - name: Assert something + ansible.builtin.assert: + that: true diff --git a/molecule/requirements.yml b/molecule/requirements.yml new file mode 100644 index 0000000..87b2525 --- /dev/null +++ b/molecule/requirements.yml @@ -0,0 +1,4 @@ +--- +collections: + - name: containers.podman + version: ">=1.10.0" diff --git a/tests/Containerfile.debian b/tests/Containerfile.debian deleted file mode 100644 index 67ca74f..0000000 --- a/tests/Containerfile.debian +++ /dev/null @@ -1,18 +0,0 @@ -FROM debian:bookworm -LABEL PROJECT "ansible_role_package" -LABEL MAINTAINER "Matthew Stobbs " - -RUN useradd -d /home/ansible -m -G wheel ansible -RUN apt update && \ - apt install -y openssh-server python3-paramiko gnupg2 -EXPOSE 22 - -USER ansible -WORKDIR /home/ansible -RUN mkdir /home/ansible/.ssh -COPY ./pubkey /home/ansible/.ssh/authorized_keys - -USER root -RUN echo "ansible ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/sudoers -RUN ssh-keygen -A -ENTRYPOINT /usr/sbin/sshd -D diff --git a/tests/Containerfile.el9 b/tests/Containerfile.el9 deleted file mode 100644 index 15f4d96..0000000 --- a/tests/Containerfile.el9 +++ /dev/null @@ -1,17 +0,0 @@ -FROM almalinux:9 -LABEL PROJECT "ansible_role_package" -LABEL MAINTAINER "Matthew Stobbs " - -RUN useradd -d /home/ansible -m -G wheel ansible -RUN dnf install -y openssh-server python3-paramiko python3-libdnf gnupg2 -EXPOSE 22 - -USER ansible -WORKDIR /home/ansible -RUN mkdir /home/ansible/.ssh -COPY ./pubkey /home/ansible/.ssh/authorized_keys - -USER root -RUN echo "ansible ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/sudoers -RUN ssh-keygen -A -ENTRYPOINT /usr/sbin/sshd -D diff --git a/tests/Containerfile.fedora b/tests/Containerfile.fedora deleted file mode 100644 index 957c9b1..0000000 --- a/tests/Containerfile.fedora +++ /dev/null @@ -1,17 +0,0 @@ -FROM fedora:41 -LABEL PROJECT "ansible_role_package" -LABEL MAINTAINER "Matthew Stobbs " - -RUN useradd -d /home/ansible -m -G wheel ansible -RUN dnf install -y openssh-server python3-paramiko python3-libdnf5 gnupg2 -EXPOSE 22 - -USER ansible -WORKDIR /home/ansible -RUN mkdir /home/ansible/.ssh -COPY ./pubkey /home/ansible/.ssh/authorized_keys - -USER root -RUN echo "ansible ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/sudoers -RUN ssh-keygen -A -ENTRYPOINT /usr/sbin/sshd -D diff --git a/tests/Containerfile.ubuntu b/tests/Containerfile.ubuntu deleted file mode 100644 index 334297c..0000000 --- a/tests/Containerfile.ubuntu +++ /dev/null @@ -1,18 +0,0 @@ -FROM ubuntu:24.04 -LABEL PROJECT "ansible_role_package" -LABEL MAINTAINER "Matthew Stobbs " - -RUN useradd -d /home/ansible -m -G wheel ansible -RUN apt update && \ - apt install -y openssh-server python3-paramiko gnupg2 -EXPOSE 22 - -USER ansible -WORKDIR /home/ansible -RUN mkdir /home/ansible/.ssh -COPY ./pubkey /home/ansible/.ssh/authorized_keys - -USER root -RUN echo "ansible ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/sudoers -RUN ssh-keygen -A -ENTRYPOINT /usr/sbin/sshd -D diff --git a/tests/cleanup.sh b/tests/cleanup.sh deleted file mode 100755 index f296948..0000000 --- a/tests/cleanup.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env zsh - -set -x - -[[ -f ./lib.sh ]] && source ./lib.sh || exit 1 - -ssh-keygen -R "[127.0.0.1]:2222" - -for os in ${OSBUILDS[@]} -do - if podman container exists ${CONTAINER}_${os} - then - podman stop ${CONTAINER}_${os} - fi -done - -if [ "$HOSTOS" = "Darwin" ] -then - podman machine stop ${MACHINENAME} -fi diff --git a/tests/inventory.yml b/tests/inventory.yml deleted file mode 100644 index dd14dfb..0000000 --- a/tests/inventory.yml +++ /dev/null @@ -1,7 +0,0 @@ -test: - hosts: - localhost: - ansible_ssh_host: 127.0.0.1 - ansible_ssh_port: 2222 - ansible_ssh_user: ansible - ansible_ssh_extra_args: "-o StrictHostKeyChecking=no" diff --git a/tests/lib.sh b/tests/lib.sh deleted file mode 100644 index 4b6986b..0000000 --- a/tests/lib.sh +++ /dev/null @@ -1,10 +0,0 @@ -HOSTOS="$(uname -o)" -IMAGE=${IMAGE:-packagetest} -CONTAINER=${CONTAINER:-packagetest} -MACHINENAME=${MACHINENAME:-podman-machine-default} -# OSBUILDS=("fedora" "el9" "debian" "ubuntu") -OSBUILDS=("fedora") - -function machine_state { - echo $(podman machine inspect $MACHINENAME | jq -r '.[].State') -} diff --git a/tests/pubkey b/tests/pubkey deleted file mode 100644 index ea6ad18..0000000 --- a/tests/pubkey +++ /dev/null @@ -1 +0,0 @@ -ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAAxfDLK7wu1ITTMV8gIzImO+KkXsjByvN7gYebqb5e+ stobbsm@ed25591 diff --git a/tests/setup.sh b/tests/setup.sh deleted file mode 100755 index 985e1aa..0000000 --- a/tests/setup.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env zsh - -set -x - -[[ -f ./lib.sh ]] && source ./lib.sh || exit 1 - -if [ "$HOSTOS" = "Darwin" ] -then - echo "Starting $MACHINENAME" - podman machine start -q $MACHINENAME - while [ "$(machine_state)" != "running" ] - do - echo $(machine_state) - echo "Wating for $MACHINENAME to start" - sleep 1 - done - - echo "Machine $MACHINENAME running" -fi - -echo "Building container images" -for os in ${OSBUILDS[@]} -do - echo "Building image for ${os}" - podman build --platform linux/amd64 -f Containerfile.${os} -t localhost/${IMAGE}:${os} . -done diff --git a/tests/test.sh b/tests/test.sh deleted file mode 100755 index 186ad87..0000000 --- a/tests/test.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/env zsh - -set -x - -[[ -f ./lib.sh ]] && source ./lib.sh || exit 1 - -for os in ${OSBUILDS[@]} -do - podman run --rm -it -d --platform linux/amd64 --name ${CONTAINER}_${os} -p 2222:22 localhost/${IMAGE}:${os} - ansible-playbook "test.yml" -i inventory.yml -done diff --git a/tests/test.yml b/tests/test.yml deleted file mode 100644 index f15442e..0000000 --- a/tests/test.yml +++ /dev/null @@ -1,12 +0,0 @@ ---- -- hosts: localhost - vars: - testpkgs: - - hyprland - tasks: - - name: Test all packages with use_local=false - ansible.builtin.include_role: - name: ansible_role_package - vars: - use_local: false - packages: "{{ testpkgs }}"