Skip to content

states.file.managed returns empty changes dict in test mode #45895

Closed

Description

@amendlik

Description of Issue/Question

The file.managed state function often returns an empty changes dict when run with test=true. In fact, the only time a non-empty changes dict is returned is when there will be a change to the file content. All other managed changes (e.g. owner, group, mode) will return an empty changes dict, which causes several requisites to misbehave, including wait, listen, prereq, and onchanges

This error was introduced with PR #26134 when the changes dict was renamed to pchanges, resulting in and empty changes dict in all cases.
It was partially fixed with PR #26982, which populated the changes dict for file content differences, which is probably why this has gone unnoticed for so long.

The real problem seems to be the original change from changes to pchanges introduced by @thatch45. No other state modules use pchanges, and I don't see any other code that makes use of a pchanges key in the returned dictionary. It seems to me that the changes made in PR #26134 should be reverted entirely.

Activity

  1. added
    pending-discussion
    on Feb 7, 2018
  2. added this to the Blocked milestone on Feb 7, 2018
  3. gtmanfred commented on Feb 7, 2018

    @gtmanfred
    Contributor

    @saltstack/team-core can i get some comments on what the plans are for pchanges?

    Because a good point is brought up here, it isn't used everywhere yet, and I am not aware of any active effort to expand on it.

    Thanks,
    Daniel

  4. amendlik commented on Feb 12, 2018

    @amendlik
    ContributorAuthor

    @gtmanfred, any update on this?

  5. gtmanfred commented on Feb 12, 2018

    @gtmanfred
    Contributor

    I am waiting on input from the rest of the core team on this.

  6. rallytime commented on Feb 12, 2018

    @rallytime
    Contributor

    I am not aware of any active effort to expand it either and, to be honest, I don't quite understand the intricacies of pchanges as well as I would like. To the best of my knowledge, it's not documented very well either as I ran into some issues about what the expected behavior is when I was trying to debug some tests with @twangboy a couple of months ago.

    @thatch45 @cachedout @terminalmage @garethgreenaway Do any of you have more to add about this? What is the intended difference between pchanges and changes? Are there plans for expansion? If so, we need to make sure pchanges behaves appropriately with the various state requisites.

  7. terminalmage commented on Mar 9, 2018

    @terminalmage
    Contributor

    I wasn't involved with pchanges and don't fully understand its use. I think this was intended to be used by prereq requisites though maybe? I believe @whiteinge was at least partially involved in adding pchanges so maybe he could chime in.

  8. thatch45 commented on Mar 9, 2018

    @thatch45
    Contributor

    The idea for pchanges was that we would put potential changes in there when we run with test=True

  9. rallytime commented on Mar 9, 2018

    @rallytime
    Contributor

    Isn't that what changes does? With the old and new keys and then result=None?

  10. whiteinge commented on Mar 9, 2018

    @whiteinge
    Contributor

    Yeah, what Tom said. pchanges provides a place for programmatically consumable information as an alternative to the semi-common practice of stuffing things into the comment field as string.

    E.g., file.managed would append the diff as a string to the comment field in test=true mode -- the changes field must remain empty since no changes were actually made. Having that valuable info in a string made it difficult to consume without parsing.

    To Nicole's question, if pchanges should continue to get built out:

    • The highstate outputter should always display it for test=true runs and optionally display it for regular runs.
    • changes must be empty for test=true.
    • pchanges (predictive changes) should always be populated for test=true runs.
    • IMO, pchanges should always be populated and changes should be additionally populated for non-dry-runs so that they can be compared. But this is debatable.
    • changes and pchanges should be structurally the same "shape" so they can be compared against each other directly.
    • Usually changes and pchanges will be identical but sometimes changes might contain additional information. E.g., pchanges can predict the result of adding a new system user but only after actually adding the user can some kinds of information be learned, such as whether the system assigned a default umask and other kinds of post-task defaults.
  11. amendlik commented on Mar 20, 2018

    @amendlik
    ContributorAuthor

    I see that the question around whether pchanges is a helpful addition or not requires more discussion, but I would like to first address the issue at hand:

    When an empty changes dict is returned, the prereq requisite fails, as well as several other requisites in test mode, i.e. wait, listen, onchanges.

    Do we agree that this should be addressed as a bug?

  12. amendlik commented on Apr 5, 2018

    @amendlik
    ContributorAuthor

    @gtmanfred Any updates?

  13. grichmond-salt commented on Apr 9, 2018

    @grichmond-salt

    Team, everything that @whiteinge described sounds very cool. In the meantime, it seems that prereq and other expected behaviors are broken. @thatch45, would it be practical for us to revert this change until pchanges has been expanded further?

    @amendlik, thanks for raising this, especially pointing out how prereq breaks in this scenario. I see how that could be potentially very bad.

  14. amendlik commented on Apr 24, 2018

    @amendlik
    ContributorAuthor

    Here is an example of how an empty changes dictionary breaks the onchanges requisite when run with test=true:

    State file:

    {% set filename = '/tmp/demo-file' %}
    
    create-file:
      file.managed:
        - name: '{{ filename }}'
        - mode: '0666'
    
    delete-file:
      file.absent:
        - name: '{{ filename }}'
        - onchanges:
          - file: 'create-file'
    

    Result when run with test=true:

    local:
    ----------
              ID: create-file
        Function: file.managed
            Name: /tmp/demo-file
          Result: None
         Comment: The file /tmp/demo-file is set to be changed
         Started: 12:38:27.422568
        Duration: 11.318 ms
         Changes:   
    ----------
              ID: delete-file
        Function: file.absent
            Name: /tmp/demo-file
          Result: True
         Comment: State was not run because none of the onchanges reqs changed
         Changes:   
    
    Summary for local
    ------------
    Succeeded: 2 (unchanged=1)
    Failed:    0
    ------------
    Total states run:     2
    Total run time:  11.318 ms
    

    Notice the delete-file state says 'State was not run because none of the onchanges reqs changed'. This is because the file.managed state returns an empty changes dict when run with test=true.

    Result when run with test=false:

    local:
    ----------
              ID: create-file
        Function: file.managed
            Name: /tmp/demo-file
          Result: True
         Comment: Empty file
         Started: 12:36:21.929767
        Duration: 4.201 ms
         Changes:   
                  ----------
                  mode:
                      0666
                  new:
                      file /tmp/demo-file created
    ----------
              ID: delete-file
        Function: file.absent
            Name: /tmp/demo-file
          Result: True
         Comment: Removed file /tmp/demo-file
         Started: 12:36:21.934233
        Duration: 0.323 ms
         Changes:   
                  ----------
                  removed:
                      /tmp/demo-file
    
    Summary for local
    ------------
    Succeeded: 2 (changed=2)
    Failed:    0
    ------------
    Total states run:     2
    Total run time:   4.524 ms
    

    Notice that when run with test=false both states execute. This is because the changes dict is populated when test=false.

  15. rallytime commented on Apr 27, 2018

    @rallytime
    Contributor

    After talking about this a little more with @cachedout today, this is a bug as it stands currently, and we should fix the original issue.

    As far as the pchanges migration question goes for other state modules, we should start moving in that direction, but that particular issue/move does not need to be handled right now, just the main issue reported here can be fixed first.

    @terminalmage Can you take a look at this one after you're finished with the work you're doing on the runtests.py + Jenkins project?

  16. 14 remaining items

  17. gtmanfred commented on Aug 13, 2018

    @gtmanfred
    Contributor

    We have gotten into this argument before, now the bottom is incorrect.

    Succeeded: 2 (unchanged=1, changed=1)
    

    That logic will need to be changed as well.

  18. grichmond-salt commented on Aug 13, 2018

    @grichmond-salt

    Great catch, @gtmanfred!

  19. amendlik commented on Aug 13, 2018

    @amendlik
    ContributorAuthor

    In my opinion, all references to pchanges should be removed until there is clear direction on that point and a plan to implement support across all state modules. That said, I am not going to throw a fit over an unused variable (but your linter might).

    But, I think we're missing the bigger picture here. Please reread these comments:

    1. This comment by @amendlik, which says that service.running and pkg.installed have the same problem. How many other states have this problem? Shouldn't we fix them all? Can we be doing more with testing and documentation to address this holistically?
    2. This comment by @whiteinge which says that contributors are violating the API intentionally. This is something that should be resolved sooner than later. Either the API is confusing and should be changed, or the API is fine and it should be used as documented. Allowing developers to continue to violate it is untenable.
  20. whiteinge commented on Aug 13, 2018

    @whiteinge
    Contributor

    Agree with @amendlik points here.

    Dry-run changes usage has always been schizophrenic -- people complain when it's populated because it looks like something did change, and people complain when it's empty because then you can't test requisites that depend on that within a dry-run.

    Dry-run changes behavior differs between execution functions and has changed over time even within the same function. Best to define in writing once and for all what the best tradeoff is, then fix all the wrong functions over time.

    changes by itself isn't sufficient to satisfy both the use-cases we need for a dry-run, thus the introduction of pchanges and mock=true. But a half-implemented pchanges isn't doing anyone any favors. If now isn't a good time to work on those features then they should probably be pulled out.

  21. amendlik commented on Aug 14, 2018

    @amendlik
    ContributorAuthor

    Be sure to also remove the documentation for pchanges introduced in 0e705d2

  22. terminalmage commented on Aug 21, 2018

    @terminalmage
    Contributor

    I personally lean towards removing pchanges, since it would not involve messing with the state compiler (which would need to be done in order to make the onchanges requisites work in test mode). This would require us to set whatever we were tracking as pchanges in the changes key before returning a None result in test mode, but that would not be difficult.

    However, pchanges is used in a few dozen files, so removing it altogether would not be something I'm comfortable with doing in a point release.

    As a compromise, as a bugfix I could convert the pchanges use to changes when file states are run in test mode, which would make those requisites work. Then, the work of removing the rest of the pchanges usage could be done in develop and would be part of the Neon release (spring 2019).

    For things like pkg states, we would have to implement a changes key for those states when running in test mode. The one caveat here is that this would often be inaccurate since installing a package routinely pulls in additional dependencies. However, if that is clearly documented, I don't think it's a big deal, as the mere fact that we have something in the changes key when returning in test mode would make onchanges requisites which depend on pkg work.

    Thoughts?

  23. angeloudy commented on Sep 25, 2018

    @angeloudy
    Contributor

    pchanges is causing prereq misbehaving #49753

  24. ari commented on Sep 25, 2018

    @ari
    Contributor

    I'd like to add my voice to this. We wasted two sysadmin days and created a 60 second website outage across our customers because of this breakage.

    salt developers need to be far more aware that making significant breaking changes in stable releases creates an overall impression that salt is unreliable. In this case, it was known that prereq was completely broken for file.managed and yet nothing appears in the documentation or release notes to state that the prereq feature has been dropped from salt 2018.3. The problem has been known since February, two months before the release.

    I'm unclear what problem pchanges tried to solve. But breaking dependency management in salt should be considered a really big deal.

    We can cope with changes being inaccurate from time to time, as long as it is consistently inaccurate. And I'm happy to submit PR requests to make file.archive give us changes in test mode when used with a source hash, or for other modules we come across.

  25. terminalmage commented on Sep 29, 2018

    @terminalmage
    Contributor

    See #49832

  26. amendlik commented on Sep 29, 2018

    @amendlik
    ContributorAuthor

    #49832 is targeted for the develop branch. Given that this is a recognized bug in current versions, it would be best to back-port the changes to all supported branches.

  27. terminalmage commented on Oct 1, 2018

    @terminalmage
    Contributor

    It's also a pretty significant change, which is why it's in develop. Once we've given it some time in develop to the point we're confident it won't break, we'll consider backporting.

  28. lordcirth commented on Mar 7, 2019

    @lordcirth
    Contributor

    @terminalmage Any news on this backport? Not having diffs is quite annoying.

  29. amendlik commented on Mar 8, 2019

    @amendlik
    ContributorAuthor

    @terminalmage I am really surprised to see that this fix did not make it into the Fluorine release. This issue was introduced in 2015, reported over a year ago, and fixed 5 months ago. Do we really need to wait another 8-12 months for the next major release to get this resolved?

  30. terminalmage commented on Mar 26, 2019

    @terminalmage
    Contributor

    This is up to @saltstack/team-core to approve for backporting. We had already reached the freeze for Fluorine by the time the PR was opened. The release was delayed by the holidays and by SaltConf, so it came out later than expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

CoreP1bugseverity-medium

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions