8. Molecule test at collection’s role level

It’s good practice to always keep test code next to the components. In the case of a collection’s role, this means placing it in the role’s directory.

roles/
 apache/
   molecule/
     apache5_with_collection/
       converge.yml
       Dockerfile.centos
       Dockerfile.ubuntu
       molecule.yml
       verify.yml
   tasks/
     main.yml

The test file layout is identical; everything is the same. It is not necessary to configure any role or collection paths, as Molecule is aware of the collection context and automatically installs the collection in the dependency stage. The collection’s role molecule.yml is super easy. The only complexity we see now is related to the CentOS platform due to systemd default behavior. I kept suppression of deprecation warnings to keep the log clear.

# molecule.yml
---
ansible:
  cfg:
    defaults:
      deprecation_warnings: false

driver:
  name: podman

platforms:
  - name: centos
    image: quay.io/centos/centos:stream9
    pre_build_image: false
    dockerfile: Dockerfile.centos
    cgroupns_mode: host
    command: ["/usr/sbin/init"]
    volumes:
      - /sys/fs/cgroup:/sys/fs/cgroup:rw
    tmpfs:
      /run: rw
      /run/lock: rw
    env:
      container: docker
    groups: [webservers]

  - name: ubuntu
    image: ubuntu:22.04
    pre_build_image: false
    dockerfile: Dockerfile.ubuntu
    groups: [webservers]

You can go to the role’s home and invoke the test. This time I will invoke all the tests:

Use the default scenario to test both Debian and RedHat.

cd roles/apache
molecule test

The Debian alone:

molecule test -s debian

And the RedHat alone:

molecule test -s redhat