File: test_deploy.py

package info (click to toggle)
awscli 2.31.35-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 156,692 kB
  • sloc: python: 213,816; xml: 14,082; makefile: 189; sh: 178; javascript: 8
file content (176 lines) | stat: -rw-r--r-- 7,173 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import json

from awscli.testutils import BaseAWSCommandParamsTest, FileCreator


class TestDeployCommand(BaseAWSCommandParamsTest):
    def setUp(self):
        super(TestDeployCommand, self).setUp()
        self.files = FileCreator()
        self.parsed_responses = [
            # First it checks to see if a stack with that name exists. So
            # we fake a response indicating that the stack exists and is in
            # an OK state.
            {
                'Stacks': {
                    'StackName': 'Stack',
                    'StackStatus': 'UPDATE_COMPLETE',
                }
            },
            # Now it creates a changeset, so we fake a response with an ID.
            {'Id': 'FakeChangeSetId'},
            # This fakes a failed response from the waiter because the
            # changeset was empty.
            {
                'StackName': 'Stack',
                'Status': 'FAILED',
                'StatusReason': (
                    'The submitted information didn\'t contain changes. '
                    'Submit different information to create a change set.'
                ),
                'ExecutionStatus': 'UNAVAILABLE',
            },
        ]
        # The template is inspected before we make any of the calls so it
        # needs to have valid JSON content.
        path = self.files.create_file('template.json', '{}')
        self.command = (
            'cloudformation deploy --template-file %s ' '--stack-name Stack'
        ) % path

    def tearDown(self):
        self.files.remove_all()
        super(TestDeployCommand, self).tearDown()

    def test_does_return_zero_exit_code_on_empty_changeset_by_default(self):
        self.run_cmd(self.command, expected_rc=0)

    def test_does_return_zero_exit_code_on_empty_changeset(self):
        self.command += ' --no-fail-on-empty-changeset'
        self.run_cmd(self.command, expected_rc=0)

    def test_does_return_non_zero_exit_code_on_empty_changeset(self):
        self.command += ' --fail-on-empty-changeset'
        self.run_cmd(self.command, expected_rc=255)


class TestDeployCommandParameterOverrides(TestDeployCommand):
    def setUp(self):
        super(TestDeployCommandParameterOverrides, self).setUp()
        template = '''{
          "AWSTemplateFormatVersion": "2010-09-09",
          "Parameters": {
            "Key1": {
              "Type": "String"
            },
            "Key2": {
              "Type": "String"
            }
          }
        }'''
        path = self.files.create_file('template.json', template)
        self.command = (
            'cloudformation deploy --template-file %s ' '--stack-name Stack '
        ) % path

    def _assert_parameters_parsed(self):
        self.assertEqual(
            self.operations_called[1][1]['Parameters'],
            [
                {'ParameterKey': 'Key1', 'ParameterValue': 'Value1'},
                {'ParameterKey': 'Key2', 'ParameterValue': 'Value2'},
            ],
        )

    def create_json_file(self, filename, data):
        return self.files.create_file(filename, json.dumps(data))

    def test_parameter_overrides_shorthand(self):
        self.command += ' --parameter-overrides Key1=Value1 Key2=Value2'
        self.run_cmd(self.command)
        self._assert_parameters_parsed()

    def test_parameter_overrides_from_inline_original_json(self):
        original_like_json = ['Key1=Value1', 'Key2=Value2']
        path = self.create_json_file('param.json', original_like_json)
        self.command += ' --parameter-overrides file://%s' % path
        self.run_cmd(self.command)
        self._assert_parameters_parsed()

    def test_parameter_overrides_from_inline_cf_like_json(self):
        cf_like_json = (
            '[{"ParameterKey":"Key1",'
            '"ParameterValue":"Value1"},'
            '{"ParameterKey":"Key2",'
            '"ParameterValue":"Value2"}]'
        )
        self.command += ' --parameter-overrides %s' % cf_like_json
        self.run_cmd(self.command)
        self._assert_parameters_parsed()

    def test_parameter_overrides_from_cf_like_json_file(self):
        cf_like_json = [
            {'ParameterKey': 'Key1', 'ParameterValue': 'Value1'},
            {'ParameterKey': 'Key2', 'ParameterValue': 'Value2'},
        ]
        path = self.create_json_file('param.json', cf_like_json)
        self.command += ' --parameter-overrides file://%s' % path
        self.run_cmd(self.command)
        self._assert_parameters_parsed()

    def test_parameter_overrides_from_inline_codepipeline_like_json(self):
        codepipeline_like_json = (
            '{"Parameters":{"Key1":"Value1",' '"Key2":"Value2"}}'
        )
        self.command += ' --parameter-overrides %s' % codepipeline_like_json
        self.run_cmd(self.command)
        self._assert_parameters_parsed()

    def test_parameter_overrides_from_codepipeline_like_json_file(self):
        codepipeline_like_json = {
            'Parameters': {'Key1': 'Value1', 'Key2': 'Value2'}
        }
        path = self.create_json_file('param.json', codepipeline_like_json)
        self.command += ' --parameter-overrides file://%s' % path
        self.run_cmd(self.command)
        self._assert_parameters_parsed()

    def test_parameter_overrides_from_original_json_file(self):
        original_like_json = ['Key1=Value1', 'Key2=Value2']
        path = self.create_json_file('param.json', original_like_json)
        self.command += ' --parameter-overrides file://%s' % path
        self.run_cmd(self.command, expected_rc=0)
        self._assert_parameters_parsed()

    def test_parameter_overrides_from_invalid_cf_like_json_file(self):
        invalid_cf_like_json = [
            {
                'ParameterKey': 'Key1',
                'ParameterValue': 'Value1',
                'RedundantKey': 'RedundantValue',
            },
            {'ParameterKey': 'Key2', 'ParameterValue': 'Value2'},
        ]
        path = self.create_json_file('param.json', invalid_cf_like_json)
        self.command += ' --parameter-overrides file://%s' % path
        _, err, _ = self.run_cmd(self.command, expected_rc=252)
        self.assertTrue('JSON passed to --parameter-overrides must be' in err)

    def test_parameter_overrides_from_invalid_json(self):
        cf_like_json = {'SomeKey': [{'RedundantKey': 'RedundantValue'}]}
        path = self.create_json_file('param.json', cf_like_json)
        self.command += ' --parameter-overrides file://%s' % path
        _, err, _ = self.run_cmd(self.command, expected_rc=252)
        self.assertTrue('JSON passed to --parameter-overrides must be' in err)