File: writing-tests.pod

package info (click to toggle)
libvirt-tck 0.1.0~2.git890d1c-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,128 kB
  • sloc: perl: 2,885; sh: 1,180; xml: 992; makefile: 6
file content (433 lines) | stat: -rw-r--r-- 13,885 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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
=pod

=head1 libvirt TCK: An introduction to writing tests

The libvirt TCK provides a framework for testing the correct
operation of libvirt drivers and their integration with the
host operating system virtualization services.

Since the focus is on functional integration testing, the tests
and driven from the public libvirt API, closely replicating the
kind of usage expected from applications using libvirt. The
tests are written in Perl, primarily using the libvirt Perl
language bindings (Sys::Virt / perl-Sys-Virt), and the common
Test::More framework.  The libvirt TCK also provides a number
of helper modules to simplify the process of creating interesting
tests

=head2 Output format for a test case

To enable automated reporting and analysis of test results, there
is a well defined output format that tests must follow. A single
test case consists of a sequence of checks each with a pass/fail
status, the aggregate status giving the pass/fail state of the test
as a whole. This information is presented in a simple, line oriented
text format

The general format can be summarized as

         1..N
         ok 1 Description # Directive
         # Diagnostic
         not ok 2 Description
         ....
         ok N Description

The first line here defines the plan, giving the expected number
of checks that will be run in the test case. This enables the
test harness to determine if a test case crashed / exited earlier
than expected without running all checks. Each line starts with
a word 'ok' or 'not ok' to indicate state of the check, followed
by the check number (assigned incrementally), and a description
of the check performed. Diagnostic comments can be output using
a leading '#' to assist in debugging / interpreting the results.

A more real example would be

         1..4
         ok 1 - Input file opened
         not ok 2 - First line of the input valid
         ok 3 - Read the rest of the file
         not ok 4 - Summarized correctly # TODO Not written yet


This is more or less all that it is neccessary to know about the
output format, though far far more details can be found by reading
the Perl documentation for 'TAP' (aka Test Anything Protocol)
available either in 'man 3 TAP' or 'man 3 Test::Harness::TAP'
depending on the Perl version.


=head2 Writing tests with a compliant output format

As if that output format were not simple enough to understand and
generate, there are helper modules to make this even easier to
deal with. The Perl Test::More  module provides a set of useful
functions can be invoked to perform checks.

The first step is to declare how many checks are intended to be
run. This is typically done at time Test::More is imported into
the script

    use Test::More tests => 15;


The rest of the test case should be a Perl script that implements
the logic you wish to test. At key points throughout the script,
checks can be inserted to validate the state. Depending on the
type of check desired, there are a number of helper functions
available

For a simple boolean condition, the 'ok' function can be used

   ok($boolean, $description);

 eg

   my $id = $dom->get_id;
   ok($id >= 0, "virtual domain ID is greater than or equal to 0");


To compare two pieces of data for equality (or inequality), the
'is'/'isnt' functions are preferred:

   is($expect, $actual, $description);
   isnt($expect, $actual, $description);

 eg

   my $name = $dom->get_name;
   is($name, "apache", "virtual domain has name 'apache'");


To compare a list or hash table, then a deep comparison is
required. NB, if comparing lists, it will also often be desirable
to sort their elements

   my @domains = sort { $a cmp $b } $conn->list_domains;
   is_deeply(\@domains, ['apache', 'dns'], $description);


Finally to output a diagnostic message, the 'diag' command is
suitable

   diag("Checking that the running guest has an ID > -1");


There are quite a few other variations on these functions, and
extensive documentation can be found in the 'Test::More' manual
page.


=head2 Helpers for writing libvirt TCK test checks


While the above functions are useful for testing simple properties
and conditions, they can be a little tedious to use when having to
deal with exceptions and objects.

The libvirt TCK thus provides a couple of helper functions.


The first thing when writing a test is to get a connection to libvirt
and make sure the test environment is clean. ie there are no existing
guests lieing around. If anything goes wrong here, we need to bail out
and not bother with rest of the test. We also want to ensure cleanup
when the test case finishes. For this there is a simple boilerplate
piece of code that can be included


    use Sys::Virt::TCK;

    my $tck = Sys::Virt::TCK->new();
    my $conn = eval { $tck->setup(); };
    BAIL_OUT "failed to setup test harness: $@" if $@;
    END { $tck->cleanup if $tck; }

Going line by line, this first imports the 'Sys::Virt::TCK' package
and its functions. Then it creates an instance of the 'Sys::Virt::TCK'
object. Then it runs the 'setup' method to obtain a libvirt
connection, catching any error that may be thrown. The fourth line
will abort the entire test if an error occurred during setup. The
final line registers a 'END' block which will perform cleanup when
Perl exits.


When testing APIs, it will often be neccessary to create / define real
guest domains with a config. Much of the time the test won't care about
the exact config, just wanting a minimal generic domain config that is
highly likely to work without error. For such cases, a nice simple
API is provided:

   my $xml = $tck->generic_domain("test")->as_xml;

This creates an XML document for a guest that is of the correct OS and
domain type to be able to run on the current hypervisor, with a name
of 'test', and a single disk. It is possible to set further parameters
if required. For example, to set an explicit UUID, give 3 virtual CPUs
and turn on ACPI:

  my $xml = $tck->generic_domain("test")->vcpus(3)
             ->uuid("11111111-1111-2222-3333-444444444444")
             ->with_acpi()->as_xml()

Notice how it allows for chaining the method calls together to build
the domain config, turning it into XML at the last step


If testing a method that is expected to return a virtual domain
object (ie an instance of Sys::Virt::Domain), the 'ok_domain'
helper should be used. This takes 2 or 3 parameters. The first
is the code block to be checks, the second is a description and
the optional third parameter is the expected name of the guest
domain.

 eg to test domain creation from an XML doc


  my $dom;
  ok_domain(sub { $dom = $conn->create_domain($xml) }, \
         "created a running domain", "test");

This creates a new running guest from '$xml', and checks that it
succeeded and returns a domain object with an expected name of
'test'. If an exception was thrown during guest creation this
will be reported as an error. If the guest has the incorrect name,
that will also be reported as an error.



If testing a method that is expected to thrown an exception and
thus not return a value, the 'ok_error' helper should be used.
This takes 2 or 3 parameters. The first is the code block to be
checked, the second is a description and the optional third
parameter is the expected error code.


 eg to ensure that the guest named 'test' does not exists,
    and that an error is raised when attempting to do a
    lookup for it.


    ok_error(sub { $conn->lookup_domain_by_name("test") }, \
           "no such domain error raised", \
            Sys::Virt::Error:ERR_NO_DOMAIN);

This code block attempts to lookup a domain based on its name.
For success, it requires that the domain does not exist and that
libvirt throws an exception with a code VIR_ERR_NO_DOMAIN. If
that does not happen, then a failure will be reported.



=head2 A real test case example walkthrough

This example will illustrate how to test operation of persistent
virtual domains. Our plan for the test is to run the following
sequence of operations

=over 4

=item *

Define a new inactive guest from XML

=item *

Start the guest config

=item *

Stop the running guest

=item *

Undefine the now inactive guest config

=back


There will be certain sanity checks at various stages. For example,
after starting the guest, it will check that the guest ID is greater
than zero. After stopping the guest, it will check the ID is -1.
After undefining the guest, it will check that another lookup fails,
to validate that it really went away.


The first step is to write the core algorithm in Perl code using the
Sys::Virt APIs. Very simplified it looks like this

    my $conn = ...get a libvirt connection...
    my $xml = "....the xml config...";
    my $dom;

    $dom = $conn->define_domain($xml);
    $dom->create;
    $dom->destroy;
    $dom->undefine;


Now it is time to start putting in sanity checks. When defining the
domain, it is neccessary to check that returned a real domain object,
and that no exception is thrown. The 'ok_domain' method can be used
for that. It is also wise to print a diagnostic method before doing
anything interesting

So the define_domain line turns into

     diag "Defining inactive domain config again";
     ok_domain(sub { $dom = $conn->define_domain($xml) }, \
          "defined persistent domain config");


After then starting the domain, the test will check that it has a
proper unique ID number. So the 'create' line turns into


     diag "Starting inactive domain config";
     $dom->create;
     ok($dom->get_id() > 0, "running domain has an ID > 0");


Since this is testing persistent domains, after stopping the running
guest, it should still be possible to look it up. Thus the line that
stops the guest, gains a check for its ID number, followed by another
check that the guest is still present


    diag "Destroying the running domain";
    $dom->destroy();
    is($dom->get_id(), -1 , "inactive domain has an ID == -1");

    diag "Checking there is still an inactive domain config";
    ok_domain(sub { $dom1 = $conn->get_domain_by_name("test") }, \
           "the inactive domain object");


Finally, after undefining the guest it is neccessary to validate that
it really has gone away, by trying to look it up based on name, and
checking that an error is raised


    diag "Undefining the inactive domain config";
    $dom->undefine;

    ok_error(sub { $conn->get_domain_by_name("test") }, \
        "NO_DOMAIN error raised from missing domain", \
        Sys::Virt::Error::ERR_NO_DOMAIN);



=head2 The completed example test script

It is good practice to include a short documentation comment in test
scripts to outline what the script intends to validate. The Perl
POD format is useful for this (see 'man perlpod') for more info.

Taking this into account, the complete example script looks like

    # -*- perl -*-
    #
    # Copyright (C) 2009 A N Other

    =pod

    =head1 NAME

    example-persistent-domain.t - Persistent domain lifecycle

    =head1 DESCRIPTION

    The test case validates the core lifecycle operations on
    persistent domains. A persistent domain is one with a
    configuration enabling it to be tracked when inactive.

    =cut

    use strict;
    use warnings;

    use Test::More tests => 5;

    use Sys::Virt::TCK;

    my $tck = Sys::Virt::TCK->new();
    my $conn = eval { $tck->setup(); };
    BAIL_OUT "failed to setup test harness: $@" if $@;
    END { $tck->cleanup if $tck; }


    my $xml = $tck->generic_domain("test")->as_xml;

    my $dom;
    diag "Defining inactive domain config again";
    ok_domain(sub { $dom = $conn->define_domain($xml) }, \
            "defined persistent domain config");


    diag "Starting inactive domain config";
    $dom->create;
    ok($dom->get_id() > 0, "running domain has an ID > 0");


    diag "Destroying the running domain";
    $dom->destroy();
    is($dom->get_id(), -1 , "inactive domain has an ID == -1");

    diag "Checking there is still an inactive domain config";
    my $dom1;
    ok_domain(sub { $dom1 = $conn->get_domain_by_name("test") }, \
          "the inactive domain object");

    diag "Undefining the inactive domain config";
    $dom->undefine;

    ok_error(sub { $conn->get_domain_by_name("test") }, \
        "NO_DOMAIN error raised from missing domain", \
        Sys::Virt::Error::ERR_NO_DOMAIN);



=head2 Running the test script


Having created the test script it can be run directly using Perl, simply by
setting an environment variable pointing to the config file


   # export LIBVIRT_TCK_CONFIG=/etc/libvirt-tck/xen.cfg
   # perl example-persistent-domain.t

If the libvirt driver being tested were bug-free it would result in the
following output

   1..5
   # Defining inactive domain config again
   ok 1 - defined persistent domain config
   # Starting inactive domain config
   ok 2 - running domain has an ID > 0
   # Destroying the running domain
   ok 3 - inactive domain has an ID == -1
   # Checking there is still an inactive domain config
   ok 4 - the inactive domain object
   # Undefining the inactive domain config
   ok 5 - NO_DOMAIN error raised from missing domain

If something went wrong, it might look like

   1..5
   # Defining inactive domain config again
   ok 1 - defined persistent domain config
   # Starting inactive domain config
   not ok 2 - running domain has an ID > 0
   #   Failed test 'running domain has an ID > 0'
   #   at /home/berrange/ex line 39.
   # Destroying the running domain
   libvirt error code: 7, message: invalid domain pointer in no domain with matching id -1
   # Looks like you planned 5 tests but only ran 2.
   # Looks like you failed 1 test of 2 run.
   # Looks like your test died just after 2.

Notice that since the test script declared upfront that it intended
to run 5 checks, Perl was able to detect that it aborted earlier
than expected.