---
- hosts: k8s-cluster

  vars:
    test_image_repo: k8s.gcr.io/echoserver
    test_image_tag: 1.4

  tasks:

  - set_fact:
      bin_dir: "/usr/bin"

  - name: Read the docker dns conf
    shell: "cat /etc/systemd/system/docker.service.d/docker-dns.conf"
    register: docker_dns_result

  - name: Check that the localhost search domain does not exist in docker dns conf
    assert:
      that: docker_dns_result.stdout.find('localhost') == -1
      msg: "localhost search domain must not exist in docker dns conf"

  - name: Start test docker container with docker run command
    shell: "{{ bin_dir }}/docker run -d {{ test_image_repo }}:{{ test_image_tag }}"

  - name: Stop docker daemon
    service:
      name: docker
      state: stopped

  - name: Make sure docker has stopped completely
    command: "{{ bin_dir }}/docker images"
    register: docker_stop
    retries: 10
    delay: 5
    until: docker_stop.rc != 0
    ignore_errors: true

  - name: Get the test container process
    shell: "ps -ef|grep 'nginx: master process nginx -g daemon off'|grep -v grep|wc -l"
    register: process_num_result

  - name: Check that the test container's process still exists
    assert:
      that: process_num_result.stdout|int != 0
      msg: "The test container process must exist when the docker daemon is stopped"

  - name: Recover docker daemon
    service:
      name: docker
      state: started
