---

- name: Registering service name
  set_fact:
    service_name: "{{ name_prefix ~ '-restart_config' }}"

- name: Registering service name
  set_fact:
    service_names: "{{ service_names + [service_name] }}"

###################################################################
## restart_config.condition #######################################
###################################################################

- name: restart_config.condition
  docker_swarm_service:
    name: "{{ service_name }}"
    image: quay.io/ansible/docker-test-containers:alpine3.8
    resolve_image: no
    command: '/bin/sh -v -c "sleep 10m"'
    restart_config:
      condition: "on-failure"
  register: restart_policy_1

- name: restart_config.condition (idempotency)
  docker_swarm_service:
    name: "{{ service_name }}"
    image: quay.io/ansible/docker-test-containers:alpine3.8
    resolve_image: no
    command: '/bin/sh -v -c "sleep 10m"'
    restart_config:
      condition: "on-failure"
  register: restart_policy_2

- name: restart_policy (idempotency, old name)
  docker_swarm_service:
    name: "{{ service_name }}"
    image: quay.io/ansible/docker-test-containers:alpine3.8
    resolve_image: no
    command: '/bin/sh -v -c "sleep 10m"'
    restart_policy: "on-failure"
  register: restart_policy_2b

- name: restart_config.condition (change)
  docker_swarm_service:
    name: "{{ service_name }}"
    image: quay.io/ansible/docker-test-containers:alpine3.8
    resolve_image: no
    command: '/bin/sh -v -c "sleep 10m"'
    restart_config:
      condition: "any"
  register: restart_policy_3

- name: cleanup
  docker_swarm_service:
    name: "{{ service_name }}"
    state: absent
  diff: no

- assert:
    that:
      - restart_policy_1 is changed
      - restart_policy_2 is not changed
      - restart_policy_2b is not changed
      - restart_policy_3 is changed

###################################################################
## restart_config.max_attempts ####################################
###################################################################

- name: restart_config.max_attempts
  docker_swarm_service:
    name: "{{ service_name }}"
    image: quay.io/ansible/docker-test-containers:alpine3.8
    resolve_image: no
    command: '/bin/sh -v -c "sleep 10m"'
    restart_config:
      max_attempts: 1
  register: restart_policy_attempts_1

- name: restart_config.max_attempts (idempotency)
  docker_swarm_service:
    name: "{{ service_name }}"
    image: quay.io/ansible/docker-test-containers:alpine3.8
    resolve_image: no
    command: '/bin/sh -v -c "sleep 10m"'
    restart_config:
      max_attempts: 1
  register: restart_policy_attempts_2

- name: restart_policy_attempts (idempotency, old name)
  docker_swarm_service:
    name: "{{ service_name }}"
    image: quay.io/ansible/docker-test-containers:alpine3.8
    resolve_image: no
    command: '/bin/sh -v -c "sleep 10m"'
    restart_policy_attempts: 1
  register: restart_policy_attempts_2b

- name: restart_config.max_attempts (change)
  docker_swarm_service:
    name: "{{ service_name }}"
    image: quay.io/ansible/docker-test-containers:alpine3.8
    resolve_image: no
    command: '/bin/sh -v -c "sleep 10m"'
    restart_config:
      max_attempts: 2
  register: restart_policy_attempts_3

- name: cleanup
  docker_swarm_service:
    name: "{{ service_name }}"
    state: absent
  diff: no

- assert:
    that:
      - restart_policy_attempts_1 is changed
      - restart_policy_attempts_2 is not changed
      - restart_policy_attempts_2b is not changed
      - restart_policy_attempts_3 is changed

###################################################################
## restart_config.delay ###########################################
###################################################################

- name: restart_config.delay
  docker_swarm_service:
    name: "{{ service_name }}"
    image: quay.io/ansible/docker-test-containers:alpine3.8
    resolve_image: no
    command: '/bin/sh -v -c "sleep 10m"'
    restart_config:
      delay: 5s
  register: restart_policy_delay_1

- name: restart_config.delay (idempotency)
  docker_swarm_service:
    name: "{{ service_name }}"
    image: quay.io/ansible/docker-test-containers:alpine3.8
    resolve_image: no
    command: '/bin/sh -v -c "sleep 10m"'
    restart_config:
      delay: 5s
  register: restart_policy_delay_2

- name: restart_policy_delay (idempotency, old name)
  docker_swarm_service:
    name: "{{ service_name }}"
    image: quay.io/ansible/docker-test-containers:alpine3.8
    resolve_image: no
    command: '/bin/sh -v -c "sleep 10m"'
    restart_policy_delay: 5000000000
  register: restart_policy_delay_2b

- name: restart_config.delay (change)
  docker_swarm_service:
    name: "{{ service_name }}"
    image: quay.io/ansible/docker-test-containers:alpine3.8
    resolve_image: no
    command: '/bin/sh -v -c "sleep 10m"'
    restart_config:
      delay: 10s
  register: restart_policy_delay_3

- name: cleanup
  docker_swarm_service:
    name: "{{ service_name }}"
    state: absent
  diff: no

- assert:
    that:
      - restart_policy_delay_1 is changed
      - restart_policy_delay_2 is not changed
      - restart_policy_delay_2b is not changed
      - restart_policy_delay_3 is changed

###################################################################
## restart_config.window ##########################################
###################################################################

- name: restart_config.window
  docker_swarm_service:
    name: "{{ service_name }}"
    image: quay.io/ansible/docker-test-containers:alpine3.8
    resolve_image: no
    command: '/bin/sh -v -c "sleep 10m"'
    restart_config:
      window: 10s
  register: restart_policy_window_1

- name: restart_config.window (idempotency)
  docker_swarm_service:
    name: "{{ service_name }}"
    image: quay.io/ansible/docker-test-containers:alpine3.8
    resolve_image: no
    command: '/bin/sh -v -c "sleep 10m"'
    restart_config:
      window: 10s
  register: restart_policy_window_2

- name: restart_policy_window (idempotency, old name)
  docker_swarm_service:
    name: "{{ service_name }}"
    image: quay.io/ansible/docker-test-containers:alpine3.8
    resolve_image: no
    command: '/bin/sh -v -c "sleep 10m"'
    restart_policy_window: 10000000000
  register: restart_policy_window_2b

- name: restart_config.window (change)
  docker_swarm_service:
    name: "{{ service_name }}"
    image: quay.io/ansible/docker-test-containers:alpine3.8
    resolve_image: no
    command: '/bin/sh -v -c "sleep 10m"'
    restart_config:
      window: 20s
  register: restart_policy_window_3

- name: cleanup
  docker_swarm_service:
    name: "{{ service_name }}"
    state: absent
  diff: no

- assert:
    that:
      - restart_policy_window_1 is changed
      - restart_policy_window_2 is not changed
      - restart_policy_window_2b is not changed
      - restart_policy_window_3 is changed
