File: test_software_config.py

package info (click to toggle)
heat-tempest-plugin 1.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 596 kB
  • sloc: python: 4,819; makefile: 21
file content (288 lines) | stat: -rw-r--r-- 10,031 bytes parent folder | download | duplicates (3)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#    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 json
import os
import requests
import subprocess
import sys
import tempfile
import time
import yaml

from oslo_utils import timeutils
from tempest.lib import decorators

from heat_tempest_plugin.common import exceptions
from heat_tempest_plugin.common import test
from heat_tempest_plugin.tests.functional import functional_base


class ParallelDeploymentsTest(functional_base.FunctionalTestsBase):
    server_template = '''
heat_template_version: "2013-05-23"
parameters:
  flavor:
    type: string
  image:
    type: string
  network:
    type: string
resources:
  server:
    type: OS::Nova::Server
    properties:
      image: {get_param: image}
      flavor: {get_param: flavor}
      user_data_format: SOFTWARE_CONFIG
      networks: [{network: {get_param: network}}]
outputs:
  server:
    value: {get_resource: server}
'''

    config_template = '''
heat_template_version: "2013-05-23"
parameters:
  server:
    type: string
resources:
  config:
    type: OS::Heat::SoftwareConfig
    properties:
'''

    deployment_snippet = '''
type: OS::Heat::SoftwareDeployments
properties:
  config: {get_resource: config}
  servers: {'0': {get_param: server}}
'''

    enable_cleanup = True

    @decorators.idempotent_id('8ee231ff-f80a-4a17-a860-5cda87e18ad0')
    def test_deployments_metadata(self):
        parms = {'flavor': self.conf.minimal_instance_type,
                 'network': self.conf.fixed_network_name,
                 'image': self.conf.minimal_image_ref}
        stack_identifier = self.stack_create(
            parameters=parms,
            template=self.server_template,
            enable_cleanup=self.enable_cleanup)
        server_stack = self.client.stacks.get(stack_identifier)
        server = server_stack.outputs[0]['output_value']

        config_stacks = []
        # add up to 3 stacks each with up to 3 deployments
        deploy_count = 0
        deploy_count = self.deploy_many_configs(
            stack_identifier,
            server,
            config_stacks,
            2,
            5,
            deploy_count)
        self.deploy_many_configs(
            stack_identifier,
            server,
            config_stacks,
            3,
            3,
            deploy_count)

        self.signal_deployments(stack_identifier)
        for config_stack in config_stacks:
            self._wait_for_stack_status(config_stack, 'CREATE_COMPLETE')

    @decorators.idempotent_id('bd539232-b999-4bec-b47d-ff4822fc8b82')
    def test_deployments_timeout_failed(self):
        parms = {'flavor': self.conf.minimal_instance_type,
                 'network': self.conf.fixed_network_name,
                 'image': self.conf.minimal_image_ref}
        stack_identifier = self.stack_create(
            parameters=parms,
            template=self.server_template,
            enable_cleanup=self.enable_cleanup)
        server_stack = self.client.stacks.get(stack_identifier)
        server = server_stack.outputs[0]['output_value']
        config_stack = self.deploy_config(server, 3, 1)
        self._wait_for_stack_status(config_stack, 'CREATE_FAILED')
        kwargs = {'server_id': server}

        def check_deployment_status():
            sd_list = self.client.software_deployments.list(**kwargs)
            for sd in sd_list:
                if sd.status != 'FAILED':
                    return False
            return True

        self.assertTrue(test.call_until_true(
            20, 0, check_deployment_status))

    def deploy_many_configs(self, stack, server, config_stacks,
                            stack_count, deploys_per_stack,
                            deploy_count_start):
        for a in range(stack_count):
            config_stacks.append(
                self.deploy_config(server, deploys_per_stack))

        new_count = deploy_count_start + stack_count * deploys_per_stack
        self.wait_for_deploy_metadata_set(stack, new_count)
        return new_count

    def deploy_config(self, server, deploy_count, timeout=None):
        parms = {'server': server}
        template = yaml.safe_load(self.config_template)
        resources = template['resources']
        resources['config']['properties'] = {'config': 'x' * 10000}
        for a in range(deploy_count):
            resources['dep_%s' % a] = yaml.safe_load(self.deployment_snippet)
        return self.stack_create(
            parameters=parms,
            template=template,
            enable_cleanup=self.enable_cleanup,
            expected_status=None,
            timeout=timeout)

    def wait_for_deploy_metadata_set(self, stack, deploy_count):
        build_timeout = self.conf.build_timeout
        build_interval = self.conf.build_interval

        start = timeutils.utcnow()
        while timeutils.delta_seconds(start,
                                      timeutils.utcnow()) < build_timeout:
            server_metadata = self.client.resources.metadata(
                stack, 'server')
            if len(server_metadata['deployments']) == deploy_count:
                return
            time.sleep(build_interval)

        message = ('Deployment resources failed to be created within '
                   'the required time (%s s).' %
                   (build_timeout))
        raise exceptions.TimeoutException(message)

    def signal_deployments(self, stack_identifier):
        server_metadata = self.client.resources.metadata(
            stack_identifier, 'server')
        for dep in server_metadata['deployments']:
            iv = dict((i['name'], i['value']) for i in dep['inputs'])
            sigurl = iv.get('deploy_signal_id')
            requests.post(sigurl, data='{}',
                          headers={'content-type': 'application/json'},
                          verify=self.verify_cert)


@test.requires_service_type('messaging')
class ZaqarSignalTransportTest(functional_base.FunctionalTestsBase):
    server_template = '''
heat_template_version: "2013-05-23"

parameters:
  flavor:
    type: string
  image:
    type: string
  network:
    type: string

resources:
  server:
    type: OS::Nova::Server
    properties:
      image: {get_param: image}
      flavor: {get_param: flavor}
      user_data_format: SOFTWARE_CONFIG
      software_config_transport: ZAQAR_MESSAGE
      networks: [{network: {get_param: network}}]
  config:
    type: OS::Heat::SoftwareConfig
    properties:
      config: echo 'foo'
  deployment:
    type: OS::Heat::SoftwareDeployment
    properties:
      config: {get_resource: config}
      server: {get_resource: server}
      signal_transport: ZAQAR_SIGNAL

outputs:
  data:
    value: {get_attr: [deployment, deploy_stdout]}
'''

    conf_template = '''
[zaqar]
user_id = %(user_id)s
password = %(password)s
project_id = %(project_id)s
auth_url = %(auth_url)s
queue_id = %(queue_id)s
    '''

    @decorators.idempotent_id('3af97ced-bead-4629-b78a-97762719e990')
    def test_signal_queues(self):
        parms = {'flavor': self.conf.minimal_instance_type,
                 'network': self.conf.fixed_network_name,
                 'image': self.conf.minimal_image_ref}
        stack_identifier = self.stack_create(
            parameters=parms,
            template=self.server_template,
            expected_status=None)
        metadata = self.wait_for_deploy_metadata_set(stack_identifier)
        config = metadata['os-collect-config']['zaqar']
        conf_content = self.conf_template % config
        fd, temp_path = tempfile.mkstemp()
        os.write(fd, conf_content.encode('utf-8'))
        os.close(fd)
        cmd = ['os-collect-config', '--one-time',
               '--config-file=%s' % temp_path, 'zaqar']
        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
        stdout_value = proc.communicate()[0]
        data = json.loads(stdout_value.decode('utf-8'))
        self.assertEqual(config, data['zaqar']['os-collect-config']['zaqar'])
        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
        stdout_value = proc.communicate()[0]
        data = json.loads(stdout_value.decode('utf-8'))

        fd, temp_path = tempfile.mkstemp()
        os.write(fd,
                 json.dumps(data['zaqar']['deployments'][0]).encode('utf-8'))
        os.close(fd)
        cmd = [sys.executable, self.conf.heat_config_notify_script, temp_path]
        proc = subprocess.Popen(cmd,
                                stderr=subprocess.PIPE,
                                stdin=subprocess.PIPE)
        proc.communicate(
            json.dumps({'deploy_stdout': 'here!'}).encode('utf-8'))
        self._wait_for_stack_status(stack_identifier, 'CREATE_COMPLETE')
        stack = self.client.stacks.get(stack_identifier)
        self.assertEqual('here!', stack.outputs[0]['output_value'])

    def wait_for_deploy_metadata_set(self, stack):
        build_timeout = self.conf.build_timeout
        build_interval = self.conf.build_interval

        start = timeutils.utcnow()
        while timeutils.delta_seconds(start,
                                      timeutils.utcnow()) < build_timeout:
            server_metadata = self.client.resources.metadata(
                stack, 'server')
            if server_metadata.get('deployments'):
                return server_metadata
            time.sleep(build_interval)

        message = ('Deployment resources failed to be created within '
                   'the required time (%s s).' %
                   (build_timeout))
        raise exceptions.TimeoutException(message)