docker_container: show warnings, fix/improve tests (#53440)

* Output warnings from docker daemon on container create and update.

* Accept warning for blkio_weight instead of idempotency.

* Value quoting.

* Avoid loop variable conflict.

* Add changelog.

* Make one test case faster.

* Add 'Docker warning: ' prefix.

* Add a generalized warning reporting function.

(cherry picked from commit 3117900b1e)
This commit is contained in:
Felix Fontein 2019-03-11 10:04:06 +01:00 committed by Toshio Kuratomi
parent 5769d46aa3
commit ca63a2a968
5 changed files with 60 additions and 40 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- "docker_container - now returns warnings from docker daemon on container creation and updating."

View file

@ -24,6 +24,7 @@ import copy
from distutils.version import LooseVersion
from ansible.module_utils.basic import AnsibleModule, env_fallback
from ansible.module_utils.common._collections_compat import Mapping, Sequence
from ansible.module_utils.six.moves.urllib.parse import urlparse
from ansible.module_utils.parsing.convert_bool import BOOLEANS_TRUE, BOOLEANS_FALSE
@ -589,6 +590,20 @@ class AnsibleDockerClient(Client):
return new_tag, old_tag == new_tag
def report_warnings(self, result, warnings_key=None):
'''
Checks result of client operation for warnings, and if present, outputs them.
'''
if warnings_key is None:
warnings_key = ['Warnings']
for key in warnings_key:
if not isinstance(result, Mapping):
return
result = result.get(key)
if isinstance(result, Sequence):
for warning in result:
self.module.warn('Docker warning: {0}'.format(warning))
def compare_dict_allow_more_present(av, bv):
'''

View file

@ -2030,6 +2030,7 @@ class ContainerManager(DockerBaseClass):
if not self.check_mode:
try:
new_container = self.client.create_container(image, **create_parameters)
self.client.report_warnings(new_container)
except Exception as exc:
self.fail("Error creating container: %s" % str(exc))
return self._get_container(new_container['Id'])
@ -2122,7 +2123,8 @@ class ContainerManager(DockerBaseClass):
self.results['changed'] = True
if not self.check_mode and callable(getattr(self.client, 'update_container')):
try:
self.client.update_container(container_id, **update_parameters)
result = self.client.update_container(container_id, **update_parameters)
self.client.report_warnings(result)
except Exception as exc:
self.fail("Error updating container %s: %s" % (container_id, str(exc)))
return self._get_container(container_id)

View file

@ -100,7 +100,7 @@
- assert:
that:
- blkio_weight_1 is changed
- blkio_weight_2 is not changed
- "blkio_weight_2 is not changed or 'Docker warning: Your kernel does not support Block I/O weight or the cgroup is not mounted. Weight discarded.' in blkio_weight_2.warnings"
- blkio_weight_3 is changed
####################################################################
@ -350,7 +350,7 @@
image: alpine:3.8
command: '/bin/sh -c "sleep 10m"'
name: "{{ cname }}"
cpuset_cpus: 0
cpuset_cpus: "0"
state: started
register: cpuset_cpus_1
@ -359,7 +359,7 @@
image: alpine:3.8
command: '/bin/sh -c "sleep 10m"'
name: "{{ cname }}"
cpuset_cpus: 0
cpuset_cpus: "0"
state: started
register: cpuset_cpus_2
@ -368,7 +368,7 @@
image: alpine:3.8
command: '/bin/sh -c "sleep 10m"'
name: "{{ cname }}"
cpuset_cpus: 1
cpuset_cpus: "1"
state: started
stop_timeout: 1
# This will fail if the system the test is run on doesn't have
@ -397,7 +397,7 @@
image: alpine:3.8
command: '/bin/sh -c "sleep 10m"'
name: "{{ cname }}"
cpuset_mems: 0
cpuset_mems: "0"
state: started
register: cpuset_mems_1
@ -406,7 +406,7 @@
image: alpine:3.8
command: '/bin/sh -c "sleep 10m"'
name: "{{ cname }}"
cpuset_mems: 0
cpuset_mems: "0"
state: started
register: cpuset_mems_2
@ -415,7 +415,7 @@
image: alpine:3.8
command: '/bin/sh -c "sleep 10m"'
name: "{{ cname }}"
cpuset_mems: 1
cpuset_mems: "1"
state: started
stop_timeout: 1
# This will fail if the system the test is run on doesn't have
@ -872,7 +872,7 @@
state: started
register: entrypoint_2
- name: entrypoint (change order idempotency)
- name: entrypoint (change order, should not be idempotent)
docker_container:
image: alpine:3.8
entrypoint:
@ -882,6 +882,7 @@
- "-v"
name: "{{ cname }}"
state: started
force_kill: yes
register: entrypoint_3
- name: entrypoint (less parameters)
@ -1108,8 +1109,8 @@
name: "{{ cname }}"
state: started
exposed_ports:
- 1234
- 5678
- "1234"
- "5678"
register: exposed_ports_1
- name: exposed_ports (idempotency)
@ -1119,8 +1120,8 @@
name: "{{ cname }}"
state: started
exposed_ports:
- 5678
- 1234
- "5678"
- "1234"
register: exposed_ports_2
- name: exposed_ports (less ports)
@ -1130,7 +1131,7 @@
name: "{{ cname }}"
state: started
exposed_ports:
- 1234
- "1234"
register: exposed_ports_3
- name: exposed_ports (more ports)
@ -1140,8 +1141,8 @@
name: "{{ cname }}"
state: started
exposed_ports:
- 1234
- 1235
- "1234"
- "1235"
stop_timeout: 1
register: exposed_ports_4
@ -1175,8 +1176,8 @@
name: "{{ cname }}"
state: started
groups:
- 1234
- 5678
- "1234"
- "5678"
register: groups_1
- name: groups (idempotency)
@ -1186,8 +1187,8 @@
name: "{{ cname }}"
state: started
groups:
- 5678
- 1234
- "5678"
- "1234"
register: groups_2
- name: groups (less groups)
@ -1197,7 +1198,7 @@
name: "{{ cname }}"
state: started
groups:
- 1234
- "1234"
register: groups_3
- name: groups (more groups)
@ -1207,8 +1208,8 @@
name: "{{ cname }}"
state: started
groups:
- 1234
- 2345
- "1234"
- "2345"
stop_timeout: 1
register: groups_4
@ -2897,7 +2898,7 @@
image: alpine:3.8
command: '/bin/sh -c "sleep 10m"'
name: "{{ cname }}"
stop_signal: 30
stop_signal: "30"
state: started
register: stop_signal_1
@ -2906,7 +2907,7 @@
image: alpine:3.8
command: '/bin/sh -c "sleep 10m"'
name: "{{ cname }}"
stop_signal: 30
stop_signal: "30"
state: started
register: stop_signal_2
@ -2915,7 +2916,7 @@
image: alpine:3.8
command: '/bin/sh -c "sleep 10m"'
name: "{{ cname }}"
stop_signal: 9
stop_signal: "9"
state: started
stop_timeout: 1
register: stop_signal_3

View file

@ -14,8 +14,8 @@
name: "{{ cname }}"
state: started
exposed_ports:
- 8080
- 8081
- "8080"
- "8081"
published_ports:
- all
stop_timeout: 1
@ -28,8 +28,8 @@
name: "{{ cname }}"
state: started
exposed_ports:
- 8080
- 8081
- "8080"
- "8081"
published_ports:
- all
stop_timeout: 1
@ -42,11 +42,11 @@
name: "{{ cname }}"
state: started
exposed_ports:
- 8080
- 8081
- "8080"
- "8081"
published_ports:
- 8080
- 8081
- "8080"
- "8081"
stop_timeout: 1
register: published_ports_3
@ -57,11 +57,11 @@
name: "{{ cname }}"
state: started
exposed_ports:
- 8080
- 8081
- "8080"
- "8081"
published_ports:
- 8080
- 8081
- "8080"
- "8081"
stop_timeout: 1
register: published_ports_4
@ -72,8 +72,8 @@
name: "{{ cname }}"
state: started
exposed_ports:
- 8080
- 8081
- "8080"
- "8081"
published_ports:
- all
stop_timeout: 1