---
- 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: cleanup
  docker_image:
    name: "{{ iname }}"
    state: absent
    force_absent: yes

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

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

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

- assert:
    that:
    - buildargs_1 is changed
    - buildargs_2 is not failed and 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', '<')

####################################################################
## build.container_limits ##########################################
####################################################################

- name: container_limits (Failed due to min memory limit)
  docker_image:
    name: "{{ iname }}"
    build:
      path: "{{ remote_tmp_dir }}/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: "{{ remote_tmp_dir }}/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)"

####################################################################
## build.dockerfile ################################################
####################################################################

- name: dockerfile
  docker_image:
    name: "{{ iname }}"
    build:
      path: "{{ remote_tmp_dir }}/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
    - "('FROM ' ~ docker_test_image_alpine) in dockerfile_1.stdout"
    - dockerfile_1['image']['Config']['WorkingDir'] == '/newdata'

####################################################################
## build.platform ##################################################
####################################################################

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

- name: build.platform
  docker_image:
    name: "{{ iname }}"
    build:
      path: "{{ remote_tmp_dir }}/files"
      platform: linux
      pull: no
    source: build
  register: platform_1
  ignore_errors: yes

- name: build.platform (idempotency)
  docker_image:
    name: "{{ iname }}"
    build:
      path: "{{ remote_tmp_dir }}/files"
      platform: linux
      pull: no
    source: build
  register: platform_2
  ignore_errors: yes

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

- assert:
    that:
    - platform_1 is changed
    - platform_2 is not failed and platform_2 is not changed
  when: docker_py_version is version('3.0.0', '>=')

- assert:
    that:
    - platform_1 is failed
    - platform_2 is failed
  when: docker_py_version is version('3.0.0', '<')

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

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

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

- name: force (unchanged)
  docker_image:
    name: "{{ iname }}"
    build:
      path: "{{ remote_tmp_dir }}/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: "{{ docker_test_image_hello_world }}"
    archive_path: "{{ remote_tmp_dir }}/image.tar"
    source: pull
  register: archive_image

- name: Archive image by ID
  docker_image:
    name: "{{ archive_image.image.Id }}"
    archive_path: "{{ remote_tmp_dir }}/image_id.tar"
    source: local
  register: archive_image_id

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

- name: remove image
  docker_image:
    name: "{{ docker_test_image_hello_world }}"
    state: absent
    force_absent: yes

- name: load image (changed)
  docker_image:
    name: "{{ docker_test_image_hello_world }}"
    load_path: "{{ remote_tmp_dir }}/image.tar"
    source: load
  register: load_image

- name: load image (idempotency)
  docker_image:
    name: "{{ docker_test_image_hello_world }}"
    load_path: "{{ remote_tmp_dir }}/image.tar"
    source: load
  register: load_image_1

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

- name: load image (invalid image)
  docker_image:
    name: foo:bar
    load_path: "{{ remote_tmp_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: "{{ remote_tmp_dir }}/image-invalid.tar"
    source: load
    api_version: "1.22"
  register: load_image_4

- name: load image (ID, idempotency)
  docker_image:
    name: "{{ archive_image.image.Id }}"
    load_path: "{{ remote_tmp_dir }}/image_id.tar"
    source: load
  register: load_image_5

- 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 '" ~ docker_test_image_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"
    - load_image_5 is not changed

####################################################################
## build.path ######################################################
####################################################################

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

- name: Build image (idempotency)
  docker_image:
    name: "{{ iname }}"
    build:
      path: "{{ remote_tmp_dir }}/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

####################################################################
## build.target ####################################################
####################################################################

- name: Build multi-stage image
  docker_image:
    name: "{{ iname }}"
    build:
      path: "{{ remote_tmp_dir }}/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: "{{ remote_tmp_dir }}/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
