Path: blob/develop/tests/functional/gamelift/test_upload_build.py
2634 views
# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.1#2# Licensed under the Apache License, Version 2.0 (the "License"). You3# may not use this file except in compliance with the License. A copy of4# the License is located at5#6# http://aws.amazon.com/apache2.0/7#8# or in the "license" file accompanying this file. This file is9# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF10# ANY KIND, either express or implied. See the License for the specific11# language governing permissions and limitations under the License.12import os13from awscli.testutils import BaseAWSCommandParamsTest, FileCreator, mock141516class TestUploadBuild(BaseAWSCommandParamsTest):1718prefix = 'gamelift upload-build'1920def setUp(self):21super().setUp()22self.files = FileCreator()2324def tearDown(self):25super().tearDown()26self.files.remove_all()2728def test_upload_build(self):29self.files.create_file('tmpfile', 'Some contents')30cmdline = self.prefix31cmdline += ' --name mybuild --build-version myversion'32cmdline += f' --build-root {self.files.rootdir}'3334self.parsed_responses = [35{'Build': {'BuildId': 'myid'}},36{'StorageLocation': {37'Bucket': 'mybucket',38'Key': 'mykey'},39'UploadCredentials': {40'AccessKeyId': 'myaccesskey',41'SecretAccessKey': 'mysecretkey',42'SessionToken': 'mytoken'}},43{}44]4546stdout, stderr, rc = self.run_cmd(cmdline, expected_rc=0)4748# First the build is created.49self.assertEqual(len(self.operations_called), 3)50self.assertEqual(self.operations_called[0][0].name, 'CreateBuild')51self.assertEqual(52self.operations_called[0][1],53{'Name': 'mybuild', 'Version': 'myversion'}54)5556# Second the credentials are requested.57self.assertEqual(58self.operations_called[1][0].name, 'RequestUploadCredentials')59self.assertEqual(60self.operations_called[1][1], {'BuildId': 'myid'})6162# The build is then uploaded to S3.63self.assertEqual(self.operations_called[2][0].name, 'PutObject')64self.assertEqual(65self.operations_called[2][1],66{'Body': mock.ANY, 'Bucket': 'mybucket', 'Key': 'mykey'}67)6869# Check the output of the command.70self.assertIn(71f'Successfully uploaded {self.files.rootdir} to AWS GameLift',72stdout)73self.assertIn('Build ID: myid', stdout)7475def test_upload_build_with_operating_system_param(self):76self.files.create_file('tmpfile', 'Some contents')77cmdline = self.prefix78cmdline += ' --name mybuild --build-version myversion'79cmdline += f' --build-root {self.files.rootdir}'80cmdline += ' --operating-system WINDOWS_2012'8182self.parsed_responses = [83{'Build': {'BuildId': 'myid'}},84{'StorageLocation': {85'Bucket': 'mybucket',86'Key': 'mykey'},87'UploadCredentials': {88'AccessKeyId': 'myaccesskey',89'SecretAccessKey': 'mysecretkey',90'SessionToken': 'mytoken'}},91{}92]9394stdout, stderr, rc = self.run_cmd(cmdline, expected_rc=0)9596# First the build is created.97self.assertEqual(len(self.operations_called), 3)98self.assertEqual(self.operations_called[0][0].name, 'CreateBuild')99self.assertEqual(100self.operations_called[0][1],101{'Name': 'mybuild', 'Version': 'myversion',102'OperatingSystem': 'WINDOWS_2012'}103)104105# Second the credentials are requested.106self.assertEqual(107self.operations_called[1][0].name, 'RequestUploadCredentials')108self.assertEqual(109self.operations_called[1][1], {'BuildId': 'myid'})110111# The build is then uploaded to S3.112self.assertEqual(self.operations_called[2][0].name, 'PutObject')113self.assertEqual(114self.operations_called[2][1],115{'Body': mock.ANY, 'Bucket': 'mybucket', 'Key': 'mykey'}116)117118# Check the output of the command.119self.assertIn(120f'Successfully uploaded {self.files.rootdir} to AWS GameLift',121stdout)122self.assertIn('Build ID: myid', stdout)123124def test_upload_build_with_empty_directory(self):125cmdline = self.prefix126cmdline += ' --name mybuild --build-version myversion'127cmdline += f' --build-root {self.files.rootdir}'128129self.parsed_responses = [130{'Build': {'BuildId': 'myid'}},131{'StorageLocation': {132'Bucket': 'mybucket',133'Key': 'mykey'},134'UploadCredentials': {135'AccessKeyId': 'myaccesskey',136'SecretAccessKey': 'mysecretkey',137'SessionToken': 'mytoken'}},138{}139]140141stdout, stderr, rc = self.run_cmd(cmdline, expected_rc=255)142143self.assertIn(144f'Fail to upload {self.files.rootdir}. '145'The build root directory is empty or does not exist.\n',146stderr)147148def test_upload_build_with_nonexistent_directory(self):149dir_not_exist = os.path.join(self.files.rootdir, 'does_not_exist')150151cmdline = self.prefix152cmdline += ' --name mybuild --build-version myversion'153cmdline += f' --build-root {dir_not_exist}'154155self.parsed_responses = [156{'Build': {'BuildId': 'myid'}},157{'StorageLocation': {158'Bucket': 'mybucket',159'Key': 'mykey'},160'UploadCredentials': {161'AccessKeyId': 'myaccesskey',162'SecretAccessKey': 'mysecretkey',163'SessionToken': 'mytoken'}},164{}165]166167stdout, stderr, rc = self.run_cmd(cmdline, expected_rc=255)168169self.assertIn(170f'Fail to upload {dir_not_exist}. '171'The build root directory is empty or does not exist.\n',172stderr)173174def test_upload_build_with_nonprovided_directory(self):175cmdline = self.prefix176cmdline += ' --name mybuild --build-version myversion'177cmdline += ' --build-root {}'.format('""')178179self.parsed_responses = [180{'Build': {'BuildId': 'myid'}},181{'StorageLocation': {182'Bucket': 'mybucket',183'Key': 'mykey'},184'UploadCredentials': {185'AccessKeyId': 'myaccesskey',186'SecretAccessKey': 'mysecretkey',187'SessionToken': 'mytoken'}},188{}189]190191stdout, stderr, rc = self.run_cmd(cmdline, expected_rc=255)192193self.assertIn(194'Fail to upload {}. '195'The build root directory is empty or does not exist.\n'.format('""'),196stderr)197198def test_upload_build_with_server_sdk_version_param(self):199self.files.create_file('tmpfile', 'Some contents')200cmdline = self.prefix201cmdline += ' --name mybuild --build-version myversion'202cmdline += f' --build-root {self.files.rootdir}'203cmdline += ' --server-sdk-version 4.0.2'204205self.parsed_responses = [206{'Build': {'BuildId': 'myid'}},207{'StorageLocation': {208'Bucket': 'mybucket',209'Key': 'mykey'},210'UploadCredentials': {211'AccessKeyId': 'myaccesskey',212'SecretAccessKey': 'mysecretkey',213'SessionToken': 'mytoken'}},214{}215]216217stdout, stderr, rc = self.run_cmd(cmdline, expected_rc=0)218219# First the build is created.220self.assertEqual(len(self.operations_called), 3)221self.assertEqual(self.operations_called[0][0].name, 'CreateBuild')222self.assertEqual(223self.operations_called[0][1],224{'Name': 'mybuild', 'Version': 'myversion',225'ServerSdkVersion': '4.0.2'}226)227228# Second the credentials are requested.229self.assertEqual(230self.operations_called[1][0].name, 'RequestUploadCredentials')231self.assertEqual(232self.operations_called[1][1], {'BuildId': 'myid'})233234# The build is then uploaded to S3.235self.assertEqual(self.operations_called[2][0].name, 'PutObject')236self.assertEqual(237self.operations_called[2][1],238{'Body': mock.ANY, 'Bucket': 'mybucket', 'Key': 'mykey'}239)240241# Check the output of the command.242self.assertIn(243f'Successfully uploaded {self.files.rootdir} to AWS GameLift',244stdout)245self.assertIn('Build ID: myid', stdout)246247def test_upload_build_with_tags_param(self):248self.files.create_file('tmpfile', 'Some contents')249250expected_tags = [251{'Key': 'Key1', 'Value': 'Value1'},252{'Key': 'Key2', 'Value': 'Value2'}253]254255cmdline = self.prefix256cmdline += ' --name mybuild --build-version myversion'257cmdline += f' --build-root {self.files.rootdir}'258cmdline += ' --tags'259for tag in expected_tags:260cmdline += ' {}={}'.format(tag['Key'], tag['Value'])261262self.parsed_responses = [263{'Build': {'BuildId': 'myid'}},264{'StorageLocation': {265'Bucket': 'mybucket',266'Key': 'mykey'},267'UploadCredentials': {268'AccessKeyId': 'myaccesskey',269'SecretAccessKey': 'mysecretkey',270'SessionToken': 'mytoken'}},271{}272]273274stdout, stderr, rc = self.run_cmd(cmdline, expected_rc=0)275276# First the build is created.277self.assertEqual(len(self.operations_called), 3)278self.assertEqual(self.operations_called[0][0].name, 'CreateBuild')279self.assertEqual(280self.operations_called[0][1],281{'Name': 'mybuild', 'Version': 'myversion',282'Tags': expected_tags}283)284285# Second the credentials are requested.286self.assertEqual(287self.operations_called[1][0].name, 'RequestUploadCredentials')288self.assertEqual(289self.operations_called[1][1], {'BuildId': 'myid'})290291# The build is then uploaded to S3.292self.assertEqual(self.operations_called[2][0].name, 'PutObject')293self.assertEqual(294self.operations_called[2][1],295{'Body': mock.ANY, 'Bucket': 'mybucket', 'Key': 'mykey'}296)297298# Check the output of the command.299self.assertIn(300f'Successfully uploaded {self.files.rootdir} to AWS GameLift',301stdout)302self.assertIn('Build ID: myid', stdout)303304305