1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import hashlib
import json
from botocore import compat
from awscli.customizations.ecs.deploy import MAX_WAIT_MIN, CodeDeployer
from awscli.customizations.ecs.exceptions import MissingPropertyError
from awscli.testutils import capture_output, mock, unittest
class TestCodeDeployer(unittest.TestCase):
TEST_APPSPEC = {
"version": 0.0,
"resources": [
{
"TestService": {
"type": "AWS::ECS::Service",
"properties": {
"taskDefinition": "arn:aws:ecs:::task-definition/test:1",
"loadBalancerInfo": {
"containerName": "web",
"containerPort": 80,
},
},
}
}
],
}
def setUp(self):
waiter = mock.Mock()
waiter.wait.return_value = {}
mock_cd = mock.Mock()
mock_cd.get_waiter.return_value = waiter
self.deployer = CodeDeployer(mock_cd, self.TEST_APPSPEC)
def test_update_task_def_arn(self):
test_arn = 'arn:aws:ecs::1234567890:task-definition/new-thing:3'
self.deployer.update_task_def_arn(test_arn)
appspec_resources = self.deployer._appspec_dict['resources']
for resource in appspec_resources:
actual_arn = resource['TestService']['properties'][
'taskDefinition'
]
self.assertEqual(actual_arn, test_arn)
def test_update_task_def_arn_error_required_key(self):
invalid_appspec = {
"version": 0.0,
"resources": [
{
"TestFunc": {
"type": "AWS::Lambda::Function",
"properties": {"name": "some-function"},
}
}
],
}
bad_deployer = CodeDeployer(None, invalid_appspec)
with self.assertRaises(MissingPropertyError):
bad_deployer.update_task_def_arn('arn:aws:ecs:task-definiton/test')
def test_get_create_deploy_request(self):
test_app = 'test-application'
test_dgp = 'test-deployment-group'
request = self.deployer._get_create_deploy_request(test_app, test_dgp)
self.assertEqual(request['applicationName'], test_app)
self.assertEqual(request['deploymentGroupName'], test_dgp)
actual_appspec = json.loads(
request['revision']['appSpecContent']['content']
)
actual_hash = request['revision']['appSpecContent']['sha256']
self.assertEqual(actual_appspec, self.deployer._appspec_dict)
self.assertEqual(actual_hash, self.deployer._get_appspec_hash())
def test_get_appspec_hash(self):
appspec_str = json.dumps(self.deployer._appspec_dict)
encoded_appspec = compat.ensure_bytes(appspec_str)
expected_hash = hashlib.sha256(encoded_appspec).hexdigest()
actual_hash = self.deployer._get_appspec_hash()
self.assertEqual(actual_hash, expected_hash)
def test_wait_for_deploy_success_default_wait(self):
mock_id = 'd-1234567XX'
expected_stdout = self.deployer.MSG_WAITING.format(
deployment_id=mock_id, wait=30
)
with capture_output() as captured:
self.deployer.wait_for_deploy_success('d-1234567XX', 0)
self.assertEqual(expected_stdout, captured.stdout.getvalue())
def test_wait_for_deploy_success_custom_wait(self):
mock_id = 'd-1234567XX'
mock_wait = 40
expected_stdout = self.deployer.MSG_WAITING.format(
deployment_id=mock_id, wait=mock_wait
)
with capture_output() as captured:
self.deployer.wait_for_deploy_success('d-1234567XX', mock_wait)
self.assertEqual(expected_stdout, captured.stdout.getvalue())
def test_wait_for_deploy_success_max_wait_exceeded(self):
mock_id = 'd-1234567XX'
mock_wait = MAX_WAIT_MIN + 15
expected_stdout = self.deployer.MSG_WAITING.format(
deployment_id=mock_id, wait=MAX_WAIT_MIN
)
with capture_output() as captured:
self.deployer.wait_for_deploy_success('d-1234567XX', mock_wait)
self.assertEqual(expected_stdout, captured.stdout.getvalue())
|