You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ansible/docs/docsite/rst/dev_guide/developing_collections_test...

84 lines
5.6 KiB
ReStructuredText

Backport/2.11/docs2 (#74484) * Add description for COLLECTIONS_SCAN_SYS_PATH (#74351) Fixes: #74275 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com> (cherry picked from commit 567361b124e79873537704bed7625141c33f35a8) * lighten navigation background to make section labels easier to read for core docs (#74356) * make section labels for /ansible-core/ docs easier to read, with black text and lighter gray background (cherry picked from commit 6119fb0a9a796887d7d8cc34b4501f69251bf4d6) * Correct splitext() description, and example (#74377) `splitext()` returns a 2-tuple of strings, and the last element of the return value includes the `.` (cherry picked from commit c295de661c8e9462f63bbf716c1d5db23b2e93e5) * Using "~" instead of "+" for concatination (#74364) Changed FAQ examples to conform with the Jinja documentation: If both values on either side of a plus/+ are numbers, they will be added whereas using "~" will convert all operands into strings and then concatenate them. Closes #73799. (cherry picked from commit e6a5245d6088894d56b8e0406f8ffed9a57046c3) * Docs - Split Developing collections page, add info on optional module_utils (#74105) * (cherry picked from commit c90922ee3670e049c9afbbaeaca32f16f0ebbd38) * Add weos4 network platform to documentation (#74088) * Add weos4 network platform to documentation * Fix small format issues (cherry picked from commit 7ca5dede978d01f53551da6ae4ccacbad365c88b) * Fix typo in Makefile (#74396) Fixed minor typo specfic -> specific (cherry picked from commit 4880fee6ca89b0733c12f61505d4462d99d428f3) * Update complex_data_manipulation.rst (#72509) (cherry picked from commit c2985c491b68ad34dd761d03da6e0e53e7c6444e) * Update VMware library installation docs (#71219) Depending upon OS/distro, please use pip/pip3. (cherry picked from commit ddfc648d37f12677935b606151d3be42bcb7d4d7) * Update AWS dev guides to use collections utils and fragments (#72312) (cherry picked from commit cf08c23b4f5708c38728da9020441777e0eebcda) * Use is_boto3_error_code in 'standard' example (#72313) Use is_boto3_error_code in 'standard' example rather than e.response['Error']['Code'] (#72313) Co-authored-by: Sloane Hertel <shertel@redhat.com> (cherry picked from commit 63afb33d8669ffc9180e017c4326cd25bf396fd3) * Update Kubernetes collection name in docs (#74440) (cherry picked from commit 8d499bbc83b6029f7e067f9f09abe11229202bdb) * Update argcomplete docs links on installation guide (#74410) Link on installation docs is outdated. Switch to currently docs at: https://kislyuk.github.io/argcomplete/ (cherry picked from commit f97787ca7489c69953710746ccb228c626f6109e) * fix spacing to fix header, reorg contributing page (#74421) Co-authored-by: John R Barker <john@johnrbarker.com> (cherry picked from commit 9d9b08bece5d9c422e8e4cfa1ec2f6f5bdfa6b06) * Update first_found documentation (#70502) * import_tasks do not work with loop, use use include_tasks instead * update documentation (cherry picked from commit bacede7a2b9615e0b1e83aeff69e5c4f080bf791) * Product-related updates. (#74454) (cherry picked from commit 34c9ed8a28e6353b927427fdc72a1ec7078040e6) Co-authored-by: Abhijeet Kasurde <akasurde@redhat.com> Co-authored-by: Sandra McCann <samccann@redhat.com> Co-authored-by: Alex Willmer <alex@moreati.org.uk> Co-authored-by: Hublerho <43293510+Hublerho@users.noreply.github.com> Co-authored-by: Ernst Oudhof <17832702+ernst-s@users.noreply.github.com> Co-authored-by: Hu Shuai <hus.fnst@cn.fujitsu.com> Co-authored-by: dhx-mike-palandra <45608336+dhx-mike-palandra@users.noreply.github.com> Co-authored-by: jakelevinez <31458570+jakelevinez@users.noreply.github.com> Co-authored-by: Mark Chappell <mchappel@redhat.com> Co-authored-by: Lidiane Taquehara <lidi.mayra@gmail.com> Co-authored-by: Alex Domoradov <alex.hha@gmail.com> Co-authored-by: Bill Nottingham <notting@redhat.com>
4 years ago
.. _testing_collections:
*******************
Testing collections
*******************
Testing your collection ensures that your code works well and integrates well with the rest of the Ansible ecosystem. Your collection should pass the general compile and sanity tests for Ansible code. You should also add unit tests to cover the code in your collection and integration tests to cover the interactions between your collection and ansible-core.
.. contents::
:local:
:depth: 2
Testing tools
=============
The main tool for testing collections is ``ansible-test``, Ansible's testing tool described in :ref:`developing_testing`. You can run several compile and sanity checks, as well as run unit and integration tests for plugins using ``ansible-test``. When you test collections, test against the ansible-core version(s) you are targeting.
[docs][2.11] Backportapalooza4 (#74662) * Change Shippable refs in docs to Azure Pipelines. (cherry picked from commit 8bd599d0481c040a2bba2dca4158ff7e1cdaf426) * Update cli_parsing.rst (#74555) Bad indent in an example (cherry picked from commit dcb807dea777434b8a507a08ace25b10947907c7) * systemd doc update (#74442) Document that systemd module manages units (timers, services, ...) and not only service. (#74442) Co-authored-by: Abhijeet Kasurde <akasurde@redhat.com> Co-authored-by: Brian Coca <bcoca@users.noreply.github.com> (cherry picked from commit 74647a1314bd21847347f568fd31720e06a781ec) * Update the module lifecycle document to also cover plugins and collections (#74413) Co-authored-by: Abhijeet Kasurde <akasurde@redhat.com> (cherry picked from commit a6cc5088223a46c06753efe1c5db823a72d516b9) * Docs: communication.rst - add ansible-community to the IRC channel list (#74570) (cherry picked from commit 5463cbb84140eafea5cc6a2020dc85cd0c9e6eb0) * adhoc clarify help (#74616) also added comments to code (cherry picked from commit 7f7d3067e3bf69be35d056d44e06981ff1a55a4d) * update docs copyright (#74643) (cherry picked from commit 719536763533598b43fb06521bd4064b8c7ef48f) * docs: Add a note about `crypt` (#74640) Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com> (cherry picked from commit 79e12ba98ef9d329bc416d1ca8a309b9194cf239) Co-authored-by: Matt Clay <matt@mystile.com> Co-authored-by: Joseph Jacobs <joej164@gmail.com> Co-authored-by: Baptiste Mille-Mathias <baptiste.millemathias@gmail.com> Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Andrew Klychkov <aklychko@redhat.com> Co-authored-by: Brian Coca <bcoca@users.noreply.github.com> Co-authored-by: Abhijeet Kasurde <akasurde@redhat.com>
4 years ago
You must always execute ``ansible-test`` from the root directory of a collection. You can run ``ansible-test`` in Docker containers without installing any special requirements. The Ansible team uses this approach in Azure Pipelines both in the ansible/ansible GitHub repository and in the large community collections such as `community.general <https://github.com/ansible-collections/community.general/>`_ and `community.network <https://github.com/ansible-collections/community.network/>`_. The examples below demonstrate running tests in Docker containers.
Backport/2.11/docs2 (#74484) * Add description for COLLECTIONS_SCAN_SYS_PATH (#74351) Fixes: #74275 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com> (cherry picked from commit 567361b124e79873537704bed7625141c33f35a8) * lighten navigation background to make section labels easier to read for core docs (#74356) * make section labels for /ansible-core/ docs easier to read, with black text and lighter gray background (cherry picked from commit 6119fb0a9a796887d7d8cc34b4501f69251bf4d6) * Correct splitext() description, and example (#74377) `splitext()` returns a 2-tuple of strings, and the last element of the return value includes the `.` (cherry picked from commit c295de661c8e9462f63bbf716c1d5db23b2e93e5) * Using "~" instead of "+" for concatination (#74364) Changed FAQ examples to conform with the Jinja documentation: If both values on either side of a plus/+ are numbers, they will be added whereas using "~" will convert all operands into strings and then concatenate them. Closes #73799. (cherry picked from commit e6a5245d6088894d56b8e0406f8ffed9a57046c3) * Docs - Split Developing collections page, add info on optional module_utils (#74105) * (cherry picked from commit c90922ee3670e049c9afbbaeaca32f16f0ebbd38) * Add weos4 network platform to documentation (#74088) * Add weos4 network platform to documentation * Fix small format issues (cherry picked from commit 7ca5dede978d01f53551da6ae4ccacbad365c88b) * Fix typo in Makefile (#74396) Fixed minor typo specfic -> specific (cherry picked from commit 4880fee6ca89b0733c12f61505d4462d99d428f3) * Update complex_data_manipulation.rst (#72509) (cherry picked from commit c2985c491b68ad34dd761d03da6e0e53e7c6444e) * Update VMware library installation docs (#71219) Depending upon OS/distro, please use pip/pip3. (cherry picked from commit ddfc648d37f12677935b606151d3be42bcb7d4d7) * Update AWS dev guides to use collections utils and fragments (#72312) (cherry picked from commit cf08c23b4f5708c38728da9020441777e0eebcda) * Use is_boto3_error_code in 'standard' example (#72313) Use is_boto3_error_code in 'standard' example rather than e.response['Error']['Code'] (#72313) Co-authored-by: Sloane Hertel <shertel@redhat.com> (cherry picked from commit 63afb33d8669ffc9180e017c4326cd25bf396fd3) * Update Kubernetes collection name in docs (#74440) (cherry picked from commit 8d499bbc83b6029f7e067f9f09abe11229202bdb) * Update argcomplete docs links on installation guide (#74410) Link on installation docs is outdated. Switch to currently docs at: https://kislyuk.github.io/argcomplete/ (cherry picked from commit f97787ca7489c69953710746ccb228c626f6109e) * fix spacing to fix header, reorg contributing page (#74421) Co-authored-by: John R Barker <john@johnrbarker.com> (cherry picked from commit 9d9b08bece5d9c422e8e4cfa1ec2f6f5bdfa6b06) * Update first_found documentation (#70502) * import_tasks do not work with loop, use use include_tasks instead * update documentation (cherry picked from commit bacede7a2b9615e0b1e83aeff69e5c4f080bf791) * Product-related updates. (#74454) (cherry picked from commit 34c9ed8a28e6353b927427fdc72a1ec7078040e6) Co-authored-by: Abhijeet Kasurde <akasurde@redhat.com> Co-authored-by: Sandra McCann <samccann@redhat.com> Co-authored-by: Alex Willmer <alex@moreati.org.uk> Co-authored-by: Hublerho <43293510+Hublerho@users.noreply.github.com> Co-authored-by: Ernst Oudhof <17832702+ernst-s@users.noreply.github.com> Co-authored-by: Hu Shuai <hus.fnst@cn.fujitsu.com> Co-authored-by: dhx-mike-palandra <45608336+dhx-mike-palandra@users.noreply.github.com> Co-authored-by: jakelevinez <31458570+jakelevinez@users.noreply.github.com> Co-authored-by: Mark Chappell <mchappel@redhat.com> Co-authored-by: Lidiane Taquehara <lidi.mayra@gmail.com> Co-authored-by: Alex Domoradov <alex.hha@gmail.com> Co-authored-by: Bill Nottingham <notting@redhat.com>
4 years ago
Compile and sanity tests
------------------------
To run all compile and sanity tests::
ansible-test sanity --docker default -v
See :ref:`testing_compile` and :ref:`testing_sanity` for more information. See the :ref:`full list of sanity tests <all_sanity_tests>` for details on the sanity tests and how to fix identified issues.
Adding unit tests
-----------------
You must place unit tests in the appropriate``tests/unit/plugins/`` directory. For example, you would place tests for ``plugins/module_utils/foo/bar.py`` in ``tests/unit/plugins/module_utils/foo/test_bar.py`` or ``tests/unit/plugins/module_utils/foo/bar/test_bar.py``. For examples, see the `unit tests in community.general <https://github.com/ansible-collections/community.general/tree/master/tests/unit/>`_.
To run all unit tests for all supported Python versions::
ansible-test units --docker default -v
To run all unit tests only for a specific Python version::
ansible-test units --docker default -v --python 3.6
To run only a specific unit test::
ansible-test units --docker default -v --python 3.6 tests/unit/plugins/module_utils/foo/test_bar.py
You can specify Python requirements in the ``tests/unit/requirements.txt`` file. See :ref:`testing_units` for more information, especially on fixture files.
Adding integration tests
------------------------
You must place integration tests in the appropriate ``tests/integration/targets/`` directory. For module integration tests, you can use the module name alone. For example, you would place integration tests for ``plugins/modules/foo.py`` in a directory called ``tests/integration/targets/foo/``. For non-module plugin integration tests, you must add the plugin type to the directory name. For example, you would place integration tests for ``plugins/connections/bar.py`` in a directory called ``tests/integration/targets/connection_bar/``. For lookup plugins, the directory must be called ``lookup_foo``, for inventory plugins, ``inventory_foo``, and so on.
You can write two different kinds of integration tests:
* Ansible role tests run with ``ansible-playbook`` and validate various aspects of the module. They can depend on other integration tests (usually named ``prepare_bar`` or ``setup_bar``, which prepare a service or install a requirement named ``bar`` in order to test module ``foo``) to set-up required resources, such as installing required libraries or setting up server services.
* ``runme.sh`` tests run directly as scripts. They can set up inventory files, and execute ``ansible-playbook`` or ``ansible-inventory`` with various settings.
For examples, see the `integration tests in community.general <https://github.com/ansible-collections/community.general/tree/master/tests/integration/targets/>`_. See also :ref:`testing_integration` for more details.
Since integration tests can install requirements, and set-up, start and stop services, we recommended running them in docker containers or otherwise restricted environments whenever possible. By default, ``ansible-test`` supports Docker images for several operating systems. See the `list of supported docker images <https://github.com/ansible/ansible/blob/devel/test/lib/ansible_test/_data/completion/docker.txt>`_ for all options. Use the ``default`` image mainly for platform-independent integration tests, such as those for cloud modules. The following examples use the ``centos8`` image.
To execute all integration tests for a collection::
ansible-test integration --docker centos8 -v
If you want more detailed output, run the command with ``-vvv`` instead of ``-v``. Alternatively, specify ``--retry-on-error`` to automatically re-run failed tests with higher verbosity levels.
To execute only the integration tests in a specific directory::
ansible-test integration --docker centos8 -v connection_bar
You can specify multiple target names. Each target name is the name of a directory in ``tests/integration/targets/``.
.. seealso::
:ref:`developing_testing`
More resources on testing Ansible
:ref:`contributing_maintained_collections`
Guidelines for contributing to selected collections
`Mailing List <https://groups.google.com/group/ansible-devel>`_
The development mailing list
`irc.freenode.net <http://irc.freenode.net>`_
#ansible IRC chat channel