summaryrefslogtreecommitdiff
path: root/heat/tests/test_multi_part.py
blob: 3d3d78679479cc8bc5e4ba9363ad04d8b0fa3e2d (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License 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 email
import uuid

import mock

from heat.common import exception as exc
from heat.engine import parser
from heat.engine.resources.openstack.heat import multi_part as mp
from heat.engine import template
from heat.tests import common
from heat.tests import utils


class MultipartMimeTest(common.HeatTestCase):

    def setUp(self):
        super(MultipartMimeTest, self).setUp()
        self.ctx = utils.dummy_context()
        self.init_config()

    def init_config(self, parts=None):
        parts = parts or []
        stack = parser.Stack(
            self.ctx, 'software_config_test_stack',
            template.Template({
                'HeatTemplateFormatVersion': '2012-12-12',
                'Resources': {
                    'config_mysql': {
                        'Type': 'OS::Heat::MultipartMime',
                        'Properties': {
                            'parts': parts
                        }}}}))
        self.config = stack['config_mysql']
        self.rpc_client = mock.MagicMock()
        self.config._rpc_client = self.rpc_client

    def test_resource_mapping(self):
        mapping = mp.resource_mapping()
        self.assertEqual(1, len(mapping))
        self.assertEqual(mp.MultipartMime,
                         mapping['OS::Heat::MultipartMime'])
        self.assertIsInstance(self.config, mp.MultipartMime)

    def test_handle_create(self):
        config_id = 'c8a19429-7fde-47ea-a42f-40045488226c'
        sc = {'id': config_id}
        self.rpc_client.create_software_config.return_value = sc
        self.config.id = 55
        self.config.uuid = uuid.uuid4().hex
        self.config.handle_create()
        self.assertEqual(config_id, self.config.resource_id)
        kwargs = self.rpc_client.create_software_config.call_args[1]
        self.assertEqual({
            'name': self.config.physical_resource_name(),
            'config': self.config.message,
            'group': 'Heat::Ungrouped'
        }, kwargs)

    def test_get_message_not_none(self):
        self.config.message = 'Not none'
        result = self.config.get_message()
        self.assertEqual('Not none', result)

    def test_get_message_empty_list(self):
        parts = []
        self.init_config(parts=parts)
        result = self.config.get_message()
        message = email.message_from_string(result)
        self.assertTrue(message.is_multipart())

    def test_get_message_text(self):
        parts = [{
            'config': '1e0e5a60-2843-4cfd-9137-d90bdf18eef5',
            'type': 'text'
        }]
        self.init_config(parts=parts)
        self.rpc_client.show_software_config.return_value = {
            'config': '#!/bin/bash'
        }
        result = self.config.get_message()
        self.assertEqual(
            '1e0e5a60-2843-4cfd-9137-d90bdf18eef5',
            self.rpc_client.show_software_config.call_args[0][1])

        message = email.message_from_string(result)
        self.assertTrue(message.is_multipart())
        subs = message.get_payload()
        self.assertEqual(1, len(subs))
        self.assertEqual('#!/bin/bash', subs[0].get_payload())

    def test_get_message_fail_back(self):
        parts = [{
            'config': '#!/bin/bash',
            'type': 'text'
        }]
        self.init_config(parts=parts)
        self.rpc_client.show_software_config.side_effect = exc.NotFound()
        result = self.config.get_message()

        self.assertEqual(
            '#!/bin/bash',
            self.rpc_client.show_software_config.call_args[0][1])

        message = email.message_from_string(result)
        self.assertTrue(message.is_multipart())
        subs = message.get_payload()
        self.assertEqual(1, len(subs))
        self.assertEqual('#!/bin/bash', subs[0].get_payload())

    def test_get_message_text_with_filename(self):
        parts = [{
            'config': '1e0e5a60-2843-4cfd-9137-d90bdf18eef5',
            'type': 'text',
            'filename': '/opt/stack/configure.d/55-heat-config'
        }]
        self.init_config(parts=parts)
        self.rpc_client.show_software_config.return_value = {
            'config': '#!/bin/bash'
        }
        result = self.config.get_message()

        self.assertEqual(
            '1e0e5a60-2843-4cfd-9137-d90bdf18eef5',
            self.rpc_client.show_software_config.call_args[0][1])

        message = email.message_from_string(result)
        self.assertTrue(message.is_multipart())
        subs = message.get_payload()
        self.assertEqual(1, len(subs))
        self.assertEqual('#!/bin/bash', subs[0].get_payload())
        self.assertEqual(parts[0]['filename'], subs[0].get_filename())

    def test_get_message_multi_part(self):
        multipart = ('Content-Type: multipart/mixed; '
                     'boundary="===============2579792489038011818=="\n'
                     'MIME-Version: 1.0\n'
                     '\n--===============2579792489038011818=='
                     '\nContent-Type: text; '
                     'charset="us-ascii"\n'
                     'MIME-Version: 1.0\n'
                     'Content-Transfer-Encoding: 7bit\n'
                     'Content-Disposition: attachment;\n'
                     ' filename="/opt/stack/configure.d/55-heat-config"\n'
                     '#!/bin/bash\n'
                     '--===============2579792489038011818==--\n')
        parts = [{
            'config': '1e0e5a60-2843-4cfd-9137-d90bdf18eef5',
            'type': 'multipart'
        }]
        self.init_config(parts=parts)

        self.rpc_client.show_software_config.return_value = {
            'config': multipart
        }

        result = self.config.get_message()

        self.assertEqual(
            '1e0e5a60-2843-4cfd-9137-d90bdf18eef5',
            self.rpc_client.show_software_config.call_args[0][1])

        message = email.message_from_string(result)
        self.assertTrue(message.is_multipart())
        subs = message.get_payload()
        self.assertEqual(1, len(subs))
        self.assertEqual('#!/bin/bash', subs[0].get_payload())
        self.assertEqual('/opt/stack/configure.d/55-heat-config',
                         subs[0].get_filename())

    def test_get_message_multi_part_bad_format(self):
        parts = [
            {'config': '1e0e5a60-2843-4cfd-9137-d90bdf18eef5',
             'type': 'multipart'},
            {'config': '9cab10ef-16ce-4be9-8b25-a67b7313eddb',
             'type': 'text'}]
        self.init_config(parts=parts)
        self.rpc_client.show_software_config.return_value = {
            'config': '#!/bin/bash'
        }
        result = self.config.get_message()

        self.assertEqual(
            '1e0e5a60-2843-4cfd-9137-d90bdf18eef5',
            self.rpc_client.show_software_config.call_args_list[0][0][1])
        self.assertEqual(
            '9cab10ef-16ce-4be9-8b25-a67b7313eddb',
            self.rpc_client.show_software_config.call_args_list[1][0][1])

        message = email.message_from_string(result)
        self.assertTrue(message.is_multipart())
        subs = message.get_payload()
        self.assertEqual(1, len(subs))
        self.assertEqual('#!/bin/bash', subs[0].get_payload())