docker_container: fix paused and add some tests (#47900)

* cleanup is already tested.

* Add test for paused.

* Add recreate and restart tests.

* timeout is a common docker option

* Implement paused and fix paused test.

* Add changelog.

* Improve paused test.

(cherry picked from commit 65768b996d)
This commit is contained in:
Felix Fontein 2018-11-01 09:08:43 +01:00 committed by Toshio Kuratomi
parent ccdf1a61c4
commit 0742f00c29
3 changed files with 177 additions and 16 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- "docker_container - fix ``paused`` option (which never worked)."

View file

@ -1284,6 +1284,12 @@ class Container(DockerBaseClass):
return True
return False
@property
def paused(self):
if self.container and self.container.get('State'):
return self.container['State'].get('Paused', False)
return False
def _compare(self, a, b, compare):
'''
Compare values a and b as described in compare.
@ -1853,6 +1859,20 @@ class ContainerManager(DockerBaseClass):
self.container_stop(container.Id)
container = self._get_container(container.Id)
if state == 'started' and container.paused != self.parameters.paused:
if not self.check_mode:
try:
if self.parameters.paused:
self.client.pause(container=container.Id)
else:
self.client.unpause(container=container.Id)
except Exception as exc:
self.fail("Error %s container %s: %s" % (
"pausing" if self.parameters.paused else "unpausing", container.Id, str(exc)
))
self.results['changed'] = True
self.results['actions'].append(dict(set_paused=self.parameters.paused))
self.facts = container.raw
def absent(self):

View file

@ -168,12 +168,6 @@
- capabilities_3 is not changed
- capabilities_4 is changed
####################################################################
## cleanup #########################################################
####################################################################
# TODO: - cleanup
####################################################################
## command #########################################################
####################################################################
@ -486,7 +480,7 @@
- debug_4 is changed
####################################################################
## detach ##########################################################
## detach, cleanup #################################################
####################################################################
- name: detach without cleanup
@ -2241,7 +2235,69 @@
## paused ##########################################################
####################################################################
# TODO: - paused
- name: paused
docker_container:
image: alpine:3.8
command: "/bin/sh -c 'sleep 1s ; yes'"
name: "{{ cname }}"
state: started
paused: yes
stop_timeout: 1
register: paused_1
- pause:
seconds: 2
- name: paused (logs)
command: docker logs --tail=20 "{{ cname }}"
register: paused_2
- name: paused (idempotent)
docker_container:
image: alpine:3.8
command: "/bin/sh -c 'sleep 1s ; yes'"
name: "{{ cname }}"
state: started
paused: yes
stop_timeout: 1
register: paused_3
- name: paused (continue)
docker_container:
image: alpine:3.8
command: "/bin/sh -c 'sleep 1s ; yes'"
name: "{{ cname }}"
state: started
paused: no
stop_timeout: 1
register: paused_4
- pause:
seconds: 2
- name: paused (stop)
docker_container:
name: "{{ cname }}"
state: stopped
stop_timeout: 1
- name: paused (logs)
command: docker logs --tail=20 "{{ cname }}"
register: paused_5
- name: cleanup
docker_container:
name: "{{ cname }}"
state: absent
stop_timeout: 1
- assert:
that:
- paused_1 is changed
- paused_2.stdout_lines | length == 0
- paused_3 is not changed
- paused_4 is changed
- paused_5.stdout_lines | length > 0
####################################################################
## pid_mode ########################################################
@ -2470,13 +2526,102 @@
## recreate ########################################################
####################################################################
# TODO: - recreate
- name: recreate (created)
docker_container:
image: alpine:3.8
command: '/bin/sh -v -c "sleep 10m"'
name: "{{ cname }}"
state: present
stop_timeout: 1
register: recreate_1
- name: recreate (created, recreate)
docker_container:
image: alpine:3.8
command: '/bin/sh -v -c "sleep 10m"'
name: "{{ cname }}"
recreate: yes
state: present
stop_timeout: 1
register: recreate_2
- name: recreate (started)
docker_container:
image: alpine:3.8
command: '/bin/sh -v -c "sleep 10m"'
name: "{{ cname }}"
state: started
stop_timeout: 1
register: recreate_3
- name: recreate (started, recreate)
docker_container:
image: alpine:3.8
command: '/bin/sh -v -c "sleep 10m"'
name: "{{ cname }}"
recreate: yes
state: started
stop_timeout: 1
register: recreate_4
- name: cleanup
docker_container:
name: "{{ cname }}"
state: absent
stop_timeout: 1
- debug: var=recreate_1
- debug: var=recreate_2
- debug: var=recreate_3
- debug: var=recreate_4
- assert:
that:
- recreate_1 is changed
- recreate_2 is changed
- recreate_3 is changed
- recreate_4 is changed
- recreate_1.ansible_facts.docker_container.Id != recreate_2.ansible_facts.docker_container.Id
- recreate_2.ansible_facts.docker_container.Id == recreate_3.ansible_facts.docker_container.Id
- recreate_3.ansible_facts.docker_container.Id != recreate_4.ansible_facts.docker_container.Id
####################################################################
## restart #########################################################
####################################################################
# TODO: - restart
- name: restart
docker_container:
image: alpine:3.8
command: '/bin/sh -v -c "sleep 10m"'
name: "{{ cname }}"
state: started
stop_timeout: 1
register: restart_1
- name: restart (restart)
docker_container:
image: alpine:3.8
command: '/bin/sh -v -c "sleep 10m"'
name: "{{ cname }}"
restart: yes
state: started
stop_timeout: 1
register: restart_2
- name: cleanup
docker_container:
name: "{{ cname }}"
state: absent
stop_timeout: 1
- debug: var=restart_1
- debug: var=restart_2
- assert:
that:
- restart_1 is changed
- restart_2 is changed
- restart_1.ansible_facts.docker_container.Id == restart_2.ansible_facts.docker_container.Id
####################################################################
## restart_policy ##################################################
@ -2821,12 +2966,6 @@
- "('version is ' ~ docker_py_version ~'. Minimum version required is 1.10.0') in sysctls_1.msg"
when: docker_py_version is version('1.10.0', '<')
####################################################################
## timeout #########################################################
####################################################################
# TODO: - timeout
####################################################################
## tmpfs ###########################################################
####################################################################