Skip to content

Commit

Permalink
Add example of using podman collection using default driver (#3947)
Browse files Browse the repository at this point in the history
Partial: #3919
  • Loading branch information
ssbarnea authored Jul 10, 2023
1 parent 21f3cf2 commit 7584a16
Show file tree
Hide file tree
Showing 16 changed files with 292 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
matrix: ${{ fromJson(needs.pre.outputs.matrix) }}

env:
PYTEST_REQPASS: 445
PYTEST_REQPASS: 446
steps:
- uses: actions/checkout@v3
with:
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ coverage.xml
pip-wheel-metadata
docs/docstree
.docker
molecule/**
tools/test-schema/node_modules
site
.DS_Store
Expand Down
41 changes: 41 additions & 0 deletions docs/podman.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Using podman containers

Below you can see a scenario that is using podman containers as test hosts.
When you run `molecule test --scenario podman` the `create`, `converge` and
`destroy` steps will be run one after another.

This example is using Ansible playbooks and it does not need any molecule
plugins to run. You can fully control which test requirements you need to be
installed.

## Config playbook

```yaml title="molecule.yml"
{!../molecule/podman/molecule.yml!}
```

```yaml title="requirements.yml"
{!../molecule/podman/requirements.yml!}
```

## Create playbook

```yaml title="create.yml"
{!../molecule/podman/create.yml!}
```

```yaml title="tasks/create-fail.yml"
{!../molecule/podman/tasks/create-fail.yml!}
```

## Converge playbook

```yaml title="converge.yml"
{!../molecule/podman/converge.yml!}
```

## Destroy playbook

```yaml title="destroy.yml"
{!../molecule/podman/destroy.yml!}
```
4 changes: 4 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ nav:
- next.md
- configuration.md
- ci.md
- Examples:
- podman.md
- examples.md
- faq.md
- contributing.md
Expand Down Expand Up @@ -88,6 +90,8 @@ plugins:
domains: [py, std]

markdown_extensions:
- markdown_include.include:
base_path: docs
- admonition
- def_list
- footnotes
Expand Down
8 changes: 8 additions & 0 deletions molecule/default/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
- name: Converge
hosts: all
gather_facts: false
tasks:
- name: Replace this task with one that validates your content
ansible.builtin.debug:
msg: "This is the effective test"
37 changes: 37 additions & 0 deletions molecule/default/create.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
- 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
24 changes: 24 additions & 0 deletions molecule/default/destroy.yml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions molecule/default/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
platforms:
- name: instance
# you might want to add your own variables here based on what provisioning
# you are doing like:
# image: quay.io/centos/centos:stream8
29 changes: 29 additions & 0 deletions molecule/podman/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
- name: Fail if molecule group is missing
hosts: localhost
tasks:
- name: Print some info
ansible.builtin.debug:
msg: "{{ groups }}"

- name: Assert group existence
ansible.builtin.assert:
that: "'molecule' in groups"
fail_msg: |
molecule group was not found inside inventory groups: {{ groups }}
- name: Converge
hosts: molecule
# We disable gather facts because it would fail due to our container not
# having python installed. This will not prevent use from running 'raw'
# commands. Most molecule users are expected to use containers that already
# have python installed in order to avoid notable delays installing it.
gather_facts: false
tasks:
- name: Check uname
ansible.builtin.raw: uname -a
register: result
changed_when: false

- name: Print some info
ansible.builtin.assert:
that: result.stdout | regex_search("^Linux")
80 changes: 80 additions & 0 deletions molecule/podman/create.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
- name: Create
hosts: localhost
gather_facts: false
vars:
molecule_inventory:
all:
hosts: {}
molecule: {}
tasks:
- name: Create a container
containers.podman.podman_container:
name: "{{ item.name }}"
image: "{{ item.image }}"
state: started
command: sleep 1d
# bash -c "while true; do sleep 10000; done"
log_driver: json-file
register: result
loop: "{{ molecule_yml.platforms }}"

- name: Print some info
ansible.builtin.debug:
msg: "{{ result.results }}"

- name: Fail if container is not running
when: >
item.container.State.ExitCode != 0 or
not item.container.State.Running
ansible.builtin.include_tasks:
file: tasks/create-fail.yml
loop: "{{ result.results }}"
loop_control:
label: "{{ item.container.Name }}"

- name: Add container to molecule_inventory
vars:
inventory_partial_yaml: |
all:
children:
molecule:
hosts:
"{{ item.name }}":
ansible_connection: containers.podman.podman
ansible.builtin.set_fact:
molecule_inventory: >
{{ molecule_inventory | combine(inventory_partial_yaml | from_yaml) }}
loop: "{{ molecule_yml.platforms }}"
loop_control:
label: "{{ item.name }}"

- name: Dump molecule_inventory
ansible.builtin.copy:
content: |
{{ molecule_inventory | to_yaml }}
dest: "{{ molecule_ephemeral_directory }}/inventory/molecule_inventory.yml"
mode: 0600

- name: Force inventory refresh
ansible.builtin.meta: refresh_inventory

- name: Fail if molecule group is missing
ansible.builtin.assert:
that: "'molecule' in groups"
fail_msg: |
molecule group was not found inside inventory groups: {{ groups }}
run_once: true # noqa: run-once[task]

# we want to avoid errors like "Failed to create temporary directory"
- name: Validate that inventory was refreshed
hosts: molecule
gather_facts: false
tasks:
- name: Check uname
ansible.builtin.raw: uname -a
register: result
changed_when: false

- name: Display uname info
ansible.builtin.debug:
msg: "{{ result.stdout }}"
24 changes: 24 additions & 0 deletions molecule/podman/destroy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
- name: Destroy molecule containers
hosts: molecule
gather_facts: false
tasks:
- name: Stop and remove container
delegate_to: localhost
containers.podman.podman_container:
name: "{{ inventory_hostname }}"
state: absent
rm: true
- name: Remove potentially stopped container
delegate_to: localhost
ansible.builtin.command:
cmd: podman container rm --ignore {{ inventory_hostname }}
changed_when: false

- name: Remove dynamic molecule inventory
hosts: localhost
gather_facts: false
tasks:
- name: Remove dynamic inventory file
ansible.builtin.file:
path: "{{ molecule_ephemeral_directory }}/inventory/molecule_inventory.yml"
state: absent
7 changes: 7 additions & 0 deletions molecule/podman/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dependency:
name: galaxy
options:
requirements-file: requirements.yml
platforms:
- name: molecule-ubuntu
image: ubuntu:18.04
2 changes: 2 additions & 0 deletions molecule/podman/requirements.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
collections:
- containers.podman
14 changes: 14 additions & 0 deletions molecule/podman/tasks/create-fail.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
- name: Retrieve container log
ansible.builtin.command:
cmd: >-
{% raw %}
podman logs
{% endraw %}
{{ item.stdout_lines[0] }}
# podman inspect --format='{{.HostConfig.LogConfig.Path}}'
changed_when: false
register: logfile_cmd

- name: Display container log
ansible.builtin.fail:
msg: "{{ logfile_cmd.stderr }}"
6 changes: 6 additions & 0 deletions src/molecule/command/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ def _get_login(self, hostname): # pragma: no cover
login_options = self._config.driver.login_options(hostname)
login_options["columns"] = columns
login_options["lines"] = lines
if not self._config.driver.login_cmd_template:
LOG.warning(
"Login command is not supported for [dim]%s[/] host because 'login_cmd_template' was not defined in driver options.",
login_options["instance"],
)
return
login_cmd = self._config.driver.login_cmd_template.format(**login_options)

cmd = shlex.split(f"/usr/bin/env {login_cmd}")
Expand Down
9 changes: 9 additions & 0 deletions src/molecule/test/b_functional/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,12 @@ def test_sample_collection() -> None:
).returncode
== 0
)


def test_podman() -> None:
assert (
run_command(
["molecule", "test", "--scenario-name", "podman"],
).returncode
== 0
)

0 comments on commit 7584a16

Please sign in to comment.