Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/customizations/cloudformation/test_package.py
1569 views
1
# Copyright 2014 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.0e
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 os
14
import sys
15
import tempfile
16
17
from io import StringIO
18
from awscli.testutils import mock, unittest, BaseAWSCommandParamsTest
19
from awscli.customizations.cloudformation.package import PackageCommand
20
from awscli.customizations.cloudformation.artifact_exporter import Template
21
from awscli.customizations.cloudformation.yamlhelper import yaml_dump
22
23
24
class FakeArgs(object):
25
def __init__(self, **kwargs):
26
self.__dict__.update(kwargs)
27
28
def __contains__(self, key):
29
return key in self.__dict__
30
31
32
def get_example_template():
33
return {
34
"Parameters": {
35
"Key1": "Value1"
36
},
37
"Resources": {
38
"Resource1": {}
39
}
40
}
41
42
43
class TestPackageCommand(unittest.TestCase):
44
45
def setUp(self):
46
self.session = mock.Mock()
47
self.session.get_scoped_config.return_value = {}
48
self.parsed_args = FakeArgs(template_file='./foo',
49
s3_bucket="s3bucket",
50
s3_prefix="s3prefix",
51
kms_key_id="kmskeyid",
52
output_template_file="./oputput",
53
use_json=False,
54
force_upload=False,
55
metadata=None)
56
self.parsed_globals = FakeArgs(region="us-east-1", endpoint_url=None,
57
verify_ssl=None)
58
self.package_command = PackageCommand(self.session)
59
60
61
@mock.patch("awscli.customizations.cloudformation.package.yaml_dump")
62
def test_main(self, mock_yaml_dump):
63
exported_template_str = "hello"
64
65
self.package_command.write_output = mock.Mock()
66
self.package_command._export = mock.Mock()
67
mock_yaml_dump.return_value = exported_template_str
68
69
# Create a temporary file and make this my template
70
with tempfile.NamedTemporaryFile() as handle:
71
for use_json in (False, True):
72
filename = handle.name
73
self.parsed_args.template_file = filename
74
self.parsed_args.use_json=use_json
75
76
rc = self.package_command._run_main(self.parsed_args, self.parsed_globals)
77
self.assertEqual(rc, 0)
78
79
self.package_command._export.assert_called_once_with(filename, use_json)
80
self.package_command.write_output.assert_called_once_with(
81
self.parsed_args.output_template_file, mock.ANY)
82
83
self.package_command._export.reset_mock()
84
self.package_command.write_output.reset_mock()
85
86
87
88
def test_main_error(self):
89
90
self.package_command._export = mock.Mock()
91
self.package_command._export.side_effect = RuntimeError()
92
93
# Create a temporary file and make this my template
94
with tempfile.NamedTemporaryFile() as handle:
95
filename = handle.name
96
self.parsed_args.template_file = filename
97
98
with self.assertRaises(RuntimeError):
99
self.package_command._run_main(self.parsed_args, self.parsed_globals)
100
101
102
@mock.patch("awscli.customizations.cloudformation.package.sys.stdout")
103
def test_write_output_to_stdout(self, stdoutmock):
104
data = u"some data"
105
filename = None
106
107
self.package_command.write_output(filename, data)
108
stdoutmock.write.assert_called_once_with(data)
109
110