Reformat everything.

This commit is contained in:
Felix Fontein
2025-11-01 12:08:41 +01:00
parent 3f2213791a
commit 340ff8586d
1008 changed files with 61301 additions and 58309 deletions

View File

@@ -28,10 +28,11 @@ def patch_rhsm_repository(mocker):
"""
Function used for mocking some parts of rhsm_repository module
"""
mocker.patch('ansible_collections.community.general.plugins.modules.rhsm_repository.AnsibleModule.get_bin_path',
return_value='/testbin/subscription-manager')
mocker.patch('ansible_collections.community.general.plugins.modules.rhsm_repository.os.getuid',
return_value=0)
mocker.patch(
"ansible_collections.community.general.plugins.modules.rhsm_repository.AnsibleModule.get_bin_path",
return_value="/testbin/subscription-manager",
)
mocker.patch("ansible_collections.community.general.plugins.modules.rhsm_repository.os.getuid", return_value=0)
class Repos:
@@ -79,8 +80,8 @@ Enabled: %s
def _set_status(self, repo_id, status):
for repo in self.repos:
if fnmatch.fnmatch(repo['id'], repo_id):
repo['enabled'] = status
if fnmatch.fnmatch(repo["id"], repo_id):
repo["enabled"] = status
def enable(self, repo_ids):
"""
@@ -113,13 +114,9 @@ Enabled: %s
return self
def _filter_by_status(self, filter, status):
return [
repo['id']
for repo in self.repos
if repo['enabled'] == status and fnmatch.fnmatch(repo['id'], filter)
]
return [repo["id"] for repo in self.repos if repo["enabled"] == status and fnmatch.fnmatch(repo["id"], filter)]
def ids_enabled(self, filter='*'):
def ids_enabled(self, filter="*"):
"""
Get a list with the enabled repositories.
@@ -127,7 +124,7 @@ Enabled: %s
"""
return self._filter_by_status(filter, True)
def ids_disabled(self, filter='*'):
def ids_disabled(self, filter="*"):
"""
Get a list with the disabled repositories.
@@ -244,9 +241,9 @@ REPOS_LIST_OUTPUT = REPOS.to_subman_list_output()
# MUST match what's in the Rhsm class in the module.
SUBMAN_KWARGS = {
'environ_update': dict(LANG='C', LC_ALL='C', LC_MESSAGES='C'),
'expand_user_and_vars': False,
'use_unsafe_shell': False,
"environ_update": dict(LANG="C", LC_ALL="C", LC_MESSAGES="C"),
"expand_user_and_vars": False,
"use_unsafe_shell": False,
}
@@ -254,527 +251,529 @@ TEST_CASES = [
# enable a disabled repository
[
{
'name': 'awesomeos-1000000000000023',
"name": "awesomeos-1000000000000023",
},
{
'id': 'test_enable_single',
'run_command.calls': [
"id": "test_enable_single",
"run_command.calls": [
(
[
'/testbin/subscription-manager',
'repos',
'--list',
"/testbin/subscription-manager",
"repos",
"--list",
],
SUBMAN_KWARGS,
(0, REPOS_LIST_OUTPUT, '')
(0, REPOS_LIST_OUTPUT, ""),
),
(
[
'/testbin/subscription-manager',
'repos',
'--enable',
'awesomeos-1000000000000023',
"/testbin/subscription-manager",
"repos",
"--enable",
"awesomeos-1000000000000023",
],
SUBMAN_KWARGS,
(0, '', '')
(0, "", ""),
),
],
'changed': True,
'repositories': REPOS.copy().enable('awesomeos-1000000000000023'),
}
"changed": True,
"repositories": REPOS.copy().enable("awesomeos-1000000000000023"),
},
],
# enable an already enabled repository
[
{
'name': 'fake-content-38072',
"name": "fake-content-38072",
},
{
'id': 'test_enable_already_enabled',
'run_command.calls': [
"id": "test_enable_already_enabled",
"run_command.calls": [
(
[
'/testbin/subscription-manager',
'repos',
'--list',
"/testbin/subscription-manager",
"repos",
"--list",
],
SUBMAN_KWARGS,
(0, REPOS_LIST_OUTPUT, '')
(0, REPOS_LIST_OUTPUT, ""),
),
],
'changed': False,
'repositories': REPOS.copy(),
}
"changed": False,
"repositories": REPOS.copy(),
},
],
# enable two disabled repositories
[
{
'name': ['awesomeos-1000000000000023', 'content-label-no-gpg-32060'],
"name": ["awesomeos-1000000000000023", "content-label-no-gpg-32060"],
},
{
'id': 'test_enable_multiple',
'run_command.calls': [
"id": "test_enable_multiple",
"run_command.calls": [
(
[
'/testbin/subscription-manager',
'repos',
'--list',
"/testbin/subscription-manager",
"repos",
"--list",
],
SUBMAN_KWARGS,
(0, REPOS_LIST_OUTPUT, '')
(0, REPOS_LIST_OUTPUT, ""),
),
(
[
'/testbin/subscription-manager',
'repos',
'--enable',
'awesomeos-1000000000000023',
'--enable',
'content-label-no-gpg-32060',
"/testbin/subscription-manager",
"repos",
"--enable",
"awesomeos-1000000000000023",
"--enable",
"content-label-no-gpg-32060",
],
SUBMAN_KWARGS,
(0, '', '')
(0, "", ""),
),
],
'changed': True,
'repositories': REPOS.copy().enable('awesomeos-1000000000000023').enable('content-label-no-gpg-32060'),
}
"changed": True,
"repositories": REPOS.copy().enable("awesomeos-1000000000000023").enable("content-label-no-gpg-32060"),
},
],
# enable two repositories, one disabled and one already enabled
[
{
'name': ['awesomeos-1000000000000023', 'fake-content-38072'],
"name": ["awesomeos-1000000000000023", "fake-content-38072"],
},
{
'id': 'test_enable_multiple_mixed',
'run_command.calls': [
"id": "test_enable_multiple_mixed",
"run_command.calls": [
(
[
'/testbin/subscription-manager',
'repos',
'--list',
"/testbin/subscription-manager",
"repos",
"--list",
],
SUBMAN_KWARGS,
(0, REPOS_LIST_OUTPUT, '')
(0, REPOS_LIST_OUTPUT, ""),
),
(
[
'/testbin/subscription-manager',
'repos',
'--enable',
'awesomeos-1000000000000023',
'--enable',
'fake-content-38072',
"/testbin/subscription-manager",
"repos",
"--enable",
"awesomeos-1000000000000023",
"--enable",
"fake-content-38072",
],
SUBMAN_KWARGS,
(0, '', '')
(0, "", ""),
),
],
'changed': True,
'repositories': REPOS.copy().enable('awesomeos-1000000000000023'),
}
"changed": True,
"repositories": REPOS.copy().enable("awesomeos-1000000000000023"),
},
],
# purge everything but never-enabled-content-801 (disabled)
[
{
'name': 'never-enabled-content-801',
'purge': True,
"name": "never-enabled-content-801",
"purge": True,
},
{
'id': 'test_purge_everything_but_one_disabled',
'run_command.calls': [
"id": "test_purge_everything_but_one_disabled",
"run_command.calls": [
(
[
'/testbin/subscription-manager',
'repos',
'--list',
"/testbin/subscription-manager",
"repos",
"--list",
],
SUBMAN_KWARGS,
(0, REPOS_LIST_OUTPUT, '')
(0, REPOS_LIST_OUTPUT, ""),
),
(
[
'/testbin/subscription-manager',
'repos',
'--enable',
'never-enabled-content-801',
] + flatten([['--disable', i] for i in REPOS.ids_enabled() if i != 'never-enabled-content-801']),
"/testbin/subscription-manager",
"repos",
"--enable",
"never-enabled-content-801",
]
+ flatten([["--disable", i] for i in REPOS.ids_enabled() if i != "never-enabled-content-801"]),
SUBMAN_KWARGS,
(0, '', '')
(0, "", ""),
),
],
'changed': True,
'repositories': REPOS.copy().disable('*').enable('never-enabled-content-801'),
}
"changed": True,
"repositories": REPOS.copy().disable("*").enable("never-enabled-content-801"),
},
],
# purge everything but awesomeos-99000 (already enabled)
[
{
'name': 'awesomeos-99000',
'purge': True,
"name": "awesomeos-99000",
"purge": True,
},
{
'id': 'test_purge_everything_but_one_enabled',
'run_command.calls': [
"id": "test_purge_everything_but_one_enabled",
"run_command.calls": [
(
[
'/testbin/subscription-manager',
'repos',
'--list',
"/testbin/subscription-manager",
"repos",
"--list",
],
SUBMAN_KWARGS,
(0, REPOS_LIST_OUTPUT, '')
(0, REPOS_LIST_OUTPUT, ""),
),
(
[
'/testbin/subscription-manager',
'repos',
'--enable',
'awesomeos-99000',
'--disable',
'content-label-27060',
'--disable',
'awesomeos-x86_64-99000',
'--disable',
'fake-content-38072',
"/testbin/subscription-manager",
"repos",
"--enable",
"awesomeos-99000",
"--disable",
"content-label-27060",
"--disable",
"awesomeos-x86_64-99000",
"--disable",
"fake-content-38072",
],
SUBMAN_KWARGS,
(0, '', '')
(0, "", ""),
),
],
'changed': True,
'repositories': REPOS.copy().disable('*').enable('awesomeos-99000'),
}
"changed": True,
"repositories": REPOS.copy().disable("*").enable("awesomeos-99000"),
},
],
# enable everything, then purge everything but content-label-27060
[
{
'name': 'content-label-27060',
'purge': True,
"name": "content-label-27060",
"purge": True,
},
{
'id': 'test_enable_everything_purge_everything_but_one_enabled',
'run_command.calls': [
"id": "test_enable_everything_purge_everything_but_one_enabled",
"run_command.calls": [
(
[
'/testbin/subscription-manager',
'repos',
'--list',
"/testbin/subscription-manager",
"repos",
"--list",
],
SUBMAN_KWARGS,
(0, REPOS.copy().enable('*').to_subman_list_output(), '')
(0, REPOS.copy().enable("*").to_subman_list_output(), ""),
),
(
[
'/testbin/subscription-manager',
'repos',
'--enable',
'content-label-27060',
'--disable',
'never-enabled-content-801',
'--disable',
'never-enabled-content-100000000000060',
'--disable',
'awesomeos-x86_64-1000000000000023',
'--disable',
'awesomeos-ppc64-100000000000011',
'--disable',
'awesomeos-99000',
'--disable',
'content-label-no-gpg-32060',
'--disable',
'awesomeos-1000000000000023',
'--disable',
'awesomeos-x86-100000000000020',
'--disable',
'awesomeos-x86_64-99000',
'--disable',
'awesomeos-s390x-99000',
'--disable',
'awesomeos-modifier-37080',
'--disable',
'awesomeos-i686-99000',
'--disable',
'fake-content-38072',
"/testbin/subscription-manager",
"repos",
"--enable",
"content-label-27060",
"--disable",
"never-enabled-content-801",
"--disable",
"never-enabled-content-100000000000060",
"--disable",
"awesomeos-x86_64-1000000000000023",
"--disable",
"awesomeos-ppc64-100000000000011",
"--disable",
"awesomeos-99000",
"--disable",
"content-label-no-gpg-32060",
"--disable",
"awesomeos-1000000000000023",
"--disable",
"awesomeos-x86-100000000000020",
"--disable",
"awesomeos-x86_64-99000",
"--disable",
"awesomeos-s390x-99000",
"--disable",
"awesomeos-modifier-37080",
"--disable",
"awesomeos-i686-99000",
"--disable",
"fake-content-38072",
],
SUBMAN_KWARGS,
(0, '', '')
(0, "", ""),
),
],
'changed': True,
'repositories': REPOS.copy().disable('*').enable('content-label-27060'),
}
"changed": True,
"repositories": REPOS.copy().disable("*").enable("content-label-27060"),
},
],
# enable all awesomeos-*
[
{
'name': 'awesomeos-*',
"name": "awesomeos-*",
},
{
'id': 'test_enable_all_awesomeos_star',
'run_command.calls': [
"id": "test_enable_all_awesomeos_star",
"run_command.calls": [
(
[
'/testbin/subscription-manager',
'repos',
'--list',
"/testbin/subscription-manager",
"repos",
"--list",
],
SUBMAN_KWARGS,
(0, REPOS_LIST_OUTPUT, '')
(0, REPOS_LIST_OUTPUT, ""),
),
(
[
'/testbin/subscription-manager',
'repos',
'--enable',
'awesomeos-x86_64-1000000000000023',
'--enable',
'awesomeos-ppc64-100000000000011',
'--enable',
'awesomeos-99000',
'--enable',
'awesomeos-1000000000000023',
'--enable',
'awesomeos-x86-100000000000020',
'--enable',
'awesomeos-x86_64-99000',
'--enable',
'awesomeos-s390x-99000',
'--enable',
'awesomeos-modifier-37080',
'--enable',
'awesomeos-i686-99000',
"/testbin/subscription-manager",
"repos",
"--enable",
"awesomeos-x86_64-1000000000000023",
"--enable",
"awesomeos-ppc64-100000000000011",
"--enable",
"awesomeos-99000",
"--enable",
"awesomeos-1000000000000023",
"--enable",
"awesomeos-x86-100000000000020",
"--enable",
"awesomeos-x86_64-99000",
"--enable",
"awesomeos-s390x-99000",
"--enable",
"awesomeos-modifier-37080",
"--enable",
"awesomeos-i686-99000",
],
SUBMAN_KWARGS,
(0, '', '')
(0, "", ""),
),
],
'changed': True,
'repositories': REPOS.copy().enable('awesomeos-*'),
}
"changed": True,
"repositories": REPOS.copy().enable("awesomeos-*"),
},
],
# purge everything but awesomeos-*
[
{
'name': REPOS.ids_enabled('awesomeos-*'),
'purge': True,
"name": REPOS.ids_enabled("awesomeos-*"),
"purge": True,
},
{
'id': 'test_purge_everything_but_awesomeos_list',
'run_command.calls': [
"id": "test_purge_everything_but_awesomeos_list",
"run_command.calls": [
(
[
'/testbin/subscription-manager',
'repos',
'--list',
"/testbin/subscription-manager",
"repos",
"--list",
],
SUBMAN_KWARGS,
(0, REPOS_LIST_OUTPUT, '')
(0, REPOS_LIST_OUTPUT, ""),
),
(
[
'/testbin/subscription-manager',
'repos',
'--enable',
'awesomeos-99000',
'--enable',
'awesomeos-x86_64-99000',
'--disable',
'content-label-27060',
'--disable',
'fake-content-38072',
"/testbin/subscription-manager",
"repos",
"--enable",
"awesomeos-99000",
"--enable",
"awesomeos-x86_64-99000",
"--disable",
"content-label-27060",
"--disable",
"fake-content-38072",
],
SUBMAN_KWARGS,
(0, '', '')
(0, "", ""),
),
],
'changed': True,
'repositories': REPOS.copy().disable('*').enable(REPOS.ids_enabled('awesomeos-*')),
}
"changed": True,
"repositories": REPOS.copy().disable("*").enable(REPOS.ids_enabled("awesomeos-*")),
},
],
# enable a repository that does not exist
[
{
'name': 'repo-that-does-not-exist',
"name": "repo-that-does-not-exist",
},
{
'id': 'test_enable_nonexisting',
'run_command.calls': [
"id": "test_enable_nonexisting",
"run_command.calls": [
(
[
'/testbin/subscription-manager',
'repos',
'--list',
"/testbin/subscription-manager",
"repos",
"--list",
],
SUBMAN_KWARGS,
(0, REPOS_LIST_OUTPUT, '')
(0, REPOS_LIST_OUTPUT, ""),
),
],
'failed': True,
'msg': 'repo-that-does-not-exist is not a valid repository ID',
}
"failed": True,
"msg": "repo-that-does-not-exist is not a valid repository ID",
},
],
# disable an enabled repository
[
{
'name': 'awesomeos-99000',
'state': 'disabled',
"name": "awesomeos-99000",
"state": "disabled",
},
{
'id': 'test_disable_single',
'run_command.calls': [
"id": "test_disable_single",
"run_command.calls": [
(
[
'/testbin/subscription-manager',
'repos',
'--list',
"/testbin/subscription-manager",
"repos",
"--list",
],
SUBMAN_KWARGS,
(0, REPOS_LIST_OUTPUT, '')
(0, REPOS_LIST_OUTPUT, ""),
),
(
[
'/testbin/subscription-manager',
'repos',
'--disable',
'awesomeos-99000',
"/testbin/subscription-manager",
"repos",
"--disable",
"awesomeos-99000",
],
SUBMAN_KWARGS,
(0, '', '')
(0, "", ""),
),
],
'changed': True,
'repositories': REPOS.copy().disable('awesomeos-99000'),
}
"changed": True,
"repositories": REPOS.copy().disable("awesomeos-99000"),
},
],
# disable an already disabled repository
[
{
'name': 'never-enabled-content-801',
'state': 'disabled',
"name": "never-enabled-content-801",
"state": "disabled",
},
{
'id': 'test_disable_already_disabled',
'run_command.calls': [
"id": "test_disable_already_disabled",
"run_command.calls": [
(
[
'/testbin/subscription-manager',
'repos',
'--list',
"/testbin/subscription-manager",
"repos",
"--list",
],
SUBMAN_KWARGS,
(0, REPOS_LIST_OUTPUT, '')
(0, REPOS_LIST_OUTPUT, ""),
),
],
'changed': False,
'repositories': REPOS.copy(),
}
"changed": False,
"repositories": REPOS.copy(),
},
],
# disable an already disabled repository, and purge
[
{
'name': 'never-enabled-content-801',
'state': 'disabled',
'purge': True,
"name": "never-enabled-content-801",
"state": "disabled",
"purge": True,
},
{
'id': 'test_disable_already_disabled_and_purge',
'run_command.calls': [
"id": "test_disable_already_disabled_and_purge",
"run_command.calls": [
(
[
'/testbin/subscription-manager',
'repos',
'--list',
"/testbin/subscription-manager",
"repos",
"--list",
],
SUBMAN_KWARGS,
(0, REPOS_LIST_OUTPUT, '')
(0, REPOS_LIST_OUTPUT, ""),
),
(
[
'/testbin/subscription-manager',
'repos',
] + flatten([['--disable', i] for i in REPOS.ids_enabled()]),
"/testbin/subscription-manager",
"repos",
]
+ flatten([["--disable", i] for i in REPOS.ids_enabled()]),
SUBMAN_KWARGS,
(0, '', '')
(0, "", ""),
),
],
'changed': True,
'repositories': REPOS.copy().disable('*'),
}
"changed": True,
"repositories": REPOS.copy().disable("*"),
},
],
# disable an enabled repository, and purge
[
{
'name': 'awesomeos-99000',
'state': 'disabled',
'purge': True,
"name": "awesomeos-99000",
"state": "disabled",
"purge": True,
},
{
'id': 'test_disable_single_and_purge',
'run_command.calls': [
"id": "test_disable_single_and_purge",
"run_command.calls": [
(
[
'/testbin/subscription-manager',
'repos',
'--list',
"/testbin/subscription-manager",
"repos",
"--list",
],
SUBMAN_KWARGS,
(0, REPOS_LIST_OUTPUT, '')
(0, REPOS_LIST_OUTPUT, ""),
),
(
[
'/testbin/subscription-manager',
'repos',
] + flatten([['--disable', i] for i in REPOS.ids_enabled()]),
"/testbin/subscription-manager",
"repos",
]
+ flatten([["--disable", i] for i in REPOS.ids_enabled()]),
SUBMAN_KWARGS,
(0, '', '')
(0, "", ""),
),
],
'changed': True,
'repositories': REPOS.copy().disable('*'),
}
"changed": True,
"repositories": REPOS.copy().disable("*"),
},
],
# disable a repository that does not exist
[
{
'name': 'repo-that-does-not-exist',
'state': 'disabled',
"name": "repo-that-does-not-exist",
"state": "disabled",
},
{
'id': 'test_disable_nonexisting',
'run_command.calls': [
"id": "test_disable_nonexisting",
"run_command.calls": [
(
[
'/testbin/subscription-manager',
'repos',
'--list',
"/testbin/subscription-manager",
"repos",
"--list",
],
SUBMAN_KWARGS,
(0, REPOS_LIST_OUTPUT, '')
(0, REPOS_LIST_OUTPUT, ""),
),
],
'failed': True,
'msg': 'repo-that-does-not-exist is not a valid repository ID',
}
"failed": True,
"msg": "repo-that-does-not-exist is not a valid repository ID",
},
],
]
TEST_CASES_IDS: list[str] = [item[1]['id'] for item in TEST_CASES] # type: ignore
TEST_CASES_IDS: list[str] = [item[1]["id"] for item in TEST_CASES] # type: ignore
@pytest.mark.parametrize('patch_ansible_module, testcase', TEST_CASES, ids=TEST_CASES_IDS, indirect=['patch_ansible_module'])
@pytest.mark.usefixtures('patch_ansible_module')
@pytest.mark.parametrize(
"patch_ansible_module, testcase", TEST_CASES, ids=TEST_CASES_IDS, indirect=["patch_ansible_module"]
)
@pytest.mark.usefixtures("patch_ansible_module")
def test_rhsm_repository(mocker, capfd, patch_rhsm_repository, testcase):
"""
Run unit tests for test cases listen in TEST_CASES
"""
# Mock function used for running commands first
call_results = [item[2] for item in testcase['run_command.calls']]
mock_run_command = mocker.patch.object(
basic.AnsibleModule,
'run_command',
side_effect=call_results)
call_results = [item[2] for item in testcase["run_command.calls"]]
mock_run_command = mocker.patch.object(basic.AnsibleModule, "run_command", side_effect=call_results)
# Try to run test case
with pytest.raises(SystemExit):
@@ -783,15 +782,15 @@ def test_rhsm_repository(mocker, capfd, patch_rhsm_repository, testcase):
out, err = capfd.readouterr()
results = json.loads(out)
if 'failed' in testcase:
assert results['failed'] == testcase['failed']
assert results['msg'] == testcase['msg']
if "failed" in testcase:
assert results["failed"] == testcase["failed"]
assert results["msg"] == testcase["msg"]
else:
assert 'changed' in results
assert results['changed'] == testcase['changed']
assert results['repositories'] == testcase['repositories'].to_list()
assert "changed" in results
assert results["changed"] == testcase["changed"]
assert results["repositories"] == testcase["repositories"].to_list()
assert basic.AnsibleModule.run_command.call_count == len(testcase['run_command.calls'])
assert basic.AnsibleModule.run_command.call_count == len(testcase["run_command.calls"])
# FIXME ideally we need also to compare the actual calls with the expected
# ones; the problem is that the module uses a dict to collect the repositories
# to enable and disable, so the order of the --enable/--disable parameters to