File: step_5_task_templates.rst

package info (click to toggle)
rally 5.0.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,368 kB
  • sloc: python: 42,541; javascript: 487; sh: 198; makefile: 192; xml: 43
file content (378 lines) | stat: -rw-r--r-- 10,059 bytes parent folder | download | duplicates (5)
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
..
      Copyright 2015 Mirantis Inc. 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. 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.

.. _tutorial_step_5_task_templates:

Step 5. Rally task templates
============================

.. contents::
   :local:

Basic template syntax
---------------------

A nice feature of the input task format used in Rally is that it supports the
**template syntax** based on `Jinja2`_. This turns out to be extremely useful
when, say, you have a fixed structure of your task but you want to parameterize
this task in some way. For example, imagine your input task file (*task.yaml*)
runs a set of Nova scenarios:

.. code-block:: yaml

    ---
      NovaServers.boot_and_delete_server:
        -
          args:
            flavor:
                name: "m1.tiny"
            image:
                name: "^cirros.*-disk$"
          runner:
            type: "constant"
            times: 2
            concurrency: 1
          context:
            users:
              tenants: 1
              users_per_tenant: 1

      NovaServers.resize_server:
        -
          args:
            flavor:
                name: "m1.tiny"
            image:
                name: "^cirros.*-disk$"
            to_flavor:
                name: "m1.small"
          runner:
            type: "constant"
            times: 3
            concurrency: 1
          context:
            users:
              tenants: 1
              users_per_tenant: 1

In both scenarios above, the *"^cirros.*-disk$"* image is passed to the
scenario as an argument (so that these scenarios use an appropriate image while
booting servers). Let's say you want to run the same set of scenarios with the
same runner/context/sla, but you want to try another image while booting server
to compare the performance. The most elegant solution is then to turn the image
name into a template variable:

.. code-block:: yaml

    ---
      NovaServers.boot_and_delete_server:
        -
          args:
            flavor:
                name: "m1.tiny"
            image:
                name: {{image_name}}
          runner:
            type: "constant"
            times: 2
            concurrency: 1
          context:
            users:
              tenants: 1
              users_per_tenant: 1

      NovaServers.resize_server:
        -
          args:
            flavor:
                name: "m1.tiny"
            image:
                name: {{image_name}}
            to_flavor:
                name: "m1.small"
          runner:
            type: "constant"
            times: 3
            concurrency: 1
          context:
            users:
              tenants: 1
              users_per_tenant: 1

and then pass the argument value for **{{image_name}}** when starting a task
with this configuration file. Rally provides you with different ways to do
that:


1. Pass the argument values directly in the command-line interface (with either
a JSON or YAML dictionary):

.. code-block:: bash

    rally task start task.yaml --task-args '{"image_name": "^cirros.*-disk$"}'
    rally task start task.yaml --task-args 'image_name: "^cirros.*-disk$"'

2. Refer to a file that specifies the argument values (JSON/YAML):

.. code-block:: bash

    rally task start task.yaml --task-args-file args.json
    rally task start task.yaml --task-args-file args.yaml

where the files containing argument values should look as follows:

*args.json*:

.. code-block:: json

    {
        "image_name": "^cirros.*-disk$"
    }

*args.yaml*:

.. code-block:: yaml

    ---
      image_name: "^cirros.*-disk$"

Passed in either way, these parameter values will be substituted by Rally when
starting a task:

.. code-block:: console

    $ rally task start task.yaml --task-args "image_name: "^cirros.*-disk$""
    --------------------------------------------------------------------------------
     Preparing input task
    --------------------------------------------------------------------------------

    Input task is:
    ---

      NovaServers.boot_and_delete_server:
        -
          args:
            flavor:
                name: "m1.tiny"
            image:
                name: ^cirros.*-disk$
          runner:
            type: "constant"
            times: 2
            concurrency: 1
          context:
            users:
              tenants: 1
              users_per_tenant: 1

      NovaServers.resize_server:
        -
          args:
            flavor:
                name: "m1.tiny"
            image:
                name: ^cirros.*-disk$
            to_flavor:
                name: "m1.small"
          runner:
            type: "constant"
            times: 3
            concurrency: 1
          context:
            users:
              tenants: 1
              users_per_tenant: 1

    --------------------------------------------------------------------------------
     Task  cbf7eb97-0f1d-42d3-a1f1-3cc6f45ce23f: started
    --------------------------------------------------------------------------------

    Running Task... This can take a while...


Using the default values
------------------------

Note that the ``Jinja2`` template syntax allows you to set the default values
for your parameters. With default values set, your task file will work even if
you don't parameterize it explicitly while starting a task. The default values
should be set using the *{% set ... %}* clause (*task.yaml*):

.. code-block:: yaml

    {% set image_name = image_name or "^cirros.*-disk$" %}
    ---

      NovaServers.boot_and_delete_server:
        -
          args:
            flavor:
                name: "m1.tiny"
            image:
                name: {{image_name}}
          runner:
            type: "constant"
            times: 2
            concurrency: 1
          context:
            users:
              tenants: 1
              users_per_tenant: 1

        ...

If you don't pass the value for *{{image_name}}* while starting a task, the
default one will be used:

.. code-block:: console

    $ rally task start task.yaml
    --------------------------------------------------------------------------------
     Preparing input task
    --------------------------------------------------------------------------------

    Input task is:
    ---

      NovaServers.boot_and_delete_server:
        -
          args:
            flavor:
                name: "m1.tiny"
            image:
                name: ^cirros.*-disk$
          runner:
            type: "constant"
            times: 2
            concurrency: 1
          context:
            users:
              tenants: 1
              users_per_tenant: 1

        ...


Advanced templates
------------------

Rally makes it possible to use all the power of ``Jinja2`` template syntax,
including the mechanism of **built-in functions**. This enables you to
construct elegant task files capable of generating complex load on your cloud.

As an example, let us make up a task file that will create new users with
increasing concurrency. The input task file (*task.yaml*) below uses the
``Jinja2`` **for-endfor** construct to accomplish that:


.. code-block:: yaml

    ---
      KeystoneBasic.create_user:
      {% for i in range(2, 11, 2) %}
        -
          args: {}
          runner:
            type: "constant"
            times: 10
            concurrency: {{i}}
          sla:
            failure_rate:
              max: 0
      {% endfor %}


In this case, you don't need to pass any arguments via
*--task-args/--task-args-file*, but as soon as you start this task, Rally will
automatically unfold the for-loop for you:

.. code-block:: console

    $ rally task start task.yaml
    --------------------------------------------------------------------------------
     Preparing input task
    --------------------------------------------------------------------------------

    Input task is:
    ---

      KeystoneBasic.create_user:

        -
          args: {}
          runner:
            type: "constant"
            times: 10
            concurrency: 2
          sla:
            failure_rate:
              max: 0

        -
          args: {}
          runner:
            type: "constant"
            times: 10
            concurrency: 4
          sla:
            failure_rate:
              max: 0

        -
          args: {}
          runner:
            type: "constant"
            times: 10
            concurrency: 6
          sla:
            failure_rate:
              max: 0

        -
          args: {}
          runner:
            type: "constant"
            times: 10
            concurrency: 8
          sla:
            failure_rate:
              max: 0

        -
          args: {}
          runner:
            type: "constant"
            times: 10
            concurrency: 10
          sla:
            failure_rate:
              max: 0


    --------------------------------------------------------------------------------
     Task  ea7e97e3-dd98-4a81-868a-5bb5b42b8610: started
    --------------------------------------------------------------------------------

    Running Task... This can take a while...

As you can see, the Rally task template syntax is a simple but powerful
mechanism that not only enables you to write elegant task configurations, but
also makes them more readable for other people. When used appropriately, it can
really improve the understanding of your testing procedures in Rally when
shared with others.

.. references:

.. _Jinja2: https://pypi.org/project/Jinja2