From d3523c4dead6402f69a151242b7c99f7f9d1fcad Mon Sep 17 00:00:00 2001 From: Ian Tewksbury Date: Wed, 21 Jul 2021 17:18:19 -0400 Subject: [PATCH] container login utils - fix truthy/falsy logic --- src/ploigos_step_runner/utils/containers.py | 12 ++-- tests/utils/test_containers.py | 75 +++++++++++++++++++++ 2 files changed, 81 insertions(+), 6 deletions(-) diff --git a/src/ploigos_step_runner/utils/containers.py b/src/ploigos_step_runner/utils/containers.py index acfae3d6..28003920 100644 --- a/src/ploigos_step_runner/utils/containers.py +++ b/src/ploigos_step_runner/utils/containers.py @@ -109,13 +109,13 @@ def container_registries_login( #pylint: disable=too-many-branches else: registry_uri = registry_key - if containers_config_tls_verify is False: - registry_tls_verify = False - else: + if containers_config_tls_verify: if 'tls-verify' in registry_conf: registry_tls_verify = registry_conf['tls-verify'] else: registry_tls_verify = True + else: + registry_tls_verify = False container_registry_login( container_registry_uri=registry_uri, @@ -140,13 +140,13 @@ def container_registries_login( #pylint: disable=too-many-branches f"Configuration for container registry " \ f"must specify a 'password': {registry_conf}" - if containers_config_tls_verify is False: - registry_tls_verify = False - else: + if containers_config_tls_verify: if 'tls-verify' in registry_conf: registry_tls_verify = registry_conf['tls-verify'] else: registry_tls_verify = True + else: + registry_tls_verify = False container_registry_login( container_registry_uri=registry_conf['uri'], diff --git a/tests/utils/test_containers.py b/tests/utils/test_containers.py index 28ba1dbe..0d7d211c 100644 --- a/tests/utils/test_containers.py +++ b/tests/utils/test_containers.py @@ -917,3 +917,78 @@ def test_list_of_dicts(self, container_registry_login_mock): ) ] container_registry_login_mock.assert_has_calls(calls) + + + @patch('ploigos_step_runner.utils.containers.container_registry_login') + def test_list_of_dicts_truthy(self, container_registry_login_mock): + registries = [ + { + 'uri': 'registry.redhat.io', + 'username': 'hello1@world.xyz', + 'password': 'nope1' + }, + { + 'uri': 'registry.internal.example.xyz', + 'username': 'hello2@example.xyz', + 'password': 'nope2' + } + ] + + container_registries_login(registries, containers_config_tls_verify=1) + + calls = [ + call( + container_registry_uri='registry.redhat.io', + container_registry_username='hello1@world.xyz', + container_registry_password='nope1', + container_registry_tls_verify=True, + containers_config_auth_file=None, + container_command_short_name=None + ), + call( + container_registry_uri='registry.internal.example.xyz', + container_registry_username='hello2@example.xyz', + container_registry_password='nope2', + container_registry_tls_verify=True, + containers_config_auth_file=None, + container_command_short_name=None + ) + ] + container_registry_login_mock.assert_has_calls(calls) + + @patch('ploigos_step_runner.utils.containers.container_registry_login') + def test_list_of_dicts_falsey(self, container_registry_login_mock): + registries = [ + { + 'uri': 'registry.redhat.io', + 'username': 'hello1@world.xyz', + 'password': 'nope1' + }, + { + 'uri': 'registry.internal.example.xyz', + 'username': 'hello2@example.xyz', + 'password': 'nope2' + } + ] + + container_registries_login(registries, containers_config_tls_verify=0) + + calls = [ + call( + container_registry_uri='registry.redhat.io', + container_registry_username='hello1@world.xyz', + container_registry_password='nope1', + container_registry_tls_verify=False, + containers_config_auth_file=None, + container_command_short_name=None + ), + call( + container_registry_uri='registry.internal.example.xyz', + container_registry_username='hello2@example.xyz', + container_registry_password='nope2', + container_registry_tls_verify=False, + containers_config_auth_file=None, + container_command_short_name=None + ) + ] + container_registry_login_mock.assert_has_calls(calls)