From 30690637b5636565592d4ec3210cc615098fc3c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Paw=C5=82owski?= Date: Fri, 14 Apr 2023 17:35:38 +0200 Subject: [PATCH] Return error if patch file passed to state file.patch is malformed If patch file provided for file.patch state is malformed then state returns `Patch was already applied` but patch is not applied. ID: patch_example Function: file.patch Name: /tmp/example Result: True Comment: Patch was already applied Started: 12:20:50.953163 Duration: 61.558 ms Changes: It is better to return error in such case. ID: patch_example Function: file.patch Name: /tmp/example Result: False Comment: /usr/bin/patch: **** malformed patch at line 7: Started: 12:33:44.915605 Duration: 59.202 ms Changes: --- changelog/59806.fixed.md | 1 + salt/states/file.py | 4 ++++ tests/pytests/integration/states/test_file.py | 3 ++- 3 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 changelog/59806.fixed.md diff --git a/changelog/59806.fixed.md b/changelog/59806.fixed.md new file mode 100644 index 000000000000..2cca505c2bfc --- /dev/null +++ b/changelog/59806.fixed.md @@ -0,0 +1 @@ +Return error if patch file passed to state file.patch is malformed. diff --git a/salt/states/file.py b/salt/states/file.py index bce54092a0f6..d591ae9d8dc2 100644 --- a/salt/states/file.py +++ b/salt/states/file.py @@ -7263,6 +7263,10 @@ def _patch(patch_file, options=None, dry_run=False): pre_check = _patch(patch_file, patch_opts) if pre_check["retcode"] != 0: + if not os.path.exists(patch_rejects) or os.path.getsize(patch_rejects) == 0: + ret["comment"] = pre_check["stderr"] + ret["result"] = False + return ret # Try to reverse-apply hunks from rejects file using a dry-run. # If this returns a retcode of 0, we know that the patch was # already applied. Rejects are written from the base of the diff --git a/tests/pytests/integration/states/test_file.py b/tests/pytests/integration/states/test_file.py index a84d2f4797e5..cd64fc918b65 100644 --- a/tests/pytests/integration/states/test_file.py +++ b/tests/pytests/integration/states/test_file.py @@ -929,6 +929,7 @@ def test_patch_directory_template( - source: {all_patch_template} - template: "jinja" - context: {context} + - strip: 1 """.format( base_dir=tmp_path, all_patch_template=all_patch_template, context=context ) @@ -945,7 +946,7 @@ def test_patch_directory_template( # Check to make sure the patch was applied okay state_run = next(iter(ret.data.values())) assert state_run["result"] is True - assert state_run["comment"] == "Patch was already applied" + assert state_run["comment"] == "Patch successfully applied" # Re-run the state, should succeed and there should be a message about # a partially-applied hunk.