You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow states to return a result of False in test=True mode? #42553
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.
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
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.
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.
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
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.
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.
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.
Activity
garethgreenaway commentedon Jul 26, 2017
@thatch45 @whiteinge Would you care to chime in on this?
thatch45 commentedon Jul 26, 2017
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
aneeshusa commentedon Jul 26, 2017
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:
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.
thatch45 commentedon Jul 26, 2017
Yes, this is the right approach, completely! Thanks!
whiteinge commentedon Jul 27, 2017
I do agree with the use-case, however the
resultfield 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
presultfield ("predictive result") as an educated guess as to whether the state will succeed or fail. This would mirror the intended goals of thepchangesfield.thatch45 commentedon Jul 27, 2017
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
aneeshusa commentedon Jul 27, 2017
I agree with @thatch45 about
pchangesandpresulthere.Relying on
resultas 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 (Truein 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 theresultfield.Also, my main reason for wanting to return
Falseis due to the more helpful output of failed states at the CLI - they turn red, etc. Addingpresultwouldn't be helpful for me until all the outputters were overhauled to understand and display it.whiteinge commentedon Aug 15, 2017
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.
aneeshusa commentedon Aug 15, 2017
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: Trueinto the state return would make the most sense there, but I agree that we can revisit that later with a new issue.