Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/customizations/gamelift/test_uploadbuild.py
1569 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
from argparse import Namespace
14
import contextlib
15
import os
16
import zipfile
17
18
from botocore.session import get_session
19
from botocore.exceptions import ClientError
20
21
from awscli.testutils import unittest, mock, FileCreator
22
from awscli.customizations.gamelift.uploadbuild import UploadBuildCommand
23
from awscli.customizations.gamelift.uploadbuild import zip_directory
24
from awscli.customizations.gamelift.uploadbuild import validate_directory
25
from awscli.compat import StringIO
26
27
28
class TestGetGameSessionLogCommand(unittest.TestCase):
29
def setUp(self):
30
self.create_client_patch = mock.patch(
31
'botocore.session.Session.create_client')
32
self.mock_create_client = self.create_client_patch.start()
33
self.session = get_session()
34
35
self.gamelift_client = mock.Mock()
36
self.s3_client = mock.Mock()
37
self.mock_create_client.side_effect = [
38
self.gamelift_client, self.s3_client
39
]
40
41
self.file_creator = FileCreator()
42
self.upload_file_patch = mock.patch(
43
'awscli.customizations.gamelift.uploadbuild.S3Transfer.upload_file'
44
)
45
self.upload_file_mock = self.upload_file_patch.start()
46
47
self.cmd = UploadBuildCommand(self.session)
48
self._setup_input_output()
49
50
def tearDown(self):
51
self.create_client_patch.stop()
52
self.file_creator.remove_all()
53
self.upload_file_patch.stop()
54
55
def _setup_input_output(self):
56
# Input values
57
self.region = 'us-west-2'
58
self.build_name = 'mybuild'
59
self.build_version = 'myversion'
60
self.build_root = self.file_creator.rootdir
61
62
self.args = [
63
'--name', self.build_name, '--build-version', self.build_version,
64
'--build-root', self.build_root
65
]
66
67
self.global_args = Namespace()
68
self.global_args.region = self.region
69
self.global_args.endpoint_url = None
70
self.global_args.verify_ssl = None
71
72
# Output values
73
self.build_id = 'myid'
74
self.bucket = 'mybucket'
75
self.key = 'mykey'
76
self.access_key = 'myaccesskey'
77
self.secret_key = 'mysecretkey'
78
self.session_token = 'mytoken'
79
80
self.gamelift_client.create_build.return_value = {
81
'Build': {
82
'BuildId': self.build_id
83
}
84
}
85
86
self.gamelift_client.request_upload_credentials.return_value = {
87
'StorageLocation': {
88
'Bucket': self.bucket,
89
'Key': self.key
90
},
91
'UploadCredentials': {
92
'AccessKeyId': self.access_key,
93
'SecretAccessKey': self.secret_key,
94
'SessionToken': self.session_token
95
}
96
}
97
98
def test_upload_build(self):
99
self.file_creator.create_file('tmpfile', 'Some contents')
100
self.cmd(self.args, self.global_args)
101
# Ensure the clients were instantiated correctly.
102
client_creation_args = self.mock_create_client.call_args_list
103
self.assertEqual(
104
client_creation_args,
105
[mock.call('gamelift', region_name=self.region,
106
endpoint_url=None, verify=None),
107
mock.call('s3', aws_access_key_id=self.access_key,
108
aws_secret_access_key=self.secret_key,
109
aws_session_token=self.session_token,
110
region_name=self.region,
111
verify=None)]
112
)
113
114
# Ensure the GameLift client was called correctly.
115
self.gamelift_client.create_build.assert_called_once_with(
116
Name=self.build_name, Version=self.build_version)
117
118
self.gamelift_client.request_upload_credentials.\
119
assert_called_once_with(BuildId=self.build_id)
120
121
# Ensure the underlying S3 transfer call was correct.
122
self.upload_file_mock.assert_called_once_with(
123
mock.ANY, self.bucket, self.key, callback=mock.ANY)
124
125
tempfile_path = self.upload_file_mock.call_args[0][0]
126
# Ensure the temporary zipfile is deleted at the end.
127
self.assertFalse(os.path.exists(tempfile_path))
128
129
def test_upload_build_when_operating_system_is_provided(self):
130
operating_system = 'WINDOWS_2012'
131
self.file_creator.create_file('tmpfile', 'Some contents')
132
self.args = [
133
'--name', self.build_name, '--build-version', self.build_version,
134
'--build-root', self.build_root,
135
'--operating-system', operating_system
136
]
137
self.cmd(self.args, self.global_args)
138
139
# Ensure the GameLift client was called correctly.
140
self.gamelift_client.create_build.assert_called_once_with(
141
Name=self.build_name, Version=self.build_version,
142
OperatingSystem=operating_system)
143
144
def test_error_message_when_directory_is_empty(self):
145
with mock.patch('sys.stderr', StringIO()) as mock_stderr:
146
self.cmd(self.args, self.global_args)
147
self.assertEqual(
148
mock_stderr.getvalue(),
149
'Fail to upload %s. '
150
'The build root directory is empty or does not exist.\n'
151
% (self.build_root)
152
)
153
154
def test_error_message_when_directory_is_not_provided(self):
155
self.args = [
156
'--name', self.build_name,
157
'--build-version', self.build_version,
158
'--build-root', ''
159
]
160
161
with mock.patch('sys.stderr', StringIO()) as mock_stderr:
162
self.cmd(self.args, self.global_args)
163
self.assertEqual(
164
mock_stderr.getvalue(),
165
'Fail to upload %s. '
166
'The build root directory is empty or does not exist.\n' % ('')
167
)
168
169
def test_error_message_when_directory_does_not_exist(self):
170
dir_not_exist = os.path.join(self.build_root, 'does_not_exist')
171
172
self.args = [
173
'--name', self.build_name,
174
'--build-version', self.build_version,
175
'--build-root', dir_not_exist
176
]
177
178
with mock.patch('sys.stderr', StringIO()) as mock_stderr:
179
self.cmd(self.args, self.global_args)
180
self.assertEqual(
181
mock_stderr.getvalue(),
182
'Fail to upload %s. '
183
'The build root directory is empty or does not exist.\n'
184
% (dir_not_exist)
185
)
186
187
def test_temporary_file_does_exist_when_fails(self):
188
self.upload_file_mock.side_effect = ClientError(
189
{'Error': {'Code': 403, 'Message': 'No Access'}}, 'PutObject')
190
with self.assertRaises(ClientError):
191
self.file_creator.create_file('tmpfile', 'Some contents')
192
self.cmd(self.args, self.global_args)
193
tempfile_path = self.upload_file_mock.call_args[0][0]
194
# Make sure the temporary file is removed.
195
self.assertFalse(os.path.exists(tempfile_path))
196
197
def test_upload_build_when_server_sdk_version_is_provided(self):
198
server_sdk_version = '4.0.2'
199
self.file_creator.create_file('tmpfile', 'Some contents')
200
self.args = [
201
'--name', self.build_name, '--build-version', self.build_version,
202
'--build-root', self.build_root,
203
'--server-sdk-version', server_sdk_version
204
]
205
self.cmd(self.args, self.global_args)
206
207
# Ensure the GameLift client was called correctly.
208
self.gamelift_client.create_build.assert_called_once_with(
209
Name=self.build_name, Version=self.build_version,
210
ServerSdkVersion=server_sdk_version)
211
212
213
class TestZipDirectory(unittest.TestCase):
214
def setUp(self):
215
self.file_creator = FileCreator()
216
self.zip_file = self.file_creator.create_file('build.zip', '')
217
self._dir_root = 'mybuild'
218
219
def tearDown(self):
220
self.file_creator.remove_all()
221
222
@property
223
def dir_root(self):
224
return self.file_creator.full_path(self._dir_root)
225
226
def add_to_directory(self, filename):
227
self.file_creator.create_file(
228
os.path.join(self._dir_root, filename), 'Some contents')
229
230
def assert_contents_of_zip_file(self, filenames):
231
zip_file_object = zipfile.ZipFile(
232
self.zip_file, 'r', zipfile.ZIP_DEFLATED)
233
with contextlib.closing(zip_file_object) as zf:
234
ref_zipfiles = []
235
zipfile_contents = zf.namelist()
236
for ref_zipfile in zipfile_contents:
237
if os.sep == '\\':
238
# Internally namelist() represent directories with
239
# forward slashes so we need to account for that if
240
# the separator is a backslash depending on the operating
241
# system.
242
ref_zipfile = ref_zipfile.replace('/', '\\')
243
ref_zipfiles.append(ref_zipfile)
244
self.assertEqual(sorted(ref_zipfiles), filenames)
245
246
def test_single_file(self):
247
self.add_to_directory('foo')
248
zip_directory(self.zip_file, self.dir_root)
249
self.assert_contents_of_zip_file(['foo'])
250
251
def test_multiple_files(self):
252
self.add_to_directory('foo')
253
self.add_to_directory('bar')
254
zip_directory(self.zip_file, self.dir_root)
255
self.assert_contents_of_zip_file(['bar', 'foo'])
256
257
def test_nested_file(self):
258
filename = os.path.join('mydir', 'foo')
259
self.add_to_directory(filename)
260
zip_directory(self.zip_file, self.dir_root)
261
self.assert_contents_of_zip_file([filename])
262
263
264
class TestValidateDirectory(unittest.TestCase):
265
def setUp(self):
266
self.file_creator = FileCreator()
267
self.dir_root = self.file_creator.rootdir
268
269
def tearDown(self):
270
self.file_creator.remove_all()
271
272
def test_directory_contains_single_file(self):
273
self.file_creator.create_file('foo', '')
274
self.assertTrue(validate_directory(self.dir_root))
275
276
def test_directory_contains_file_and_empty_directory(self):
277
dirname = os.path.join(self.dir_root, 'foo')
278
os.makedirs(dirname)
279
self.file_creator.create_file('bar', '')
280
self.assertTrue(validate_directory(self.dir_root))
281
282
def test_nested_file(self):
283
self.file_creator.create_file('mydir/bar', '')
284
self.assertTrue(validate_directory(self.dir_root))
285
286
def test_empty_directory(self):
287
self.assertFalse(validate_directory(self.dir_root))
288
289
def test_nonexistent_directory(self):
290
dir_not_exist = os.path.join(self.dir_root, 'does_not_exist')
291
self.assertFalse(validate_directory(dir_not_exist))
292
293
def test_nonprovided_directory(self):
294
self.assertFalse(validate_directory(''))
295
296
def test_empty_nested_directory(self):
297
dirname = os.path.join(self.dir_root, 'foo')
298
os.makedirs(dirname)
299
self.assertFalse(validate_directory(self.dir_root))
300
301