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

Add example of using docker collection using default driver #3978

Merged
merged 1 commit into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
41 changes: 41 additions & 0 deletions docs/docker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Using docker containers

Below you can see a scenario that is using docker containers as test hosts.
When you run `molecule test --scenario docker` 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/docker/molecule.yml!}
```

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

## Create playbook

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

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

## Converge playbook

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

## Destroy playbook

```yaml title="destroy.yml"
{!../molecule/docker/destroy.yml!}
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ nav:
- configuration.md
- ci.md
- Examples:
- docker.md
- podman.md
- examples.md
- faq.md
Expand Down
29 changes: 29 additions & 0 deletions molecule/docker/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")
79 changes: 79 additions & 0 deletions molecule/docker/create.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
- name: Create
hosts: localhost
gather_facts: false
vars:
molecule_inventory:
all:
hosts: {}
molecule: {}
tasks:
- name: Create a container
community.docker.docker_container:
name: "{{ item.name }}"
image: "{{ item.image }}"
state: started
command: sleep 1d
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: community.docker.docker
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 }}"
19 changes: 19 additions & 0 deletions molecule/docker/destroy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
- name: Destroy molecule containers
hosts: molecule
gather_facts: false
tasks:
- name: Stop and remove container
delegate_to: localhost
community.docker.docker_container:
name: "{{ inventory_hostname }}"
state: absent
auto_remove: true

- 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/docker/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/docker/requirements.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
collections:
- community.docker
13 changes: 13 additions & 0 deletions molecule/docker/tasks/create-fail.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
- name: Retrieve container log
ansible.builtin.command:
cmd: >-
{% raw %}
docker logs
{% endraw %}
{{ item.stdout_lines[0] }}
changed_when: false
register: logfile_cmd

- name: Display container log
ansible.builtin.fail:
msg: "{{ logfile_cmd.stderr }}"
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 @@ -343,3 +343,12 @@ def test_podman() -> None:
).returncode
== 0
)


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