Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/customizations/servicecatalog/test_generateprovisioningartifact.py
1569 views
1
# Copyright 2012-2017 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
14
from argparse import Namespace
15
16
from awscli.customizations.servicecatalog import exceptions
17
from awscli.customizations.servicecatalog.generateprovisioningartifact \
18
import GenerateProvisioningArtifactCommand
19
from awscli.testutils import unittest, mock, capture_output
20
from botocore.compat import json
21
22
23
class TestCreateProvisioningArtifactCommand(unittest.TestCase):
24
def setUp(self):
25
self.session = mock.Mock()
26
self.servicecatalog_client = mock.Mock()
27
self.s3_client = mock.Mock()
28
self.session.create_client.side_effect = [self.s3_client,
29
self.servicecatalog_client]
30
self.session.get_available_regions.return_value = ['us-east-1',
31
'eu-west-1']
32
self.cmd = GenerateProvisioningArtifactCommand(self.session)
33
34
self.args = Namespace()
35
self.args.file_path = 'foo-file-path'
36
self.args.bucket_name = 'foo-bucket-name'
37
self.args.provisioning_artifact_name = 'foo-pa-name'
38
self.args.provisioning_artifact_description = 'foo-pa-desc'
39
self.args.provisioning_artifact_type = 'CLOUD_FORMATION_TEMPLATE'
40
self.args.product_id = 'prod-1234567890abc'
41
42
self.s3_url = "https://s3.amazonaws.com/foo-bucket-name/foo-file-path"
43
44
# set global args
45
self.global_args = Namespace()
46
self.global_args.region = 'us-east-1'
47
self.global_args.endpoint_url = None
48
self.global_args.verify_ssl = None
49
50
@mock.patch('os.path.getsize', return_value=1)
51
def test_happy_path(self, getsize_patch):
52
# Arrange
53
self.servicecatalog_client.create_provisioning_artifact\
54
.return_value = self.get_create_provisioning_artifact_output()
55
expected_pa_detail = self.get_create_provisioning_artifact_output()
56
del expected_pa_detail['ResponseMetadata']
57
expected_response_output = json.dumps(expected_pa_detail,
58
indent=2,
59
ensure_ascii=False)
60
61
# Act
62
with capture_output() as captured:
63
result = self.cmd._run_main(self.args, self.global_args)
64
65
# Assert
66
self.session.create_client.assert_called_with(
67
'servicecatalog',
68
region_name=self.global_args.region,
69
endpoint_url=None,
70
verify=None)
71
self.servicecatalog_client.create_provisioning_artifact.\
72
assert_called_once_with(
73
ProductId=self.args.product_id,
74
Parameters=self.
75
get_provisioning_artifact_parameters(
76
self.args.provisioning_artifact_name,
77
self.args.provisioning_artifact_description,
78
self.args.provisioning_artifact_type
79
)
80
)
81
self.assertEqual(expected_response_output,
82
captured.stdout.getvalue())
83
self.assertEqual(0, result)
84
85
@mock.patch('os.path.getsize', return_value=1)
86
def test_happy_path_unicode(self, getsize_patch):
87
# Arrange
88
self.args.provisioning_artifact_name = u'\u05d1\u05e8\u05d9\u05e6'
89
self.args.provisioning_artifact_description = u'\u00fd\u00a9\u0194'
90
self.servicecatalog_client.create_provisioning_artifact\
91
.return_value = self.get_create_provisioning_artifact_output()
92
expected_pa_detail = self.get_create_provisioning_artifact_output()
93
del expected_pa_detail['ResponseMetadata']
94
expected_response_output = json.dumps(expected_pa_detail,
95
indent=2,
96
ensure_ascii=False)
97
98
# Act
99
with capture_output() as captured:
100
result = self.cmd._run_main(self.args, self.global_args)
101
102
# Assert
103
self.session.create_client.assert_called_with(
104
'servicecatalog',
105
region_name=self.global_args.region,
106
endpoint_url=None,
107
verify=None)
108
self.servicecatalog_client.create_provisioning_artifact.\
109
assert_called_once_with(
110
ProductId=self.args.product_id,
111
Parameters=self.
112
get_provisioning_artifact_parameters(
113
self.args.provisioning_artifact_name,
114
self.args.provisioning_artifact_description,
115
self.args.provisioning_artifact_type
116
)
117
)
118
self.assertEqual(expected_response_output,
119
captured.stdout.getvalue())
120
self.assertEqual(0, result)
121
122
def test_region_not_supported(self):
123
self.global_args.region = 'not-supported-region'
124
with self.assertRaisesRegex(exceptions.InvalidParametersException,
125
"not supported"):
126
self.cmd._run_main(self.args, self.global_args)
127
128
def get_provisioning_artifact_parameters(self, pa_name,
129
pa_description, pa_type):
130
return {
131
'Name': pa_name,
132
'Description': pa_description,
133
'Info': {
134
'LoadTemplateFromURL': self.s3_url
135
},
136
'Type': pa_type
137
}
138
139
def get_create_provisioning_artifact_output(self):
140
return {
141
'Info': {
142
'TemplateUrl': self.s3_url
143
},
144
'ProvisioningArtifactDetail': {
145
'Description': self.args.provisioning_artifact_description,
146
'Id': 'pa-inifmhpr47ft2',
147
'Name': self.args.provisioning_artifact_name,
148
'Type': self.args.provisioning_artifact_type
149
},
150
'Status': "CREATING",
151
'ResponseMetadata': {}
152
}
153
154