GetStartedGuide.rst 19.6 KB
Newer Older
1 2
.. _get-started:

3 4 5
===============
Getting Started
===============
6

7
The first step towards using Avocado is, quite obviously, installing it.
8

9 10 11 12 13 14
.. Note: this section section shares content with the project README
         file.  When editing this section, also sync the content with
         the README file.  Also notice that this file uses a larger
         set of ReST/sphinx statements, which do not look as good on a
         plain README file.

15 16
Installing Avocado
==================
17

18 19 20
Installing from Packages
------------------------

21 22 23 24 25
Fedora
~~~~~~

Avocado is available in stock Fedora 24 and later.  The main package
name is ``python-avocado``, and can be installed with::
26

27
    dnf install python-avocado
28

29
Other available packages (depending on the Avocado version) may include:
30

31 32 33 34 35
* ``python-avocado-examples``: contains example tests and other example files
* ``python2-avocado-plugins-output-html``: HTML job report plugin
* ``python2-avocado-plugins-runner-remote``: execution of jobs on a remote machine
* ``python2-avocado-plugins-runner-vm``: execution of jobs on a libvirt based VM
* ``python2-avocado-plugins-runner-docker``: execution of jobs on a Docker container
36

C
Cleber Rosa 已提交
37 38
.. _fedora-from-avocados-own-repo:

39 40 41 42 43 44 45
Fedora from Avocado's own Repo
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The Avocado project also makes the latest release, and the LTS (Long
Term Stability) releases available from its own package repository.
To use it, first get the package repositories configuration file by
running the following command::
46

47
    sudo curl https://repos-avocadoproject.rhcloud.com/static/avocado-fedora.repo -o /etc/yum.repos.d/avocado.repo
48 49 50 51 52 53 54 55 56 57 58 59

Now check if you have the ``avocado`` and ``avocado-lts`` repositories configured by running::

    sudo dnf repolist avocado avocado-lts
    ...
    repo id      repo name                          status
    avocado      Avocado                            50
    avocado-lts  Avocado LTS (Long Term Stability)  disabled

Regular users of Avocado will want to use the standard ``avocado``
repository, which tracks the latest Avocado releases.  For more
information about the LTS releases, please refer to the `Avocado Long
C
Cleber Rosa 已提交
60
Term Stability`_ thread and to your package management docs on
61 62 63 64 65
how to switch to the ``avocado-lts`` repo.

Finally, after deciding between regular Avocado releases or LTS, you
can install the RPM packages by running the following commands::

66
    dnf install python-avocado
67

68
Additionally, other Avocado packages are available for Fedora:
69

70 71 72 73 74
* ``python-avocado-examples``: contains example tests and other example files
* ``python2-avocado-plugins-output-html``: HTML job report plugin
* ``python2-avocado-plugins-runner-remote``: execution of jobs on a remote machine
* ``python2-avocado-plugins-runner-vm``: execution of jobs on a libvirt based VM
* ``python2-avocado-plugins-runner-docker``: execution of jobs on a Docker container
75

76
Enterprise Linux
77
~~~~~~~~~~~~~~~~
78

79 80 81 82 83 84 85 86 87 88 89
Avocado packages for Enterprise Linux are available from the Avocado
project RPM repository.  Additionally, some packages from the EPEL repo are
necessary, so you need to enable it first.  For EL7, running the
following command should do it::

    yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

Then you must use the Avocado project RHEL repo
(https://repos-avocadoproject.rhcloud.com/static/avocado-el.repo).
Running the following command should give you the basic Avocado
installation ready::
90

91 92
    curl https://repos-avocadoproject.rhcloud.com/static/avocado-el.repo -o /etc/yum.repos.d/avocado.repo
    yum install python-avocado
93

94
Other available packages (depending on the Avocado version) may include:
95

96 97 98 99 100
* ``python-avocado-examples``: contains example tests and other example files
* ``python2-avocado-plugins-output-html``: HTML job report plugin
* ``python2-avocado-plugins-runner-remote``: execution of jobs on a remote machine
* ``python2-avocado-plugins-runner-vm``: execution of jobs on a libvirt based VM
* ``python2-avocado-plugins-runner-docker``: execution of jobs on a Docker container
101

C
Cleber Rosa 已提交
102
The LTS (Long Term Stability) repositories are also available for
103
Enterprise Linux.  Please refer to the `Avocado Long Term
C
Cleber Rosa 已提交
104
Stability`_ thread and to your package management docs on how to
105 106
switch to the ``avocado-lts`` repo.

107 108 109 110 111 112 113 114
OpenSUSE
~~~~~~~~

The `OpenSUSE`_ project packages LTS versions of Avocado.  You can
install packages by running the following commands::

  sudo zypper install avocado

115 116 117 118 119 120 121
Debian
~~~~~~

DEB package support is available in the source tree (look at the
``contrib/packages/debian`` directory.  No actual packages are
provided by the Avocado project or the Debian repos.

122 123
Generic installation from a GIT repository
------------------------------------------
124

125 126 127 128
First make sure you have a basic set of packages installed. The
following applies to Fedora based distributions, please adapt to
your platform::

129
    sudo yum install -y git gcc python-devel python-pip libvirt-devel libyaml-devel redhat-rpm-config xz-devel
130 131

Then to install Avocado from the git repository run::
132

133
    git clone git://github.com/avocado-framework/avocado.git
134
    cd avocado
135
    sudo make requirements
136
    sudo python setup.py install
137 138 139 140 141

Note that `python` and `pip` should point to the Python interpreter version 2.7.x.
If you're having trouble to install, you can try again and use the command line
utilities `python2.7` and `pip2.7`.

142 143 144 145 146 147
Please note that some Avocado functionality may be implemented by
optional plugins.  To install say, the HTML report plugin, run::

    cd optional_plugins/html
    sudo python setup.py install

148 149
If you intend to hack on Avocado, you may want to look at :ref:`hacking-and-using`.

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
Installing from standard Python tools
-------------------------------------

Avocado can also be installed by the standard Python packaging tools,
namely ``pip``.  On most POSIX systems with Python >= 2.7 and ``pip``
available, installation can be performed with the following commands::

  pip install avocado-framework

.. note:: As a design decision, only the dependencies for the core
          Avocado test runner will be installed.  You may notice,
          depending on your system, that some plugins will fail to load,
          due to those missing dependencies.

If you want to install all the requirements for all plugins, you may
attempt to do so by running::

  pip install -r https://raw.githubusercontent.com/avocado-framework/avocado/master/requirements.txt

169 170 171 172
This way you only get the base avocado-framework without the optional
plugins. Additionally the installation requires correctly configured
system with the right compilers, header files and libraries available.
The more predictable and complete Avocado experience can be achieved with
173 174
the official RPM packages.

175 176
Using Avocado
=============
177

178 179
You should first experience Avocado by using the test runner, that is, the command
line tool that will conveniently run your tests and collect their results.
180

181 182 183
Running Tests
-------------

184 185 186
To do so, please run ``avocado`` with the ``run`` sub-command followed by
a test reference, which could be either a path to the file, or a
recognizable name::
187 188 189 190

    $ avocado run /bin/true
    JOB ID    : 381b849a62784228d2fd208d929cc49f310412dc
    JOB LOG   : $HOME/avocado/job-results/job-2014-08-12T15.39-381b849a/job.log
191
     (1/1) /bin/true: PASS (0.01 s)
192
    RESULTS    : PASS 1 | ERROR 0 | FAIL 0 | SKIP 0 | WARN 0 | INTERRUPT 0
193
    JOB TIME   : 0.11 s
194
    JOB HTML  : $HOME/avocado/job-results/job-2014-08-12T15.39-381b849a/html/results.html
195 196 197 198 199

You probably noticed that we used ``/bin/true`` as a test, and in accordance with our
expectations, it passed! These are known as `simple tests`, but there is also another
type of test, which we call `instrumented tests`. See more at :ref:`test-types` or just
keep reading.
200

J
Jun Guo 已提交
201
.. note:: Although in most cases running ``avocado run $test1 $test3 ...`` is
202 203 204 205 206 207
          fine, it can lead to argument vs. test name clashes. The safest
          way to execute tests is ``avocado run --$argument1 --$argument2
          -- $test1 $test2``. Everything after `--` will be considered
          positional arguments, therefore test names (in case of
          ``avocado run``)

208 209
Listing tests
-------------
210

211 212 213 214 215 216 217 218
You have two ways of discovering the tests. You can simulate the execution by
using the ``--dry-run`` argument::

    avocado run /bin/true --dry-run
    JOB ID     : 0000000000000000000000000000000000000000
    JOB LOG    : /tmp/avocado-dry-runSeWniM/job-2015-10-16T15.46-0000000/job.log
     (1/1) /bin/true: SKIP
    RESULTS    : PASS 0 | ERROR 0 | FAIL 0 | SKIP 1 | WARN 0 | INTERRUPT 0
219
    JOB TIME   : 0.10 s
220
    JOB HTML   : /tmp/avocado-dry-runSeWniM/job-2015-10-16T15.46-0000000/html/results.html
221 222 223 224 225 226

which supports all ``run`` arguments, simulates the run and even lists the test params.

The other way is to use ``list`` subcommand that lists the discovered tests
If no arguments provided, Avocado lists "default" tests per each plugin.
The output might look like this::
227 228

    $ avocado list
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    INSTRUMENTED /usr/share/avocado/tests/abort.py
    INSTRUMENTED /usr/share/avocado/tests/datadir.py
    INSTRUMENTED /usr/share/avocado/tests/doublefail.py
    INSTRUMENTED /usr/share/avocado/tests/doublefree.py
    INSTRUMENTED /usr/share/avocado/tests/errortest.py
    INSTRUMENTED /usr/share/avocado/tests/failtest.py
    INSTRUMENTED /usr/share/avocado/tests/fiotest.py
    INSTRUMENTED /usr/share/avocado/tests/gdbtest.py
    INSTRUMENTED /usr/share/avocado/tests/gendata.py
    INSTRUMENTED /usr/share/avocado/tests/linuxbuild.py
    INSTRUMENTED /usr/share/avocado/tests/multiplextest.py
    INSTRUMENTED /usr/share/avocado/tests/passtest.py
    INSTRUMENTED /usr/share/avocado/tests/sleeptenmin.py
    INSTRUMENTED /usr/share/avocado/tests/sleeptest.py
    INSTRUMENTED /usr/share/avocado/tests/synctest.py
    INSTRUMENTED /usr/share/avocado/tests/timeouttest.py
    INSTRUMENTED /usr/share/avocado/tests/trinity.py
    INSTRUMENTED /usr/share/avocado/tests/warntest.py
    INSTRUMENTED /usr/share/avocado/tests/whiteboard.py
248
    ...
249

250 251 252
These Python files are considered by Avocado to contain ``INSTRUMENTED``
tests.

253
Let's now list only the executable shell scripts::
254

255 256 257 258 259 260
    $ avocado list | grep ^SIMPLE
    SIMPLE       /usr/share/avocado/tests/env_variables.sh
    SIMPLE       /usr/share/avocado/tests/output_check.sh
    SIMPLE       /usr/share/avocado/tests/simplewarning.sh
    SIMPLE       /usr/share/avocado/tests/failtest.sh
    SIMPLE       /usr/share/avocado/tests/passtest.sh
261

262 263 264
Here, as mentioned before, ``SIMPLE`` means that those files are executables
treated as simple tests. You can also give the ``--verbose`` or ``-V`` flag to
display files that were found by Avocado, but are not considered Avocado tests::
265 266 267 268 269 270 271 272 273 274 275

    $ avocado list examples/gdb-prerun-scripts/ -V
    Type       file
    NOT_A_TEST examples/gdb-prerun-scripts/README
    NOT_A_TEST examples/gdb-prerun-scripts/pass-sigusr1

    SIMPLE: 0
    INSTRUMENTED: 0
    MISSING: 0
    NOT_A_TEST: 2

276
Notice that the verbose flag also adds summary information.
277

278 279
Writing a Simple Test
=====================
280

281
This very simple example of simple test written in shell script::
282

283 284 285
    $ echo '#!/bin/bash' > /tmp/simple_test.sh
    $ echo 'exit 0' >> /tmp/simple_test.sh
    $ chmod +x /tmp/simple_test.sh
286

287 288 289
Notice that the file is given executable permissions, which is a requirement for
Avocado to treat it as a simple test. Also notice that the script exits with status
code 0, which signals a successful result to Avocado.
290

291 292
Running A More Complex Test Job
===============================
293

294
You can run any number of test in an arbitrary order, as well as mix and match
295
instrumented and simple tests::
296

297
    $ avocado run failtest.py sleeptest.py synctest.py failtest.py synctest.py /tmp/simple_test.sh
298 299
    JOB ID    : 86911e49b5f2c36caeea41307cee4fecdcdfa121
    JOB LOG   : $HOME/avocado/job-results/job-2014-08-12T15.42-86911e49/job.log
300 301 302 303 304
     (1/6) failtest.py:FailTest.test: FAIL (0.00 s)
     (2/6) sleeptest.py:SleepTest.test: PASS (1.00 s)
     (3/6) synctest.py:SyncTest.test: PASS (2.43 s)
     (4/6) failtest.py:FailTest.test: FAIL (0.00 s)
     (5/6) synctest.py:SyncTest.test: PASS (2.44 s)
305
     (6/6) /tmp/simple_test.sh.1: PASS (0.02 s)
306
    RESULTS    : PASS 4 | ERROR 0 | FAIL 2 | SKIP 0 | WARN 0 | INTERRUPT 0
307
    JOB TIME   : 5.98 s
308
    JOB HTML  : $HOME/avocado/job-results/job-2014-08-12T15.42-86911e49/html/results.html
309

310 311 312
Interrupting The Job On First Failed Test (failfast)
====================================================

A
Amador Pahim 已提交
313 314
The Avocado ``run`` command has the option ``--failfast on`` to exit the job
on first failed test::
315

A
Amador Pahim 已提交
316
    $ avocado run --failfast on /bin/true /bin/false /bin/true /bin/true
317 318 319 320 321 322
    JOB ID     : eaf51b8c7d6be966bdf5562c9611b1ec2db3f68a
    JOB LOG    : $HOME/avocado/job-results/job-2016-07-19T09.43-eaf51b8/job.log
     (1/4) /bin/true: PASS (0.01 s)
     (2/4) /bin/false: FAIL (0.01 s)
    Interrupting job (failfast).
    RESULTS    : PASS 1 | ERROR 0 | FAIL 1 | SKIP 2 | WARN 0 | INTERRUPT 0
323
    JOB TIME   : 0.12 s
324
    JOB HTML   : /home/apahim/avocado/job-results/job-2016-07-19T09.43-eaf51b8/html/results.html
325

A
Amador Pahim 已提交
326 327 328 329
The ``--failfast`` option accepts the argument ``off``. Since it's disabled
by default, the ``off`` argument only makes sense in replay jobs, when the
original job was executed with ``--failfast on``.

A
Amador Pahim 已提交
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
Ignoring Missing Test References
================================

When you provide a list of test references, Avocado will try to resolve
all of them to tests. If one or more test references can not be resolved
to tests, the Job will not be created. Example::

    $ avocado run passtest.py badtest.py
    Unable to resolve reference(s) 'badtest.py' with plugins(s) 'file', 'robot', 'external', try running 'avocado list -V badtest.py' to see the details.

But if you want to execute the Job anyway, with the tests that could be
resolved, you can use ``--ignore-missing-references on``. The same message
will appear in the UI, but the Job will be executed::

    $ avocado run passtest.py badtest.py --ignore-missing-references on
    Unable to resolve reference(s) 'badtest.py' with plugins(s) 'file', 'robot', 'external', try running 'avocado list -V badtest.py' to see the details.
    JOB ID     : 85927c113074b9defd64ea595d6d1c3fdfc1f58f
    JOB LOG    : $HOME/avocado/job-results/job-2017-05-17T10.54-85927c1/job.log
     (1/1) passtest.py:PassTest.test: PASS (0.02 s)
    RESULTS    : PASS 1 | ERROR 0 | FAIL 0 | SKIP 0 | WARN 0 | INTERRUPT 0 | CANCEL 0
    JOB TIME   : 0.11 s
    JOB HTML   : $HOME/avocado/job-results/job-2017-05-17T10.54-85927c1/html/results.html

The ``--ignore-missing-references`` option accepts the argument ``off``.
Since it's disabled by default, the ``off`` argument only makes sense in
replay jobs, when the original job was executed with
``--ignore-missing-references on``.

358
.. _running-external-runner:
C
Cleber Rosa 已提交
359

360 361
Running Tests With An External Runner
=====================================
C
Cleber Rosa 已提交
362 363 364 365 366 367 368 369 370 371 372

It's quite common to have organically grown test suites in most
software projects. These usually include a custom built, very specific
test runner that knows how to find and run their own tests.

Still, running those tests inside Avocado may be a good idea for
various reasons, including being able to have results in different
human and machine readable formats, collecting system information
alongside those tests (the Avocado's `sysinfo` functionality), and
more.

373
Avocado makes that possible by means of its "external runner" feature. The
C
Cleber Rosa 已提交
374 375
most basic way of using it is::

376
    $ avocado run --external-runner=/path/to/external_runner foo bar baz
C
Cleber Rosa 已提交
377 378 379

In this example, Avocado will report individual test results for tests
`foo`, `bar` and `baz`. The actual results will be based on the return
380 381
code of individual executions of `/path/to/external_runner foo`,
`/path/to/external_runner bar` and finally `/path/to/external_runner baz`.
C
Cleber Rosa 已提交
382 383

As another way to explain an show how this feature works, think of the
384
"external runner" as some kind of interpreter and the individual tests as
C
Cleber Rosa 已提交
385
anything that this interpreter recognizes and is able to execute. A
386
UNIX shell, say `/bin/sh` could be considered an external runner, and
C
Cleber Rosa 已提交
387 388 389 390
files with shell code could be considered tests::

    $ echo "exit 0" > /tmp/pass
    $ echo "exit 1" > /tmp/fail
391
    $ avocado run --external-runner=/bin/sh /tmp/pass /tmp/fail
C
Cleber Rosa 已提交
392 393 394 395 396
    JOB ID     : 4a2a1d259690cc7b226e33facdde4f628ab30741
    JOB LOG    : /home/<user>/avocado/job-results/job-<date>-<shortid>/job.log
    (1/2) /tmp/pass: PASS (0.01 s)
    (2/2) /tmp/fail: FAIL (0.01 s)
    RESULTS    : PASS 1 | ERROR 0 | FAIL 1 | SKIP 0 | WARN 0 | INTERRUPT 0
397
    JOB TIME   : 0.11 s
398
    JOB HTML   : /home/<user>/avocado/job-results/job-<date>-<shortid>/html/results.html
C
Cleber Rosa 已提交
399 400 401 402 403 404 405 406

This example is pretty obvious, and could be achieved by giving
`/tmp/pass` and `/tmp/fail` shell "shebangs" (`#!/bin/sh`), making
them executable (`chmod +x /tmp/pass /tmp/fail)`, and running them as
"SIMPLE" tests.

But now consider the following example::

407
    $ avocado run --external-runner=/bin/curl http://local-avocado-server:9405/jobs/ \
C
Cleber Rosa 已提交
408 409 410 411 412 413
                                           http://remote-avocado-server:9405/jobs/
    JOB ID     : 56016a1ffffaba02492fdbd5662ac0b958f51e11
    JOB LOG    : /home/<user>/avocado/job-results/job-<date>-<shortid>/job.log
    (1/2) http://local-avocado-server:9405/jobs/: PASS (0.02 s)
    (2/2) http://remote-avocado-server:9405/jobs/: FAIL (3.02 s)
    RESULTS    : PASS 1 | ERROR 0 | FAIL 1 | SKIP 0 | WARN 0 | INTERRUPT 0
414
    JOB TIME   : 3.14 s
415
    JOB HTML   : /home/<user>/avocado/job-results/job-<date>-<shortid>/html/results.html
C
Cleber Rosa 已提交
416

417
This effectively makes `/bin/curl` an "external test runner", responsible for
C
Cleber Rosa 已提交
418 419
trying to fetch those URLs, and reporting PASS or FAIL for each of them.

420
Debugging tests
421
===============
422

423 424 425
Showing test output
-------------------

426 427
When developing new tests, you frequently want to look straight at the
job log, without switching screens or having to "tail" the job log.
428

429 430
In order to do that, you can use ``avocado --show test run ...`` or
``avocado run --show-job-log ...`` options::
431

432
    $ avocado --show test run examples/tests/sleeptest.py
433
    ...
434
    Job ID: f9ea1742134e5352dec82335af584d1f151d4b85
435

436
    START 1-sleeptest.py:SleepTest.test
437

438 439
    PARAMS (key=timeout, path=*, default=None) => None
    PARAMS (key=sleep_length, path=*, default=1) => 1
440
    Sleeping for 1.00 seconds
441
    PASS 1-sleeptest.py:SleepTest.test
442

443
    Test results available in $HOME/avocado/job-results/job-2015-06-02T10.45-f9ea174
444

445 446
As you can see, the UI output is suppressed and only the job log is shown,
making this a useful feature for test development and debugging.
447

448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
Interrupting tests execution
----------------------------

To interrupt a job execution a user can press ``ctrl+c`` which after a single
press sends SIGTERM to the main test's process and waits for it to finish.
If this does not help user can press ``ctrl+c`` again (after 2s grace period)
which destroys the test's process ungracefully and safely finishes the job
execution always providing the test results.

To pause the test execution a user can use ``ctrl+z`` which sends ``SIGSTOP``
to all processes inherited from the test's PID. We do our best to stop all
processes, but the operation is not atomic and some new processes might
not be stopped. Another ``ctrl+z`` sends ``SIGCONT`` to all
processes inherited by the test's PID resuming the execution. Note the
test execution time (concerning the test timeout) are still running while
the test's process is stopped.

The test can also be interrupted by an Avocado feature. One example would
be the `Debugging with GDB` :doc:`DebuggingWithGDB` feature.

For custom interactions it is also possible to use other means like ``pdb``
or ``pydevd`` :doc:`DevelopmentTips` breakpoints. Beware it's not possible
to use ``STDIN`` from tests (unless dark magic is used).

C
Cleber Rosa 已提交
472
.. _Avocado Long Term Stability: https://www.redhat.com/archives/avocado-devel/2016-April/msg00038.html
473
.. _OpenSUSE: https://build.opensuse.org/package/show/Virtualization:Tests/avocado