Path: blob/develop/tests/unit/customizations/servicecatalog/test_generateprovisioningartifact.py
1569 views
# Copyright 2012-2017 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 argparse import Namespace1415from awscli.customizations.servicecatalog import exceptions16from awscli.customizations.servicecatalog.generateprovisioningartifact \17import GenerateProvisioningArtifactCommand18from awscli.testutils import unittest, mock, capture_output19from botocore.compat import json202122class TestCreateProvisioningArtifactCommand(unittest.TestCase):23def setUp(self):24self.session = mock.Mock()25self.servicecatalog_client = mock.Mock()26self.s3_client = mock.Mock()27self.session.create_client.side_effect = [self.s3_client,28self.servicecatalog_client]29self.session.get_available_regions.return_value = ['us-east-1',30'eu-west-1']31self.cmd = GenerateProvisioningArtifactCommand(self.session)3233self.args = Namespace()34self.args.file_path = 'foo-file-path'35self.args.bucket_name = 'foo-bucket-name'36self.args.provisioning_artifact_name = 'foo-pa-name'37self.args.provisioning_artifact_description = 'foo-pa-desc'38self.args.provisioning_artifact_type = 'CLOUD_FORMATION_TEMPLATE'39self.args.product_id = 'prod-1234567890abc'4041self.s3_url = "https://s3.amazonaws.com/foo-bucket-name/foo-file-path"4243# set global args44self.global_args = Namespace()45self.global_args.region = 'us-east-1'46self.global_args.endpoint_url = None47self.global_args.verify_ssl = None4849@mock.patch('os.path.getsize', return_value=1)50def test_happy_path(self, getsize_patch):51# Arrange52self.servicecatalog_client.create_provisioning_artifact\53.return_value = self.get_create_provisioning_artifact_output()54expected_pa_detail = self.get_create_provisioning_artifact_output()55del expected_pa_detail['ResponseMetadata']56expected_response_output = json.dumps(expected_pa_detail,57indent=2,58ensure_ascii=False)5960# Act61with capture_output() as captured:62result = self.cmd._run_main(self.args, self.global_args)6364# Assert65self.session.create_client.assert_called_with(66'servicecatalog',67region_name=self.global_args.region,68endpoint_url=None,69verify=None)70self.servicecatalog_client.create_provisioning_artifact.\71assert_called_once_with(72ProductId=self.args.product_id,73Parameters=self.74get_provisioning_artifact_parameters(75self.args.provisioning_artifact_name,76self.args.provisioning_artifact_description,77self.args.provisioning_artifact_type78)79)80self.assertEqual(expected_response_output,81captured.stdout.getvalue())82self.assertEqual(0, result)8384@mock.patch('os.path.getsize', return_value=1)85def test_happy_path_unicode(self, getsize_patch):86# Arrange87self.args.provisioning_artifact_name = u'\u05d1\u05e8\u05d9\u05e6'88self.args.provisioning_artifact_description = u'\u00fd\u00a9\u0194'89self.servicecatalog_client.create_provisioning_artifact\90.return_value = self.get_create_provisioning_artifact_output()91expected_pa_detail = self.get_create_provisioning_artifact_output()92del expected_pa_detail['ResponseMetadata']93expected_response_output = json.dumps(expected_pa_detail,94indent=2,95ensure_ascii=False)9697# Act98with capture_output() as captured:99result = self.cmd._run_main(self.args, self.global_args)100101# Assert102self.session.create_client.assert_called_with(103'servicecatalog',104region_name=self.global_args.region,105endpoint_url=None,106verify=None)107self.servicecatalog_client.create_provisioning_artifact.\108assert_called_once_with(109ProductId=self.args.product_id,110Parameters=self.111get_provisioning_artifact_parameters(112self.args.provisioning_artifact_name,113self.args.provisioning_artifact_description,114self.args.provisioning_artifact_type115)116)117self.assertEqual(expected_response_output,118captured.stdout.getvalue())119self.assertEqual(0, result)120121def test_region_not_supported(self):122self.global_args.region = 'not-supported-region'123with self.assertRaisesRegex(exceptions.InvalidParametersException,124"not supported"):125self.cmd._run_main(self.args, self.global_args)126127def get_provisioning_artifact_parameters(self, pa_name,128pa_description, pa_type):129return {130'Name': pa_name,131'Description': pa_description,132'Info': {133'LoadTemplateFromURL': self.s3_url134},135'Type': pa_type136}137138def get_create_provisioning_artifact_output(self):139return {140'Info': {141'TemplateUrl': self.s3_url142},143'ProvisioningArtifactDetail': {144'Description': self.args.provisioning_artifact_description,145'Id': 'pa-inifmhpr47ft2',146'Name': self.args.provisioning_artifact_name,147'Type': self.args.provisioning_artifact_type148},149'Status': "CREATING",150'ResponseMetadata': {}151}152153154