Path: blob/develop/tests/unit/customizations/ecs/test_codedeployer.py
1569 views
# Copyright 2018 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.1213import hashlib14import json1516from botocore import compat17from awscli.testutils import capture_output, mock, unittest18from awscli.customizations.ecs.deploy import CodeDeployer, MAX_WAIT_MIN19from awscli.customizations.ecs.exceptions import MissingPropertyError202122class TestCodeDeployer(unittest.TestCase):23TEST_APPSPEC = {24"version": 0.0,25"resources": [{26"TestService": {27"type": "AWS::ECS::Service",28"properties": {29"taskDefinition": "arn:aws:ecs:::task-definition/test:1",30"loadBalancerInfo": {31"containerName": "web",32"containerPort": 8033}34}35}36}]37}3839def setUp(self):40waiter = mock.Mock()41waiter.wait.return_value = {}42mock_cd = mock.Mock()43mock_cd.get_waiter.return_value = waiter4445self.deployer = CodeDeployer(mock_cd, self.TEST_APPSPEC)4647def test_update_task_def_arn(self):48test_arn = 'arn:aws:ecs::1234567890:task-definition/new-thing:3'49self.deployer.update_task_def_arn(test_arn)5051appspec_resources = self.deployer._appspec_dict['resources']52for resource in appspec_resources:53actual_arn = \54resource['TestService']['properties']['taskDefinition']55self.assertEqual(actual_arn, test_arn)5657def test_update_task_def_arn_error_required_key(self):58invalid_appspec = {59"version": 0.0,60"resources": [{61"TestFunc": {62"type": "AWS::Lambda::Function",63"properties": {64"name": "some-function"65}66}67}]68}69bad_deployer = CodeDeployer(None, invalid_appspec)7071with self.assertRaises(MissingPropertyError):72bad_deployer.update_task_def_arn('arn:aws:ecs:task-definiton/test')7374def test_get_create_deploy_request(self):75test_app = 'test-application'76test_dgp = 'test-deployment-group'77request = self.deployer._get_create_deploy_request(test_app, test_dgp)7879self.assertEqual(request['applicationName'], test_app)80self.assertEqual(request['deploymentGroupName'], test_dgp)8182actual_appspec = \83json.loads(request['revision']['appSpecContent']['content'])84actual_hash = request['revision']['appSpecContent']['sha256']8586self.assertEqual(actual_appspec, self.deployer._appspec_dict)87self.assertEqual(actual_hash, self.deployer._get_appspec_hash())8889def test_get_appspec_hash(self):90appspec_str = json.dumps(self.deployer._appspec_dict)91encoded_appspec = compat.ensure_bytes(appspec_str)92expected_hash = hashlib.sha256(encoded_appspec).hexdigest()9394actual_hash = self.deployer._get_appspec_hash()95self.assertEqual(actual_hash, expected_hash)9697def test_wait_for_deploy_success_default_wait(self):98mock_id = 'd-1234567XX'99expected_stdout = self.deployer.MSG_WAITING.format(100deployment_id=mock_id, wait=30)101102with capture_output() as captured:103self.deployer.wait_for_deploy_success('d-1234567XX', 0)104self.assertEqual(expected_stdout, captured.stdout.getvalue())105106def test_wait_for_deploy_success_custom_wait(self):107mock_id = 'd-1234567XX'108mock_wait = 40109110expected_stdout = self.deployer.MSG_WAITING.format(111deployment_id=mock_id, wait=mock_wait)112113with capture_output() as captured:114self.deployer.wait_for_deploy_success('d-1234567XX', mock_wait)115self.assertEqual(expected_stdout, captured.stdout.getvalue())116117def test_wait_for_deploy_success_max_wait_exceeded(self):118mock_id = 'd-1234567XX'119mock_wait = MAX_WAIT_MIN + 15120121expected_stdout = self.deployer.MSG_WAITING.format(122deployment_id=mock_id, wait=MAX_WAIT_MIN)123124with capture_output() as captured:125self.deployer.wait_for_deploy_success('d-1234567XX', mock_wait)126self.assertEqual(expected_stdout, captured.stdout.getvalue())127128129