Path: blob/develop/tests/unit/customizations/configure/test_addmodel.py
1569 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 json13import os1415from botocore.session import get_session16from botocore.loaders import Loader1718from awscli.customizations.configure.addmodel import get_model_location19from awscli.testutils import unittest, FileCreator202122class TestGetModelLocation(unittest.TestCase):23def setUp(self):24self.session = get_session()25self.files = FileCreator()2627# Create our own loader for the unit test and not rely on the28# customer's actual ~/.aws/models nor the builtin botocore data29# directory.30self.customer_data_root = os.path.join(self.files.rootdir, 'customer')31os.mkdir(self.customer_data_root)3233self.builtin_data_root = os.path.join(self.files.rootdir, 'builtin')34os.mkdir(self.builtin_data_root)3536self.data_loader = Loader(37[self.customer_data_root, self.builtin_data_root],38include_default_search_paths=False39)40self.data_loader.CUSTOMER_DATA_PATH = self.customer_data_root41self.session.register_component('data_loader', self.data_loader)4243# Add some models into the builtin model directory44# We are going to add two models. One with a matching service name45# and endpoint and another without.46self.matching_service = 'matching'47self.non_matching_service = 'nonmatching'48self.non_matching_prefix = 'nonmatching-prefix'49self.default_api_version = '2015-10-01'5051matching_service_path = os.path.join(52self.builtin_data_root, self.matching_service,53self.default_api_version, 'service-2.json'54)55os.makedirs(os.path.dirname(matching_service_path))5657non_matching_service_path = os.path.join(58self.builtin_data_root, self.non_matching_service,59self.default_api_version, 'service-2.json'60)61os.makedirs(os.path.dirname(non_matching_service_path))6263# Write the models to the builtin directory64with open(matching_service_path, 'w') as f:65json.dump(self._create_service_definition(66self.matching_service, self.default_api_version), f)6768with open(non_matching_service_path, 'w') as f:69json.dump(self._create_service_definition(70self.non_matching_prefix, self.default_api_version), f)7172def tearDown(self):73self.files.remove_all()7475def _create_service_definition(self, endpoint_prefix, api_version):76return {77"version": "2.0",78"metadata": {79"apiVersion": api_version,80"endpointPrefix": endpoint_prefix,81},82"operations": {},83"shapes": {}84}8586def test_get_model_location_for_matching_prefix_and_name(self):87model_location = get_model_location(88self.session, self._create_service_definition(89self.matching_service, self.default_api_version))90self.assertEqual(91os.path.join(92self.data_loader.CUSTOMER_DATA_PATH,93self.matching_service, self.default_api_version,94'service-2.json'), model_location)9596def test_get_model_location_with_nonmatching_prefix_and_name(self):97model_location = get_model_location(98self.session, self._create_service_definition(99self.non_matching_prefix, self.default_api_version))100self.assertEqual(101os.path.join(102self.data_loader.CUSTOMER_DATA_PATH,103self.non_matching_service, self.default_api_version,104'service-2.json'), model_location)105106def test_get_model_location_of_nonexistent_service(self):107model_location = get_model_location(108self.session, self._create_service_definition(109'nonexistent', self.default_api_version))110self.assertEqual(111os.path.join(112self.data_loader.CUSTOMER_DATA_PATH,113'nonexistent', self.default_api_version,114'service-2.json'), model_location)115116def test_get_model_location_when_service_name_provided(self):117model_location = get_model_location(118self.session, self._create_service_definition(119'nonexistent', self.default_api_version), 'override')120self.assertEqual(121os.path.join(122self.data_loader.CUSTOMER_DATA_PATH,123'override', self.default_api_version,124'service-2.json'), model_location)125126def test_get_model_location_with_non_v2(self):127service_definition = self._create_service_definition(128'existent', self.default_api_version)129service_definition['version'] = '3.0'130model_location = get_model_location(self.session, service_definition)131self.assertEqual(132os.path.join(133self.data_loader.CUSTOMER_DATA_PATH,134'existent', self.default_api_version,135'service-3.json'), model_location)136137def test_get_model_location_with_missing_version(self):138service_definition = self._create_service_definition(139'existent', self.default_api_version)140service_definition.pop('version')141model_location = get_model_location(self.session, service_definition)142self.assertEqual(143os.path.join(144self.data_loader.CUSTOMER_DATA_PATH,145'existent', self.default_api_version,146'service-2.json'), model_location)147148149