def test_context_provided_to_hook(self): hooks = [ Hook({ "path": "stacker.tests.test_util.context_hook", "required": True }) ] handle_hooks("missing", hooks, "us-east-1", self.context)
def post_run(self, outline=False, *args, **kwargs): """Any steps that need to be taken after running the action.""" post_destroy = self.context.config.post_destroy if not outline and post_destroy: handle_hooks(stage="post_destroy", hooks=post_destroy, provider=self.provider, context=self.context)
def test_valid_enabled_false_hook(self): hooks = [ Hook({ "path": "stacker.tests.test_util.mock_hook", "required": True, "enabled": False }) ] handle_hooks("missing", hooks, self.provider, self.context) self.assertTrue(hook_queue.empty())
def test_valid_hook(self): hooks = [ Hook({ "path": "stacker.tests.test_util.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_return_data_hook_duplicate_key(self): hooks = [ Hook({ "path": "stacker.tests.test_util.result_hook", "data_key": "my_hook_results" }), Hook({ "path": "stacker.tests.test_util.result_hook", "data_key": "my_hook_results" }) ] with self.assertRaises(KeyError): handle_hooks("result", hooks, "us-east-1", self.context)
def test_return_data_hook(self): hooks = [ Hook({ "path": "stacker.tests.test_util.result_hook", "data_key": "my_hook_results" }), # Shouldn't return data Hook({"path": "stacker.tests.test_util.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 handle_hooks(stage, hooks, provider, context, dump, outline): """Handle pre/post hooks. Args: stage (str): The name of the hook stage - pre_build/post_build. hooks (list): A list of dictionaries containing the hooks to execute. provider (:class:`stacker.provider.base.BaseProvider`): The provider the current stack is using. context (:class:`stacker.context.Context`): The current stacker context. dump (bool): Whether running with dump set or not. outline (bool): Whether running with outline set or not. """ if not outline and not dump and hooks: utils.handle_hooks(stage=stage, hooks=hooks, provider=provider, context=context)
def test_hook_with_sys_path(self): config = Config({ "namespace": "test", "sys_path": "stacker/tests", "pre_build": [{ "data_key": "myHook", "path": "fixtures.mock_hooks.mock_hook", "required": True, "args": { "value": "mockResult" } }] }) load(config) context = Context(config=config) stage = "pre_build" handle_hooks(stage, context.config[stage], "mock-region-1", context) self.assertEqual("mockResult", context.hook_data["myHook"]["result"])
def test_hook_failure(self): hooks = [ Hook({ "path": "stacker.tests.test_util.fail_hook", "required": True }) ] with self.assertRaises(SystemExit): handle_hooks("fail", hooks, self.provider, self.context) hooks = [{ "path": "stacker.tests.test_util.exception_hook", "required": True }] with self.assertRaises(Exception): handle_hooks("fail", hooks, self.provider, self.context) hooks = [ Hook({ "path": "stacker.tests.test_util.exception_hook", "required": False }) ] # Should pass handle_hooks("ignore_exception", hooks, self.provider, self.context)
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_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_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_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_empty_hook_stage(self): hooks = [] handle_hooks("fake", hooks, self.provider, self.context) self.assertTrue(hook_queue.empty())