Exemplo n.º 1
0
    def test_handle_provider_exception(self, mock_should_use, exception,
                                       default, caplog):
        """Test handle provider raise exception."""
        caplog.set_level(logging.DEBUG, logger="runway.lookups.handlers.cfn")
        mock_should_use.return_value = True
        mock_provider = MagicMock(region="us-east-1")
        mock_provider.get_output.side_effect = exception
        raw_query = "test-stack.output1"

        if default:
            assert (CfnLookup.handle(
                raw_query + "::default=" + default,
                context=None,
                provider=mock_provider,
            ) == default)
            mock_should_use.assert_called_once_with({"default": default},
                                                    mock_provider)
            assert ("unable to resolve lookup for CloudFormation Stack output "
                    '"{}"; using default'.format(raw_query)) in caplog.messages
        else:
            if isinstance(exception, (ClientError, StackDoesNotExist)):
                with pytest.raises(exception.__class__):
                    assert not CfnLookup.handle(
                        raw_query, context=None, provider=mock_provider)
            else:
                with pytest.raises(OutputDoesNotExist) as excinfo:
                    assert not CfnLookup.handle(
                        raw_query, context=None, provider=mock_provider)
                assert excinfo.value.stack_name == "test-stack"
                assert excinfo.value.output == "output1"
            mock_should_use.assert_called_once_with({}, mock_provider)
        mock_provider.get_output.assert_called_once_with(
            "test-stack", "output1")
Exemplo n.º 2
0
    def test_handle_exception(self, mock_should_use, exception, default,
                              caplog, monkeypatch):
        """Test handle cls.get_stack_output raise exception."""
        caplog.set_level(logging.DEBUG, logger="runway.lookups.handlers.cfn")
        mock_context = MagicMock(name="context")
        mock_session = MagicMock(name="session")
        mock_context.get_session.return_value = mock_session
        mock_session.client.return_value = mock_session
        mock_should_use.return_value = False
        monkeypatch.setattr(CfnLookup, "get_stack_output", MagicMock())
        CfnLookup.get_stack_output.side_effect = exception

        raw_query = "test-stack.output1"
        query = OutputQuery(*raw_query.split("."))

        if default:
            assert (CfnLookup.handle(raw_query + "::default=" + default,
                                     mock_context) == default)
            mock_should_use.assert_called_once_with({"default": default}, None)
            assert ("unable to resolve lookup for CloudFormation Stack output "
                    '"{}"; using default'.format(raw_query)) in caplog.messages
        else:
            if isinstance(exception, (ClientError, StackDoesNotExist)):
                with pytest.raises(exception.__class__):
                    assert not CfnLookup.handle(raw_query, mock_context)
            else:
                with pytest.raises(OutputDoesNotExist) as excinfo:
                    assert not CfnLookup.handle(raw_query, mock_context)
                assert excinfo.value.stack_name == "test-stack"
                assert excinfo.value.output == "output1"
            mock_should_use.assert_called_once_with({}, None)

        mock_context.get_session.assert_called_once()
        mock_session.client.assert_called_once_with("cloudformation")
        CfnLookup.get_stack_output.assert_called_once_with(mock_session, query)
Exemplo n.º 3
0
    def test_handle(self, mocker: MockerFixture) -> None:
        """Test handle."""
        mock_format_results = mocker.patch.object(CfnLookup,
                                                  "format_results",
                                                  return_value="success")
        mock_get_stack_output = mocker.patch.object(CfnLookup,
                                                    "get_stack_output",
                                                    return_value="cls.success")
        mock_should_use = mocker.patch.object(CfnLookup,
                                              "should_use_provider",
                                              side_effect=[True, False])
        mock_context = MagicMock(name="context")
        mock_session = MagicMock(name="session")
        mock_context.get_session.return_value = mock_session
        mock_session.client.return_value = mock_session
        mock_provider = MagicMock(name="provider")
        mock_provider.get_output.return_value = "provider.success"

        raw_query = "test-stack.output1"
        query = OutputQuery(*raw_query.split("."))
        region = "us-west-2"
        args = "::region={region}".format(region=region)
        value = raw_query + args
        mock_parse = mocker.patch.object(CfnLookup,
                                         "parse",
                                         return_value=(raw_query, {
                                             "region": region
                                         }))

        # test happy path when used from CFNgin (provider)
        assert (CfnLookup.handle(value,
                                 context=mock_context,
                                 provider=mock_provider) == "success")
        mock_parse.assert_called_once_with(value)
        mock_provider.get_output.assert_called_once_with(*query)
        mock_should_use.assert_called_once_with({"region": region},
                                                mock_provider)
        mock_format_results.assert_called_once_with("provider.success",
                                                    region=region)
        mock_context.get_session.assert_not_called()
        mock_get_stack_output.assert_not_called()

        # test happy path when use from runway (no provider)
        assert CfnLookup.handle(value, context=mock_context) == "success"
        mock_should_use.assert_called_with({"region": region}, None)
        mock_context.get_session.assert_called_once_with(region=region)
        mock_session.client.assert_called_once_with("cloudformation")
        mock_get_stack_output.assert_called_once_with(mock_session, query)
        mock_format_results.assert_called_with("cls.success", region=region)
Exemplo n.º 4
0
 def test_handle_valueerror(self,
                            runway_context: MockRunwayContext) -> None:
     """Test handle raising ValueError."""
     with pytest.raises(ValueError) as excinfo:
         assert CfnLookup.handle("something", runway_context)
     assert (str(excinfo.value) ==
             'query must be <stack-name>.<output-name>; got "something"')
Exemplo n.º 5
0
 def test_handle_valueerror(self):
     """Test handle raising ValueError."""
     with pytest.raises(ValueError) as excinfo:
         assert CfnLookup.handle("something", None)
     assert (str(excinfo.value) ==
             'query must be <stack-name>.<output-name>; got "something"')