-
Notifications
You must be signed in to change notification settings - Fork 165
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
Resolve lookups in hook args #708
Open
ITProKyle
wants to merge
10
commits into
cloudtools:master
Choose a base branch
from
ITProKyle:hook-resolve-lookups
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+234
−153
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5868c9d
move handle_hook to avoid circle import, add lookup resolve to handle…
ITProKyle 64b69fe
update handle_hooks import in actions
ITProKyle f747c84
move handle_hooks tests to appropriate file, update import in tests
ITProKyle 2d0f88e
add test for resolved lookup in hook_data
ITProKyle 89eeda4
updated CHANGELOG
ITProKyle 191b9cf
Merge branch 'master' into hook-resolve-lookups
phobologic 943acbf
Fix lint issues
phobologic cdeea85
add error detail for pre_build hooks using output lookup or similar
ITProKyle a3d023e
add lookup usage to config/hooks documentation
ITProKyle 742e89f
updated condition and log message to include all pre/post hooks
ITProKyle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
from __future__ import print_function | ||
from __future__ import division | ||
from __future__ import absolute_import | ||
from future import standard_library | ||
standard_library.install_aliases() | ||
|
||
import unittest | ||
|
||
import queue | ||
|
||
from stacker.config import Hook | ||
from stacker.hooks.utils import handle_hooks | ||
|
||
from ..factories import ( | ||
mock_context, | ||
mock_provider, | ||
) | ||
|
||
hook_queue = queue.Queue() | ||
|
||
|
||
def mock_hook(*args, **kwargs): | ||
hook_queue.put(kwargs) | ||
return True | ||
|
||
|
||
def fail_hook(*args, **kwargs): | ||
return None | ||
|
||
|
||
def exception_hook(*args, **kwargs): | ||
raise Exception | ||
|
||
|
||
def context_hook(*args, **kwargs): | ||
return "context" in kwargs | ||
|
||
|
||
def result_hook(*args, **kwargs): | ||
return {"foo": "bar"} | ||
|
||
|
||
def kwargs_hook(*args, **kwargs): | ||
return kwargs | ||
|
||
|
||
class TestHooks(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.context = mock_context(namespace="namespace") | ||
self.provider = mock_provider(region="us-east-1") | ||
|
||
def test_empty_hook_stage(self): | ||
hooks = [] | ||
handle_hooks("fake", hooks, self.provider, self.context) | ||
self.assertTrue(hook_queue.empty()) | ||
|
||
def test_missing_required_hook(self): | ||
hooks = [Hook({"path": "not.a.real.path", "required": True})] | ||
with self.assertRaises(ImportError): | ||
handle_hooks("missing", hooks, self.provider, self.context) | ||
|
||
def test_missing_required_hook_method(self): | ||
hooks = [{"path": "stacker.hooks.blah", "required": True}] | ||
with self.assertRaises(AttributeError): | ||
handle_hooks("missing", hooks, self.provider, self.context) | ||
|
||
def test_missing_non_required_hook_method(self): | ||
hooks = [Hook({"path": "stacker.hooks.blah", "required": False})] | ||
handle_hooks("missing", hooks, self.provider, self.context) | ||
self.assertTrue(hook_queue.empty()) | ||
|
||
def test_default_required_hook(self): | ||
hooks = [Hook({"path": "stacker.hooks.blah"})] | ||
with self.assertRaises(AttributeError): | ||
handle_hooks("missing", hooks, self.provider, self.context) | ||
|
||
def test_valid_hook(self): | ||
hooks = [ | ||
Hook({"path": "stacker.tests.hooks.test_utils.mock_hook", | ||
"required": True})] | ||
handle_hooks("missing", hooks, self.provider, self.context) | ||
good = hook_queue.get_nowait() | ||
self.assertEqual(good["provider"].region, "us-east-1") | ||
with self.assertRaises(queue.Empty): | ||
hook_queue.get_nowait() | ||
|
||
def test_valid_enabled_hook(self): | ||
hooks = [ | ||
Hook({"path": "stacker.tests.hooks.test_utils.mock_hook", | ||
"required": True, "enabled": True})] | ||
handle_hooks("missing", hooks, self.provider, self.context) | ||
good = hook_queue.get_nowait() | ||
self.assertEqual(good["provider"].region, "us-east-1") | ||
with self.assertRaises(queue.Empty): | ||
hook_queue.get_nowait() | ||
|
||
def test_valid_enabled_false_hook(self): | ||
hooks = [ | ||
Hook({"path": "stacker.tests.hooks.test_utils.mock_hook", | ||
"required": True, "enabled": False})] | ||
handle_hooks("missing", hooks, self.provider, self.context) | ||
self.assertTrue(hook_queue.empty()) | ||
|
||
def test_context_provided_to_hook(self): | ||
hooks = [ | ||
Hook({"path": "stacker.tests.hooks.test_utils.context_hook", | ||
"required": True})] | ||
handle_hooks("missing", hooks, "us-east-1", self.context) | ||
|
||
def test_hook_failure(self): | ||
hooks = [ | ||
Hook({"path": "stacker.tests.hooks.test_utils.fail_hook", | ||
"required": True})] | ||
with self.assertRaises(SystemExit): | ||
handle_hooks("fail", hooks, self.provider, self.context) | ||
hooks = [{"path": "stacker.tests.hooks.test_utils.exception_hook", | ||
"required": True}] | ||
with self.assertRaises(Exception): | ||
handle_hooks("fail", hooks, self.provider, self.context) | ||
hooks = [ | ||
Hook({"path": "stacker.tests.hooks.test_utils.exception_hook", | ||
"required": False})] | ||
# Should pass | ||
handle_hooks("ignore_exception", hooks, self.provider, self.context) | ||
|
||
def test_return_data_hook(self): | ||
hooks = [ | ||
Hook({ | ||
"path": "stacker.tests.hooks.test_utils.result_hook", | ||
"data_key": "my_hook_results" | ||
}), | ||
# Shouldn't return data | ||
Hook({ | ||
"path": "stacker.tests.hooks.test_utils.context_hook" | ||
}) | ||
] | ||
handle_hooks("result", hooks, "us-east-1", self.context) | ||
|
||
self.assertEqual( | ||
self.context.hook_data["my_hook_results"]["foo"], | ||
"bar" | ||
) | ||
# Verify only the first hook resulted in stored data | ||
self.assertEqual( | ||
list(self.context.hook_data.keys()), ["my_hook_results"] | ||
) | ||
|
||
def test_return_data_hook_duplicate_key(self): | ||
hooks = [ | ||
Hook({ | ||
"path": "stacker.tests.hooks.test_utils.result_hook", | ||
"data_key": "my_hook_results" | ||
}), | ||
Hook({ | ||
"path": "stacker.tests.hooks.test_utils.result_hook", | ||
"data_key": "my_hook_results" | ||
}) | ||
] | ||
|
||
with self.assertRaises(KeyError): | ||
handle_hooks("result", hooks, "us-east-1", self.context) | ||
|
||
def test_resolve_lookups_in_args(self): | ||
hooks = [ | ||
Hook({ | ||
"path": "stacker.tests.hooks.test_utils.kwargs_hook", | ||
"data_key": "my_hook_results", | ||
"args": { | ||
"default_lookup": "${default env_var::default_value}" | ||
} | ||
}) | ||
] | ||
handle_hooks("lookups", hooks, "us-east-1", self.context) | ||
|
||
self.assertEqual( | ||
self.context.hook_data["my_hook_results"]["default_lookup"], | ||
"default_value" | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any idea how this would work with the
output
lookup, which changes the order of execution? I think it will likely break- especially if it's a pre_build hook that runs before the graph gets executed. Even with that, I think this is still good - we just might want to handle that case, and explicitly indicate somehow that lookups that change the order of execution can only run as post_build hooks.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested output with a post_build hook, it works as expected. However, you are right that it does not work in pre_build. It raises FailedVariableLookup and uses the closest key in args as the variable in the exception.
I added a log output that explains the use of output and similar lookups in args. With the condition used it won't trigger for failed lookups that include another exception like StackDoesNotExist. If you have a better idea, please let me know. I avoided checking the lookups provided here before trying to resolve them here to not add time. could maybe pass stage to resolve_variables/Variable.resolve() if we do want to add validation logic around it?