Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/customizations/codedeploy/test_utils.py
2637 views
1
# Copyright 2015 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 sys
15
16
from socket import timeout
17
from argparse import Namespace
18
from awscli.customizations.codedeploy.systems import Ubuntu, Windows, RHEL, System
19
from awscli.customizations.codedeploy.utils import \
20
validate_region, validate_instance_name, validate_tags, \
21
validate_iam_user_arn, validate_instance, validate_s3_location, \
22
MAX_INSTANCE_NAME_LENGTH, MAX_TAGS_PER_INSTANCE, MAX_TAG_KEY_LENGTH, \
23
MAX_TAG_VALUE_LENGTH
24
from awscli.testutils import mock, unittest
25
26
27
class TestUtils(unittest.TestCase):
28
def setUp(self):
29
self.iam_user_arn = 'arn:aws:iam::012345678912:user/AWS/CodeDeploy/foo'
30
self.region = 'us-east-1'
31
self.arg_name = 's3-location'
32
self.bucket = 'bucket'
33
self.key = 'key'
34
35
self.system_patcher = mock.patch('platform.system')
36
self.system = self.system_patcher.start()
37
self.system.return_value = 'Linux'
38
39
self.linux_distribution_patcher = mock.patch('awscli.compat.linux_distribution')
40
self.linux_distribution = self.linux_distribution_patcher.start()
41
self.linux_distribution.return_value = ('Ubuntu', '', '')
42
43
self.urlopen_patcher = mock.patch(
44
'awscli.customizations.codedeploy.utils.urlopen'
45
)
46
self.urlopen = self.urlopen_patcher.start()
47
self.urlopen.side_effect = timeout('Not EC2 instance')
48
49
self.globals = mock.MagicMock()
50
self.session = mock.MagicMock()
51
self.params = Namespace()
52
self.params.session = self.session
53
54
def tearDown(self):
55
self.system_patcher.stop()
56
self.linux_distribution_patcher.stop()
57
self.urlopen_patcher.stop()
58
59
def test_validate_region_returns_global_region(self):
60
self.globals.region = self.region
61
self.session.get_config_variable.return_value = None
62
validate_region(self.params, self.globals)
63
self.assertIn('region', self.params)
64
self.assertEqual(self.region, self.params.region)
65
66
def test_validate_region_returns_session_region(self):
67
self.globals.region = None
68
self.session.get_config_variable.return_value = self.region
69
validate_region(self.params, self.globals)
70
self.assertIn('region', self.params)
71
self.assertEqual(self.region, self.params.region)
72
73
def test_validate_region_throws_on_no_region(self):
74
self.globals.region = None
75
self.session.get_config_variable.return_value = None
76
with self.assertRaisesRegex(RuntimeError, 'Region not specified.'):
77
validate_region(self.params, self.globals)
78
79
def test_validate_instance_name(self):
80
instance_name = 'instance-name'
81
self.params.instance_name = instance_name
82
validate_instance_name(self.params)
83
84
def test_validate_instance_name_throws_on_invalid_characters(self):
85
self.params.instance_name = '!#$%^&*()<>/?;:[{]}'
86
with self.assertRaisesRegex(
87
ValueError, 'Instance name contains invalid characters.'):
88
validate_instance_name(self.params)
89
90
def test_validate_instance_name_throws_on_i_dash(self):
91
self.params.instance_name = 'i-instance'
92
with self.assertRaisesRegex(
93
ValueError, "Instance name cannot start with 'i-'."):
94
validate_instance_name(self.params)
95
96
def test_validate_instance_name_throws_on_long_name(self):
97
self.params.instance_name = (
98
'01234567890123456789012345678901234567890123456789'
99
'012345678901234567890123456789012345678901234567891'
100
)
101
with self.assertRaisesRegex(
102
ValueError,
103
'Instance name cannot be longer than {0} characters.'.format(
104
MAX_INSTANCE_NAME_LENGTH)):
105
validate_instance_name(self.params)
106
107
def test_validate_tags_throws_on_too_many_tags(self):
108
self.params.tags = [
109
{'Key': 'k' + str(x), 'Value': 'v' + str(x)} for x in range(11)
110
]
111
with self.assertRaisesRegex(
112
ValueError,
113
'Instances can only have a maximum of {0} '
114
'tags.'.format(MAX_TAGS_PER_INSTANCE)):
115
validate_tags(self.params)
116
117
def test_validate_tags_throws_on_max_key_not_accepted(self):
118
key = 'k' * 128
119
self.params.tags = [{'Key': key, 'Value': 'v1'}]
120
validate_tags(self.params)
121
122
def test_validate_tags_throws_on_long_key(self):
123
key = 'k' * 129
124
self.params.tags = [{'Key': key, 'Value': 'v1'}]
125
with self.assertRaisesRegex(
126
ValueError,
127
'Tag Key cannot be longer than {0} characters.'.format(
128
MAX_TAG_KEY_LENGTH)):
129
validate_tags(self.params)
130
131
def test_validate_tags_throws_on_max_value_not_accepted(self):
132
value = 'v' * 256
133
self.params.tags = [{'Key': 'k1', 'Value': value}]
134
validate_tags(self.params)
135
136
def test_validate_tags_throws_on_long_value(self):
137
value = 'v' * 257
138
self.params.tags = [{'Key': 'k1', 'Value': value}]
139
with self.assertRaisesRegex(
140
ValueError,
141
'Tag Value cannot be longer than {0} characters.'.format(
142
MAX_TAG_VALUE_LENGTH)):
143
validate_tags(self.params)
144
145
def test_validate_iam_user_arn(self):
146
self.params.iam_user_arn = self.iam_user_arn
147
validate_iam_user_arn(self.params)
148
149
def test_validate_iam_user_arn_throws_on_invalid_arn_pattern(self):
150
self.params.iam_user_arn = 'invalid-arn-pattern'
151
with self.assertRaisesRegex(ValueError, 'Invalid IAM user ARN.'):
152
validate_iam_user_arn(self.params)
153
154
def test_validate_instance_ubuntu(self):
155
self.urlopen.side_effect = timeout('Not EC2 instance')
156
self.system.return_value = 'Linux'
157
self.linux_distribution.return_value = ('Ubuntu', None, None)
158
self.params.session = self.session
159
self.params.region = self.region
160
validate_instance(self.params)
161
self.assertIn('system', self.params)
162
self.assertTrue(isinstance(self.params.system, Ubuntu))
163
164
def test_validate_instance_rhel(self):
165
self.urlopen.side_effect = timeout('Not EC2 instance')
166
self.system.return_value = 'Linux'
167
self.linux_distribution.return_value = ('Red Hat Enterprise Linux Server', None, None)
168
self.params.session = self.session
169
self.params.region = self.region
170
validate_instance(self.params)
171
self.assertIn('system', self.params)
172
self.assertTrue(isinstance(self.params.system, RHEL))
173
174
def test_validate_instance_windows(self):
175
self.urlopen.side_effect = timeout('Not EC2 instance')
176
self.system.return_value = 'Windows'
177
self.params.session = self.session
178
self.params.region = self.region
179
validate_instance(self.params)
180
self.assertIn('system', self.params)
181
self.assertTrue(isinstance(self.params.system, Windows))
182
183
def test_validate_instance_throws_on_unsupported_system(self):
184
self.system.return_value = 'Unsupported'
185
with self.assertRaisesRegex(
186
RuntimeError, System.UNSUPPORTED_SYSTEM_MSG):
187
validate_instance(self.params)
188
189
def test_validate_instance_throws_on_ec2_instance(self):
190
self.params.session = self.session
191
self.params.region = self.region
192
self.urlopen.side_effect = None
193
with self.assertRaisesRegex(
194
RuntimeError, 'Amazon EC2 instances are not supported.'):
195
validate_instance(self.params)
196
197
def test_validate_s3_location_returns_bucket_key(self):
198
self.params.s3_location = 's3://{0}/{1}'.format(self.bucket, self.key)
199
validate_s3_location(self.params, self.arg_name)
200
self.assertIn('bucket', self.params)
201
self.assertEqual(self.bucket, self.params.bucket)
202
self.assertIn('key', self.params)
203
self.assertEqual(self.key, self.params.key)
204
205
def test_validate_s3_location_not_present(self):
206
validate_s3_location(self.params, 'unknown')
207
self.assertNotIn('bucket', self.params)
208
self.assertNotIn('key', self.params)
209
210
def test_validate_s3_location_throws_on_invalid_location(self):
211
self.params.s3_location = 'invalid-s3-location'
212
with self.assertRaisesRegex(
213
ValueError,
214
'--{0} must specify the Amazon S3 URL format as '
215
's3://<bucket>/<key>.'.format(self.arg_name)):
216
validate_s3_location(self.params, self.arg_name)
217
218
219
if __name__ == "__main__":
220
unittest.main()
221
222