diff --git a/test/conftest.py b/test/conftest.py index 8455d809c..6d52d8f6f 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -3,11 +3,11 @@ from pathlib import Path from packaging.version import Version +import pytest + from ansible_runner import defaults from ansible_runner.utils.importlib_compat import importlib_metadata -import pytest - CONTAINER_RUNTIMES = ( 'docker', diff --git a/test/unit/config/test__base.py b/test/unit/config/test__base.py index 7470e97a5..5025115ad 100644 --- a/test/unit/config/test__base.py +++ b/test/unit/config/test__base.py @@ -2,24 +2,17 @@ import os import re -import six - from functools import partial -import pytest +from test.utils.common import RSAKey +import pytest +import six from pexpect import TIMEOUT, EOF from ansible_runner.config._base import BaseConfig, BaseExecutionMode from ansible_runner.loader import ArtifactLoader from ansible_runner.exceptions import ConfigurationError -from test.utils.common import RSAKey - -try: - Pattern = re._pattern_type -except AttributeError: - # Python 3.7 - Pattern = re.Pattern def load_file_side_effect(path, value=None, *args, **kwargs): @@ -74,9 +67,9 @@ def test_base_config_project_dir(tmp_path): def test_prepare_environment_vars_only_strings_from_file(mocker): - rc = BaseConfig(envvars=dict(D='D')) + rc = BaseConfig(envvars={'D': 'D'}) - value = dict(A=1, B=True, C="foo") + value = {"A": 1, "B": True, "C": "foo"} envvar_side_effect = partial(load_file_side_effect, 'env/envvars', value) mocker.patch.object(rc.loader, 'load_file', side_effect=envvar_side_effect) @@ -93,7 +86,7 @@ def test_prepare_environment_vars_only_strings_from_file(mocker): def test_prepare_environment_vars_only_strings_from_interface(): - rc = BaseConfig(envvars=dict(D='D', A=1, B=True, C="foo")) + rc = BaseConfig(envvars={'D': 'D', 'A': 1, 'B': True, 'C': 'foo'}) rc._prepare_env() assert 'A' in rc.env @@ -128,7 +121,7 @@ def test_prepare_env_passwords(mocker): rc.expect_passwords.pop(TIMEOUT) rc.expect_passwords.pop(EOF) assert len(rc.expect_passwords) == 1 - assert isinstance(list(rc.expect_passwords.keys())[0], Pattern) + assert isinstance(list(rc.expect_passwords.keys())[0], re.Pattern) assert 'secret' in rc.expect_passwords.values() @@ -285,7 +278,7 @@ def test_container_volume_mounting_with_Z(tmp_path, mocker): if mount.endswith('project_path/:Z'): break else: - raise Exception('Could not find expected mount, args: {}'.format(new_args)) + raise Exception(f'Could not find expected mount, args: {new_args}') @pytest.mark.parametrize('runtime', ('docker', 'podman')) @@ -323,8 +316,8 @@ def test_containerization_settings(tmp_path, runtime, mocker): '--interactive', '--workdir', '/runner/project', - '-v', '{}/.ssh/:/home/runner/.ssh/'.format(str(tmp_path)), - '-v', '{}/.ssh/:/root/.ssh/'.format(str(tmp_path)), + '-v', f'{str(tmp_path)}/.ssh/:/home/runner/.ssh/', + '-v', f'{str(tmp_path)}/.ssh/:/root/.ssh/', ] if os.path.exists('/etc/ssh/ssh_known_hosts'): @@ -334,9 +327,9 @@ def test_containerization_settings(tmp_path, runtime, mocker): expected_command_start.extend(['--group-add=root', '--ipc=host']) expected_command_start.extend([ - '-v', '{}/artifacts/:/runner/artifacts/:Z'.format(rc.private_data_dir), - '-v', '{}/:/runner/:Z'.format(rc.private_data_dir), - '--env-file', '{}/env.list'.format(rc.artifact_dir), + '-v', f'{rc.private_data_dir}/artifacts/:/runner/artifacts/:Z', + '-v', f'{rc.private_data_dir}/:/runner/:Z', + '--env-file', f'{rc.artifact_dir}/env.list', ]) expected_command_start.extend(extra_container_args) diff --git a/test/unit/config/test_ansible_cfg.py b/test/unit/config/test_ansible_cfg.py index 1157a5664..91a240f41 100644 --- a/test/unit/config/test_ansible_cfg.py +++ b/test/unit/config/test_ansible_cfg.py @@ -81,8 +81,8 @@ def test_prepare_config_command_with_containerization(tmp_path, runtime, mocker) '--interactive', '--workdir', '/runner/project', - '-v', '{}/.ssh/:/home/runner/.ssh/'.format(rc.private_data_dir), - '-v', '{}/.ssh/:/root/.ssh/'.format(str(tmp_path)), + '-v', f'{rc.private_data_dir}/.ssh/:/home/runner/.ssh/', + '-v', f'{str(tmp_path)}/.ssh/:/root/.ssh/', ] if os.path.exists('/etc/ssh/ssh_known_hosts'): @@ -92,9 +92,9 @@ def test_prepare_config_command_with_containerization(tmp_path, runtime, mocker) expected_command_start.extend(['--group-add=root', '--ipc=host']) expected_command_start.extend([ - '-v', '{}/artifacts/:/runner/artifacts/:Z'.format(rc.private_data_dir), - '-v', '{}/:/runner/:Z'.format(rc.private_data_dir), - '--env-file', '{}/env.list'.format(rc.artifact_dir), + '-v', f'{rc.private_data_dir}/artifacts/:/runner/artifacts/:Z', + '-v', f'{rc.private_data_dir}/:/runner/:Z', + '--env-file', f'{rc.artifact_dir}/env.list', ]) expected_command_start.extend(extra_container_args) diff --git a/test/unit/config/test_command.py b/test/unit/config/test_command.py index fc65b4e72..1f65f80c8 100644 --- a/test/unit/config/test_command.py +++ b/test/unit/config/test_command.py @@ -94,9 +94,9 @@ def test_prepare_run_command_with_containerization(tmp_path, runtime, mocker): '--interactive', '--workdir', '/runner/project', - '-v', '{}/:{}/'.format(cwd, cwd), - '-v', '{}/.ssh/:/home/runner/.ssh/'.format(rc.private_data_dir), - '-v', '{}/.ssh/:/root/.ssh/'.format(rc.private_data_dir), + '-v', f'{cwd}/:{cwd}/', + '-v', f'{rc.private_data_dir}/.ssh/:/home/runner/.ssh/', + '-v', f'{rc.private_data_dir}/.ssh/:/root/.ssh/', ] if os.path.exists('/etc/ssh/ssh_known_hosts'): @@ -106,9 +106,9 @@ def test_prepare_run_command_with_containerization(tmp_path, runtime, mocker): expected_command_start.extend(['--group-add=root', '--ipc=host']) expected_command_start.extend([ - '-v', '{}/artifacts/:/runner/artifacts/:Z'.format(rc.private_data_dir), - '-v', '{}/:/runner/:Z'.format(rc.private_data_dir), - '--env-file', '{}/env.list'.format(rc.artifact_dir), + '-v', f'{rc.private_data_dir}/artifacts/:/runner/artifacts/:Z', + '-v', f'{rc.private_data_dir}/:/runner/:Z', + '--env-file', f'{rc.artifact_dir}/env.list', ]) expected_command_start.extend(extra_container_args) diff --git a/test/unit/config/test_container_volmount_generation.py b/test/unit/config/test_container_volmount_generation.py index 14e43385f..17f87be37 100644 --- a/test/unit/config/test_container_volmount_generation.py +++ b/test/unit/config/test_container_volmount_generation.py @@ -2,10 +2,10 @@ predictably and consistently """ import os -import pytest - from typing import NamedTuple +import pytest + from ansible_runner.config._base import BaseConfig from ansible_runner.exceptions import ConfigurationError diff --git a/test/unit/config/test_doc.py b/test/unit/config/test_doc.py index 655ce09e7..68d552d7c 100755 --- a/test/unit/config/test_doc.py +++ b/test/unit/config/test_doc.py @@ -91,8 +91,8 @@ def test_prepare_plugin_docs_command_with_containerization(tmp_path, runtime, mo '--interactive', '--workdir', '/runner/project', - '-v', '{}/.ssh/:/home/runner/.ssh/'.format(rc.private_data_dir), - '-v', '{}/.ssh/:/root/.ssh/'.format(rc.private_data_dir), + '-v', f'{rc.private_data_dir}/.ssh/:/home/runner/.ssh/', + '-v', f'{rc.private_data_dir}/.ssh/:/root/.ssh/', ] if os.path.exists('/etc/ssh/ssh_known_hosts'): @@ -102,9 +102,9 @@ def test_prepare_plugin_docs_command_with_containerization(tmp_path, runtime, mo expected_command_start.extend(['--group-add=root', '--ipc=host']) expected_command_start.extend([ - '-v', '{}/artifacts/:/runner/artifacts/:Z'.format(rc.private_data_dir), - '-v', '{}/:/runner/:Z'.format(rc.private_data_dir), - '--env-file', '{}/env.list'.format(rc.artifact_dir), + '-v', f'{rc.private_data_dir}/artifacts/:/runner/artifacts/:Z', + '-v', f'{rc.private_data_dir}/:/runner/:Z', + '--env-file', f'{rc.artifact_dir}/env.list', ]) expected_command_start.extend(extra_container_args) @@ -162,8 +162,8 @@ def test_prepare_plugin_list_command_with_containerization(tmp_path, runtime, mo '--interactive', '--workdir', '/runner/project', - '-v', '{}/.ssh/:/home/runner/.ssh/'.format(rc.private_data_dir), - '-v', '{}/.ssh/:/root/.ssh/'.format(rc.private_data_dir), + '-v', f'{rc.private_data_dir}/.ssh/:/home/runner/.ssh/', + '-v', f'{rc.private_data_dir}/.ssh/:/root/.ssh/', ] if os.path.exists('/etc/ssh/ssh_known_hosts'): @@ -173,9 +173,9 @@ def test_prepare_plugin_list_command_with_containerization(tmp_path, runtime, mo expected_command_start.extend(['--group-add=root', '--ipc=host']) expected_command_start.extend([ - '-v', '{}/artifacts/:/runner/artifacts/:Z'.format(rc.private_data_dir), - '-v', '{}/:/runner/:Z'.format(rc.private_data_dir), - '--env-file', '{}/env.list'.format(rc.artifact_dir), + '-v', f'{rc.private_data_dir}/artifacts/:/runner/artifacts/:Z', + '-v', f'{rc.private_data_dir}/:/runner/:Z', + '--env-file', f'{rc.artifact_dir}/env.list', ]) expected_command_start.extend(extra_container_args) diff --git a/test/unit/config/test_inventory.py b/test/unit/config/test_inventory.py index c701b9e21..c4f72c785 100644 --- a/test/unit/config/test_inventory.py +++ b/test/unit/config/test_inventory.py @@ -116,8 +116,8 @@ def test_prepare_inventory_command_with_containerization(tmp_path, runtime, mock '--interactive', '--workdir', '/runner/project', - '-v', '{}/.ssh/:/home/runner/.ssh/'.format(rc.private_data_dir), - '-v', '{}/.ssh/:/root/.ssh/'.format(rc.private_data_dir), + '-v', f'{rc.private_data_dir}/.ssh/:/home/runner/.ssh/', + '-v', f'{rc.private_data_dir}/.ssh/:/root/.ssh/', ] if os.path.exists('/etc/ssh/ssh_known_hosts'): @@ -127,9 +127,9 @@ def test_prepare_inventory_command_with_containerization(tmp_path, runtime, mock expected_command_start.extend(['--group-add=root', '--ipc=host']) expected_command_start.extend([ - '-v', '{}/artifacts/:/runner/artifacts/:Z'.format(rc.private_data_dir), - '-v', '{}/:/runner/:Z'.format(rc.private_data_dir), - '--env-file', '{}/env.list'.format(rc.artifact_dir), + '-v', f'{rc.private_data_dir}/artifacts/:/runner/artifacts/:Z', + '-v', f'{rc.private_data_dir}/:/runner/:Z', + '--env-file', f'{rc.artifact_dir}/env.list', ]) expected_command_start.extend(extra_container_args) diff --git a/test/unit/config/test_runner.py b/test/unit/config/test_runner.py index 9f46b5baf..a8f440ab4 100644 --- a/test/unit/config/test_runner.py +++ b/test/unit/config/test_runner.py @@ -4,23 +4,17 @@ from io import StringIO import os import re -import six -from pexpect import TIMEOUT, EOF +from test.utils.common import RSAKey +from pexpect import TIMEOUT, EOF import pytest +import six from ansible_runner.config.runner import RunnerConfig, ExecutionMode from ansible_runner.interface import init_runner from ansible_runner.loader import ArtifactLoader from ansible_runner.exceptions import ConfigurationError -from test.utils.common import RSAKey - -try: - Pattern = re._pattern_type -except AttributeError: - # Python 3.7 - Pattern = re.Pattern def load_file_side_effect(path, value=None, *args, **kwargs): @@ -41,7 +35,7 @@ def test_runner_config_init_defaults(mocker): assert rc.limit is None assert rc.module is None assert rc.module_args is None - assert rc.artifact_dir == os.path.join('/artifacts/%s' % rc.ident) + assert rc.artifact_dir == os.path.join(f'/artifacts/{rc.ident}') assert isinstance(rc.loader, ArtifactLoader) @@ -79,9 +73,9 @@ def test_runner_config_project_dir(mocker): def test_prepare_environment_vars_only_strings(mocker): mocker.patch('os.makedirs', return_value=True) - rc = RunnerConfig(private_data_dir="/", envvars=dict(D='D')) + rc = RunnerConfig(private_data_dir="/", envvars={'D': 'D'}) - value = dict(A=1, B=True, C="foo") + value = {'A': 1, 'B': True, 'C': 'foo'} envvar_side_effect = partial(load_file_side_effect, 'env/envvars', value) mocker.patch.object(rc.loader, 'load_file', side_effect=envvar_side_effect) @@ -138,7 +132,7 @@ def test_prepare_env_passwords(mocker): rc.expect_passwords.pop(TIMEOUT) rc.expect_passwords.pop(EOF) assert len(rc.expect_passwords) == 1 - assert isinstance(list(rc.expect_passwords.keys())[0], Pattern) + assert isinstance(list(rc.expect_passwords.keys())[0], re.Pattern) assert 'secret' in rc.expect_passwords.values() @@ -303,7 +297,7 @@ def test_generate_ansible_command(mocker): cmd = rc.generate_ansible_command() assert cmd == ['ansible-playbook', '-i', '/inventory', 'main.yaml'] - rc.extra_vars = dict(test="key") + rc.extra_vars = {'test': 'key'} cmd = rc.generate_ansible_command() assert cmd == ['ansible-playbook', '-i', '/inventory', '-e', '{"test":"key"}', 'main.yaml'] rc.extra_vars = None @@ -708,7 +702,7 @@ def test_container_volume_mounting_with_Z(mocker, tmp_path): if mount.endswith(':/tmp/project_path/:Z'): break else: - raise Exception('Could not find expected mount, args: {}'.format(new_args)) + raise Exception(f'Could not find expected mount, args: {new_args}') @pytest.mark.parametrize('runtime', ('docker', 'podman')) @@ -738,7 +732,7 @@ def test_containerization_settings(tmp_path, runtime, mocker): # validate ANSIBLE_CALLBACK_PLUGINS contains callback plugin dir callback_plugins = rc.env['ANSIBLE_CALLBACK_PLUGINS'].split(':') - callback_dir = os.path.join("/runner/artifacts", "{}".format(rc.ident), "callback") + callback_dir = os.path.join("/runner/artifacts", str(rc.ident), "callback") assert callback_dir in callback_plugins extra_container_args = [] @@ -748,9 +742,9 @@ def test_containerization_settings(tmp_path, runtime, mocker): extra_container_args = [f'--user={os.getuid()}'] expected_command_start = [runtime, 'run', '--rm', '--tty', '--interactive', '--workdir', '/runner/project'] + \ - ['-v', '{}/:/runner/:Z'.format(rc.private_data_dir)] + \ + ['-v', f'{rc.private_data_dir}/:/runner/:Z'] + \ ['-v', '/host1/:/container1/', '-v', '/host2/:/container2/'] + \ - ['--env-file', '{}/env.list'.format(rc.artifact_dir)] + \ + ['--env-file', f'{rc.artifact_dir}/env.list'] + \ extra_container_args + \ ['--name', 'ansible_runner_foo'] + \ ['my_container', 'ansible-playbook', '-i', '/runner/inventory/hosts', 'main.yaml'] diff --git a/test/unit/test_event_filter.py b/test/unit/test_event_filter.py index b9e08e67f..0bc676405 100644 --- a/test/unit/test_event_filter.py +++ b/test/unit/test_event_filter.py @@ -1,9 +1,8 @@ -import pytest import base64 import json from io import StringIO -from six.moves import xrange +import pytest from ansible_runner.utils import OutputEventFilter @@ -15,9 +14,9 @@ def write_encoded_event_data(fileobj, data): b64data = base64.b64encode(json.dumps(data).encode('utf-8')).decode() # pattern corresponding to OutputEventFilter expectation fileobj.write(u'\x1b[K') - for offset in xrange(0, len(b64data), MAX_WIDTH): + for offset in range(0, len(b64data), MAX_WIDTH): chunk = b64data[offset:offset + MAX_WIDTH] - escaped_chunk = u'{}\x1b[{}D'.format(chunk, len(chunk)) + escaped_chunk = f'{chunk}\x1b[{len(chunk)}D' fileobj.write(escaped_chunk) fileobj.write(u'\x1b[K') @@ -43,7 +42,7 @@ def job_event_callback(fake_callback, fake_cache): def method(event_data): print('fake callback called') if 'uuid' in event_data: - cache_event = fake_cache.get(':1:ev-{}'.format(event_data['uuid']), None) + cache_event = fake_cache.get(f":1:ev-{event_data['uuid']}", None) if cache_event is not None: event_data.update(cache_event) fake_callback.append(event_data) @@ -52,7 +51,7 @@ def method(event_data): def test_event_recomb(fake_callback, fake_cache, wrapped_handle): # Pretend that this is done by the Ansible callback module - fake_cache[':1:ev-{}'.format(EXAMPLE_UUID)] = {'event': 'foo'} + fake_cache[f':1:ev-{EXAMPLE_UUID}'] = {'event': 'foo'} write_encoded_event_data(wrapped_handle, { 'uuid': EXAMPLE_UUID }) @@ -84,7 +83,7 @@ def test_separate_verbose_events(fake_callback, wrapped_handle): def test_large_data_payload(fake_callback, fake_cache, wrapped_handle): # Pretend that this is done by the Ansible callback module - fake_cache[':1:ev-{}'.format(EXAMPLE_UUID)] = {'event': 'foo'} + fake_cache[f':1:ev-{EXAMPLE_UUID}'] = {'event': 'foo'} event_data_to_encode = { 'uuid': EXAMPLE_UUID, 'host': 'localhost', @@ -107,7 +106,7 @@ def test_large_data_payload(fake_callback, fake_cache, wrapped_handle): def test_event_lazy_parsing(fake_callback, fake_cache, wrapped_handle): # Pretend that this is done by the Ansible callback module - fake_cache[':1:ev-{}'.format(EXAMPLE_UUID)] = {'event': 'foo'} + fake_cache[f':1:ev-{EXAMPLE_UUID}'] = {'event': 'foo'} buff = StringIO() event_data_to_encode = { 'uuid': EXAMPLE_UUID, diff --git a/test/unit/test_runner.py b/test/unit/test_runner.py index d5207ed79..198586ac6 100644 --- a/test/unit/test_runner.py +++ b/test/unit/test_runner.py @@ -3,15 +3,15 @@ import codecs import datetime import os - +import sys import json from pathlib import Path + +from test.utils.common import iterate_timeout + import pexpect import pytest import six -import sys - -from test.utils.common import iterate_timeout from ansible_runner import Runner from ansible_runner.exceptions import CallbackError, AnsibleRunnerException @@ -102,7 +102,7 @@ def test_event_callback_data_check(rc, mocker): runner.event_handler = mocker.Mock() with pytest.raises(AnsibleRunnerException) as exc: - runner.event_callback(dict(uuid="testuuid", counter=0)) + runner.event_callback({"uuid": "testuuid", "counter": 0}) assert "Failed to open ansible stdout callback plugin partial data" in str(exc) @@ -111,16 +111,19 @@ def test_event_callback_interface_has_ident(rc, mocker): rc.ident = "testident" runner = Runner(config=rc, remove_partials=False) runner.event_handler = mocker.Mock() - mocker.patch('codecs.open', mocker.mock_open(read_data=json.dumps(dict(event="test")))) + mocker.patch('codecs.open', mocker.mock_open(read_data=json.dumps({"event": "test"}))) chmod = mocker.patch('os.chmod', mocker.Mock()) mocker.patch('os.mkdir', mocker.Mock()) - runner.event_callback(dict(uuid="testuuid", counter=0)) + runner.event_callback({"uuid": "testuuid", "counter": 0}) assert runner.event_handler.call_count == 1 - runner.event_handler.assert_called_with(dict( - runner_ident='testident', counter=0, uuid='testuuid', event='test', - created=mocker.ANY - )) + runner.event_handler.assert_called_with({ + 'runner_ident': 'testident', + 'counter': 0, + 'uuid': 'testuuid', + 'event': 'test', + 'created': mocker.ANY + }) chmod.assert_called_once() runner.status_callback("running") @@ -131,12 +134,15 @@ def test_event_callback_interface_calls_event_handler_for_verbose_event(rc, mock runner = Runner(config=rc, event_handler=event_handler) mocker.patch('os.mkdir', mocker.Mock()) - runner.event_callback(dict(uuid="testuuid", event='verbose', counter=0)) + runner.event_callback({"uuid": "testuuid", "event": "verbose", "counter": 0}) assert event_handler.call_count == 1 - event_handler.assert_called_with(dict( - runner_ident='testident', counter=0, uuid='testuuid', event='verbose', - created=mocker.ANY - )) + event_handler.assert_called_with({ + 'runner_ident': 'testident', + 'counter': 0, + 'uuid': 'testuuid', + 'event': 'verbose', + 'created': mocker.ANY + }) def test_status_callback_interface(rc, mocker): @@ -145,7 +151,9 @@ def test_status_callback_interface(rc, mocker): runner.status_handler = mocker.Mock() runner.status_callback("running") assert runner.status_handler.call_count == 1 - runner.status_handler.assert_called_with(dict(status='running', runner_ident=str(rc.ident)), runner_config=runner.config) + runner.status_handler.assert_called_with( + {'status': 'running', 'runner_ident': str(rc.ident)}, + runner_config=runner.config) assert runner.status == 'running' diff --git a/test/unit/utils/test_fifo_pipe.py b/test/unit/utils/test_fifo_pipe.py index f94f281be..6035d3225 100644 --- a/test/unit/utils/test_fifo_pipe.py +++ b/test/unit/utils/test_fifo_pipe.py @@ -1,6 +1,7 @@ -from ansible_runner.utils import open_fifo_write from os import remove +from ansible_runner.utils import open_fifo_write + def test_fifo_write_bytes(tmp_path): path = tmp_path / "bytes_test" diff --git a/test/utils/common.py b/test/utils/common.py index 3e5c2bad0..ccd06bbb2 100644 --- a/test/utils/common.py +++ b/test/utils/common.py @@ -15,7 +15,7 @@ def iterate_timeout(max_seconds, purpose, interval=2): count += 1 yield count time.sleep(interval) - raise Exception("Timeout waiting for %s" % purpose) + raise Exception(f"Timeout waiting for {purpose}") class RSAKey: diff --git a/tox.ini b/tox.ini index d5306e389..02bc5851a 100644 --- a/tox.ini +++ b/tox.ini @@ -31,7 +31,7 @@ commands = yamllint --version yamllint -s . mypy src/ansible_runner - pylint src/ansible_runner + pylint src/ansible_runner test [testenv:unit{,-py39,-py310,-py311}] description = Run unit tests