67 lines
2.4 KiB
Python
67 lines
2.4 KiB
Python
""" Tests for lambda_verification function """
|
|
# pylint: disable=no-self-use,missing-function-docstring,missing-class-docstring
|
|
|
|
import os
|
|
import pytest
|
|
import mock
|
|
from src import lambda_verification
|
|
|
|
PAYLOAD = {
|
|
"DeploymentId": "TestDeployment001",
|
|
"LifecycleEventHookExecutionId": "LifeCycleTest001",
|
|
}
|
|
|
|
os.environ["CurrentVersion"] = "CurVer"
|
|
os.environ["TestData"] = '{"value":"10"}'
|
|
os.environ["TestExpected"] = "2,5"
|
|
|
|
|
|
class TestLambdaVerfication:
|
|
def test_handler_bad_payload(self):
|
|
with pytest.raises(Exception):
|
|
assert lambda_verification.handler({}, {})
|
|
|
|
@mock.patch("src.lambda_verification.CODEDEPLOY")
|
|
@mock.patch("src.lambda_verification.AWSLAMBDA")
|
|
@pytest.mark.parametrize(
|
|
"response,expected", [("2,4", "Failed"), ("2,5", "Succeeded")],
|
|
)
|
|
def test_handler(self, mock_lambda, mock_codedeploy, response, expected):
|
|
response_mock = mock.MagicMock()
|
|
response_mock.read.return_value = response
|
|
mock_lambda.invoke.return_value = {"Payload": response_mock}
|
|
|
|
lambda_verification.handler(PAYLOAD, {})
|
|
|
|
mock_codedeploy.put_lifecycle_event_hook_execution_status.assert_called_with(
|
|
status=expected,
|
|
deploymentId="TestDeployment001",
|
|
lifecycleEventHookExecutionId="LifeCycleTest001",
|
|
)
|
|
|
|
@mock.patch("src.lambda_verification.CODEDEPLOY")
|
|
@mock.patch("src.lambda_verification.AWSLAMBDA")
|
|
def test_handler_lambda_failure(self, mock_lambda, mock_codedeploy):
|
|
mock_lambda.invoke.side_effect = Exception()
|
|
with pytest.raises(Exception):
|
|
assert lambda_verification.handler(PAYLOAD, {})
|
|
|
|
mock_codedeploy.put_lifecycle_event_hook_execution_status.assert_called_with(
|
|
status="Failed",
|
|
deploymentId="TestDeployment001",
|
|
lifecycleEventHookExecutionId="LifeCycleTest001",
|
|
)
|
|
|
|
@mock.patch("src.lambda_verification.CODEDEPLOY")
|
|
@mock.patch("src.lambda_verification.AWSLAMBDA")
|
|
def test_handler_codedeploy_failure(self, mock_lambda, mock_codedeploy):
|
|
response_mock = mock.MagicMock()
|
|
response_mock.read.return_value = "2,5"
|
|
mock_lambda.invoke.return_value = {"Payload": response_mock}
|
|
mock_codedeploy.put_lifecycle_event_hook_execution_status.side_effect = (
|
|
Exception()
|
|
)
|
|
|
|
with pytest.raises(Exception):
|
|
assert lambda_verification.handler(PAYLOAD, {})
|