summaryrefslogtreecommitdiff
path: root/tests/software_config/test_heat_config_notify.py
blob: c3d5d209d89646cce853a5026636c327ae9e3f6c (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
207
208
209
#
#    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 cStringIO
import json
import tempfile

import fixtures
import mock

from tests.software_config import common
from tests.software_config import heat_config_notify as hcn


class HeatConfigNotifyTest(common.RunScriptTest):

    data_signal_id = {
        'id': '5555',
        'group': 'script',
        'inputs': [{
            'name': 'deploy_signal_id',
            'value': 'mock://192.0.2.3/foo'
        }],
        'config': 'five'
    }

    data_signal_id_put = {
        'id': '5555',
        'group': 'script',
        'inputs': [{
            'name': 'deploy_signal_id',
            'value': 'mock://192.0.2.3/foo'
        }, {
            'name': 'deploy_signal_verb',
            'value': 'PUT'
        }],
        'config': 'five'
    }

    data_heat_signal = {
        'id': '5555',
        'group': 'script',
        'inputs': [{
            'name': 'deploy_auth_url',
            'value': 'mock://192.0.2.3/auth'
        }, {
            'name': 'deploy_user_id',
            'value': 'aaaa'
        }, {
            'name': 'deploy_password',
            'value': 'password'
        }, {
            'name': 'deploy_project_id',
            'value': 'bbbb'
        }, {
            'name': 'deploy_stack_id',
            'value': 'cccc'
        }, {
            'name': 'deploy_resource_name',
            'value': 'the_resource'
        }],
        'config': 'five'
    }

    def setUp(self):
        super(HeatConfigNotifyTest, self).setUp()
        self.deployed_dir = self.useFixture(fixtures.TempDir())
        hcn.init_logging = mock.MagicMock()

    def write_config_file(self, data):
        config_file = tempfile.NamedTemporaryFile()
        config_file.write(json.dumps(data))
        config_file.flush()
        return config_file

    def test_notify_missing_file(self):

        signal_data = json.dumps({'foo': 'bar'})
        stdin = cStringIO.StringIO(signal_data)

        with self.write_config_file(self.data_signal_id) as config_file:
            config_file_name = config_file.name

        self.assertEqual(
            1, hcn.main(['heat-config-notify', config_file_name], stdin))

    def test_notify_missing_file_arg(self):

        signal_data = json.dumps({'foo': 'bar'})
        stdin = cStringIO.StringIO(signal_data)

        self.assertEqual(
            1, hcn.main(['heat-config-notify'], stdin))

    def test_notify_signal_id(self):
        requests = mock.MagicMock()
        hcn.requests = requests

        requests.post.return_value = '[200]'

        signal_data = json.dumps({'foo': 'bar'})
        stdin = cStringIO.StringIO(signal_data)

        with self.write_config_file(self.data_signal_id) as config_file:
            self.assertEqual(
                0, hcn.main(['heat-config-notify', config_file.name], stdin))

        requests.post.assert_called_once_with(
            'mock://192.0.2.3/foo',
            data=signal_data,
            headers={'content-type': 'application/json'})

    def test_notify_signal_id_put(self):
        requests = mock.MagicMock()
        hcn.requests = requests

        requests.post.return_value = '[200]'

        signal_data = json.dumps({'foo': 'bar'})
        stdin = cStringIO.StringIO(signal_data)

        with self.write_config_file(self.data_signal_id_put) as config_file:
            self.assertEqual(
                0, hcn.main(['heat-config-notify', config_file.name], stdin))

        requests.put.assert_called_once_with(
            'mock://192.0.2.3/foo',
            data=signal_data,
            headers={'content-type': 'application/json'})

    def test_notify_signal_id_empty_data(self):
        requests = mock.MagicMock()
        hcn.requests = requests

        requests.post.return_value = '[200]'

        stdin = cStringIO.StringIO()

        with self.write_config_file(self.data_signal_id) as config_file:
            self.assertEqual(
                0, hcn.main(['heat-config-notify', config_file.name], stdin))

        requests.post.assert_called_once_with(
            'mock://192.0.2.3/foo',
            data='{}',
            headers={'content-type': 'application/json'})

    def test_notify_signal_id_invalid_json_data(self):
        requests = mock.MagicMock()
        hcn.requests = requests

        requests.post.return_value = '[200]'

        stdin = cStringIO.StringIO('{{{"hi')

        with self.write_config_file(self.data_signal_id) as config_file:
            self.assertEqual(
                0, hcn.main(['heat-config-notify', config_file.name], stdin))

        requests.post.assert_called_once_with(
            'mock://192.0.2.3/foo',
            data='{}',
            headers={'content-type': 'application/json'})

    def test_notify_heat_signal(self):
        ksclient = mock.MagicMock()
        hcn.ksclient = ksclient
        ks = mock.MagicMock()
        ksclient.Client.return_value = ks

        heatclient = mock.MagicMock()
        hcn.heatclient = heatclient
        heat = mock.MagicMock()
        heatclient.Client.return_value = heat

        signal_data = json.dumps({'foo': 'bar'})
        stdin = cStringIO.StringIO(signal_data)

        ks.service_catalog.url_for.return_value = 'mock://192.0.2.3/heat'
        heat.resources.signal.return_value = 'all good'

        with self.write_config_file(self.data_heat_signal) as config_file:
            self.assertEqual(
                0, hcn.main(['heat-config-notify', config_file.name], stdin))

        ksclient.Client.assert_called_once_with(
            auth_url='mock://192.0.2.3/auth',
            user_id='aaaa',
            password='password',
            project_id='bbbb')
        ks.service_catalog.url_for.assert_called_once_with(
            service_type='orchestration', endpoint_type='publicURL')

        heatclient.Client.assert_called_once_with(
            '1', 'mock://192.0.2.3/heat', token=ks.auth_token)
        heat.resources.signal.assert_called_once_with(
            'cccc',
            'the_resource',
            data={'foo': 'bar'})