summaryrefslogtreecommitdiff
path: root/test/units
diff options
context:
space:
mode:
authorLee Garrett <lgarrett@rocketjump.eu>2021-03-24 02:59:56 +0100
committerLee Garrett <lgarrett@rocketjump.eu>2021-03-24 02:59:56 +0100
commitddff31825e92941ec9bfa2aa3ef36e98a4b3b4a7 (patch)
tree31ecc68a40758cea2cd14db073f471ddfc6dea53 /test/units
parent0addf1f445fd4e07fc0be2f62931208314ac5603 (diff)
downloaddebian-ansible-core-ddff31825e92941ec9bfa2aa3ef36e98a4b3b4a7.zip
New upstream version 2.10.7+dfsg
Diffstat (limited to 'test/units')
-rw-r--r--test/units/ansible_test/test_docker_util.py131
-rw-r--r--test/units/errors/test_errors.py46
-rw-r--r--test/units/mock/loader.py5
-rw-r--r--test/units/module_utils/basic/test_atomic_move.py1
-rw-r--r--test/units/module_utils/facts/system/distribution/fixtures/almalinux_8_3_beta.json53
-rw-r--r--test/units/module_utils/facts/virtual/__init__.py0
-rw-r--r--test/units/module_utils/facts/virtual/test_linux.py26
7 files changed, 252 insertions, 10 deletions
diff --git a/test/units/ansible_test/test_docker_util.py b/test/units/ansible_test/test_docker_util.py
new file mode 100644
index 00000000..8427f0f2
--- /dev/null
+++ b/test/units/ansible_test/test_docker_util.py
@@ -0,0 +1,131 @@
+# This file is part of Ansible
+# -*- coding: utf-8 -*-
+#
+#
+# Ansible is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Ansible is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
+#
+
+# Make coding more python3-ish
+from __future__ import (absolute_import, division, print_function)
+__metaclass__ = type
+
+import os
+
+import pytest
+from units.compat.mock import call, patch, MagicMock
+
+# docker images quay.io/ansible/centos7-test-container --format '{{json .}}'
+DOCKER_OUTPUT_MULTIPLE = """
+{"Containers":"N/A","CreatedAt":"2020-06-11 17:05:58 -0500 CDT","CreatedSince":"3 months ago","Digest":"\u003cnone\u003e","ID":"b0f914b26cc1","Repository":"quay.io/ansible/centos7-test-container","SharedSize":"N/A","Size":"556MB","Tag":"1.17.0","UniqueSize":"N/A","VirtualSize":"555.6MB"}
+{"Containers":"N/A","CreatedAt":"2020-06-11 17:05:58 -0500 CDT","CreatedSince":"3 months ago","Digest":"\u003cnone\u003e","ID":"b0f914b26cc1","Repository":"quay.io/ansible/centos7-test-container","SharedSize":"N/A","Size":"556MB","Tag":"latest","UniqueSize":"N/A","VirtualSize":"555.6MB"}
+{"Containers":"N/A","CreatedAt":"2019-04-01 19:59:39 -0500 CDT","CreatedSince":"18 months ago","Digest":"\u003cnone\u003e","ID":"dd3d10e03dd3","Repository":"quay.io/ansible/centos7-test-container","SharedSize":"N/A","Size":"678MB","Tag":"1.8.0","UniqueSize":"N/A","VirtualSize":"678MB"}
+""".lstrip() # noqa: E501
+
+PODMAN_OUTPUT = """
+[
+ {
+ "id": "dd3d10e03dd3580de865560c3440c812a33fd7a1fca8ed8e4a1219ff3d809e3a",
+ "names": [
+ "quay.io/ansible/centos7-test-container:1.8.0"
+ ],
+ "digest": "sha256:6e5d9c99aa558779715a80715e5cf0c227a4b59d95e6803c148290c5d0d9d352",
+ "created": "2019-04-02T00:59:39.234584184Z",
+ "size": 702761933
+ },
+ {
+ "id": "b0f914b26cc1088ab8705413c2f2cf247306ceeea51260d64c26894190d188bd",
+ "names": [
+ "quay.io/ansible/centos7-test-container:latest"
+ ],
+ "digest": "sha256:d8431aa74f60f4ff0f1bd36bc9a227bbb2066330acd8bf25e29d8614ee99e39c",
+ "created": "2020-06-11T22:05:58.382459136Z",
+ "size": 578513505
+ }
+]
+""".lstrip()
+
+
+@pytest.fixture
+def docker_images():
+ from ansible_test._internal.docker_util import docker_images
+ return docker_images
+
+
+@pytest.fixture
+def ansible_test(ansible_test):
+ import ansible_test
+ return ansible_test
+
+
+@pytest.fixture
+def subprocess_error():
+ from ansible_test._internal.util import SubprocessError
+ return SubprocessError
+
+
+@pytest.mark.parametrize(
+ ('returned_items_count', 'patched_dc_stdout'),
+ (
+ (3, (DOCKER_OUTPUT_MULTIPLE, '')),
+ (2, (PODMAN_OUTPUT, '')),
+ (0, ('', '')),
+ ),
+ ids=('docker JSONL', 'podman JSON sequence', 'empty output'))
+def test_docker_images(docker_images, mocker, returned_items_count, patched_dc_stdout):
+ mocker.patch(
+ 'ansible_test._internal.docker_util.docker_command',
+ return_value=patched_dc_stdout)
+ ret = docker_images('', 'quay.io/ansible/centos7-test-container')
+ assert len(ret) == returned_items_count
+
+
+def test_podman_fallback(ansible_test, docker_images, subprocess_error, mocker):
+ '''Test podman >2 && <2.2 fallback'''
+
+ cmd = ['docker', 'images', 'quay.io/ansible/centos7-test-container', '--format', '{{json .}}']
+ docker_command_results = [
+ subprocess_error(cmd, status=1, stderr='function "json" not defined'),
+ (PODMAN_OUTPUT, ''),
+ ]
+ mocker.patch(
+ 'ansible_test._internal.docker_util.docker_command',
+ side_effect=docker_command_results)
+
+ ret = docker_images('', 'quay.io/ansible/centos7-test-container')
+ calls = [
+ call(
+ '',
+ ['images', 'quay.io/ansible/centos7-test-container', '--format', '{{json .}}'],
+ capture=True,
+ always=True),
+ call(
+ '',
+ ['images', 'quay.io/ansible/centos7-test-container', '--format', 'json'],
+ capture=True,
+ always=True),
+ ]
+ ansible_test._internal.docker_util.docker_command.assert_has_calls(calls)
+ assert len(ret) == 2
+
+
+def test_podman_no_such_image(ansible_test, docker_images, subprocess_error, mocker):
+ '''Test podman "no such image" error'''
+
+ cmd = ['docker', 'images', 'quay.io/ansible/centos7-test-container', '--format', '{{json .}}']
+ exc = subprocess_error(cmd, status=1, stderr='no such image'),
+ mocker.patch(
+ 'ansible_test._internal.docker_util.docker_command',
+ side_effect=exc)
+ ret = docker_images('', 'quay.io/ansible/centos7-test-container')
+ assert ret == []
diff --git a/test/units/errors/test_errors.py b/test/units/errors/test_errors.py
index ab5c19cd..136a2695 100644
--- a/test/units/errors/test_errors.py
+++ b/test/units/errors/test_errors.py
@@ -97,14 +97,15 @@ class TestErrors(unittest.TestCase):
"the exact syntax problem.\n\nThe offending line appears to be:\n\n\nthis is line 1\n^ here\n")
)
- # this line will not be found, as it is out of the index range
- self.obj.ansible_pos = ('foo.yml', 2, 1)
- e = AnsibleError(self.message, self.obj)
- self.assertEqual(
- e.message,
- ("This is the error message\n\nThe error appears to be in 'foo.yml': line 2, column 1, but may\nbe elsewhere in the file depending on "
- "the exact syntax problem.\n\n(specified line no longer in file, maybe it changed?)")
- )
+ with patch('ansible.errors.to_text', side_effect=IndexError('Raised intentionally')):
+ # raise an IndexError
+ self.obj.ansible_pos = ('foo.yml', 2, 1)
+ e = AnsibleError(self.message, self.obj)
+ self.assertEqual(
+ e.message,
+ ("This is the error message\n\nThe error appears to be in 'foo.yml': line 2, column 1, but may\nbe elsewhere in the file depending on "
+ "the exact syntax problem.\n\n(specified line no longer in file, maybe it changed?)")
+ )
m = mock_open()
m.return_value.readlines.return_value = ['this line has unicode \xf0\x9f\x98\xa8 in it!\n']
@@ -119,3 +120,32 @@ class TestErrors(unittest.TestCase):
"file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\nthis line has unicode \xf0\x9f\x98\xa8 in it!\n^ "
"here\n")
)
+
+ def test_get_error_lines_error_in_last_line(self):
+ m = mock_open()
+ m.return_value.readlines.return_value = ['this is line 1\n', 'this is line 2\n', 'this is line 3\n']
+
+ with patch('{0}.open'.format(BUILTINS), m):
+ # If the error occurs in the last line of the file, use the correct index to get the line
+ # and avoid the IndexError
+ self.obj.ansible_pos = ('foo.yml', 4, 1)
+ e = AnsibleError(self.message, self.obj)
+ self.assertEqual(
+ e.message,
+ ("This is the error message\n\nThe error appears to be in 'foo.yml': line 4, column 1, but may\nbe elsewhere in the file depending on "
+ "the exact syntax problem.\n\nThe offending line appears to be:\n\nthis is line 2\nthis is line 3\n^ here\n")
+ )
+
+ def test_get_error_lines_error_empty_lines_around_error(self):
+ """Test that trailing whitespace after the error is removed"""
+ m = mock_open()
+ m.return_value.readlines.return_value = ['this is line 1\n', 'this is line 2\n', 'this is line 3\n', ' \n', ' \n', ' ']
+
+ with patch('{0}.open'.format(BUILTINS), m):
+ self.obj.ansible_pos = ('foo.yml', 5, 1)
+ e = AnsibleError(self.message, self.obj)
+ self.assertEqual(
+ e.message,
+ ("This is the error message\n\nThe error appears to be in 'foo.yml': line 5, column 1, but may\nbe elsewhere in the file depending on "
+ "the exact syntax problem.\n\nThe offending line appears to be:\n\nthis is line 2\nthis is line 3\n^ here\n")
+ )
diff --git a/test/units/mock/loader.py b/test/units/mock/loader.py
index 0ee47fbb..c47ec39e 100644
--- a/test/units/mock/loader.py
+++ b/test/units/mock/loader.py
@@ -39,10 +39,11 @@ class DictDataLoader(DataLoader):
self._vault_secrets = None
def load_from_file(self, path, cache=True, unsafe=False):
+ data = None
path = to_text(path)
if path in self._file_mapping:
- return self.load(self._file_mapping[path], path)
- return None
+ data = self.load(self._file_mapping[path], path)
+ return data
# TODO: the real _get_file_contents returns a bytestring, so we actually convert the
# unicode/text it's created with to utf-8
diff --git a/test/units/module_utils/basic/test_atomic_move.py b/test/units/module_utils/basic/test_atomic_move.py
index 7bd9496e..bbdb0519 100644
--- a/test/units/module_utils/basic/test_atomic_move.py
+++ b/test/units/module_utils/basic/test_atomic_move.py
@@ -23,6 +23,7 @@ def atomic_am(am, mocker):
am.selinux_context = mocker.MagicMock()
am.selinux_default_context = mocker.MagicMock()
am.set_context_if_different = mocker.MagicMock()
+ am._unsafe_writes = mocker.MagicMock()
yield am
diff --git a/test/units/module_utils/facts/system/distribution/fixtures/almalinux_8_3_beta.json b/test/units/module_utils/facts/system/distribution/fixtures/almalinux_8_3_beta.json
new file mode 100644
index 00000000..2d8df50b
--- /dev/null
+++ b/test/units/module_utils/facts/system/distribution/fixtures/almalinux_8_3_beta.json
@@ -0,0 +1,53 @@
+{
+ "name": "AlmaLinux 8.3",
+ "distro": {
+ "codename": "Purple Manul",
+ "id": "almalinux",
+ "name": "AlmaLinux",
+ "version": "8.3",
+ "version_best": "8.3",
+ "lsb_release_info": {
+ "lsb_version": ":core-4.1-amd64:core-4.1-noarch",
+ "distributor_id": "AlmaLinux",
+ "description": "AlmaLinux release 8.3 Beta (Purple Manul)",
+ "release": "8.3",
+ "codename": "PurpleManul"
+ },
+ "os_release_info": {
+ "name": "AlmaLinux",
+ "version": "8.3 (Purple Manul)",
+ "id": "almalinux",
+ "id_like": "rhel centos fedora",
+ "version_id": "8.3",
+ "platform_id": "platform:el8",
+ "pretty_name": "AlmaLinux 8.3 Beta (Purple Manul)",
+ "ansi_color": "0;34",
+ "cpe_name": "cpe:/o:almalinux:almalinux:8.3:beta",
+ "home_url": "https://almalinux.org/",
+ "bug_report_url": "https://bugs.almalinux.org/",
+ "almalinux_mantisbt_project": "AlmaLinux-8",
+ "almalinux_mantisbt_project_version": "8",
+ "codename": "Purple Manul"
+ }
+ },
+ "input": {
+ "/etc/centos-release": "AlmaLinux release 8.3 Beta (Purple Manul)\n",
+ "/etc/redhat-release": "AlmaLinux release 8.3 Beta (Purple Manul)\n",
+ "/etc/system-release": "AlmaLinux release 8.3 Beta (Purple Manul)\n",
+ "/etc/os-release": "NAME=\"AlmaLinux\"\nVERSION=\"8.3 (Purple Manul)\"\nID=\"almalinux\"\nID_LIKE=\"rhel centos fedora\"\nVERSION_ID=\"8.3\"\nPLATFORM_ID=\"platform:el8\"\nPRETTY_NAME=\"AlmaLinux 8.3 Beta (Purple Manul)\"\nANSI_COLOR=\"0;34\"\nCPE_NAME=\"cpe:/o:almalinux:almalinux:8.3:beta\"\nHOME_URL=\"https://almalinux.org/\"\nBUG_REPORT_URL=\"https://bugs.almalinux.org/\"\n\nALMALINUX_MANTISBT_PROJECT=\"AlmaLinux-8\" \nALMALINUX_MANTISBT_PROJECT_VERSION=\"8\" \n\n",
+ "/usr/lib/os-release": "NAME=\"AlmaLinux\"\nVERSION=\"8.3 (Purple Manul)\"\nID=\"almalinux\"\nID_LIKE=\"rhel centos fedora\"\nVERSION_ID=\"8.3\"\nPLATFORM_ID=\"platform:el8\"\nPRETTY_NAME=\"AlmaLinux 8.3 Beta (Purple Manul)\"\nANSI_COLOR=\"0;34\"\nCPE_NAME=\"cpe:/o:almalinux:almalinux:8.3:beta\"\nHOME_URL=\"https://almalinux.org/\"\nBUG_REPORT_URL=\"https://bugs.almalinux.org/\"\n\nALMALINUX_MANTISBT_PROJECT=\"AlmaLinux-8\" \nALMALINUX_MANTISBT_PROJECT_VERSION=\"8\" \n\n"
+ },
+ "platform.dist": [
+ "almalinux",
+ "8.3",
+ "Purple Manul"
+ ],
+ "result": {
+ "distribution": "AlmaLinux",
+ "distribution_version": "8.3",
+ "distribution_release": "Purple Manul",
+ "distribution_major_version": "8",
+ "os_family": "RedHat"
+ },
+ "platform.release": "4.18.0-240.el8.x86_64"
+}
diff --git a/test/units/module_utils/facts/virtual/__init__.py b/test/units/module_utils/facts/virtual/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/test/units/module_utils/facts/virtual/__init__.py
diff --git a/test/units/module_utils/facts/virtual/test_linux.py b/test/units/module_utils/facts/virtual/test_linux.py
new file mode 100644
index 00000000..d534478c
--- /dev/null
+++ b/test/units/module_utils/facts/virtual/test_linux.py
@@ -0,0 +1,26 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2020 Ansible Project
+# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
+
+from __future__ import absolute_import, division, print_function
+__metaclass__ = type
+
+from ansible.module_utils.facts.virtual import linux
+
+
+def test_get_virtual_facts_bhyve(mocker):
+ mocker.patch('os.path.exists', return_value=False)
+ mocker.patch('ansible.module_utils.facts.virtual.linux.get_file_content', return_value='')
+ mocker.patch('ansible.module_utils.facts.virtual.linux.get_file_lines', return_value=[])
+
+ module = mocker.Mock()
+ module.run_command.return_value = (0, 'BHYVE\n', '')
+ inst = linux.LinuxVirtual(module)
+
+ facts = inst.get_virtual_facts()
+ expected = {
+ 'virtualization_role': 'guest',
+ 'virtualization_type': 'bhyve',
+ }
+
+ assert facts == expected