---

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

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

####################################################################
## limits.cpus #####################################################
####################################################################

- name: limits.cpus
  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"'
    limits:
      cpus: 1
  register: limit_cpu_1

- name: limits.cpus (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"'
    limits:
      cpus: 1
  register: limit_cpu_2

- name: limit_cpu (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"'
    limit_cpu: 1
  register: limit_cpu_2b

- name: limits.cpus (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"'
    limits:
      cpus: 0.5
  register: limit_cpu_3

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

- assert:
    that:
      - limit_cpu_1 is changed
      - limit_cpu_2 is not changed
      - limit_cpu_2b is not changed
      - limit_cpu_3 is changed

###################################################################
## limits.memory ##################################################
###################################################################

- name: limits.memory
  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"'
    limits:
      memory: 64M
  register: limit_memory_1

- name: limits.memory (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"'
    limits:
      memory: 64M
  register: limit_memory_2

- name: limit_memory (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"'
    limit_memory: "67108864"
  register: limit_memory_2b

- name: limits.memory (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"'
    limit_memory: 32M
  register: limit_memory_3

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

- assert:
    that:
      - limit_memory_1 is changed
      - limit_memory_2 is not changed
      - limit_memory_2b is not changed
      - limit_memory_3 is changed

###################################################################
## reservations.cpus ##############################################
###################################################################

- name: reserve_cpu
  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"'
    reservations:
      cpus: 1
  register: reserve_cpu_1

- name: reserve_cpu (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"'
    reservations:
      cpus: 1
  register: reserve_cpu_2

- name: reserve_cpu (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"'
    reserve_cpu: 1
  register: reserve_cpu_2b

- name: reserve_cpu (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"'
    reservations:
      cpus: 0.5
  register: reserve_cpu_3

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

- assert:
    that:
      - reserve_cpu_1 is changed
      - reserve_cpu_2 is not changed
      - reserve_cpu_2b is not changed
      - reserve_cpu_3 is changed

###################################################################
## reservations.memory ############################################
###################################################################

- name: reservations.memory
  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"'
    reservations:
      memory: 64M
  register: reserve_memory_1

- name: reservations.memory (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"'
    reserve_memory: 64M
  register: reserve_memory_2

- name: reserve_memory (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"'
    reserve_memory: "67108864"
  register: reserve_memory_2b

- name: reservations.memory (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"'
    reserve_memory: 32M
  register: reserve_memory_3

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

- assert:
    that:
      - reserve_memory_1 is changed
      - reserve_memory_2 is not changed
      - reserve_memory_2b is not changed
      - reserve_memory_3 is changed
