Path: blob/develop/tests/unit/customizations/ecs/test_filehelpers.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.1213from awscli.testutils import unittest14from awscli.customizations.ecs.exceptions import MissingPropertyError15from awscli.customizations.ecs.filehelpers import (APP_PREFIX,16DGP_PREFIX,17find_required_key,18get_app_name,19get_cluster_name_from_arn,20get_deploy_group_name,21MAX_CHAR_LENGTH,22parse_appspec)232425class TestFilehelpers(unittest.TestCase):2627YAML_APPSPEC = """28version: 0.029resources:30- TestService:31type: AWS::ECS::Service32properties:33taskDefinition: arn:aws:ecs:::task-definition/test:134loadBalancerInfo:35containerName: web36containerPort: 8037"""3839PARSED_APPSPEC = {40"version": 0.0,41"resources": [{42"TestService": {43"type": "AWS::ECS::Service",44"properties": {45"taskDefinition": "arn:aws:ecs:::task-definition/test:1",46"loadBalancerInfo": {47"containerName": "web",48"containerPort": 8049}50}51}52}]53}5455MIXED_CASE_APPSPEC = {56"version": 0.0,57"Resources": [{58"TestService": {59"TYPE": "AWS::ECS::Service",60"PROperties": {61"TaskDefinition": "arn:aws:ecs:::task-definition/test:1",62"loadbalancerInfo": {63"containerName": "web",64"containerPort": 8065}66}67}68}]69}7071def test_find_required_key(self):72test_properties_dict = {73"TaskDefinition": "arn:aws:ecs:::task-definition/test:1",74"loadbalancerInfo": {75"containerName": "web",76"containerPort": 8077}78}79test_key = 'taskDefinition'80expected_result = 'TaskDefinition'8182result = find_required_key(83'task definition', test_properties_dict, test_key)84self.assertEqual(result, expected_result)8586def test_find_required_key_error_missing_key(self):87invalid_properties_dict = {88'name': 'some-lambda-function'89}90test_key = 'taskDefinition'9192with self.assertRaises(MissingPropertyError):93find_required_key('task definition', invalid_properties_dict, test_key)9495def test_find_required_key_error_empty_object(self):96test_key = 'taskDefinition'9798with self.assertRaises(MissingPropertyError):99find_required_key('task definition', dict(), test_key)100101def test_get_app_name_long(self):102cluster = 'ClusterClusterClusterClusterClusterClusterClusterCluster'103service = 'ServiceServiceServiceServiceServiceServiceServiceService'104105expected = APP_PREFIX + cluster[:MAX_CHAR_LENGTH] + '-' + service[:MAX_CHAR_LENGTH]106response = get_app_name(service, cluster, None)107108self.assertEqual(expected, response)109110def test_get_app_name_no_cluster(self):111service = 'test-service'112expected_name = 'AppECS-default-test-service'113114response = get_app_name(service, None, None)115self.assertEqual(expected_name, response)116117def test_get_app_name_provided(self):118cluster = 'test-cluster'119service = 'test-service'120app_name = 'test-app'121122response = get_app_name(service, cluster, app_name)123self.assertEqual(app_name, response)124125def test_get_cluster_name_from_arn(self):126name = "default"127arn = 'arn:aws:ecs:::cluster/default'128129response = get_cluster_name_from_arn(arn)130self.assertEqual(name, response)131132def test_get_deploy_group_name_long(self):133cluster = 'ClusterClusterClusterClusterClusterClusterClusterCluster'134service = 'ServiceServiceServiceServiceServiceServiceServiceService'135136expected = DGP_PREFIX + cluster[:MAX_CHAR_LENGTH] + '-' + service[:MAX_CHAR_LENGTH]137response = get_deploy_group_name(service, cluster, None)138139self.assertEqual(expected, response)140141def test_get_deploy_group_name_no_cluster(self):142service = 'test-service'143expected_name = 'DgpECS-default-test-service'144145response = get_deploy_group_name(service, None, None)146self.assertEqual(expected_name, response)147148def test_get_deploy_group_name_provided(self):149cluster = 'test-cluster'150service = 'test-service'151dgp_name = 'test-deployment-group'152153response = get_deploy_group_name(service, cluster, dgp_name)154self.assertEqual(dgp_name, response)155156""" def test_get_ecs_suffix_no_cluster(self):157service = 'myService'158expected_suffix = 'default-myService'159160output = get_ecs_suffix(service, None)161self.assertEqual(output, expected_suffix)162163def test_get_ecs_suffix_cluster_provided(self):164service = 'myService'165cluster = 'myCluster'166expected_suffix = 'myCluster-myService'167168output = get_ecs_suffix(service, cluster)169self.assertEqual(output, expected_suffix) """170171def test_parse_appsec(self):172output = parse_appspec(self.YAML_APPSPEC)173self.assertEqual(output, self.PARSED_APPSPEC)174175def test_parse_appsec_json(self):176output = parse_appspec(str(self.PARSED_APPSPEC))177self.assertEqual(output, self.PARSED_APPSPEC)178179