Exemplo n.º 1
0
 def test_valid_hook(self, mock_load: MagicMock) -> None:
     """Test valid hook."""
     mock_load.side_effect = [mock_hook, MockHook]
     hooks = [
         CfnginHookDefinitionModel(
             **{
                 "path": "tests.unit.cfngin.hooks.test_utils.mock_hook",
                 "required": True,
             }),
         CfnginHookDefinitionModel(
             **{
                 "path": "tests.unit.cfngin.hooks.test_utils.MockHook",
                 "required": True,
             }),
     ]
     handle_hooks("pre_deploy", hooks, self.provider, self.context)
     assert mock_load.call_count == 2
     mock_load.assert_has_calls([
         call(hooks[0].path, try_reload=True),
         call(hooks[1].path, try_reload=True)
     ])
     good = HOOK_QUEUE.get_nowait()
     self.assertEqual(good["provider"].region, "us-east-1")
     with self.assertRaises(queue.Empty):
         HOOK_QUEUE.get_nowait()
Exemplo n.º 2
0
 def test_hook_failure(self) -> None:
     """Test hook failure."""
     hooks = [
         CfnginHookDefinitionModel(
             **{
                 "path": "tests.unit.cfngin.hooks.test_utils.fail_hook",
                 "required": True,
             })
     ]
     with self.assertRaises(SystemExit):
         handle_hooks("fail", hooks, self.provider, self.context)
     hooks = [
         CfnginHookDefinitionModel.parse_obj({
             "path": "tests.unit.cfngin.hooks.test_utils.exception_hook",
             "required": True,
         })
     ]
     with self.assertRaises(Exception):
         handle_hooks("fail", hooks, self.provider, self.context)
     hooks = [
         CfnginHookDefinitionModel(
             **{
                 "path":
                 "tests.unit.cfngin.hooks.test_utils.exception_hook",
                 "required": False,
             })
     ]
     # Should pass
     handle_hooks("ignore_exception", hooks, self.provider, self.context)
Exemplo n.º 3
0
 def test_default_required_hook(self) -> None:
     """Test default required hook."""
     hooks = [
         CfnginHookDefinitionModel(**{"path": "runway.cfngin.hooks.blah"})
     ]
     with self.assertRaises(AttributeError):
         handle_hooks("missing", hooks, self.provider, self.context)
Exemplo n.º 4
0
 def test_required_fields(self) -> None:
     """Test required fields."""
     with pytest.raises(ValidationError) as excinfo:
         CfnginHookDefinitionModel()
     errors = excinfo.value.errors()
     assert len(errors) == 1
     assert errors[0]["loc"] == ("path", )
     assert errors[0]["msg"] == "field required"
Exemplo n.º 5
0
 def test_field_defaults(self) -> None:
     """Test field default values."""
     obj = CfnginHookDefinitionModel(path="something")
     assert obj.args == {}
     assert not obj.data_key
     assert obj.enabled
     assert obj.path == "something"
     assert obj.required
Exemplo n.º 6
0
 def test_extra(self) -> None:
     """Test extra fields."""
     with pytest.raises(ValidationError) as excinfo:
         CfnginHookDefinitionModel(invalid="something", path="something")
     errors = excinfo.value.errors()
     assert len(errors) == 1
     assert errors[0]["loc"] == ("invalid", )
     assert errors[0]["msg"] == "extra fields not permitted"
Exemplo n.º 7
0
    def test_return_data_hook_duplicate_key(self) -> None:
        """Test return data hook duplicate key."""
        hooks = [
            CfnginHookDefinitionModel(
                **{
                    "path": "tests.unit.cfngin.hooks.test_utils.result_hook",
                    "data_key": "my_hook_results",
                }),
            CfnginHookDefinitionModel(
                **{
                    "path": "tests.unit.cfngin.hooks.test_utils.result_hook",
                    "data_key": "my_hook_results",
                }),
        ]

        with self.assertRaises(KeyError):
            handle_hooks("result", hooks, self.provider, self.context)
Exemplo n.º 8
0
 def test_missing_non_required_hook_method(self) -> None:
     """Test missing non required hook method."""
     hooks = [
         CfnginHookDefinitionModel(**{
             "path": "runway.cfngin.hooks.blah",
             "required": False
         })
     ]
     handle_hooks("missing", hooks, self.provider, self.context)
     self.assertTrue(HOOK_QUEUE.empty())
Exemplo n.º 9
0
 def test_missing_required_hook_method(self) -> None:
     """Test missing required hook method."""
     with self.assertRaises(AttributeError):
         hooks = [
             CfnginHookDefinitionModel.parse_obj({
                 "path": "runway.cfngin.hooks.blah",
                 "required": True
             })
         ]
         handle_hooks("missing", hooks, self.provider, self.context)
Exemplo n.º 10
0
 def test_missing_required_hook(self) -> None:
     """Test missing required hook."""
     hooks = [
         CfnginHookDefinitionModel(**{
             "path": "not.a.real.path",
             "required": True
         })
     ]
     with self.assertRaises(ImportError):
         handle_hooks("missing", hooks, self.provider, self.context)
Exemplo n.º 11
0
    def test_return_data_hook(self) -> None:
        """Test return data hook."""
        hooks = [
            CfnginHookDefinitionModel(
                **{
                    "path": "tests.unit.cfngin.hooks.test_utils.result_hook",
                    "data_key": "my_hook_results",
                }),
            # Shouldn't return data
            CfnginHookDefinitionModel(
                **{"path": "tests.unit.cfngin.hooks.test_utils.context_hook"}),
        ]
        handle_hooks("result", hooks, self.provider, 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"])
Exemplo n.º 12
0
 def test_context_provided_to_hook(self) -> None:
     """Test context provided to hook."""
     hooks = [
         CfnginHookDefinitionModel(
             **{
                 "path": "tests.unit.cfngin.hooks.test_utils.context_hook",
                 "required": True,
             })
     ]
     handle_hooks("missing", hooks, self.provider, self.context)
Exemplo n.º 13
0
 def test_valid_enabled_false_hook(self) -> None:
     """Test valid enabled false hook."""
     hooks = [
         CfnginHookDefinitionModel(
             **{
                 "path": "tests.unit.cfngin.hooks.test_utils.mock_hook",
                 "required": True,
                 "enabled": False,
             })
     ]
     handle_hooks("missing", hooks, self.provider, self.context)
     self.assertTrue(HOOK_QUEUE.empty())
Exemplo n.º 14
0
 def test_valid_enabled_hook(self) -> None:
     """Test valid enabled hook."""
     hooks = [
         CfnginHookDefinitionModel(
             **{
                 "path": "tests.unit.cfngin.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()
Exemplo n.º 15
0
    def test_resolve_lookups_in_args(self) -> None:
        """Test the resolution of lookups in hook args."""
        hooks = [
            CfnginHookDefinitionModel(
                **{
                    "path": "tests.unit.cfngin.hooks.test_utils.kwargs_hook",
                    "data_key": "my_hook_results",
                    "args": {
                        "default_lookup": "${default env_var::default_value}"
                    },
                })
        ]
        handle_hooks("lookups", hooks, self.provider, self.context)

        self.assertEqual(
            self.context.hook_data["my_hook_results"]["default_lookup"],
            "default_value")