---
- name: Registering container name
  set_fact:
    cname: "{{ cname_prefix ~ '-comparisons' }}"
- name: Registering container name
  set_fact:
    cnames: "{{ cnames + [cname] }}"

####################################################################
## value ###########################################################
####################################################################

- name: value
  docker_container:
    image: quay.io/ansible/docker-test-containers:alpine3.8
    command: '/bin/sh -c "sleep 10m"'
    name: "{{ cname }}"
    state: started
    hostname: example.com
  register: value_1

- name: value (change, ignore)
  docker_container:
    image: quay.io/ansible/docker-test-containers:alpine3.8
    command: '/bin/sh -c "sleep 10m"'
    name: "{{ cname }}"
    state: started
    hostname: example.org
    force_kill: yes
    comparisons:
      hostname: ignore
  register: value_2

- name: value (change, strict)
  docker_container:
    image: quay.io/ansible/docker-test-containers:alpine3.8
    command: '/bin/sh -c "sleep 10m"'
    name: "{{ cname }}"
    state: started
    hostname: example.org
    force_kill: yes
    comparisons:
      hostname: strict
  register: value_3

- name: cleanup
  docker_container:
    name: "{{ cname }}"
    state: absent
    force_kill: yes
  diff: no

- assert:
    that:
    - value_1 is changed
    - value_2 is not changed
    - value_3 is changed

####################################################################
## list ############################################################
####################################################################

- name: list
  docker_container:
    image: quay.io/ansible/docker-test-containers:alpine3.8
    command: '/bin/sh -c "sleep 10m"'
    name: "{{ cname }}"
    state: started
    dns_servers:
    - 1.1.1.1
    - 8.8.8.8
  register: list_1

- name: list (change, ignore)
  docker_container:
    image: quay.io/ansible/docker-test-containers:alpine3.8
    command: '/bin/sh -c "sleep 10m"'
    name: "{{ cname }}"
    state: started
    dns_servers:
    - 9.9.9.9
    force_kill: yes
    comparisons:
      dns_servers: ignore
  register: list_2

- name: list (change, strict)
  docker_container:
    image: quay.io/ansible/docker-test-containers:alpine3.8
    command: '/bin/sh -c "sleep 10m"'
    name: "{{ cname }}"
    state: started
    dns_servers:
    - 9.9.9.9
    force_kill: yes
    comparisons:
      dns_servers: strict
  register: list_3

- name: cleanup
  docker_container:
    name: "{{ cname }}"
    state: absent
    force_kill: yes
  diff: no

- assert:
    that:
    - list_1 is changed
    - list_2 is not changed
    - list_3 is changed

####################################################################
## set #############################################################
####################################################################

- name: set
  docker_container:
    image: quay.io/ansible/docker-test-containers:alpine3.8
    command: '/bin/sh -c "sleep 10m"'
    name: "{{ cname }}"
    state: started
    groups:
    - "1010"
    - "1011"
  register: set_1

- name: set (change, ignore)
  docker_container:
    image: quay.io/ansible/docker-test-containers:alpine3.8
    command: '/bin/sh -c "sleep 10m"'
    name: "{{ cname }}"
    state: started
    groups:
    - "1010"
    - "1011"
    - "1012"
    force_kill: yes
    comparisons:
      groups: ignore
  register: set_2

- name: set (change, allow_more_present)
  docker_container:
    image: quay.io/ansible/docker-test-containers:alpine3.8
    command: '/bin/sh -c "sleep 10m"'
    name: "{{ cname }}"
    state: started
    groups:
    - "1010"
    - "1011"
    - "1012"
    force_kill: yes
    comparisons:
      groups: allow_more_present
  register: set_3

- name: set (change, allow_more_present)
  docker_container:
    image: quay.io/ansible/docker-test-containers:alpine3.8
    command: '/bin/sh -c "sleep 10m"'
    name: "{{ cname }}"
    state: started
    groups:
    - "1010"
    - "1012"
    force_kill: yes
    comparisons:
      groups: allow_more_present
  register: set_4

- name: set (change, strict)
  docker_container:
    image: quay.io/ansible/docker-test-containers:alpine3.8
    command: '/bin/sh -c "sleep 10m"'
    name: "{{ cname }}"
    state: started
    groups:
    - "1010"
    - "1012"
    force_kill: yes
    comparisons:
      groups: strict
  register: set_5

- name: cleanup
  docker_container:
    name: "{{ cname }}"
    state: absent
    force_kill: yes
  diff: no

- assert:
    that:
    - set_1 is changed
    - set_2 is not changed
    - set_3 is changed
    - set_4 is not changed
    - set_5 is changed

####################################################################
## set(dict) #######################################################
####################################################################

- name: set(dict)
  docker_container:
    image: quay.io/ansible/docker-test-containers:alpine3.8
    command: '/bin/sh -c "sleep 10m"'
    name: "{{ cname }}"
    state: started
    devices:
    - "/dev/random:/dev/virt-random:rwm"
    - "/dev/urandom:/dev/virt-urandom:rwm"
  register: set_dict_1

- name: set(dict) (change, ignore)
  docker_container:
    image: quay.io/ansible/docker-test-containers:alpine3.8
    command: '/bin/sh -c "sleep 10m"'
    name: "{{ cname }}"
    state: started
    devices:
    - "/dev/random:/dev/virt-random:rwm"
    - "/dev/urandom:/dev/virt-urandom:rwm"
    - "/dev/null:/dev/virt-null:rwm"
    force_kill: yes
    comparisons:
      devices: ignore
  register: set_dict_2

- name: set(dict) (change, allow_more_present)
  docker_container:
    image: quay.io/ansible/docker-test-containers:alpine3.8
    command: '/bin/sh -c "sleep 10m"'
    name: "{{ cname }}"
    state: started
    devices:
    - "/dev/random:/dev/virt-random:rwm"
    - "/dev/urandom:/dev/virt-urandom:rwm"
    - "/dev/null:/dev/virt-null:rwm"
    force_kill: yes
    comparisons:
      devices: allow_more_present
  register: set_dict_3

- name: set(dict) (change, allow_more_present)
  docker_container:
    image: quay.io/ansible/docker-test-containers:alpine3.8
    command: '/bin/sh -c "sleep 10m"'
    name: "{{ cname }}"
    state: started
    devices:
    - "/dev/random:/dev/virt-random:rwm"
    - "/dev/null:/dev/virt-null:rwm"
    force_kill: yes
    comparisons:
      devices: allow_more_present
  register: set_dict_4

- name: set(dict) (change, strict)
  docker_container:
    image: quay.io/ansible/docker-test-containers:alpine3.8
    command: '/bin/sh -c "sleep 10m"'
    name: "{{ cname }}"
    state: started
    devices:
    - "/dev/random:/dev/virt-random:rwm"
    - "/dev/null:/dev/virt-null:rwm"
    force_kill: yes
    comparisons:
      devices: strict
  register: set_dict_5

- name: cleanup
  docker_container:
    name: "{{ cname }}"
    state: absent
    force_kill: yes
  diff: no

- assert:
    that:
    - set_dict_1 is changed
    - set_dict_2 is not changed
    - set_dict_3 is changed
    - set_dict_4 is not changed
    - set_dict_5 is changed

####################################################################
## dict ############################################################
####################################################################

- name: dict
  docker_container:
    image: quay.io/ansible/docker-test-containers:alpine3.8
    command: '/bin/sh -c "sleep 10m"'
    name: "{{ cname }}"
    state: started
    labels:
      ansible.test.1: hello
      ansible.test.2: world
  register: dict_1

- name: dict (change, ignore)
  docker_container:
    image: quay.io/ansible/docker-test-containers:alpine3.8
    command: '/bin/sh -c "sleep 10m"'
    name: "{{ cname }}"
    state: started
    labels:
      ansible.test.1: hello
      ansible.test.2: world
      ansible.test.3: ansible
    force_kill: yes
    comparisons:
      labels: ignore
  register: dict_2

- name: dict (change, allow_more_present)
  docker_container:
    image: quay.io/ansible/docker-test-containers:alpine3.8
    command: '/bin/sh -c "sleep 10m"'
    name: "{{ cname }}"
    state: started
    labels:
      ansible.test.1: hello
      ansible.test.2: world
      ansible.test.3: ansible
    force_kill: yes
    comparisons:
      labels: allow_more_present
  register: dict_3

- name: dict (change, allow_more_present)
  docker_container:
    image: quay.io/ansible/docker-test-containers:alpine3.8
    command: '/bin/sh -c "sleep 10m"'
    name: "{{ cname }}"
    state: started
    labels:
      ansible.test.1: hello
      ansible.test.3: ansible
    force_kill: yes
    comparisons:
      labels: allow_more_present
  register: dict_4

- name: dict (change, strict)
  docker_container:
    image: quay.io/ansible/docker-test-containers:alpine3.8
    command: '/bin/sh -c "sleep 10m"'
    name: "{{ cname }}"
    state: started
    labels:
      ansible.test.1: hello
      ansible.test.3: ansible
    force_kill: yes
    comparisons:
      labels: strict
  register: dict_5

- name: cleanup
  docker_container:
    name: "{{ cname }}"
    state: absent
    force_kill: yes
  diff: no

- assert:
    that:
    - dict_1 is changed
    - dict_2 is not changed
    - dict_3 is changed
    - dict_4 is not changed
    - dict_5 is changed

####################################################################
## wildcard ########################################################
####################################################################

- name: Pull hello-world image to make sure wildcard_2 test succeeds
  # If the image isn't there, it will pull it and return 'changed'.
  docker_image:
    name: quay.io/ansible/docker-test-containers:hello-world
    pull: true

- name: wildcard
  docker_container:
    image: quay.io/ansible/docker-test-containers:alpine3.8
    command: '/bin/sh -c "sleep 10m"'
    name: "{{ cname }}"
    state: started
    hostname: example.com
    stop_timeout: 1
    labels:
      ansible.test.1: hello
      ansible.test.2: world
      ansible.test.3: ansible
  register: wildcard_1

- name: wildcard (change, ignore)
  docker_container:
    image: quay.io/ansible/docker-test-containers:hello-world
    command: '/bin/sh -c "sleep 10m"'
    name: "{{ cname }}"
    state: started
    hostname: example.org
    stop_timeout: 2
    labels:
      ansible.test.1: hello
      ansible.test.4: ignore
    force_kill: yes
    comparisons:
      '*': ignore
  register: wildcard_2

- name: wildcard (change, strict)
  docker_container:
    image: quay.io/ansible/docker-test-containers:alpine3.8
    command: '/bin/sh -c "sleep 10m"'
    name: "{{ cname }}"
    state: started
    hostname: example.org
    stop_timeout: 1
    labels:
      ansible.test.1: hello
      ansible.test.2: world
      ansible.test.3: ansible
    force_kill: yes
    comparisons:
      '*': strict
  register: wildcard_3

- name: wildcard (no change, strict)
  docker_container:
    image: quay.io/ansible/docker-test-containers:alpine3.8
    command: '/bin/sh -c "sleep 10m"'
    name: "{{ cname }}"
    state: started
    hostname: example.org
    stop_timeout: 1
    labels:
      ansible.test.1: hello
      ansible.test.2: world
      ansible.test.3: ansible
    force_kill: yes
    comparisons:
      '*': strict
  register: wildcard_4

- name: cleanup
  docker_container:
    name: "{{ cname }}"
    state: absent
    force_kill: yes
  diff: no

- assert:
    that:
    - wildcard_1 is changed
    - wildcard_2 is not changed
    - wildcard_3 is changed
    - wildcard_4 is not changed
