Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run pylint on test files #1274

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
33 changes: 13 additions & 20 deletions test/unit/config/test__base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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()


Expand Down Expand Up @@ -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'))
Expand Down Expand Up @@ -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'):
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions test/unit/config/test_ansible_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand All @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions test/unit/config/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions test/unit/config/test_container_volmount_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 10 additions & 10 deletions test/unit/config/test_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand All @@ -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)
Expand Down Expand Up @@ -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'):
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions test/unit/config/test_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand All @@ -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)
Expand Down
30 changes: 12 additions & 18 deletions test/unit/config/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)


Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'))
Expand Down Expand Up @@ -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 = []
Expand All @@ -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']
Expand Down
15 changes: 7 additions & 8 deletions test/unit/test_event_filter.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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')

Expand All @@ -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)
Expand All @@ -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
})
Expand Down Expand Up @@ -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',
Expand All @@ -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,
Expand Down
Loading
Loading