Skip to content

Allow states to return a result of False in test=True mode? #42553

Closed

Description

@aneeshusa

The guide to writing state modules currently recommends that states return a result of None in test=True mode even for failed changes. This means that failed states are masked in the highstate output, and just look like they are going to make changes.

I've been working on updating some of the boto execution and state modules.
These modules generally make AWS API calls to get the current configuration of a resource, to see if changes are necessary. If we get an error while calling one of these APIs during a dry-run, it's very likely that the same call will also fail when not in a dry-run (e.g. if credentials are wrong, a required config parameter was not specified, etc.).

I think it would be helpful to allow states to return a result of False if they encounter errors in test=True mode to increase the SNR of dry runs and catch more errors at testing time.
Some states may have false negatives due to e.g. requisites that haven't run yet, and IMO those should continue to return a result of None.

Activity

  1. added this to the Blocked milestone on Jul 26, 2017
  2. added
    pending-discussion
    on Jul 26, 2017
  3. garethgreenaway commented on Jul 26, 2017

    @garethgreenaway
    Contributor

    @thatch45 @whiteinge Would you care to chime in on this?

  4. thatch45 commented on Jul 26, 2017

    @thatch45
    Contributor

    If a state is going to fail and we can figure that out without applying the change then it should return False. Does this not work? Or is this just a documentation update to make this more clear?
    Basically, you should not put a check like that in the test portion of the code, but there is noting wrong with returning false before you get to the test check

  5. aneeshusa commented on Jul 26, 2017

    @aneeshusa
    ContributorAuthor

    This works fine technically, this is a request for a best practices clarification/documentation update.

    Here's a simplified version of some code I wrote recently with this pattern:

    # In states/boto_sqs.py
    def present(name, region, key, keyid, profile):
        ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}
    
        r = __salt__['boto_sqs.exists'](name, region, key, keyid, profile)
        if 'error' in r:
            ret['result'] = False # NOTE: this returns False even in test=True mode
            ret['comment'] = 'Could not check SQS queue {0}'.format(name)
            return ret
    
        if r['result']: 
            ret['comment'] = 'SQS queue {0} present'.format(name)
            return ret
    
        if __opts__['test']:
            ret['result'] = None
            ret['comment'] = 'SQS queue {0} is set to be created'.format(name)
            return ret
    
        r = __salt__['boto_sqs.create'](name, region, key, keyid, profile)
        if 'error' in r:
            ret['result'] = False
            ret['comment'] = 'Failed to create SQS queue {0}: {1}'.format(name, r['error'])
            return ret
    
        ret['comment'] = 'SQS queue {0} created'.format(name)
        return ret
    

    Does this sound acceptable (specifically, the section I've annotated with a NOTE comment)?
    If so, I can put in a PR to update the docs.

  6. thatch45 commented on Jul 26, 2017

    @thatch45
    Contributor

    Yes, this is the right approach, completely! Thanks!

  7. added
    documentation
    on Jul 26, 2017
  8. whiteinge commented on Jul 27, 2017

    @whiteinge
    Contributor

    I do agree with the use-case, however the result field is the only source-of-truth we have to know whether a state was executed as dry-run or not. I suspect muddying that distinction will have repercussions -- or at least hit competing expectations such as just wanting to see if a function was executed as dry-run.

    As an alternate suggestion: what if we were instead to include a new presult field ("predictive result") as an educated guess as to whether the state will succeed or fail. This would mirror the intended goals of the pchanges field.

  9. thatch45 commented on Jul 27, 2017

    @thatch45
    Contributor

    I am a big fan of the pchanges field, but I think that the presult would be overfill, the result from a test=True run should always be presented or known as such, and if it fails in dry run then it will fail in a real run

  10. aneeshusa commented on Jul 27, 2017

    @aneeshusa
    ContributorAuthor

    I agree with @thatch45 about pchanges and presult here.

    Relying on result as a source-of-truth for dry-run vs not seems suspicious to me. The possible values for that already overlap between dry-run/not if everything looks good (True in both cases).
    I think it would be better to track that info at a higher level (was this highstate a dry run or not?), or add a separate key/value pair to state returns (i.e. add in 'test': True), or some other mechanism to avoid conflating that info with the result field.

    Also, my main reason for wanting to return False is due to the more helpful output of failed states at the CLI - they turn red, etc. Adding presult wouldn't be helpful for me until all the outputters were overhauled to understand and display it.

  11. whiteinge commented on Aug 15, 2017

    @whiteinge
    Contributor

    Sorry for the slow reply; busy couple of weeks and this fell off my radar.

    The reason I like tracking this at the individual state level instead of at the job level is because states can be run in dry-run even though the full state run is not (e.g., prereq). Bottom-line: we don't have enough fields to represent all possible scenarios without making at least one of the proposed additions so I'll acquiesce here and we can revisit if/when we have a concrete need.

  12. removed
    pending-discussion
    on Aug 15, 2017
  13. modified the milestones: Approved, Blocked on Aug 15, 2017
  14. aneeshusa commented on Aug 15, 2017

    @aneeshusa
    ContributorAuthor

    Now that #42576 is merged, this is fixed from my perspective so I'm closing this. Thanks all for the feedback.

    I didn't know about prereq and running individual states in dry-run mode, sounds like a cool feature! I think injecting a test: True into the state return would make the most sense there, but I agree that we can revisit that later with a new issue.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentation

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions