---
- name: Registering image name
  set_fact:
    iname: "{{ name_prefix ~ '-options' }}"
    iname_1: "{{ name_prefix ~ '-options-1' }}"

- name: Registering image name
  set_fact:
    inames: "{{ inames + [iname, iname_1] }}"

####################################################################
## build.args ######################################################
####################################################################

- name: buildargs
  docker_image:
    name: "{{ iname }}"
    build:
      path: "{{ role_path }}/files"
      args:
        TEST1: val1
        TEST2: val2
        TEST3: "True"
      pull: no
    source: build
  register: buildargs_1

- name: buildargs (idempotency)
  docker_image:
    name: "{{ iname }}"
    build:
      path: "{{ role_path }}/files"
      args:
        TEST1: val1
        TEST2: val2
        TEST3: "True"
      pull: no
    source: build
  register: buildargs_2

- name: cleanup
  docker_image:
    name: "{{ iname }}"
    state: absent
    force_absent: yes

- assert:
    that:
    - buildargs_1 is changed
    - buildargs_2 is not changed
  when: docker_py_version is version('1.6.0', '>=')

- assert:
    that:
    - buildargs_1 is failed
    - buildargs_2 is failed
  when: docker_py_version is version('1.6.0', '<')

####################################################################
## container_limits ################################################
####################################################################

- name: container_limits (Failed due to min memory limit)
  docker_image:
    name: "{{ iname }}"
    build:
      path: "{{ role_path }}/files"
      container_limits:
        memory: 4000
      pull: no
    source: build
  ignore_errors: yes
  register: container_limits_1

- name: container_limits
  docker_image:
    name: "{{ iname }}"
    build:
      path: "{{ role_path }}/files"
      container_limits:
        memory: 7000000
        memswap: 8000000
      pull: no
    source: build
  register: container_limits_2

- name: cleanup
  docker_image:
    name: "{{ iname }}"
    state: absent
    force_absent: yes

- assert:
    that:
    # It *sometimes* happens that the first task does not fail.
    # For now, we work around this by
    #   a) requiring that if it fails, the message must
    #      contain 'Minimum memory limit allowed is (4|6)MB', and
    #   b) requiring that either the first task, or the second
    #      task is changed, but not both.
    - "not container_limits_1 is failed or ('Minimum memory limit allowed is ') in container_limits_1.msg"
    - "container_limits_1 is changed or container_limits_2 is changed and not (container_limits_1 is changed and container_limits_2 is changed)"

####################################################################
## dockerfile ######################################################
####################################################################

- name: dockerfile
  docker_image:
    name: "{{ iname }}"
    build:
      path: "{{ role_path }}/files"
      dockerfile: "MyDockerfile"
      pull: no
    source: build
  register: dockerfile_1

- name: cleanup
  docker_image:
    name: "{{ iname }}"
    state: absent
    force_absent: yes

- assert:
    that:
    - dockerfile_1 is changed
    - dockerfile_1['image']['Config']['WorkingDir'] == '/newdata'

####################################################################
## force ###########################################################
####################################################################

- name: Build an image
  docker_image:
    name: "{{ iname }}"
    build:
      path: "{{ role_path }}/files"
      pull: no
    source: build

- name: force (changed)
  docker_image:
    name: "{{ iname }}"
    build:
      path: "{{ role_path }}/files"
      dockerfile: "MyDockerfile"
      pull: no
    source: build
    force_source: yes
  register: force_1

- name: force (unchanged)
  docker_image:
    name: "{{ iname }}"
    build:
      path: "{{ role_path }}/files"
      dockerfile: "MyDockerfile"
      pull: no
    source: build
    force_source: yes
  register: force_2

- name: cleanup
  docker_image:
    name: "{{ iname }}"
    state: absent
    force_absent: yes

- assert:
    that:
    - force_1 is changed
    - force_2 is not changed

####################################################################
## load path #######################################################
####################################################################

- name: Archive image
  docker_image:
    name: "quay.io/ansible/docker-test-containers:hello-world"
    archive_path: "{{ output_dir }}/image.tar"
    source: pull
  register: archive_image

- name: Create invalid archive
  copy:
    dest: "{{ output_dir }}/image-invalid.tar"
    content: "this is not a valid image"

- name: remove image
  docker_image:
    name: "quay.io/ansible/docker-test-containers:hello-world"
    state: absent
    force_absent: yes

- name: load image (changed)
  docker_image:
    name: "quay.io/ansible/docker-test-containers:hello-world"
    load_path: "{{ output_dir }}/image.tar"
    source: load
  register: load_image

- name: load image (idempotency)
  docker_image:
    name: "quay.io/ansible/docker-test-containers:hello-world"
    load_path: "{{ output_dir }}/image.tar"
    source: load
  register: load_image_1

- name: load image (wrong name)
  docker_image:
    name: foo:bar
    load_path: "{{ output_dir }}/image.tar"
    source: load
  register: load_image_2
  ignore_errors: true

- name: load image (invalid image)
  docker_image:
    name: foo:bar
    load_path: "{{ output_dir }}/image-invalid.tar"
    source: load
  register: load_image_3
  ignore_errors: true

- name: load image (invalid image, old API version)
  docker_image:
    name: foo:bar
    load_path: "{{ output_dir }}/image-invalid.tar"
    source: load
    api_version: "1.22"
  register: load_image_4

- assert:
    that:
    - load_image is changed
    - archive_image['image']['Id'] == load_image['image']['Id']
    - load_image_1 is not changed
    - load_image_2 is failed
    - >-
      "The archive did not contain image 'foo:bar'. Instead, found 'quay.io/ansible/docker-test-containers:hello-world'." == load_image_2.msg
    - load_image_3 is failed
    - '"Detected no loaded images. Archive potentially corrupt?" == load_image_3.msg'
    - load_image_4 is changed
    - "'The API version of your Docker daemon is < 1.23, which does not return the image loading result from the Docker daemon. Therefore, we cannot verify whether the expected image was loaded, whether multiple images where loaded, or whether the load actually succeeded. You should consider upgrading your Docker daemon.' in load_image_4.warnings"

####################################################################
## path ############################################################
####################################################################

- name: Build image
  docker_image:
    name: "{{ iname }}"
    build:
      path: "{{ role_path }}/files"
      pull: no
    source: build
  register: path_1

- name: Build image (idempotency)
  docker_image:
    name: "{{ iname }}"
    build:
      path: "{{ role_path }}/files"
      pull: no
    source: build
  register: path_2

- name: cleanup
  docker_image:
    name: "{{ iname }}"
    state: absent
    force_absent: yes

- assert:
    that:
      - path_1 is changed
      - path_2 is not changed

####################################################################
## target ##########################################################
####################################################################

- name: Build multi-stage image
  docker_image:
    name: "{{ iname }}"
    build:
      path: "{{ role_path }}/files"
      dockerfile: "StagedDockerfile"
      target: first
      pull: no
    source: build
  register: dockerfile_2

- name: cleanup
  docker_image:
    name: "{{ iname }}"
    state: absent
    force_absent: yes

- assert:
    that:
    - dockerfile_2 is changed
    - dockerfile_2.image.Config.WorkingDir == '/first'

####################################################################
## build.etc_hosts #################################################
####################################################################

- name: Build image with custom etc_hosts
  docker_image:
    name: "{{ iname }}"
    build:
      path: "{{ role_path }}/files"
      dockerfile: "EtcHostsDockerfile"
      pull: no
      etc_hosts:
        some-custom-host: "127.0.0.1"
    source: build
  register: path_1

- name: cleanup
  docker_image:
    name: "{{ iname }}"
    state: absent
    force_absent: yes

- assert:
    that:
      - path_1 is changed
