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

[BUG] unused assignment in file.patch #57204

Closed
OrangeDog opened this issue May 11, 2020 · 5 comments · Fixed by #60226
Closed

[BUG] unused assignment in file.patch #57204

OrangeDog opened this issue May 11, 2020 · 5 comments · Fixed by #60226
Labels
Bug broken, incorrect, or confusing behavior Confirmed Salt engineer has confirmed bug/feature - often including a MCVE Core relates to code central or existential to Salt severity-medium 3rd level, incorrect or bad functionality, confusing and lacks a work around State-Module
Milestone

Comments

@OrangeDog
Copy link
Contributor

Description
The state file.patch documents an options argument.

Extra options to pass to patch. This should not be necessary in most cases.

However, the code never uses them. It sanitises them and reports errors, but the variable is unused after this line: https://github.com/saltstack/salt/blob/v3000.2/salt/states/file.py#L6192

options = sanitized_options
@OrangeDog OrangeDog added the Bug broken, incorrect, or confusing behavior label May 11, 2020
@DmitryKuzmenko
Copy link
Contributor

@OrangeDog I see after the line the options argument is used in the inline function _patch here
https://github.com/saltstack/salt/blob/v3000.2/salt/states/file.py#L6263
The function is used next to pre-check and apply the state.
Could you please provide an example where the options aren't passed to the file.patch execution module function?

@DmitryKuzmenko DmitryKuzmenko added the info-needed waiting for more info label May 12, 2020
@DmitryKuzmenko DmitryKuzmenko added this to the Blocked milestone May 12, 2020
@OrangeDog
Copy link
Contributor Author

@DmitryKuzmenko read more carefully. That's not the same options. It's a new parameter of the _patch function.

You can trivially construct any example:

patch example:
  - file.patch:
    - name: /path
    - source: salt://foo.patch
    - options: [--backup]

The option won't be used, and no backups will be made.

@DmitryKuzmenko
Copy link
Contributor

@OrangeDog good catch. You're right, thank you.

@DmitryKuzmenko DmitryKuzmenko added Confirmed Salt engineer has confirmed bug/feature - often including a MCVE Core relates to code central or existential to Salt severity-medium 3rd level, incorrect or bad functionality, confusing and lacks a work around P3 Priority 3 State-Module and removed info-needed waiting for more info labels May 13, 2020
@DmitryKuzmenko DmitryKuzmenko modified the milestones: Blocked, Approved May 13, 2020
@sagetherage sagetherage removed the P3 Priority 3 label Jun 3, 2020
@piterpunk
Copy link

I made this state to test this issue:

patch_tst:
  file.patch:
    - name: /srv/salt/filepatch/file
    - source: salt://filepatch/patch.diff
    - options: [--backup]

Then I executed the state.apply:

local:
----------
          ID: patch_tst
    Function: file.patch
        Name: /srv/salt/filepatch/file
      Result: True
     Comment: Patch successfully applied
     Started: 23:29:17.419073
    Duration: 358.606 ms
     Changes:   
              ----------
              pid:
                  20581
              retcode:
                  0
              stderr:
              stdout:
                  patching file /srv/salt/filepatch/file

Summary for local
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time: 358.606 ms

During execution, I could see the '--backup' option passed to 'patch' command:

[DEBUG   ] file.managed: {'changes': {'diff': '--- \n+++ \n@@ -0,0 +1,5 @@\n+--- file.orig\t2020-07-15 23:24:07.722616027 -0300\n++++ file.dest\t2020-07-15 23:24:29.290689009 -0300\n+@@ -1 +1 @@\n+-THIS IS THE ORIGINAL FILE\n++THIS IS THE PATCHED FILE\n'}, 'comment': 'File /tmp/__salt.tmp.fbzi3q53 updated', 'name': '/tmp/__salt.tmp.fbzi3q53', 'result': True}
[DEBUG   ] LazyLoaded cmd.run_all
[INFO    ] Executing command ['/usr/bin/patch', '--backup', '-N', '-r', '/tmp/__salt.tmp.kcisx1x0', '-o', '/tmp/__salt.tmp.gfnpreju', '-i', '/tmp/__salt.tmp.fbzi3q53', '/srv/salt/filepatch/file'] in directory '/root'
[DEBUG   ] stdout: patching file /tmp/__salt.tmp.gfnpreju (read from /srv/salt/filepatch/file)
[INFO    ] Executing command ['/usr/bin/patch', '--backup', '--forward', '--reject-file=-', '-i', '/tmp/__salt.tmp.fbzi3q53', '/srv/salt/filepatch/file'] in directory '/root'

And the backup file was created as expected:

-rw-r--r-- 1 root root 25 Jul 15 23:29 file
-rw-r--r-- 1 root root 26 Jul 15 23:25 file.orig

The code, indeed, doesn't use the "options" variable after setting "options = sanitized_options", but uses the original "sanitized_options":

        def _patch(patch_file, options=None, dry_run=False):
            patch_opts = copy.copy(sanitized_options)
            if options is not None:
                patch_opts.extend(options)
            return __salt__["file.patch"](
                name, patch_file, options=patch_opts, dry_run=dry_run
            )

Am I missing something?

@OrangeDog
Copy link
Contributor Author

@piterpunk hmm, you are correct. Not sure how Dmitry and I convinced ourselves otherwise.

I think all that needs doing is remove the options = sanitized_options line to prevent future confusion.

@OrangeDog OrangeDog changed the title [BUG] file.patch ignores all options [BUG] unused assignment in file.patch Nov 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug broken, incorrect, or confusing behavior Confirmed Salt engineer has confirmed bug/feature - often including a MCVE Core relates to code central or existential to Salt severity-medium 3rd level, incorrect or bad functionality, confusing and lacks a work around State-Module
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants