From f4da73280c1201ad412f24c7b2e95f554533433f Mon Sep 17 00:00:00 2001 From: Raphael Meudec Date: Wed, 7 Nov 2018 14:31:12 +0100 Subject: [PATCH] Bug 42787 create volume with label (#46527) * add None value to docker-version so it can be mocked in tests --- lib/ansible/module_utils/docker_common.py | 2 ++ .../cloud/docker/test_docker_volume.py | 31 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 test/units/modules/cloud/docker/test_docker_volume.py diff --git a/lib/ansible/module_utils/docker_common.py b/lib/ansible/module_utils/docker_common.py index ac95d5e1b93..0b9e05706ae 100644 --- a/lib/ansible/module_utils/docker_common.py +++ b/lib/ansible/module_utils/docker_common.py @@ -110,6 +110,8 @@ BYTE_SUFFIXES = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'] if not HAS_DOCKER_PY: + docker_version = None + # No docker-py. Create a place holder client to allow # instantiation of AnsibleModule and proper error handing class Client(object): diff --git a/test/units/modules/cloud/docker/test_docker_volume.py b/test/units/modules/cloud/docker/test_docker_volume.py new file mode 100644 index 00000000000..855c3b5899b --- /dev/null +++ b/test/units/modules/cloud/docker/test_docker_volume.py @@ -0,0 +1,31 @@ +# Copyright (c) 2018 Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) +import json + +import pytest + +from ansible.modules.cloud.docker import docker_volume +from ansible.module_utils import docker_common + +pytestmark = pytest.mark.usefixtures('patch_ansible_module') + +TESTCASE_DOCKER_VOLUME = [ + { + 'name': 'daemon_config', + 'state': 'present' + } +] + + +@pytest.mark.parametrize('patch_ansible_module', TESTCASE_DOCKER_VOLUME, indirect=['patch_ansible_module']) +def test_create_volume_on_invalid_docker_version(mocker, capfd): + mocker.patch.object(docker_common, 'HAS_DOCKER_PY', True) + mocker.patch.object(docker_common, 'docker_version', '1.8.0') + + with pytest.raises(SystemExit): + docker_volume.main() + + out, dummy = capfd.readouterr() + results = json.loads(out) + assert results['failed'] + assert 'Error: docker / docker-py version is 1.8.0. Minimum version required is 1.10.0.' in results['msg']