Skip to content

Commit

Permalink
Implement the rest unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
honglu committed Jan 19, 2019
1 parent 0ded6ca commit 2b20844
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
139 changes: 139 additions & 0 deletions test/unit/test_deployer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 2b20844

Please sign in to comment.