---
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

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

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

####################################################################
## args ############################################################
####################################################################

- name: cleanup
  docker_image_remove:
    name: "{{ iname }}"

- name: buildargs
  docker_image_build:
    name: "{{ iname }}"
    path: "{{ remote_tmp_dir }}/files"
    dockerfile: "ArgsDockerfile"
    args:
      IMAGE: "{{ docker_test_image_busybox }}"
      TEST1: val1
      TEST2: val2
      TEST3: "True"
    pull: false
  register: buildargs_1

- name: buildargs (idempotency)
  docker_image_build:
    name: "{{ iname }}"
    path: "{{ remote_tmp_dir }}/files"
    dockerfile: "ArgsDockerfile"
    args:
      IMAGE: "{{ docker_test_image_busybox }}"
      TEST1: val1
      TEST2: val2
      TEST3: "True"
    pull: false
  register: buildargs_2

- name: cleanup
  docker_image_remove:
    name: "{{ iname }}"

- assert:
    that:
    - buildargs_1 is changed
    - buildargs_2 is not changed

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

- name: dockerfile
  docker_image_build:
    name: "{{ iname }}"
    path: "{{ remote_tmp_dir }}/files"
    dockerfile: "MyDockerfile"
    pull: false
  register: dockerfile_1

- name: cleanup
  docker_image_remove:
    name: "{{ iname }}"

- assert:
    that:
    - dockerfile_1 is changed
    - "('FROM ' ~ docker_test_image_alpine) in dockerfile_1.stderr"
    - dockerfile_1['image']['Config']['WorkingDir'] == '/newdata'

####################################################################
## platform ########################################################
####################################################################

- name: cleanup
  docker_image_remove:
    name: "{{ iname }}"

- name: platform
  docker_image_build:
    name: "{{ iname }}"
    path: "{{ remote_tmp_dir }}/files"
    platform: linux
    pull: false
  register: platform_1

- name: platform (idempotency)
  docker_image_build:
    name: "{{ iname }}"
    path: "{{ remote_tmp_dir }}/files"
    platform: linux
    pull: false
  register: platform_2

- name: cleanup
  docker_image_remove:
    name: "{{ iname }}"

- assert:
    that:
    - platform_1 is changed
    - platform_2 is not changed

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

- name: Build multi-stage image
  docker_image_build:
    name: "{{ iname }}"
    path: "{{ remote_tmp_dir }}/files"
    dockerfile: "StagedDockerfile"
    target: first
    pull: false
  register: dockerfile_2

- name: cleanup
  docker_image_remove:
    name: "{{ iname }}"

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

####################################################################
## etc_hosts #######################################################
####################################################################

- name: Build image with custom etc_hosts
  docker_image_build:
    name: "{{ iname }}"
    path: "{{ remote_tmp_dir }}/files"
    dockerfile: "EtcHostsDockerfile"
    pull: false
    etc_hosts:
      some-custom-host: "127.0.0.1"
  register: path_1

- name: cleanup
  docker_image_remove:
    name: "{{ iname }}"

- assert:
    that:
      - path_1 is changed

####################################################################
## shm_size ########################################################
####################################################################

- name: Build image with custom shm_size
  docker_image_build:
    name: "{{ iname }}"
    path: "{{ remote_tmp_dir }}/files"
    dockerfile: "MyDockerfile"
    pull: false
    shm_size: 128MB
  register: path_1

- name: cleanup
  docker_image_remove:
    name: "{{ iname }}"

- assert:
    that:
      - path_1 is changed

####################################################################
## labels ##########################################################
####################################################################

- name: Build image with labels
  docker_image_build:
    name: "{{ iname }}"
    path: "{{ remote_tmp_dir }}/files"
    dockerfile: "MyDockerfile"
    pull: false
    labels:
      FOO: BAR
      this is a label: this is the label's value
  register: labels_1

- name: cleanup
  docker_image_remove:
    name: "{{ iname }}"

- name: Show image information
  debug:
    var: labels_1.image

- assert:
    that:
      - labels_1 is changed
      - labels_1.image.Config.Labels.FOO == 'BAR'
      - labels_1.image.Config.Labels["this is a label"] == "this is the label's value"

####################################################################
## secrets #########################################################
####################################################################

- name: Generate secret
  set_fact:
    docker_image_build_secret_value: this is my secret {{ '%0x' % ((2**32) | random) }}

- when: buildx_version is version('0.6.0', '>=')
  block:
    - name: Build image with secrets via environment variables
      docker_image_build:
        name: "{{ iname }}"
        path: "{{ remote_tmp_dir }}/files"
        dockerfile: "SecretsDockerfile"
        pull: false
        secrets:
          - id: my-awesome-secret
            type: value
            value: '{{ docker_image_build_secret_value }}'
        nocache: true  # using a cache can result in the output step being CACHED
      register: secrets_1

    - name: cleanup
      docker_image_remove:
        name: "{{ iname }}"

    - name: Show image information
      debug:
        var: secrets_1.stderr_lines

    - assert:
        that:
          - secrets_1 is changed
          - (docker_image_build_secret_value | b64encode) in secrets_1.stderr

####################################################################
## outputs #########################################################
####################################################################

- when: buildx_version is version('0.13.0', '>=')
  block:
    - name: Make sure the image is not there
      docker_image_remove:
        name: "{{ iname }}"

    - name: Make sure the image tarball is not there
      file:
        path: "{{ remote_tmp_dir }}/container.tar"
        state: absent

    - name: Build image with outputs
      docker_image_build:
        name: "{{ iname }}"
        path: "{{ remote_tmp_dir }}/files"
        dockerfile: "Dockerfile"
        pull: false
        outputs:
          - type: tar
            dest: "{{ remote_tmp_dir }}/container.tar"
      ignore_errors: true
      register: outputs_1

    - when: outputs_1 is not failed
      block:
        - name: cleanup (should be changed)
          docker_image_remove:
            name: "{{ iname }}"
          register: outputs_1_cleanup

        - name: Gather information on tarball
          stat:
            path: "{{ remote_tmp_dir }}/container.tar"
          register: outputs_1_stat

        - name: Show image information
          debug:
            var: outputs_1.image

        - name: Show tarball information
          debug:
            var: outputs_1_stat.stat

        - assert:
            that:
              - outputs_1 is changed
              - outputs_1.image | length > 0
              - outputs_1_cleanup is changed
              - outputs_1_stat.stat.exists

    - when: outputs_1 is failed
      assert:
        that:
          - >-
            'ERROR: multiple outputs currently unsupported by the current BuildKit daemon' in outputs_1.stderr
