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