From 18b575b593221c3712dc87d5153a071bfa69eeca Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Wed, 19 Sep 2018 13:01:23 +0200 Subject: [PATCH] integration tests for docker_container (#45747) * First round of integration tests for docker_container. * Added regression test for #45700. * Work around dict order randomization. --- .../targets/docker_container/aliases | 4 + .../targets/docker_container/meta/main.yml | 3 + .../targets/docker_container/tasks/main.yml | 23 ++ .../docker_container/tasks/run-test.yml | 3 + .../regression-45700-dont-parse-on-absent.yml | 34 ++ .../tasks/tests/start-stop.yml | 296 ++++++++++++++++++ 6 files changed, 363 insertions(+) create mode 100644 test/integration/targets/docker_container/aliases create mode 100644 test/integration/targets/docker_container/meta/main.yml create mode 100644 test/integration/targets/docker_container/tasks/main.yml create mode 100644 test/integration/targets/docker_container/tasks/run-test.yml create mode 100644 test/integration/targets/docker_container/tasks/tests/regression-45700-dont-parse-on-absent.yml create mode 100644 test/integration/targets/docker_container/tasks/tests/start-stop.yml diff --git a/test/integration/targets/docker_container/aliases b/test/integration/targets/docker_container/aliases new file mode 100644 index 00000000000..2b3832dde58 --- /dev/null +++ b/test/integration/targets/docker_container/aliases @@ -0,0 +1,4 @@ +shippable/posix/group2 +skip/osx +skip/freebsd +destructive diff --git a/test/integration/targets/docker_container/meta/main.yml b/test/integration/targets/docker_container/meta/main.yml new file mode 100644 index 00000000000..07da8c6ddae --- /dev/null +++ b/test/integration/targets/docker_container/meta/main.yml @@ -0,0 +1,3 @@ +--- +dependencies: + - setup_docker diff --git a/test/integration/targets/docker_container/tasks/main.yml b/test/integration/targets/docker_container/tasks/main.yml new file mode 100644 index 00000000000..0c107d4b2e5 --- /dev/null +++ b/test/integration/targets/docker_container/tasks/main.yml @@ -0,0 +1,23 @@ +--- +- block: + - name: Create random container name prefix + set_fact: + cname_prefix: "{{ 'ansible-test-%0x' % ((2**32) | random) }}" + cnames: [] + + - debug: + msg: "Using container name prefix {{ cname_prefix }}" + + - block: + - include_tasks: run-test.yml + with_fileglob: + - "tests/*.yml" + + always: + - name: "Make sure all containers are removed" + docker_container: + name: "{{ item }}" + state: absent + stop_timeout: 1 + with_items: "{{ cnames }}" + when: ansible_os_family != 'RedHat' or ansible_distribution_major_version != '6' diff --git a/test/integration/targets/docker_container/tasks/run-test.yml b/test/integration/targets/docker_container/tasks/run-test.yml new file mode 100644 index 00000000000..a2999370142 --- /dev/null +++ b/test/integration/targets/docker_container/tasks/run-test.yml @@ -0,0 +1,3 @@ +--- +- name: "Loading tasks from {{ item }}" + include_tasks: "{{ item }}" diff --git a/test/integration/targets/docker_container/tasks/tests/regression-45700-dont-parse-on-absent.yml b/test/integration/targets/docker_container/tasks/tests/regression-45700-dont-parse-on-absent.yml new file mode 100644 index 00000000000..85ce6f035ee --- /dev/null +++ b/test/integration/targets/docker_container/tasks/tests/regression-45700-dont-parse-on-absent.yml @@ -0,0 +1,34 @@ +--- +# Regression test for https://github.com/ansible/ansible/pull/45700 +- name: Registering container name + set_fact: + cname: "{{ cname_prefix ~ '-45700' }}" +- name: Registering container name + set_fact: + cnames: "{{ cnames }} + [cname]" + +- name: Start container + docker_container: + image: alpine:3.8 + command: '/bin/sh -c "sleep 1h"' + name: "{{ cname }}" + state: started + +- name: Stop container with a lot of invalid options + docker_container: + name: "{{ cname }}" + stop_timeout: 1 + # Some options with "invalid" values, which would + # have to be parsed. The values are "invalid" because + # the containers and networks listed here do not exist. + # This can happen because the networks are removed + # before the container is stopped (see + # https://github.com/ansible/ansible/issues/45486). + networks: + - name: "nonexistant-network-{{ (2**32) | random }}" + published_ports: + - '1:2' + - '3' + links: + - "nonexistant-container-{{ (2**32) | random }}:test" + state: absent diff --git a/test/integration/targets/docker_container/tasks/tests/start-stop.yml b/test/integration/targets/docker_container/tasks/tests/start-stop.yml new file mode 100644 index 00000000000..a479cb1fe03 --- /dev/null +++ b/test/integration/targets/docker_container/tasks/tests/start-stop.yml @@ -0,0 +1,296 @@ +--- +- name: Registering container name + set_fact: + cname: "{{ cname_prefix ~ '-hi' }}" +- name: Registering container name + set_fact: + cnames: "{{ cnames }} + [cname]" + +#################################################################### +## Creation ######################################################## +#################################################################### + +- name: Create container (check) + docker_container: + image: alpine:3.8 + command: '/bin/sh -c "sleep 1h"' + name: "{{ cname }}" + state: present + check_mode: yes + register: create_1 + +- name: Create container + docker_container: + image: alpine:3.8 + command: '/bin/sh -c "sleep 1h"' + name: "{{ cname }}" + state: present + register: create_2 + +- name: Create container (idempotent) + docker_container: + image: alpine:3.8 + command: '/bin/sh -c "sleep 1h"' + name: "{{ cname }}" + state: present + register: create_3 + +- name: Create container (idempotent check) + docker_container: + image: alpine:3.8 + command: '/bin/sh -c "sleep 1h"' + name: "{{ cname }}" + state: present + check_mode: yes + register: create_4 + +- assert: + that: + - create_1 is changed + - create_2 is changed + - create_3 is not changed + - create_4 is not changed + +#################################################################### +## Starting (after creation) ####################################### +#################################################################### + +- name: Start container (check) + docker_container: + name: "{{ cname }}" + state: started + check_mode: yes + register: start_1 + +- name: Start container + docker_container: + name: "{{ cname }}" + state: started + register: start_2 + +- name: Start container (idempotent) + docker_container: + name: "{{ cname }}" + state: started + register: start_3 + +- name: Start container (idempotent check) + docker_container: + name: "{{ cname }}" + state: started + check_mode: yes + register: start_4 + +- assert: + that: + - start_1 is changed + - start_2 is changed + - start_3 is not changed + - start_4 is not changed + +#################################################################### +## Present check for running container ############################# +#################################################################### + +- name: Present check for running container (check) + docker_container: + image: alpine:3.8 + command: '/bin/sh -c "sleep 1h"' + name: "{{ cname }}" + state: present + check_mode: yes + register: present_check_1 + +- name: Present check for running container + docker_container: + image: alpine:3.8 + command: '/bin/sh -c "sleep 1h"' + name: "{{ cname }}" + state: present + register: present_check_2 + +- assert: + that: + - present_check_1 is not changed + - present_check_2 is not changed + +#################################################################### +## Starting (from scratch) ######################################### +#################################################################### + +- name: Remove container (setup for starting from scratch) + docker_container: + name: "{{ cname }}" + state: absent + stop_timeout: 1 + +- name: Start container from scratch (check) + docker_container: + image: alpine:3.8 + command: '/bin/sh -c "sleep 1h"' + name: "{{ cname }}" + state: started + check_mode: yes + register: start_scratch_1 + +- name: Start container from scratch + docker_container: + image: alpine:3.8 + command: '/bin/sh -c "sleep 1h"' + name: "{{ cname }}" + state: started + register: start_scratch_2 + +- name: Start container from scratch (idempotent) + docker_container: + image: alpine:3.8 + command: '/bin/sh -c "sleep 1h"' + name: "{{ cname }}" + state: started + register: start_scratch_3 + +- name: Start container from scratch (idempotent check) + docker_container: + image: alpine:3.8 + command: '/bin/sh -c "sleep 1h"' + name: "{{ cname }}" + state: started + check_mode: yes + register: start_scratch_4 + +- assert: + that: + - start_scratch_1 is changed + - start_scratch_2 is changed + - start_scratch_3 is not changed + - start_scratch_4 is not changed + +#################################################################### +## Stopping ######################################################## +#################################################################### + +- name: Stop container (check) + docker_container: + image: alpine:3.8 + name: "{{ cname }}" + state: stopped + stop_timeout: 1 + check_mode: yes + register: stop_1 + +- name: Stop container + docker_container: + image: alpine:3.8 + name: "{{ cname }}" + state: stopped + stop_timeout: 1 + register: stop_2 + +- name: Stop container (idempotent) + docker_container: + image: alpine:3.8 + name: "{{ cname }}" + state: stopped + stop_timeout: 1 + register: stop_3 + +- name: Stop container (idempotent check) + docker_container: + image: alpine:3.8 + name: "{{ cname }}" + state: stopped + stop_timeout: 1 + check_mode: yes + register: stop_4 + +- assert: + that: + - stop_1 is changed + - stop_2 is changed + - stop_3 is not changed + - stop_4 is not changed + +#################################################################### +## Removing ######################################################## +#################################################################### + +- name: Remove container (check) + docker_container: + name: "{{ cname }}" + state: absent + check_mode: yes + register: remove_1 + +- name: Remove container + docker_container: + name: "{{ cname }}" + state: absent + register: remove_2 + +- name: Remove container (idempotent) + docker_container: + name: "{{ cname }}" + state: absent + register: remove_3 + +- name: Remove container (idempotent check) + docker_container: + name: "{{ cname }}" + state: absent + check_mode: yes + register: remove_4 + +- assert: + that: + - remove_1 is changed + - remove_2 is changed + - remove_3 is not changed + - remove_4 is not changed + +#################################################################### +## Removing (from running) ######################################### +#################################################################### + +- name: Start container (setup for removing from running) + docker_container: + image: alpine:3.8 + command: '/bin/sh -c "sleep 1h"' + name: "{{ cname }}" + state: started + +- name: Remove container from running (check) + docker_container: + name: "{{ cname }}" + state: absent + stop_timeout: 1 + check_mode: yes + register: remove_from_running_1 + +- name: Remove container from running + docker_container: + name: "{{ cname }}" + state: absent + stop_timeout: 1 + register: remove_from_running_2 + +- name: Remove container from running (idempotent) + docker_container: + name: "{{ cname }}" + state: absent + stop_timeout: 1 + register: remove_from_running_3 + +- name: Remove container from running (idempotent check) + docker_container: + name: "{{ cname }}" + state: absent + stop_timeout: 1 + check_mode: yes + register: remove_from_running_4 + +- assert: + that: + - remove_from_running_1 is changed + - remove_from_running_2 is changed + - remove_from_running_3 is not changed + - remove_from_running_4 is not changed