From db27171a0b7c17022bcc8869e36e0caf2b83d2a6 Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Wed, 4 Oct 2023 21:29:31 +0100 Subject: [PATCH] Run `pyupgrade` against the changed files from the merge-forward Signed-off-by: Pedro Algarvio --- salt/modules/schedule.py | 96 +++++++++---------- tests/integration/modules/test_ssh.py | 24 ++--- .../runners/state/orchestrate/test_events.py | 8 +- 3 files changed, 54 insertions(+), 74 deletions(-) diff --git a/salt/modules/schedule.py b/salt/modules/schedule.py index b8553bc6f78..50ec9846ba6 100644 --- a/salt/modules/schedule.py +++ b/salt/modules/schedule.py @@ -260,14 +260,12 @@ def purge(**kwargs): if "test" in kwargs and kwargs["test"]: ret["result"] = True - ret["comment"].append( - "Job: {} would be deleted from schedule.".format(name) - ) + ret["comment"].append(f"Job: {name} would be deleted from schedule.") else: if kwargs.get("offline"): del current_schedule[name] - ret["comment"].append("Deleted job: {} from schedule.".format(name)) + ret["comment"].append(f"Deleted job: {name} from schedule.") ret["changes"][name] = "removed" else: @@ -291,7 +289,7 @@ def purge(**kwargs): ret["result"] = True ret["changes"][name] = "removed" ret["comment"].append( - "Deleted job: {} from schedule.".format(name) + f"Deleted job: {name} from schedule." ) else: ret["comment"].append( @@ -342,7 +340,7 @@ def delete(name, **kwargs): """ ret = { - "comment": "Failed to delete job {} from schedule.".format(name), + "comment": f"Failed to delete job {name} from schedule.", "result": False, "changes": {}, } @@ -351,7 +349,7 @@ def delete(name, **kwargs): ret["comment"] = "Job name is required." if "test" in kwargs and kwargs["test"]: - ret["comment"] = "Job: {} would be deleted from schedule.".format(name) + ret["comment"] = f"Job: {name} would be deleted from schedule." ret["result"] = True else: if kwargs.get("offline"): @@ -380,7 +378,7 @@ def delete(name, **kwargs): ) ret["result"] = True - ret["comment"] = "Deleted Job {} from schedule.".format(name) + ret["comment"] = f"Deleted Job {name} from schedule." ret["changes"][name] = "removed" else: persist = kwargs.get("persist", True) @@ -405,7 +403,7 @@ def delete(name, **kwargs): "persist": False, } else: - ret["comment"] = "Job {} does not exist.".format(name) + ret["comment"] = f"Job {name} does not exist." return ret try: @@ -589,7 +587,7 @@ def add(name, **kwargs): """ ret = { - "comment": "Failed to add job {} to schedule.".format(name), + "comment": f"Failed to add job {name} to schedule.", "result": False, "changes": {}, } @@ -598,7 +596,7 @@ def add(name, **kwargs): ) if name in current_schedule: - ret["comment"] = "Job {} already exists in schedule.".format(name) + ret["comment"] = f"Job {name} already exists in schedule." ret["result"] = False return ret @@ -634,7 +632,7 @@ def add(name, **kwargs): schedule_data[name] = _new if "test" in kwargs and kwargs["test"]: - ret["comment"] = "Job: {} would be added to schedule.".format(name) + ret["comment"] = f"Job: {name} would be added to schedule." ret["result"] = True else: if kwargs.get("offline"): @@ -656,7 +654,7 @@ def add(name, **kwargs): ) ret["result"] = True - ret["comment"] = "Added job: {} to schedule.".format(name) + ret["comment"] = f"Added job: {name} to schedule." ret["changes"][name] = "added" else: try: @@ -733,7 +731,7 @@ def modify(name, **kwargs): ) if name not in current_schedule: - ret["comment"] = "Job {} does not exist in schedule.".format(name) + ret["comment"] = f"Job {name} does not exist in schedule." ret["result"] = False return ret @@ -756,7 +754,7 @@ def modify(name, **kwargs): return _new if _new == _current: - ret["comment"] = "Job {} in correct state".format(name) + ret["comment"] = f"Job {name} in correct state" return ret ret["changes"][name] = { @@ -765,7 +763,7 @@ def modify(name, **kwargs): } if "test" in kwargs and kwargs["test"]: - ret["comment"] = "Job: {} would be modified in schedule.".format(name) + ret["comment"] = f"Job: {name} would be modified in schedule." else: if kwargs.get("offline"): current_schedule[name].update(_new) @@ -786,7 +784,7 @@ def modify(name, **kwargs): ) ret["result"] = True - ret["comment"] = "Modified job: {} in schedule.".format(name) + ret["comment"] = f"Modified job: {name} in schedule." else: persist = kwargs.get("persist", True) @@ -808,9 +806,9 @@ def modify(name, **kwargs): out = __salt__["event.fire"](event_data, "manage_schedule") if out: - ret["comment"] = "Modified job: {} in schedule.".format(name) + ret["comment"] = f"Modified job: {name} in schedule." else: - ret["comment"] = "Failed to modify job {} in schedule.".format(name) + ret["comment"] = f"Failed to modify job {name} in schedule." ret["result"] = False return ret @@ -839,18 +837,18 @@ def run_job(name, force=False): if name in schedule: data = schedule[name] if "enabled" in data and not data["enabled"] and not force: - ret["comment"] = "Job {} is disabled.".format(name) + ret["comment"] = f"Job {name} is disabled." else: out = __salt__["event.fire"]( {"name": name, "func": "run_job"}, "manage_schedule" ) if out: - ret["comment"] = "Scheduling Job {} on minion.".format(name) + ret["comment"] = f"Scheduling Job {name} on minion." else: - ret["comment"] = "Failed to run job {} on minion.".format(name) + ret["comment"] = f"Failed to run job {name} on minion." ret["result"] = False else: - ret["comment"] = "Job {} does not exist.".format(name) + ret["comment"] = f"Job {name} does not exist." ret["result"] = False return ret @@ -873,7 +871,7 @@ def enable_job(name, **kwargs): ret["result"] = False if "test" in __opts__ and __opts__["test"]: - ret["comment"] = "Job: {} would be enabled in schedule.".format(name) + ret["comment"] = f"Job: {name} would be enabled in schedule." else: persist = kwargs.get("persist", True) @@ -887,7 +885,7 @@ def enable_job(name, **kwargs): "persist": False, } else: - ret["comment"] = "Job {} does not exist.".format(name) + ret["comment"] = f"Job {name} does not exist." ret["result"] = False return ret @@ -904,13 +902,11 @@ def enable_job(name, **kwargs): # check item exists in schedule and is enabled if name in schedule and schedule[name]["enabled"]: ret["result"] = True - ret["comment"] = "Enabled Job {} in schedule.".format(name) + ret["comment"] = f"Enabled Job {name} in schedule." ret["changes"][name] = "enabled" else: ret["result"] = False - ret[ - "comment" - ] = "Failed to enable job {} in schedule.".format(name) + ret["comment"] = f"Failed to enable job {name} in schedule." return ret except KeyError: # Effectively a no-op, since we can't really return without an event system @@ -936,7 +932,7 @@ def disable_job(name, **kwargs): ret["result"] = False if "test" in kwargs and kwargs["test"]: - ret["comment"] = "Job: {} would be disabled in schedule.".format(name) + ret["comment"] = f"Job: {name} would be disabled in schedule." else: persist = kwargs.get("persist", True) @@ -950,7 +946,7 @@ def disable_job(name, **kwargs): "persist": False, } else: - ret["comment"] = "Job {} does not exist.".format(name) + ret["comment"] = f"Job {name} does not exist." ret["result"] = False return ret @@ -967,13 +963,13 @@ def disable_job(name, **kwargs): # check item exists in schedule and is enabled if name in schedule and not schedule[name]["enabled"]: ret["result"] = True - ret["comment"] = "Disabled Job {} in schedule.".format(name) + ret["comment"] = f"Disabled Job {name} in schedule." ret["changes"][name] = "disabled" else: ret["result"] = False ret[ "comment" - ] = "Failed to disable job {} in schedule.".format(name) + ] = f"Failed to disable job {name} in schedule." return ret except KeyError: # Effectively a no-op, since we can't really return without an event system @@ -1138,9 +1134,7 @@ def reload_(): try: schedule = salt.utils.yaml.safe_load(fp_) except salt.utils.yaml.YAMLError as exc: - ret["comment"].append( - "Unable to read existing schedule file: {}".format(exc) - ) + ret["comment"].append(f"Unable to read existing schedule file: {exc}") if schedule: if "schedule" in schedule and schedule["schedule"]: @@ -1188,7 +1182,7 @@ def move(name, target, **kwargs): ret["result"] = False if "test" in kwargs and kwargs["test"]: - ret["comment"] = "Job: {} would be moved from schedule.".format(name) + ret["comment"] = f"Job: {name} would be moved from schedule." else: opts_schedule = list_(show_all=True, where="opts", return_yaml=False) pillar_schedule = list_(show_all=True, where="pillar", return_yaml=False) @@ -1200,13 +1194,13 @@ def move(name, target, **kwargs): schedule_data = pillar_schedule[name] where = "pillar" else: - ret["comment"] = "Job {} does not exist.".format(name) + ret["comment"] = f"Job {name} does not exist." ret["result"] = False return ret schedule_opts = [] for key, value in schedule_data.items(): - temp = "{}={}".format(key, value) + temp = f"{key}={value}" schedule_opts.append(temp) response = __salt__["publish.publish"](target, "schedule.add", schedule_opts) @@ -1229,7 +1223,7 @@ def move(name, target, **kwargs): else: delete(name, where=where) ret["result"] = True - ret["comment"] = "Moved Job {} from schedule.".format(name) + ret["comment"] = f"Moved Job {name} from schedule." ret["minions"] = minions return ret return ret @@ -1253,7 +1247,7 @@ def copy(name, target, **kwargs): ret["result"] = False if "test" in kwargs and kwargs["test"]: - ret["comment"] = "Job: {} would be copied from schedule.".format(name) + ret["comment"] = f"Job: {name} would be copied from schedule." else: opts_schedule = list_(show_all=True, where="opts", return_yaml=False) pillar_schedule = list_(show_all=True, where="pillar", return_yaml=False) @@ -1263,13 +1257,13 @@ def copy(name, target, **kwargs): elif name in pillar_schedule: schedule_data = pillar_schedule[name] else: - ret["comment"] = "Job {} does not exist.".format(name) + ret["comment"] = f"Job {name} does not exist." ret["result"] = False return ret schedule_opts = [] for key, value in schedule_data.items(): - temp = "{}={}".format(key, value) + temp = f"{key}={value}" schedule_opts.append(temp) response = __salt__["publish.publish"](target, "schedule.add", schedule_opts) @@ -1291,7 +1285,7 @@ def copy(name, target, **kwargs): return ret else: ret["result"] = True - ret["comment"] = "Copied Job {} from schedule to minion(s).".format(name) + ret["comment"] = f"Copied Job {name} from schedule to minion(s)." ret["minions"] = minions return ret return ret @@ -1354,7 +1348,7 @@ def postpone_job(name, current_time, new_time, **kwargs): return ret if "test" in __opts__ and __opts__["test"]: - ret["comment"] = "Job: {} would be postponed in schedule.".format(name) + ret["comment"] = f"Job: {name} would be postponed in schedule." else: if name in list_(show_all=True, where="opts", return_yaml=False): @@ -1375,7 +1369,7 @@ def postpone_job(name, current_time, new_time, **kwargs): "func": "postpone_job", } else: - ret["comment"] = "Job {} does not exist.".format(name) + ret["comment"] = f"Job {name} does not exist." ret["result"] = False return ret @@ -1399,7 +1393,7 @@ def postpone_job(name, current_time, new_time, **kwargs): ret["result"] = False ret[ "comment" - ] = "Failed to postpone job {} in schedule.".format(name) + ] = f"Failed to postpone job {name} in schedule." return ret except KeyError: # Effectively a no-op, since we can't really return without an event system @@ -1445,7 +1439,7 @@ def skip_job(name, current_time, **kwargs): return ret if "test" in __opts__ and __opts__["test"]: - ret["comment"] = "Job: {} would be skipped in schedule.".format(name) + ret["comment"] = f"Job: {name} would be skipped in schedule." else: if name in list_(show_all=True, where="opts", return_yaml=False): @@ -1464,7 +1458,7 @@ def skip_job(name, current_time, **kwargs): "func": "skip_job", } else: - ret["comment"] = "Job {} does not exist.".format(name) + ret["comment"] = f"Job {name} does not exist." ret["result"] = False return ret @@ -1486,9 +1480,7 @@ def skip_job(name, current_time, **kwargs): ) else: ret["result"] = False - ret[ - "comment" - ] = "Failed to skip job {} in schedule.".format(name) + ret["comment"] = f"Failed to skip job {name} in schedule." return ret except KeyError: # Effectively a no-op, since we can't really return without an event system diff --git a/tests/integration/modules/test_ssh.py b/tests/integration/modules/test_ssh.py index 55586211622..751a09db304 100644 --- a/tests/integration/modules/test_ssh.py +++ b/tests/integration/modules/test_ssh.py @@ -86,9 +86,7 @@ class SSHModuleTest(ModuleCase): ) self.assertEqual(key_data["fingerprint"], GITHUB_FINGERPRINT) except AssertionError as exc: - raise AssertionError( - "AssertionError: {}. Function returned: {}".format(exc, ret) - ) + raise AssertionError(f"AssertionError: {exc}. Function returned: {ret}") @pytest.mark.slow_test def test_bad_enctype(self): @@ -125,9 +123,7 @@ class SSHModuleTest(ModuleCase): self.assertEqual(ret["key"], self.key) self.assertEqual(ret["fingerprint"], GITHUB_FINGERPRINT) except AssertionError as exc: - raise AssertionError( - "AssertionError: {}. Function returned: {}".format(exc, ret) - ) + raise AssertionError(f"AssertionError: {exc}. Function returned: {ret}") @pytest.mark.skip_on_photonos( reason="Skip on PhotonOS. Attempting to receive the SSH key from Github, using RSA keys which are disabled.", @@ -146,9 +142,7 @@ class SSHModuleTest(ModuleCase): self.assertEqual(ret[0]["key"], self.key) self.assertEqual(ret[0]["fingerprint"], GITHUB_FINGERPRINT) except AssertionError as exc: - raise AssertionError( - "AssertionError: {}. Function returned: {}".format(exc, ret) - ) + raise AssertionError(f"AssertionError: {exc}. Function returned: {ret}") @pytest.mark.slow_test def test_check_known_host_add(self): @@ -239,9 +233,7 @@ class SSHModuleTest(ModuleCase): self.assertEqual(ret["old"], None) self.assertEqual(ret["new"][0]["fingerprint"], GITHUB_FINGERPRINT) except AssertionError as exc: - raise AssertionError( - "AssertionError: {}. Function returned: {}".format(exc, ret) - ) + raise AssertionError(f"AssertionError: {exc}. Function returned: {ret}") # check that item does exist ret = self.run_function( "ssh.get_known_host_entries", @@ -251,9 +243,7 @@ class SSHModuleTest(ModuleCase): try: self.assertEqual(ret["fingerprint"], GITHUB_FINGERPRINT) except AssertionError as exc: - raise AssertionError( - "AssertionError: {}. Function returned: {}".format(exc, ret) - ) + raise AssertionError(f"AssertionError: {exc}. Function returned: {ret}") # add the same item once again ret = self.run_function( "ssh.set_known_host", ["root", "github.com"], config=self.known_hosts @@ -261,6 +251,4 @@ class SSHModuleTest(ModuleCase): try: self.assertEqual(ret["status"], "exists") except AssertionError as exc: - raise AssertionError( - "AssertionError: {}. Function returned: {}".format(exc, ret) - ) + raise AssertionError(f"AssertionError: {exc}. Function returned: {ret}") diff --git a/tests/pytests/integration/runners/state/orchestrate/test_events.py b/tests/pytests/integration/runners/state/orchestrate/test_events.py index 5ebaf500765..b1cde89ba73 100644 --- a/tests/pytests/integration/runners/state/orchestrate/test_events.py +++ b/tests/pytests/integration/runners/state/orchestrate/test_events.py @@ -170,7 +170,7 @@ def test_jid_in_ret_event(salt_run_cli, salt_master, salt_minion, event_listener for step_data in orch_job_data["data"][salt_master.id].values(): assert "__jid__" in step_data - expected_event_tag = "salt/run/{}/ret".format(jid) + expected_event_tag = f"salt/run/{jid}/ret" event_pattern = (salt_master.id, expected_event_tag) matched_events = event_listener.wait_for_events( @@ -178,7 +178,7 @@ def test_jid_in_ret_event(salt_run_cli, salt_master, salt_minion, event_listener ) assert ( matched_events.found_all_events - ), "Failed to receive the event with the tag '{}'".format(expected_event_tag) + ), f"Failed to receive the event with the tag '{expected_event_tag}'" for event in matched_events.matches: for job_data in event.data["return"]["data"][salt_master.id].values(): assert "__jid__" in job_data @@ -237,7 +237,7 @@ def test_parallel_orchestrations( assert duration > 20 assert duration < 19 * 10 / 2 - expected_event_tag = "salt/run/{}/ret".format(jid) + expected_event_tag = f"salt/run/{jid}/ret" event_pattern = (salt_master.id, expected_event_tag) matched_events = event_listener.wait_for_events( @@ -245,7 +245,7 @@ def test_parallel_orchestrations( ) assert ( matched_events.found_all_events - ), "Failed to receive the event with the tag '{}'".format(expected_event_tag) + ), f"Failed to receive the event with the tag '{expected_event_tag}'" for event in matched_events.matches: for job_data in event.data["return"]["data"][salt_master.id].values(): # we expect each duration to be greater than 10s