Path: blob/develop/tests/unit/customizations/cloudformation/test_package.py
1569 views
# Copyright 2014 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.0e7#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 os13import sys14import tempfile1516from io import StringIO17from awscli.testutils import mock, unittest, BaseAWSCommandParamsTest18from awscli.customizations.cloudformation.package import PackageCommand19from awscli.customizations.cloudformation.artifact_exporter import Template20from awscli.customizations.cloudformation.yamlhelper import yaml_dump212223class FakeArgs(object):24def __init__(self, **kwargs):25self.__dict__.update(kwargs)2627def __contains__(self, key):28return key in self.__dict__293031def get_example_template():32return {33"Parameters": {34"Key1": "Value1"35},36"Resources": {37"Resource1": {}38}39}404142class TestPackageCommand(unittest.TestCase):4344def setUp(self):45self.session = mock.Mock()46self.session.get_scoped_config.return_value = {}47self.parsed_args = FakeArgs(template_file='./foo',48s3_bucket="s3bucket",49s3_prefix="s3prefix",50kms_key_id="kmskeyid",51output_template_file="./oputput",52use_json=False,53force_upload=False,54metadata=None)55self.parsed_globals = FakeArgs(region="us-east-1", endpoint_url=None,56verify_ssl=None)57self.package_command = PackageCommand(self.session)585960@mock.patch("awscli.customizations.cloudformation.package.yaml_dump")61def test_main(self, mock_yaml_dump):62exported_template_str = "hello"6364self.package_command.write_output = mock.Mock()65self.package_command._export = mock.Mock()66mock_yaml_dump.return_value = exported_template_str6768# Create a temporary file and make this my template69with tempfile.NamedTemporaryFile() as handle:70for use_json in (False, True):71filename = handle.name72self.parsed_args.template_file = filename73self.parsed_args.use_json=use_json7475rc = self.package_command._run_main(self.parsed_args, self.parsed_globals)76self.assertEqual(rc, 0)7778self.package_command._export.assert_called_once_with(filename, use_json)79self.package_command.write_output.assert_called_once_with(80self.parsed_args.output_template_file, mock.ANY)8182self.package_command._export.reset_mock()83self.package_command.write_output.reset_mock()84858687def test_main_error(self):8889self.package_command._export = mock.Mock()90self.package_command._export.side_effect = RuntimeError()9192# Create a temporary file and make this my template93with tempfile.NamedTemporaryFile() as handle:94filename = handle.name95self.parsed_args.template_file = filename9697with self.assertRaises(RuntimeError):98self.package_command._run_main(self.parsed_args, self.parsed_globals)99100101@mock.patch("awscli.customizations.cloudformation.package.sys.stdout")102def test_write_output_to_stdout(self, stdoutmock):103data = u"some data"104filename = None105106self.package_command.write_output(filename, data)107stdoutmock.write.assert_called_once_with(data)108109110