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

docker_network: add basic integration tests #46137

Merged
merged 2 commits into from
Oct 19, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions test/integration/targets/docker_network/aliases
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
shippable/posix/group2
skip/osx
skip/freebsd
destructive
3 changes: 3 additions & 0 deletions test/integration/targets/docker_network/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
dependencies:
- setup_docker
31 changes: 31 additions & 0 deletions test/integration/targets/docker_network/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
- name: Create random name prefix
set_fact:
name_prefix: "{{ 'ansible-test-%0x' % ((2**32) | random) }}"
cnames: []
dnetworks: []

- debug:
msg: "Using name prefix {{ name_prefix }}"

- block:
- include_tasks: run-test.yml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use - include_tasks: "{{ item }}" directly, no need to use run-test.yml (- include_tasks: tests/basic.yml works too)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's one big advantage over doing it this way: if you do all include_tasks directly, you will get the included: .../tasks/tests/xxx.yml for localhost all at the same time, while with this indirect include, you will get them before the tasks are actually executed. This makes it easier to find out in which file a task is defined (if you're hunting down something that breaks).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I originally did it directly for the docker_container tests, but after I had two tests instead of only one, I noticed this and changed it this way to make it easier to find out to which "sub-test" tests belong to.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing to change here, I would have done differently (maybe using debug module :)

with_fileglob:
- "tests/*.yml"

always:
- name: "Make sure all containers are removed"
docker_container:
name: "{{ item }}"
state: absent
stop_timeout: 1
loop: "{{ cnames }}"
- name: "Make sure all networks are removed"
docker_network:
name: "{{ item }}"
state: absent
force: yes
loop: "{{ dnetworks }}"

# Skip for CentOS 6
when: ansible_distribution != 'CentOS' or ansible_distribution_major_version|int > 6
3 changes: 3 additions & 0 deletions test/integration/targets/docker_network/tasks/run-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
- name: "Loading tasks from {{ item }}"
include_tasks: "{{ item }}"
117 changes: 117 additions & 0 deletions test/integration/targets/docker_network/tasks/tests/basic.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
---
- name: Registering container and network names
set_fact:
cname_1: "{{ name_prefix ~ '-container-1' }}"
cname_2: "{{ name_prefix ~ '-container-2' }}"
cname_3: "{{ name_prefix ~ '-container-3' }}"
nname_1: "{{ name_prefix ~ '-network-1' }}"
nname_2: "{{ name_prefix ~ '-network-2' }}"
- name: Registering container and network names
set_fact:
cnames: "{{ cnames }} + [cname_1, cname_2, cname_3]"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't cnames: "[cname_1, cname_2, cname_3]" sufficient ? Then set_fact: cnames at beginning of main.yml could be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now there's only one sub-test (basic.yml), so yes, that would work, but as soon as one more is added, this is necessary (so that all names are collected, and the objects will be destroyed even in case of failing tests and other errors).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what this future test will look like :)

dnetworks: "{{ dnetworks }} + [nname_1, nname_2]"

- name: Create containers
docker_container:
name: "{{ container_name }}"
image: hello-world
state: present
loop:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loop: {{ cnames }} could be used here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only at this point where there's only one sub-test.

- "{{ cname_1 }}"
- "{{ cname_2 }}"
- "{{ cname_3 }}"
loop_control:
loop_var: container_name

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no idempotency check for container ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that's already done enough many times in the docker_container integration tests ;)

####################################################################

- name: Create network
docker_network:
name: "{{ nname_1 }}"
state: present
register: networks_1

- name: Connect network to containers 1 and 2
docker_network:
name: "{{ nname_1 }}"
state: present
connected:
- "{{ cname_1 }}"
- "{{ cname_2 }}"
register: networks_2

- name: Connect network to containers 1 and 2 (idempotency)
docker_network:
name: "{{ nname_1 }}"
state: present
connected:
- "{{ cname_1 }}"
- "{{ cname_2 }}"
register: networks_2_idem

- name: Connect network to container 3
docker_network:
name: "{{ nname_1 }}"
state: present
connected:
- "{{ cname_3 }}"
appends: yes
register: networks_3

- name: Connect network to container 3 (idempotency)
docker_network:
name: "{{ nname_1 }}"
state: present
connected:
- "{{ cname_3 }}"
appends: yes
register: networks_3_idem

- name: Disconnect network from container 1
docker_network:
name: "{{ nname_1 }}"
state: present
connected:
- "{{ cname_2 }}"
- "{{ cname_3 }}"
register: networks_4

- name: Disconnect network from container 1 (idempotency)
docker_network:
name: "{{ nname_1 }}"
state: present
connected:
- "{{ cname_2 }}"
- "{{ cname_3 }}"
register: networks_4_idem

- name: Cleanup
docker_network:
name: "{{ nname_1 }}"
state: absent

# The idempotency tests do NOT work currently.

- assert:
that:
- networks_1 is changed
- networks_2 is changed
# - networks_2_idem is not changed
- networks_3 is changed
# - networks_3_idem is not changed
- networks_4 is changed
# - networks_4_idem is not changed

####################################################################

- name: Delete containers
docker_container:
name: "{{ container_name }}"
state: absent
stop_timeout: 1
loop:
- "{{ cname_1 }}"
- "{{ cname_2 }}"
- "{{ cname_3 }}"
loop_control:
loop_var: container_name