Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/customizations/configure/test_addmodel.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
import json
14
import os
15
16
from botocore.session import get_session
17
from botocore.loaders import Loader
18
19
from awscli.customizations.configure.addmodel import get_model_location
20
from awscli.testutils import unittest, FileCreator
21
22
23
class TestGetModelLocation(unittest.TestCase):
24
def setUp(self):
25
self.session = get_session()
26
self.files = FileCreator()
27
28
# Create our own loader for the unit test and not rely on the
29
# customer's actual ~/.aws/models nor the builtin botocore data
30
# directory.
31
self.customer_data_root = os.path.join(self.files.rootdir, 'customer')
32
os.mkdir(self.customer_data_root)
33
34
self.builtin_data_root = os.path.join(self.files.rootdir, 'builtin')
35
os.mkdir(self.builtin_data_root)
36
37
self.data_loader = Loader(
38
[self.customer_data_root, self.builtin_data_root],
39
include_default_search_paths=False
40
)
41
self.data_loader.CUSTOMER_DATA_PATH = self.customer_data_root
42
self.session.register_component('data_loader', self.data_loader)
43
44
# Add some models into the builtin model directory
45
# We are going to add two models. One with a matching service name
46
# and endpoint and another without.
47
self.matching_service = 'matching'
48
self.non_matching_service = 'nonmatching'
49
self.non_matching_prefix = 'nonmatching-prefix'
50
self.default_api_version = '2015-10-01'
51
52
matching_service_path = os.path.join(
53
self.builtin_data_root, self.matching_service,
54
self.default_api_version, 'service-2.json'
55
)
56
os.makedirs(os.path.dirname(matching_service_path))
57
58
non_matching_service_path = os.path.join(
59
self.builtin_data_root, self.non_matching_service,
60
self.default_api_version, 'service-2.json'
61
)
62
os.makedirs(os.path.dirname(non_matching_service_path))
63
64
# Write the models to the builtin directory
65
with open(matching_service_path, 'w') as f:
66
json.dump(self._create_service_definition(
67
self.matching_service, self.default_api_version), f)
68
69
with open(non_matching_service_path, 'w') as f:
70
json.dump(self._create_service_definition(
71
self.non_matching_prefix, self.default_api_version), f)
72
73
def tearDown(self):
74
self.files.remove_all()
75
76
def _create_service_definition(self, endpoint_prefix, api_version):
77
return {
78
"version": "2.0",
79
"metadata": {
80
"apiVersion": api_version,
81
"endpointPrefix": endpoint_prefix,
82
},
83
"operations": {},
84
"shapes": {}
85
}
86
87
def test_get_model_location_for_matching_prefix_and_name(self):
88
model_location = get_model_location(
89
self.session, self._create_service_definition(
90
self.matching_service, self.default_api_version))
91
self.assertEqual(
92
os.path.join(
93
self.data_loader.CUSTOMER_DATA_PATH,
94
self.matching_service, self.default_api_version,
95
'service-2.json'), model_location)
96
97
def test_get_model_location_with_nonmatching_prefix_and_name(self):
98
model_location = get_model_location(
99
self.session, self._create_service_definition(
100
self.non_matching_prefix, self.default_api_version))
101
self.assertEqual(
102
os.path.join(
103
self.data_loader.CUSTOMER_DATA_PATH,
104
self.non_matching_service, self.default_api_version,
105
'service-2.json'), model_location)
106
107
def test_get_model_location_of_nonexistent_service(self):
108
model_location = get_model_location(
109
self.session, self._create_service_definition(
110
'nonexistent', self.default_api_version))
111
self.assertEqual(
112
os.path.join(
113
self.data_loader.CUSTOMER_DATA_PATH,
114
'nonexistent', self.default_api_version,
115
'service-2.json'), model_location)
116
117
def test_get_model_location_when_service_name_provided(self):
118
model_location = get_model_location(
119
self.session, self._create_service_definition(
120
'nonexistent', self.default_api_version), 'override')
121
self.assertEqual(
122
os.path.join(
123
self.data_loader.CUSTOMER_DATA_PATH,
124
'override', self.default_api_version,
125
'service-2.json'), model_location)
126
127
def test_get_model_location_with_non_v2(self):
128
service_definition = self._create_service_definition(
129
'existent', self.default_api_version)
130
service_definition['version'] = '3.0'
131
model_location = get_model_location(self.session, service_definition)
132
self.assertEqual(
133
os.path.join(
134
self.data_loader.CUSTOMER_DATA_PATH,
135
'existent', self.default_api_version,
136
'service-3.json'), model_location)
137
138
def test_get_model_location_with_missing_version(self):
139
service_definition = self._create_service_definition(
140
'existent', self.default_api_version)
141
service_definition.pop('version')
142
model_location = get_model_location(self.session, service_definition)
143
self.assertEqual(
144
os.path.join(
145
self.data_loader.CUSTOMER_DATA_PATH,
146
'existent', self.default_api_version,
147
'service-2.json'), model_location)
148
149