summaryrefslogtreecommitdiff
path: root/test/units
diff options
context:
space:
mode:
authorLee Garrett <lgarrett@rocketjump.eu>2022-03-06 17:56:03 +0100
committerLee Garrett <lgarrett@rocketjump.eu>2022-03-06 17:56:03 +0100
commit37d585600d2e2d1c64e33ffaaadce57e4c8407bd (patch)
tree75af9ca60f59e065776d037d1b151eebbf3589bd /test/units
parentb1739f3e93dadd7d8fa794644ceedc24bddc8388 (diff)
downloaddebian-ansible-core-37d585600d2e2d1c64e33ffaaadce57e4c8407bd.zip
New upstream version 2.12.3
Diffstat (limited to 'test/units')
-rw-r--r--test/units/galaxy/test_collection.py83
-rw-r--r--test/units/module_utils/facts/virtual/test_linux.py24
-rw-r--r--test/units/parsing/yaml/test_loader.py8
-rw-r--r--test/units/plugins/become/test_sudo.py27
-rw-r--r--test/units/plugins/connection/test_ssh.py40
5 files changed, 160 insertions, 22 deletions
diff --git a/test/units/galaxy/test_collection.py b/test/units/galaxy/test_collection.py
index 3de2e89a..65243df1 100644
--- a/test/units/galaxy/test_collection.py
+++ b/test/units/galaxy/test_collection.py
@@ -218,7 +218,49 @@ def server_config(monkeypatch):
@pytest.mark.parametrize('global_ignore_certs', [True, False])
-def test_validate_certs(global_ignore_certs, server_config, monkeypatch):
+def test_validate_certs(global_ignore_certs, monkeypatch):
+ cli_args = [
+ 'ansible-galaxy',
+ 'collection',
+ 'install',
+ 'namespace.collection:1.0.0',
+ ]
+ if global_ignore_certs:
+ cli_args.append('--ignore-certs')
+
+ galaxy_cli = GalaxyCLI(args=cli_args)
+ mock_execute_install = MagicMock()
+ monkeypatch.setattr(galaxy_cli, '_execute_install_collection', mock_execute_install)
+ galaxy_cli.run()
+
+ assert len(galaxy_cli.api_servers) == 1
+ assert galaxy_cli.api_servers[0].validate_certs is not global_ignore_certs
+
+
+@pytest.mark.parametrize('global_ignore_certs', [True, False])
+def test_validate_certs_with_server_url(global_ignore_certs, monkeypatch):
+ cli_args = [
+ 'ansible-galaxy',
+ 'collection',
+ 'install',
+ 'namespace.collection:1.0.0',
+ '-s',
+ 'https://galaxy.ansible.com'
+ ]
+ if global_ignore_certs:
+ cli_args.append('--ignore-certs')
+
+ galaxy_cli = GalaxyCLI(args=cli_args)
+ mock_execute_install = MagicMock()
+ monkeypatch.setattr(galaxy_cli, '_execute_install_collection', mock_execute_install)
+ galaxy_cli.run()
+
+ assert len(galaxy_cli.api_servers) == 1
+ assert galaxy_cli.api_servers[0].validate_certs is not global_ignore_certs
+
+
+@pytest.mark.parametrize('global_ignore_certs', [True, False])
+def test_validate_certs_with_server_config(global_ignore_certs, server_config, monkeypatch):
get_plugin_options = MagicMock(side_effect=server_config)
monkeypatch.setattr(C.config, 'get_plugin_options', get_plugin_options)
@@ -289,6 +331,45 @@ def test_build_existing_output_with_force(collection_input):
assert tarfile.is_tarfile(existing_output)
+def test_build_with_existing_files_and_manifest(collection_input):
+ input_dir, output_dir = collection_input
+
+ with open(os.path.join(input_dir, 'MANIFEST.json'), "wb") as fd:
+ fd.write(b'{"collection_info": {"version": "6.6.6"}, "version": 1}')
+
+ with open(os.path.join(input_dir, 'FILES.json'), "wb") as fd:
+ fd.write(b'{"files": [], "format": 1}')
+
+ with open(os.path.join(input_dir, "plugins", "MANIFEST.json"), "wb") as fd:
+ fd.write(b"test data that should be in build")
+
+ collection.build_collection(to_text(input_dir, errors='surrogate_or_strict'), to_text(output_dir, errors='surrogate_or_strict'), False)
+
+ output_artifact = os.path.join(output_dir, 'ansible_namespace-collection-0.1.0.tar.gz')
+ assert tarfile.is_tarfile(output_artifact)
+
+ with tarfile.open(output_artifact, mode='r') as actual:
+ members = actual.getmembers()
+
+ manifest_file = next(m for m in members if m.path == "MANIFEST.json")
+ manifest_file_obj = actual.extractfile(manifest_file.name)
+ manifest_file_text = manifest_file_obj.read()
+ manifest_file_obj.close()
+ assert manifest_file_text != b'{"collection_info": {"version": "6.6.6"}, "version": 1}'
+
+ json_file = next(m for m in members if m.path == "MANIFEST.json")
+ json_file_obj = actual.extractfile(json_file.name)
+ json_file_text = json_file_obj.read()
+ json_file_obj.close()
+ assert json_file_text != b'{"files": [], "format": 1}'
+
+ sub_manifest_file = next(m for m in members if m.path == "plugins/MANIFEST.json")
+ sub_manifest_file_obj = actual.extractfile(sub_manifest_file.name)
+ sub_manifest_file_text = sub_manifest_file_obj.read()
+ sub_manifest_file_obj.close()
+ assert sub_manifest_file_text == b"test data that should be in build"
+
+
@pytest.mark.parametrize('galaxy_yml_dir', [b'namespace: value: broken'], indirect=True)
def test_invalid_yaml_galaxy_file(galaxy_yml_dir):
galaxy_file = os.path.join(galaxy_yml_dir, b'galaxy.yml')
diff --git a/test/units/module_utils/facts/virtual/test_linux.py b/test/units/module_utils/facts/virtual/test_linux.py
index 25503750..7c13299e 100644
--- a/test/units/module_utils/facts/virtual/test_linux.py
+++ b/test/units/module_utils/facts/virtual/test_linux.py
@@ -8,6 +8,30 @@ __metaclass__ = type
from ansible.module_utils.facts.virtual import linux
+def mock_os_path_is_file_docker(filename):
+ if filename in ('/.dockerenv', '/.dockerinit'):
+ return True
+ return False
+
+
+def test_get_virtual_facts_docker(mocker):
+ mocker.patch('os.path.exists', mock_os_path_is_file_docker)
+
+ module = mocker.Mock()
+ module.run_command.return_value = (0, '', '')
+ inst = linux.LinuxVirtual(module)
+ facts = inst.get_virtual_facts()
+
+ expected = {
+ 'virtualization_role': 'guest',
+ 'virtualization_tech_host': set(),
+ 'virtualization_type': 'docker',
+ 'virtualization_tech_guest': set(['docker', 'container']),
+ }
+
+ assert facts == expected
+
+
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='')
diff --git a/test/units/parsing/yaml/test_loader.py b/test/units/parsing/yaml/test_loader.py
index d6989f44..fbe69a97 100644
--- a/test/units/parsing/yaml/test_loader.py
+++ b/test/units/parsing/yaml/test_loader.py
@@ -35,12 +35,8 @@ from ansible.parsing.yaml.dumper import AnsibleDumper
from units.mock.yaml_helper import YamlTestUtils
from units.mock.vault_helper import TextVaultSecret
-try:
- from _yaml import ParserError
- from _yaml import ScannerError
-except ImportError:
- from yaml.parser import ParserError
- from yaml.scanner import ScannerError
+from yaml.parser import ParserError
+from yaml.scanner import ScannerError
class NameStringIO(StringIO):
diff --git a/test/units/plugins/become/test_sudo.py b/test/units/plugins/become/test_sudo.py
index 8ccb2a12..67eb9a46 100644
--- a/test/units/plugins/become/test_sudo.py
+++ b/test/units/plugins/become/test_sudo.py
@@ -38,3 +38,30 @@ def test_sudo(mocker, parser, reset_cli_args):
cmd = sudo.build_become_command('/bin/foo', sh)
assert re.match(r"""sudo\s+-s\s-H\s+-p "\[sudo via ansible, key=.+?\] password:" -u foo /bin/bash -c 'echo BECOME-SUCCESS-.+? ; /bin/foo'""", cmd), cmd
+
+ sudo.set_options(direct={
+ 'become_user': 'foo',
+ 'become_flags': '-snH',
+ 'become_pass': 'testpass',
+ })
+
+ cmd = sudo.build_become_command('/bin/foo', sh)
+ assert re.match(r"""sudo\s+-sH\s+-p "\[sudo via ansible, key=.+?\] password:" -u foo /bin/bash -c 'echo BECOME-SUCCESS-.+? ; /bin/foo'""", cmd), cmd
+
+ sudo.set_options(direct={
+ 'become_user': 'foo',
+ 'become_flags': '--non-interactive -s -H',
+ 'become_pass': 'testpass',
+ })
+
+ cmd = sudo.build_become_command('/bin/foo', sh)
+ assert re.match(r"""sudo\s+-s\s-H\s+-p "\[sudo via ansible, key=.+?\] password:" -u foo /bin/bash -c 'echo BECOME-SUCCESS-.+? ; /bin/foo'""", cmd), cmd
+
+ sudo.set_options(direct={
+ 'become_user': 'foo',
+ 'become_flags': '--non-interactive -nC5 -s -H',
+ 'become_pass': 'testpass',
+ })
+
+ cmd = sudo.build_become_command('/bin/foo', sh)
+ assert re.match(r"""sudo\s+-C5\s-s\s-H\s+-p "\[sudo via ansible, key=.+?\] password:" -u foo /bin/bash -c 'echo BECOME-SUCCESS-.+? ; /bin/foo'""", cmd), cmd
diff --git a/test/units/plugins/connection/test_ssh.py b/test/units/plugins/connection/test_ssh.py
index d693313f..9b3e3c9d 100644
--- a/test/units/plugins/connection/test_ssh.py
+++ b/test/units/plugins/connection/test_ssh.py
@@ -102,6 +102,7 @@ class TestConnectionBaseClass(unittest.TestCase):
def test_plugins_connection_ssh__examine_output(self):
pc = PlayContext()
new_stdin = StringIO()
+ become_success_token = b'BECOME-SUCCESS-abcdefghijklmnopqrstuvxyz'
conn = connection_loader.get('ssh', pc, new_stdin)
conn.set_become_plugin(become_loader.get('sudo'))
@@ -112,24 +113,16 @@ class TestConnectionBaseClass(unittest.TestCase):
conn.become.check_missing_password = MagicMock()
def _check_password_prompt(line):
- if b'foo' in line:
- return True
- return False
+ return b'foo' in line
def _check_become_success(line):
- if b'BECOME-SUCCESS-abcdefghijklmnopqrstuvxyz' in line:
- return True
- return False
+ return become_success_token in line
def _check_incorrect_password(line):
- if b'incorrect password' in line:
- return True
- return False
+ return b'incorrect password' in line
def _check_missing_password(line):
- if b'bad password' in line:
- return True
- return False
+ return b'bad password' in line
# test examining output for prompt
conn._flags = dict(
@@ -172,9 +165,9 @@ class TestConnectionBaseClass(unittest.TestCase):
pc.prompt = False
conn.become.prompt = False
- pc.success_key = u'BECOME-SUCCESS-abcdefghijklmnopqrstuvxyz'
- conn.become.success = u'BECOME-SUCCESS-abcdefghijklmnopqrstuvxyz'
- output, unprocessed = conn._examine_output(u'source', u'state', b'line 1\nline 2\nBECOME-SUCCESS-abcdefghijklmnopqrstuvxyz\nline 3\n', False)
+ pc.success_key = str(become_success_token)
+ conn.become.success = str(become_success_token)
+ output, unprocessed = conn._examine_output(u'source', u'state', b'line 1\nline 2\n%s\nline 3\n' % become_success_token, False)
self.assertEqual(output, b'line 1\nline 2\nline 3\n')
self.assertEqual(unprocessed, b'')
self.assertFalse(conn._flags['become_prompt'])
@@ -182,6 +175,23 @@ class TestConnectionBaseClass(unittest.TestCase):
self.assertFalse(conn._flags['become_error'])
self.assertFalse(conn._flags['become_nopasswd_error'])
+ # test we dont detect become success from ssh debug: lines
+ conn._flags = dict(
+ become_prompt=False,
+ become_success=False,
+ become_error=False,
+ become_nopasswd_error=False,
+ )
+
+ pc.prompt = False
+ conn.become.prompt = True
+ pc.success_key = str(become_success_token)
+ conn.become.success = str(become_success_token)
+ output, unprocessed = conn._examine_output(u'source', u'state', b'line 1\nline 2\ndebug1: %s\nline 3\n' % become_success_token, False)
+ self.assertEqual(output, b'line 1\nline 2\ndebug1: %s\nline 3\n' % become_success_token)
+ self.assertEqual(unprocessed, b'')
+ self.assertFalse(conn._flags['become_success'])
+
# test examining output for become failure
conn._flags = dict(
become_prompt=False,