mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 13:52:54 +00:00
docker_swarm_service: Add healthcheck option (#52419)
* Define yaml versions as strings * Add healthcheck option * Add changelog fragment * Don’t set version_added as strings * Use single quoted string * Disable healthcheck tests on python 2.6 * Bring back quoting on already quoted version-added * Python 2.6 compat * Move functions to docker-common * Move parse_healthcheck to docker-common * Cast exception to str before printing failure * Extend parse_healthcheck tests
This commit is contained in:
committed by
John R Barker
parent
77d116f66e
commit
18b968d486
@@ -889,6 +889,133 @@
|
||||
- "('version is ' ~ docker_api_version ~'. Minimum version required is 1.25') in force_update_1.msg"
|
||||
when: docker_api_version is version('1.25', '<')
|
||||
|
||||
####################################################################
|
||||
## healthcheck #####################################################
|
||||
####################################################################
|
||||
|
||||
- name: healthcheck
|
||||
docker_swarm_service:
|
||||
name: "{{ service_name }}"
|
||||
image: alpine:3.8
|
||||
command: '/bin/sh -v -c "sleep 10m"'
|
||||
healthcheck:
|
||||
test:
|
||||
- CMD
|
||||
- sleep
|
||||
- 1
|
||||
timeout: 2s
|
||||
interval: 0h0m2s3ms4us
|
||||
retries: 2
|
||||
register: healthcheck_1
|
||||
|
||||
- name: healthcheck (idempotency)
|
||||
docker_swarm_service:
|
||||
name: "{{ service_name }}"
|
||||
image: alpine:3.8
|
||||
command: '/bin/sh -v -c "sleep 10m"'
|
||||
healthcheck:
|
||||
test:
|
||||
- CMD
|
||||
- sleep
|
||||
- 1
|
||||
timeout: 2s
|
||||
interval: 0h0m2s3ms4us
|
||||
retries: 2
|
||||
register: healthcheck_2
|
||||
|
||||
- name: healthcheck (changed)
|
||||
docker_swarm_service:
|
||||
name: "{{ service_name }}"
|
||||
image: alpine:3.8
|
||||
command: '/bin/sh -v -c "sleep 10m"'
|
||||
healthcheck:
|
||||
test:
|
||||
- CMD
|
||||
- sleep
|
||||
- 1
|
||||
timeout: 3s
|
||||
interval: 0h1m2s3ms4us
|
||||
retries: 3
|
||||
register: healthcheck_3
|
||||
|
||||
- name: healthcheck (disabled)
|
||||
docker_swarm_service:
|
||||
name: "{{ service_name }}"
|
||||
image: alpine:3.8
|
||||
command: '/bin/sh -v -c "sleep 10m"'
|
||||
healthcheck:
|
||||
test:
|
||||
- NONE
|
||||
register: healthcheck_4
|
||||
|
||||
- name: healthcheck (disabled, idempotency)
|
||||
docker_swarm_service:
|
||||
name: "{{ service_name }}"
|
||||
image: alpine:3.8
|
||||
command: '/bin/sh -v -c "sleep 10m"'
|
||||
healthcheck:
|
||||
test:
|
||||
- NONE
|
||||
register: healthcheck_5
|
||||
|
||||
- name: healthcheck (string in healthcheck test, changed)
|
||||
docker_swarm_service:
|
||||
name: "{{ service_name }}"
|
||||
image: alpine:3.8
|
||||
command: '/bin/sh -v -c "sleep 10m"'
|
||||
healthcheck:
|
||||
test: "sleep 1"
|
||||
register: healthcheck_6
|
||||
|
||||
- name: healthcheck (string in healthcheck test, idempotency)
|
||||
docker_swarm_service:
|
||||
name: "{{ service_name }}"
|
||||
image: alpine:3.8
|
||||
command: '/bin/sh -v -c "sleep 10m"'
|
||||
healthcheck:
|
||||
test: "sleep 1"
|
||||
register: healthcheck_7
|
||||
|
||||
- name: healthcheck (empty)
|
||||
docker_swarm_service:
|
||||
name: "{{ service_name }}"
|
||||
image: alpine:3.8
|
||||
command: '/bin/sh -v -c "sleep 10m"'
|
||||
labels: {}
|
||||
register: healthcheck_8
|
||||
|
||||
- name: healthcheck (empty idempotency)
|
||||
docker_swarm_service:
|
||||
name: "{{ service_name }}"
|
||||
image: alpine:3.8
|
||||
command: '/bin/sh -v -c "sleep 10m"'
|
||||
labels: {}
|
||||
register: healthcheck_9
|
||||
|
||||
- name: cleanup
|
||||
docker_swarm_service:
|
||||
name: "{{ service_name }}"
|
||||
state: absent
|
||||
diff: no
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- healthcheck_1 is changed
|
||||
- healthcheck_2 is not changed
|
||||
- healthcheck_3 is changed
|
||||
- healthcheck_4 is changed
|
||||
- healthcheck_5 is not changed
|
||||
- healthcheck_6 is changed
|
||||
- healthcheck_7 is not changed
|
||||
- healthcheck_8 is changed
|
||||
- healthcheck_9 is not changed
|
||||
when: docker_py_version is version('2.4.0', '>=')
|
||||
- assert:
|
||||
that:
|
||||
- healthcheck_1 is failed
|
||||
- "('version is ' ~ docker_py_version ~'. Minimum version required is 2.4.0') in healthcheck_1.msg"
|
||||
when: docker_py_version is version('2.4.0', '<')
|
||||
|
||||
###################################################################
|
||||
## hostname #######################################################
|
||||
###################################################################
|
||||
|
||||
@@ -12,6 +12,8 @@ service_expected_output:
|
||||
endpoint_mode: vip
|
||||
env: null
|
||||
force_update: null
|
||||
healthcheck: null
|
||||
healthcheck_disabled: null
|
||||
hostname: null
|
||||
image: busybox
|
||||
labels: null
|
||||
|
||||
@@ -3,6 +3,8 @@ import pytest
|
||||
from ansible.module_utils.docker.common import (
|
||||
compare_dict_allow_more_present,
|
||||
compare_generic,
|
||||
convert_duration_to_nanosecond,
|
||||
parse_healthcheck
|
||||
)
|
||||
|
||||
DICT_ALLOW_MORE_PRESENT = (
|
||||
@@ -462,3 +464,52 @@ def test_dict_allow_more_present(entry):
|
||||
@pytest.mark.parametrize("entry", COMPARE_GENERIC)
|
||||
def test_compare_generic(entry):
|
||||
assert compare_generic(entry['a'], entry['b'], entry['method'], entry['type']) == entry['result']
|
||||
|
||||
|
||||
def test_convert_duration_to_nanosecond():
|
||||
nanoseconds = convert_duration_to_nanosecond('5s')
|
||||
assert nanoseconds == 5000000000
|
||||
nanoseconds = convert_duration_to_nanosecond('1m5s')
|
||||
assert nanoseconds == 65000000000
|
||||
with pytest.raises(ValueError):
|
||||
convert_duration_to_nanosecond([1, 2, 3])
|
||||
with pytest.raises(ValueError):
|
||||
convert_duration_to_nanosecond('10x')
|
||||
|
||||
|
||||
def test_parse_healthcheck():
|
||||
result, disabled = parse_healthcheck({
|
||||
'test': 'sleep 1',
|
||||
'interval': '1s',
|
||||
})
|
||||
assert disabled is False
|
||||
assert result == {
|
||||
'test': ['CMD-SHELL', 'sleep 1'],
|
||||
'interval': 1000000000
|
||||
}
|
||||
|
||||
result, disabled = parse_healthcheck({
|
||||
'test': ['NONE'],
|
||||
})
|
||||
assert result is None
|
||||
assert disabled
|
||||
|
||||
result, disabled = parse_healthcheck({
|
||||
'test': 'sleep 1',
|
||||
'interval': '1s423ms'
|
||||
})
|
||||
assert result == {
|
||||
'test': ['CMD-SHELL', 'sleep 1'],
|
||||
'interval': 1423000000
|
||||
}
|
||||
assert disabled is False
|
||||
|
||||
result, disabled = parse_healthcheck({
|
||||
'test': 'sleep 1',
|
||||
'interval': '1h1m2s3ms4us'
|
||||
})
|
||||
assert result == {
|
||||
'test': ['CMD-SHELL', 'sleep 1'],
|
||||
'interval': 3662003004000
|
||||
}
|
||||
assert disabled is False
|
||||
|
||||
Reference in New Issue
Block a user