---
# 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 container name
  set_fact:
    cname: "{{ cname_prefix ~ '-c' }}"
- name: Registering container name
  set_fact:
    cnames: "{{ cnames + [cname] }}"

# Create container

- name: Create container
  docker_container:
    image: "{{ docker_test_image_alpine }}"
    command:
      - /bin/sh
      - "-c"
      - >-
        mkdir /dir;
        ln -s file /lnk;
        ln -s lnk3 /lnk2;
        ln -s lnk2 /lnk1;
        sleep 10m;
    name: "{{ cname }}"
    state: started

################################################################################################
# Do tests

- name: Copy content without mode
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/file'
  register: result
  ignore_errors: true

- name: Check results
  assert:
    that:
      - result is failed
      - |-
        result.msg in [
          "missing parameter(s) required by 'content': mode",
        ]

######################### Copy

- name: Copy content (check mode)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/file'
    mode: 0644
  check_mode: true
  diff: false
  register: result_1

- name: Copy content (check mode, diff)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/file'
    mode: 0644
  check_mode: true
  diff: true
  register: result_1_diff

- name: Copy content (check mode)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/file'
    mode: 0644
  register: result_2

- name: Copy content (idempotent, check mode)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/file'
    mode: 0644
  check_mode: true
  diff: false
  register: result_3

- name: Copy content (idempotent, check mode, diff)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/file'
    mode: 0644
  check_mode: true
  diff: true
  register: result_3_diff

- name: Copy content (idempotent)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/file'
    mode: 0644
  register: result_4

- name: Copy content (idempotent, check mode, base 64)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: "{{ 'Content 1\n' | b64encode }}"
    content_is_b64: true
    container_path: '/file'
    mode: 0644
  check_mode: true
  diff: false
  register: result_3b64

- name: Copy content (idempotent, check mode, base 64, diff)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: "{{ 'Content 1\n' | b64encode }}"
    content_is_b64: true
    container_path: '/file'
    mode: 0644
  check_mode: true
  diff: true
  register: result_3b64_diff

- name: Copy content (idempotent, base 64)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: "{{ 'Content 1\n' | b64encode }}"
    content_is_b64: true
    container_path: '/file'
    mode: 0644
  register: result_4b64

- name: Dump file
  docker_container_exec:
    container: '{{ cname }}'
    argv:
      - /bin/sh
      - "-c"
      - >-
        cat /file | base64;
        stat -c '%s %a %F %u %g %N' /file > /dev/stderr
    chdir: /root
  register: result_5

- name: Copy content (force, check mode)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/file'
    mode: 0644
    force: true
  check_mode: true
  diff: false
  register: result_6

- name: Copy content (force, check mode, diff)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/file'
    mode: 0644
    force: true
  check_mode: true
  diff: true
  register: result_6_diff

- name: Copy content (force)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/file'
    mode: 0644
    force: true
  register: result_7

- name: Dump file
  docker_container_exec:
    container: '{{ cname }}'
    argv:
      - /bin/sh
      - "-c"
      - >-
        cat /file | base64;
        stat -c '%s %a %F %u %g %N' /file > /dev/stderr
    chdir: /root
  register: result_8

- name: Copy content (force=false, check mode)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Some other content
    container_path: '/file'
    mode: 0777
    owner_id: 123
    group_id: 321
    force: false
  check_mode: true
  diff: false
  register: result_9

- name: Copy content (force=false, check mode, diff)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Some other content
    container_path: '/file'
    mode: 0777
    owner_id: 123
    group_id: 321
    force: false
  check_mode: true
  diff: true
  register: result_9_diff

- name: Copy content (force=false)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Some other content
    container_path: '/file'
    mode: 0777
    owner_id: 123
    group_id: 321
    force: false
  register: result_10

- name: Dump file
  docker_container_exec:
    container: '{{ cname }}'
    argv:
      - /bin/sh
      - "-c"
      - >-
        cat /file | base64;
        stat -c '%s %a %F %u %g %N' /file > /dev/stderr
    chdir: /root
  register: result_11

- name: Check results
  assert:
    that:
      - result_1 is changed
      - "'diff' not in result_1"
      - result_1_diff.diff.before == ''
      - result_1_diff.diff.before_header == '/file'
      - result_1_diff.diff.after == 'Content 1\n'
      - result_1_diff.diff.after_header == 'dynamically generated'
      - result_1 == (result_1_diff | dict2items | rejectattr('key', 'eq', 'diff') | items2dict)
      - result_2 is changed
      - result_3 is not changed
      - "'diff' not in result_3"
      - result_3_diff.diff.before == 'Content 1\n'
      - result_3_diff.diff.before_header == '/file'
      - result_3_diff.diff.after == 'Content 1\n'
      - result_3_diff.diff.after_header == 'dynamically generated'
      - result_3 == (result_3_diff | dict2items | rejectattr('key', 'eq', 'diff') | items2dict)
      - result_4 is not changed
      - result_3b64 is not changed
      - "'diff' not in result_3b64"
      - result_3b64_diff.diff.before == 'Content 1\n'
      - result_3b64_diff.diff.before_header == '/file'
      - result_3b64_diff.diff.after == 'Content 1\n'
      - result_3b64_diff.diff.after_header == 'dynamically generated'
      - result_3b64 == (result_3b64_diff | dict2items | rejectattr('key', 'eq', 'diff') | items2dict)
      - result_4b64 is not changed
      - result_5.stdout | b64decode == 'Content 1\n'
      - result_5.stderr == '10 644 regular file 0 0 /file'
      - result_6 is changed
      - "'diff' not in result_6"
      - result_6_diff.diff.before == 'Content 1\n'
      - result_6_diff.diff.before_header == '/file'
      - result_6_diff.diff.after == 'Content 1\n'
      - result_6_diff.diff.after_header == 'dynamically generated'
      - result_6 == (result_6_diff | dict2items | rejectattr('key', 'eq', 'diff') | items2dict)
      - result_7 is changed
      - result_8.stdout | b64decode == 'Content 1\n'
      - result_8.stderr == '10 644 regular file 0 0 /file'
      - result_9 is not changed
      - "'diff' not in result_9"
      - result_9_diff.diff.before == 'Content 1\n'
      - result_9_diff.diff.before_header == '/file'
      - result_9_diff.diff.after == 'Content 1\n'  # note that force=false
      - result_9_diff.diff.after_header == '/file'  # note that force=false
      - result_9 == (result_9_diff | dict2items | rejectattr('key', 'eq', 'diff') | items2dict)
      - result_10 is not changed
      - result_11.stdout | b64decode == 'Content 1\n'
      - result_11.stderr == '10 644 regular file 0 0 /file'

######################### Follow link - idempotence

- name: Dump file
  docker_container_exec:
    container: '{{ cname }}'
    argv:
      - /bin/sh
      - "-c"
      - >-
        cat /lnk | base64;
        stat -c '%s %a %F %u %g %N' /lnk > /dev/stderr;
    chdir: /root
  register: result_0

- name: Copy content following link (check mode)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/lnk'
    mode: 0644
    follow: true
  check_mode: true
  diff: false
  register: result_1

- name: Copy content following link (check mode, diff)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/lnk'
    mode: 0644
    follow: true
  check_mode: true
  diff: true
  register: result_1_diff

- name: Copy content following link
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/lnk'
    mode: 0644
    follow: true
  register: result_2

- name: Dump file
  docker_container_exec:
    container: '{{ cname }}'
    argv:
      - /bin/sh
      - "-c"
      - >-
        cat /lnk | base64;
        stat -c '%s %a %F %u %g %N' /lnk > /dev/stderr;
        stat -c '%s %a %F %u %g %N' /file > /dev/stderr
    chdir: /root
  register: result_3

- name: Copy content following link (force, check mode)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/lnk'
    mode: 0644
    follow: true
    force: true
  check_mode: true
  diff: false
  register: result_4

- name: Copy content following link (force, check mode, diff)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/lnk'
    mode: 0644
    follow: true
    force: true
  check_mode: true
  diff: true
  register: result_4_diff

- name: Copy content following link (force)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/lnk'
    mode: 0644
    follow: true
    force: true
  register: result_5

- name: Dump file
  docker_container_exec:
    container: '{{ cname }}'
    argv:
      - /bin/sh
      - "-c"
      - >-
        cat /lnk | base64;
        stat -c '%s %a %F %u %g %N' /lnk > /dev/stderr;
        stat -c '%s %a %F %u %g %N' /file > /dev/stderr
    chdir: /root
  register: result_6

- name: Check results
  assert:
    that:
      - result_0.stdout | b64decode == 'Content 1\n'
      - result_0.stderr == "4 777 symbolic link 0 0 '/lnk' -> 'file'"
      - result_1 is not changed
      - result_1.container_path == '/file'
      - "'diff' not in result_1"
      - result_1_diff.diff.before == 'Content 1\n'
      - result_1_diff.diff.before_header == '/file'
      - result_1_diff.diff.after == 'Content 1\n'
      - result_1_diff.diff.after_header == 'dynamically generated'
      - result_1 == (result_1_diff | dict2items | rejectattr('key', 'eq', 'diff') | items2dict)
      - result_2 is not changed
      - result_2.container_path == '/file'
      - result_3.stdout | b64decode == 'Content 1\n'
      - result_3.stderr_lines[0] == "4 777 symbolic link 0 0 '/lnk' -> 'file'"
      - result_3.stderr_lines[1] == '10 644 regular file 0 0 /file'
      - result_4 is changed
      - result_4.container_path == '/file'
      - "'diff' not in result_4"
      - result_4_diff.diff.before == 'Content 1\n'
      - result_4_diff.diff.before_header == '/file'
      - result_4_diff.diff.after == 'Content 1\n'
      - result_4_diff.diff.after_header == 'dynamically generated'
      - result_4 == (result_4_diff | dict2items | rejectattr('key', 'eq', 'diff') | items2dict)
      - result_5 is changed
      - result_5.container_path == '/file'
      - result_6.stdout | b64decode == 'Content 1\n'
      - result_6.stderr_lines[0] == "4 777 symbolic link 0 0 '/lnk' -> 'file'"
      - result_6.stderr_lines[1] == '10 644 regular file 0 0 /file'

######################### Do not follow link - replace by file

- name: Copy content not following link (check mode)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/lnk'
    mode: 0644
    follow: false
  check_mode: true
  diff: false
  register: result_1

- name: Copy content not following link (check mode, diff)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/lnk'
    mode: 0644
    follow: false
  check_mode: true
  diff: true
  register: result_1_diff

- name: Copy content not following link
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/lnk'
    mode: 0644
    follow: false
  register: result_2

- name: Copy content not following link (idempotent, check mode)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/lnk'
    mode: 0644
  check_mode: true
  diff: false
  register: result_3

- name: Copy content not following link (idempotent, check mode, diff)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/lnk'
    mode: 0644
  check_mode: true
  diff: true
  register: result_3_diff

- name: Copy content not following link (idempotent)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/lnk'
    mode: 0644
  register: result_4

- name: Dump file
  docker_container_exec:
    container: '{{ cname }}'
    argv:
      - /bin/sh
      - "-c"
      - >-
        cat /lnk | base64;
        stat -c '%s %a %F %u %g %N' /lnk > /dev/stderr
    chdir: /root
  register: result_5

- name: Copy content not following link (force, check mode)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/lnk'
    mode: 0644
    force: true
  check_mode: true
  diff: false
  register: result_6

- name: Copy content not following link (force, check mode, diff)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/lnk'
    mode: 0644
    force: true
  check_mode: true
  diff: true
  register: result_6_diff

- name: Copy content not following link (force)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/lnk'
    mode: 0644
    force: true
  register: result_7

- name: Dump file
  docker_container_exec:
    container: '{{ cname }}'
    argv:
      - /bin/sh
      - "-c"
      - >-
        cat /lnk | base64;
        stat -c '%s %a %F %u %g %N' /lnk > /dev/stderr
    chdir: /root
  register: result_8

- name: Check results
  assert:
    that:
      - result_1 is changed
      - result_1.container_path == '/lnk'
      - "'diff' not in result_1"
      - result_1_diff.diff.before == '/file'
      - result_1_diff.diff.before_header == '/lnk'
      - result_1_diff.diff.after == 'Content 1\n'
      - result_1_diff.diff.after_header == 'dynamically generated'
      - result_1 == (result_1_diff | dict2items | rejectattr('key', 'eq', 'diff') | items2dict)
      - result_2 is changed
      - result_2.container_path == '/lnk'
      - result_3 is not changed
      - "'diff' not in result_3"
      - result_3_diff.diff.before == 'Content 1\n'
      - result_3_diff.diff.before_header == '/lnk'
      - result_3_diff.diff.after == 'Content 1\n'
      - result_3_diff.diff.after_header == 'dynamically generated'
      - result_3 == (result_3_diff | dict2items | rejectattr('key', 'eq', 'diff') | items2dict)
      - result_4 is not changed
      - result_5.stdout | b64decode == 'Content 1\n'
      - result_5.stderr == '10 644 regular file 0 0 /lnk'
      - result_6 is changed
      - result_6.container_path == '/lnk'
      - "'diff' not in result_6"
      - result_6_diff.diff.before == 'Content 1\n'
      - result_6_diff.diff.before_header == '/lnk'
      - result_6_diff.diff.after == 'Content 1\n'
      - result_6_diff.diff.after_header == 'dynamically generated'
      - result_6 == (result_6_diff | dict2items | rejectattr('key', 'eq', 'diff') | items2dict)
      - result_7 is changed
      - result_7.container_path == '/lnk'
      - result_8.stdout | b64decode == 'Content 1\n'
      - result_8.stderr == '10 644 regular file 0 0 /lnk'

######################### Replace directory by file

- name: Copy content to replace directory (check mode)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/dir'
    mode: 0644
    follow: false
  check_mode: true
  diff: false
  register: result_1

- name: Copy content to replace directory (check mode, diff)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/dir'
    mode: 0644
    follow: false
  check_mode: true
  diff: true
  register: result_1_diff

- name: Copy content to replace directory (check mode)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/dir'
    mode: 0644
    follow: false
  register: result_2

- name: Copy content to replace directory (idempotent, check mode)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/dir'
    mode: 0644
  check_mode: true
  diff: false
  register: result_3

- name: Copy content to replace directory (idempotent, check mode, diff)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/dir'
    mode: 0644
  check_mode: true
  diff: true
  register: result_3_diff

- name: Copy content to replace directory (idempotent)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/dir'
    mode: 0644
  register: result_4

- name: Dump file
  docker_container_exec:
    container: '{{ cname }}'
    argv:
      - /bin/sh
      - "-c"
      - >-
        cat /dir | base64;
        stat -c '%s %a %F %u %g %N' /dir > /dev/stderr
    chdir: /root
  register: result_5

- name: Check results
  assert:
    that:
      - result_1 is changed
      - result_1.container_path == '/dir'
      - "'diff' not in result_1"
      - result_1_diff.diff.before == '(directory)'
      - result_1_diff.diff.before_header == '/dir'
      - result_1_diff.diff.after == 'Content 1\n'
      - result_1_diff.diff.after_header == 'dynamically generated'
      - result_1 == (result_1_diff | dict2items | rejectattr('key', 'eq', 'diff') | items2dict)
      - result_2 is changed
      - result_2.container_path == '/dir'
      - result_3 is not changed
      - "'diff' not in result_3"
      - result_3_diff.diff.before == 'Content 1\n'
      - result_3_diff.diff.before_header == '/dir'
      - result_3_diff.diff.after == 'Content 1\n'
      - result_3_diff.diff.after_header == 'dynamically generated'
      - result_3 == (result_3_diff | dict2items | rejectattr('key', 'eq', 'diff') | items2dict)
      - result_4 is not changed
      - result_5.stdout | b64decode == 'Content 1\n'
      - result_5.stderr == '10 644 regular file 0 0 /dir'

######################### Modify

- name: Copy content (changed, check mode)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |-
      Content 2
      Extra line
    container_path: '/file'
    mode: 0644
  check_mode: true
  diff: false
  register: result_1

- name: Copy content (changed, check mode, diff)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |-
      Content 2
      Extra line
    container_path: '/file'
    mode: 0644
  check_mode: true
  diff: true
  register: result_1_diff

- name: Copy content (changed)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |-
      Content 2
      Extra line
    container_path: '/file'
    mode: 0644
  register: result_2

- name: Dump file
  docker_container_exec:
    container: '{{ cname }}'
    argv:
      - /bin/sh
      - "-c"
      - >-
        cat /file | base64;
        stat -c '%s %a %F %u %g %N' /file > /dev/stderr
    chdir: /root
  register: result_3

- name: Check results
  assert:
    that:
      - result_1 is changed
      - "'diff' not in result_1"
      - result_1_diff.diff.before == 'Content 1\n'
      - result_1_diff.diff.before_header == '/file'
      - result_1_diff.diff.after == 'Content 2\nExtra line'
      - result_1_diff.diff.after_header == 'dynamically generated'
      - result_1 == (result_1_diff | dict2items | rejectattr('key', 'eq', 'diff') | items2dict)
      - result_2 is changed
      - result_3.stdout | b64decode == 'Content 2\nExtra line'
      - result_3.stderr == '20 644 regular file 0 0 /file'

######################### Change mode

- name: Copy content (mode changed, check mode)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |-
      Content 2
      Extra line
    container_path: '/file'
    mode: 0707
  check_mode: true
  diff: false
  register: result_1

- name: Copy content (mode changed, check mode, diff)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |-
      Content 2
      Extra line
    container_path: '/file'
    mode: 0707
  check_mode: true
  diff: true
  register: result_1_diff

- name: Copy content (mode changed)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |-
      Content 2
      Extra line
    container_path: '/file'
    mode: 0707
  register: result_2

- name: Copy content (idempotent, check mode)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |-
      Content 2
      Extra line
    container_path: '/file'
    mode: 0707
  check_mode: true
  diff: false
  register: result_3

- name: Copy content (idempotent, check mode, diff)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |-
      Content 2
      Extra line
    container_path: '/file'
    mode: 0707
  check_mode: true
  diff: true
  register: result_3_diff

- name: Copy content (idempotent)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |-
      Content 2
      Extra line
    container_path: '/file'
    mode: 0707
  register: result_4

- name: Dump file
  docker_container_exec:
    container: '{{ cname }}'
    argv:
      - /bin/sh
      - "-c"
      - >-
        cat /file | base64;
        stat -c '%s %a %F %u %g %N' /file > /dev/stderr
    chdir: /root
  register: result_5

- name: Check results
  assert:
    that:
      - result_1 is changed
      - "'diff' not in result_1"
      - result_1_diff.diff.before == 'Content 2\nExtra line'
      - result_1_diff.diff.before_header == '/file'
      - result_1_diff.diff.after == 'Content 2\nExtra line'
      - result_1_diff.diff.after_header == 'dynamically generated'
      - result_1 == (result_1_diff | dict2items | rejectattr('key', 'eq', 'diff') | items2dict)
      - result_2 is changed
      - result_3 is not changed
      - "'diff' not in result_3"
      - result_3_diff.diff.before == 'Content 2\nExtra line'
      - result_3_diff.diff.before_header == '/file'
      - result_3_diff.diff.after == 'Content 2\nExtra line'
      - result_3_diff.diff.after_header == 'dynamically generated'
      - result_3 == (result_3_diff | dict2items | rejectattr('key', 'eq', 'diff') | items2dict)
      - result_4 is not changed
      - result_5.stdout | b64decode == 'Content 2\nExtra line'
      - result_5.stderr == '20 707 regular file 0 0 /file'

######################### Change owner and group

- name: Copy content (owner/group changed, check mode)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |-
      Content 2
      Extra line
    container_path: '/file'
    mode: 0707
    owner_id: 12
    group_id: 910
  check_mode: true
  diff: false
  register: result_1

- name: Copy content (owner/group changed, check mode, diff)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |-
      Content 2
      Extra line
    container_path: '/file'
    mode: 0707
    owner_id: 12
    group_id: 910
  check_mode: true
  diff: true
  register: result_1_diff

- name: Copy content (owner/group changed)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |-
      Content 2
      Extra line
    container_path: '/file'
    mode: 0707
    owner_id: 12
    group_id: 910
  register: result_2

- name: Copy content (idempotent, check mode)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |-
      Content 2
      Extra line
    container_path: '/file'
    mode: 0707
    owner_id: 12
    group_id: 910
  check_mode: true
  diff: false
  register: result_3

- name: Copy content (idempotent, check mode, diff)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |-
      Content 2
      Extra line
    container_path: '/file'
    mode: 0707
    owner_id: 12
    group_id: 910
  check_mode: true
  diff: true
  register: result_3_diff

- name: Copy content (idempotent)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |-
      Content 2
      Extra line
    container_path: '/file'
    mode: 0707
    owner_id: 12
    group_id: 910
  register: result_4

- name: Dump file
  docker_container_exec:
    container: '{{ cname }}'
    argv:
      - /bin/sh
      - "-c"
      - >-
        cat /file | base64;
        stat -c '%s %a %F %u %g %N' /file > /dev/stderr
    chdir: /root
  register: result_5

- name: Copy content (owner/group changed again, check mode)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |-
      Content 2
      Extra line
    container_path: '/file'
    mode: 0707
    owner_id: 13
    group_id: 13
  check_mode: true
  diff: false
  register: result_6

- name: Copy content (owner/group changed again, check mode, diff)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |-
      Content 2
      Extra line
    container_path: '/file'
    mode: 0707
    owner_id: 13
    group_id: 13
  check_mode: true
  diff: true
  register: result_6_diff

- name: Copy content (owner/group changed again)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |-
      Content 2
      Extra line
    container_path: '/file'
    mode: 0707
    owner_id: 13
    group_id: 13
  register: result_7

- name: Dump file
  docker_container_exec:
    container: '{{ cname }}'
    argv:
      - /bin/sh
      - "-c"
      - >-
        cat /file | base64;
        stat -c '%s %a %F %u %g %N' /file > /dev/stderr
    chdir: /root
  register: result_8

- name: Check results
  assert:
    that:
      - result_1 is changed
      - "'diff' not in result_1"
      - result_1_diff.diff.before == 'Content 2\nExtra line'
      - result_1_diff.diff.before_header == '/file'
      - result_1_diff.diff.after == 'Content 2\nExtra line'
      - result_1_diff.diff.after_header == 'dynamically generated'
      - result_1 == (result_1_diff | dict2items | rejectattr('key', 'eq', 'diff') | items2dict)
      - result_2 is changed
      - result_3 is not changed
      - "'diff' not in result_3"
      - result_3_diff.diff.before == 'Content 2\nExtra line'
      - result_3_diff.diff.before_header == '/file'
      - result_3_diff.diff.after == 'Content 2\nExtra line'
      - result_3_diff.diff.after_header == 'dynamically generated'
      - result_3 == (result_3_diff | dict2items | rejectattr('key', 'eq', 'diff') | items2dict)
      - result_4 is not changed
      - result_5.stdout | b64decode == 'Content 2\nExtra line'
      - result_5.stderr == '20 707 regular file 12 910 /file'
      - result_6 is changed
      - "'diff' not in result_6"
      - result_6_diff.diff.before == 'Content 2\nExtra line'
      - result_6_diff.diff.before_header == '/file'
      - result_6_diff.diff.after == 'Content 2\nExtra line'
      - result_6_diff.diff.after_header == 'dynamically generated'
      - result_6 == (result_6_diff | dict2items | rejectattr('key', 'eq', 'diff') | items2dict)
      - result_7 is changed
      - result_8.stdout | b64decode == 'Content 2\nExtra line'
      - result_8.stderr == '20 707 regular file 13 13 /file'

######################### Operate with stopped container

- name: Stop container
  docker_container:
    name: "{{ cname }}"
    state: stopped
    stop_timeout: 1

- name: Copy content (stopped container, check mode)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/file'
    mode: 0707
    owner_id: 12
    group_id: 910
  check_mode: true
  diff: false
  register: result_1

- name: Copy content (stopped container, check mode, diff)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/file'
    mode: 0707
    owner_id: 12
    group_id: 910
  check_mode: true
  diff: true
  register: result_1_diff

- name: Copy content (stopped container)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/file'
    mode: 0707
    owner_id: 12
    group_id: 910
  register: result_2

- name: Copy content (stopped container, idempotent, check mode)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/file'
    mode: 0707
    owner_id: 12
    group_id: 910
  check_mode: true
  diff: false
  register: result_3

- name: Copy content (stopped container, idempotent, check mode, diff)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/file'
    mode: 0707
    owner_id: 12
    group_id: 910
  check_mode: true
  diff: true
  register: result_3_diff

- name: Copy content (stopped container, idempotent)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/file'
    mode: 0707
    owner_id: 12
    group_id: 910
  register: result_4

- name: Copy content (stopped container, no owner/group provided, should fail)
  docker_container_copy_into:
    container: '{{ cname }}'
    content: |
      Content 1
    container_path: '/file'
    mode: 0707
  register: result_5
  ignore_errors: true

- name: Start container
  docker_container:
    name: "{{ cname }}"
    state: started

- name: Dump file
  docker_container_exec:
    container: '{{ cname }}'
    argv:
      - /bin/sh
      - "-c"
      - >-
        cat /file | base64;
        stat -c '%s %a %F %u %g %N' /file > /dev/stderr
    chdir: /root
  register: result_6

- name: Check results
  assert:
    that:
      - result_1 is changed
      - "'diff' not in result_1"
      - result_1_diff.diff.before == 'Content 2\nExtra line'
      - result_1_diff.diff.before_header == '/file'
      - result_1_diff.diff.after == 'Content 1\n'
      - result_1_diff.diff.after_header == 'dynamically generated'
      - result_1 == (result_1_diff | dict2items | rejectattr('key', 'eq', 'diff') | items2dict)
      - result_2 is changed
      - result_3 is not changed
      - "'diff' not in result_3"
      - result_3_diff.diff.before == 'Content 1\n'
      - result_3_diff.diff.before_header == '/file'
      - result_3_diff.diff.after == 'Content 1\n'
      - result_3_diff.diff.after_header == 'dynamically generated'
      - result_3 == (result_3_diff | dict2items | rejectattr('key', 'eq', 'diff') | items2dict)
      - result_4 is not changed
      - result_5 is failed
      - result_5.msg == ('Cannot execute command in paused container "' ~ cname ~ '"')
      - result_6.stdout | b64decode == 'Content 1\n'
      - result_6.stderr == '10 707 regular file 12 910 /file'

################################################################################################
# Cleanup

- name: Remove container
  docker_container:
    name: "{{ cname }}"
    state: absent
    force_kill: true
