From 2b20844921f33eab5377138a20d1076a68194107 Mon Sep 17 00:00:00 2001 From: Lu Hong Date: Fri, 18 Jan 2019 19:54:07 -0800 Subject: [PATCH] Implement the rest unit tests --- Makefile | 2 +- template.yml | 4 +- test/unit/test_deployer.py | 139 +++++++++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 73c18dd..027d2f6 100644 --- a/Makefile +++ b/Makefile @@ -42,7 +42,7 @@ compile: pipenv run flake8 $(TST_DIR) pipenv run pydocstyle $(SRC_DIR) pipenv run cfn-lint template.yml - pipenv run py.test --cov=$(SRC_DIR) --cov-fail-under=70 -vv $(TST_DIR) + pipenv run py.test --cov=$(SRC_DIR) --cov-fail-under=85 -vv $(TST_DIR) pipenv lock --requirements > $(SRC_DIR)/requirements.txt pipenv run sam build diff --git a/template.yml b/template.yml index c815537..b10b55c 100644 --- a/template.yml +++ b/template.yml @@ -13,8 +13,8 @@ Metadata: ReadmeUrl: ../../README.md Labels: [serverless] HomePageUrl: https://github.com/honglu/aws-serverlessrepo-auto-deploy - SemanticVersion: 1.0.2 - SourceCodeUrl: https://github.com/honglu/aws-serverlessrepo-auto-deploy/tree/1.0.2 + SemanticVersion: 1.0.3 + SourceCodeUrl: https://github.com/honglu/aws-serverlessrepo-auto-deploy/tree/1.0.3 Parameters: LogLevel: diff --git a/test/unit/test_deployer.py b/test/unit/test_deployer.py index 7da2dbb..dccd237 100644 --- a/test/unit/test_deployer.py +++ b/test/unit/test_deployer.py @@ -29,3 +29,142 @@ def test_handler_stack_no_tag(mock_cfn): with pytest.raises(Exception): deployer.handler({}, None) mock_cfn.describe_stack.assert_called_once_with("serverlessrepo-{}".format(test_constants.STACK_NAME)) + + +def test_handler_stack_no_application_id_in_tag(mock_cfn): + mock_cfn.describe_stack.return_value = { + 'StackStatus': 'CREATE_COMPLETE', + 'Tags': [{ + 'Key': 'serverlessrepo:semanticVersion', + 'Value': '1.0.0' + }] + } + + with pytest.raises(Exception): + deployer.handler({}, None) + mock_cfn.describe_stack.assert_called_once_with("serverlessrepo-{}".format(test_constants.STACK_NAME)) + + +def test_handler_stack_no_semantic_version_in_tag(mock_cfn): + mock_cfn.describe_stack.return_value = { + 'StackStatus': 'CREATE_COMPLETE', + 'Tags': [{ + 'Key': 'serverlessrepo:applicationId', + 'Value': test_constants.APPLICATION_ID + }] + } + + with pytest.raises(Exception): + deployer.handler({}, None) + mock_cfn.describe_stack.assert_called_once_with("serverlessrepo-{}".format(test_constants.STACK_NAME)) + + +def test_handler_stack_application_id_not_match(mock_cfn): + mock_cfn.describe_stack.return_value = { + 'StackStatus': 'CREATE_COMPLETE', + 'Tags': [ + { + 'Key': 'serverlessrepo:applicationId', + 'Value': 'not match' + }, + { + 'Key': 'serverlessrepo:semanticVersion', + 'Value': '1.0.0' + } + ] + } + + with pytest.raises(Exception): + deployer.handler({}, None) + mock_cfn.describe_stack.assert_called_once_with("serverlessrepo-{}".format(test_constants.STACK_NAME)) + + +def test_handler_stack_no_version(mock_cfn, mock_repo): + mock_cfn.describe_stack.return_value = { + 'StackStatus': 'CREATE_COMPLETE', + 'Tags': [ + { + 'Key': 'serverlessrepo:applicationId', + 'Value': test_constants.APPLICATION_ID + }, + { + 'Key': 'serverlessrepo:semanticVersion', + 'Value': '1.0.0' + } + ] + } + mock_repo.get_application.return_value = {} + + with pytest.raises(Exception): + deployer.handler({}, None) + mock_cfn.describe_stack.assert_called_once_with("serverlessrepo-{}".format(test_constants.STACK_NAME)) + mock_repo.get_application.assert_called_once_with(test_constants.APPLICATION_ID) + mock_repo.create_change_set_with_template.assert_not_called() + + +def test_handler_stack_no_newer_version(mock_cfn, mock_repo): + mock_cfn.describe_stack.return_value = { + 'StackStatus': 'CREATE_COMPLETE', + 'Tags': [ + { + 'Key': 'serverlessrepo:applicationId', + 'Value': test_constants.APPLICATION_ID + }, + { + 'Key': 'serverlessrepo:semanticVersion', + 'Value': '1.0.0' + } + ] + } + mock_repo.get_application.return_value = {'Version': {'SemanticVersion': '0.0.1'}} + + deployer.handler({}, None) + mock_cfn.describe_stack.assert_called_once_with("serverlessrepo-{}".format(test_constants.STACK_NAME)) + mock_repo.get_application.assert_called_once_with(test_constants.APPLICATION_ID) + mock_repo.create_change_set_with_template.assert_not_called() + mock_repo.create_change_set_with_template.assert_not_called() + + +def test_handler(mock_cfn, mock_repo): + change_set_id = 'myChangeSet' + mock_cfn.describe_stack.return_value = { + 'StackStatus': 'CREATE_COMPLETE', + 'Tags': [ + { + 'Key': 'serverlessrepo:applicationId', + 'Value': test_constants.APPLICATION_ID + }, + { + 'Key': 'serverlessrepo:semanticVersion', + 'Value': '1.0.0' + } + ] + } + mock_repo.get_application.return_value = {'Version': {'SemanticVersion': '1.0.1'}} + mock_repo.create_change_set_with_template.return_value = {'ChangeSetId': change_set_id} + + deployer.handler({}, None) + mock_cfn.describe_stack.assert_called_once_with("serverlessrepo-{}".format(test_constants.STACK_NAME)) + mock_repo.get_application.assert_called_once_with(test_constants.APPLICATION_ID) + mock_repo.create_change_set_with_template.assert_called_once_with( + application_id=test_constants.APPLICATION_ID, + stack_name=test_constants.STACK_NAME, + parameter_overrides={"Name": "myParam", "Value": 1}, + capabilities=["CAPABILITY_IAM"]) + mock_cfn.wait_and_execute_change_set.assert_called_once_with(change_set_id) + + +def test_handler_new_stack(mock_cfn, mock_repo): + change_set_id = 'myChangeSet' + mock_cfn.describe_stack.return_value = None + mock_repo.create_change_set_with_template.return_value = {'ChangeSetId': change_set_id} + + deployer.handler({}, None) + mock_cfn.describe_stack.assert_called_once_with("serverlessrepo-{}".format(test_constants.STACK_NAME)) + mock_repo.get_application.assert_not_called() + mock_repo.create_change_set_with_template.assert_called_once_with( + application_id=test_constants.APPLICATION_ID, + stack_name=test_constants.STACK_NAME, + parameter_overrides={"Name": "myParam", "Value": 1}, + capabilities=["CAPABILITY_IAM"]) + mock_cfn.wait_and_execute_change_set.assert_called_once_with(change_set_id)