Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/customizations/ecs/test_filehelpers.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
from awscli.testutils import unittest
15
from awscli.customizations.ecs.exceptions import MissingPropertyError
16
from awscli.customizations.ecs.filehelpers import (APP_PREFIX,
17
DGP_PREFIX,
18
find_required_key,
19
get_app_name,
20
get_cluster_name_from_arn,
21
get_deploy_group_name,
22
MAX_CHAR_LENGTH,
23
parse_appspec)
24
25
26
class TestFilehelpers(unittest.TestCase):
27
28
YAML_APPSPEC = """
29
version: 0.0
30
resources:
31
- TestService:
32
type: AWS::ECS::Service
33
properties:
34
taskDefinition: arn:aws:ecs:::task-definition/test:1
35
loadBalancerInfo:
36
containerName: web
37
containerPort: 80
38
"""
39
40
PARSED_APPSPEC = {
41
"version": 0.0,
42
"resources": [{
43
"TestService": {
44
"type": "AWS::ECS::Service",
45
"properties": {
46
"taskDefinition": "arn:aws:ecs:::task-definition/test:1",
47
"loadBalancerInfo": {
48
"containerName": "web",
49
"containerPort": 80
50
}
51
}
52
}
53
}]
54
}
55
56
MIXED_CASE_APPSPEC = {
57
"version": 0.0,
58
"Resources": [{
59
"TestService": {
60
"TYPE": "AWS::ECS::Service",
61
"PROperties": {
62
"TaskDefinition": "arn:aws:ecs:::task-definition/test:1",
63
"loadbalancerInfo": {
64
"containerName": "web",
65
"containerPort": 80
66
}
67
}
68
}
69
}]
70
}
71
72
def test_find_required_key(self):
73
test_properties_dict = {
74
"TaskDefinition": "arn:aws:ecs:::task-definition/test:1",
75
"loadbalancerInfo": {
76
"containerName": "web",
77
"containerPort": 80
78
}
79
}
80
test_key = 'taskDefinition'
81
expected_result = 'TaskDefinition'
82
83
result = find_required_key(
84
'task definition', test_properties_dict, test_key)
85
self.assertEqual(result, expected_result)
86
87
def test_find_required_key_error_missing_key(self):
88
invalid_properties_dict = {
89
'name': 'some-lambda-function'
90
}
91
test_key = 'taskDefinition'
92
93
with self.assertRaises(MissingPropertyError):
94
find_required_key('task definition', invalid_properties_dict, test_key)
95
96
def test_find_required_key_error_empty_object(self):
97
test_key = 'taskDefinition'
98
99
with self.assertRaises(MissingPropertyError):
100
find_required_key('task definition', dict(), test_key)
101
102
def test_get_app_name_long(self):
103
cluster = 'ClusterClusterClusterClusterClusterClusterClusterCluster'
104
service = 'ServiceServiceServiceServiceServiceServiceServiceService'
105
106
expected = APP_PREFIX + cluster[:MAX_CHAR_LENGTH] + '-' + service[:MAX_CHAR_LENGTH]
107
response = get_app_name(service, cluster, None)
108
109
self.assertEqual(expected, response)
110
111
def test_get_app_name_no_cluster(self):
112
service = 'test-service'
113
expected_name = 'AppECS-default-test-service'
114
115
response = get_app_name(service, None, None)
116
self.assertEqual(expected_name, response)
117
118
def test_get_app_name_provided(self):
119
cluster = 'test-cluster'
120
service = 'test-service'
121
app_name = 'test-app'
122
123
response = get_app_name(service, cluster, app_name)
124
self.assertEqual(app_name, response)
125
126
def test_get_cluster_name_from_arn(self):
127
name = "default"
128
arn = 'arn:aws:ecs:::cluster/default'
129
130
response = get_cluster_name_from_arn(arn)
131
self.assertEqual(name, response)
132
133
def test_get_deploy_group_name_long(self):
134
cluster = 'ClusterClusterClusterClusterClusterClusterClusterCluster'
135
service = 'ServiceServiceServiceServiceServiceServiceServiceService'
136
137
expected = DGP_PREFIX + cluster[:MAX_CHAR_LENGTH] + '-' + service[:MAX_CHAR_LENGTH]
138
response = get_deploy_group_name(service, cluster, None)
139
140
self.assertEqual(expected, response)
141
142
def test_get_deploy_group_name_no_cluster(self):
143
service = 'test-service'
144
expected_name = 'DgpECS-default-test-service'
145
146
response = get_deploy_group_name(service, None, None)
147
self.assertEqual(expected_name, response)
148
149
def test_get_deploy_group_name_provided(self):
150
cluster = 'test-cluster'
151
service = 'test-service'
152
dgp_name = 'test-deployment-group'
153
154
response = get_deploy_group_name(service, cluster, dgp_name)
155
self.assertEqual(dgp_name, response)
156
157
""" def test_get_ecs_suffix_no_cluster(self):
158
service = 'myService'
159
expected_suffix = 'default-myService'
160
161
output = get_ecs_suffix(service, None)
162
self.assertEqual(output, expected_suffix)
163
164
def test_get_ecs_suffix_cluster_provided(self):
165
service = 'myService'
166
cluster = 'myCluster'
167
expected_suffix = 'myCluster-myService'
168
169
output = get_ecs_suffix(service, cluster)
170
self.assertEqual(output, expected_suffix) """
171
172
def test_parse_appsec(self):
173
output = parse_appspec(self.YAML_APPSPEC)
174
self.assertEqual(output, self.PARSED_APPSPEC)
175
176
def test_parse_appsec_json(self):
177
output = parse_appspec(str(self.PARSED_APPSPEC))
178
self.assertEqual(output, self.PARSED_APPSPEC)
179