pytest-testinfra

repository·main·Indexed 25 days ago

https://github.com/pytest-dev/pytest-testinfra

A pytest plugin used to write Python tests to verify the actual state of infrastructure, such as files, packages, and services. It serves as a Python-based equivalent to Serverspec and supports multiple connection backends including SSH, Docker, Podman, Ansible, Salt, kubectl, OpenShift, and WinRM. The library can be used as a pytest plugin or independently via the Connection API using testinfra.get_host().

Tokens
5.4K
Snippets
17
Records
33
Agent score
82%

What's inside pytest-testinfra

  1. Parametrize Testinfra tests with pytest

    main

    To avoid a single failure stopping the entire test suite, use pytest.mark.parametrize instead of iterating over a list inside a single test function. This ensures each package or configuration is treated as a distinct test case, providing clear pass/fail status for each item in the parameter list.

    import pytest
    
    @pytest.mark.parametrize("name,version", [
        ("nginx", "1.6"),
        ("python", "2.7"),
    ])
    def test_packages(host, name, version):
        pkg = host.package(name)
        assert pkg.is_installed
        assert pkg.version.startswith(version)
  2. Get help with testinfra

    main

    If you encounter issues or have questions regarding testinfra, you can seek assistance through the following channels:

    • Issue Tracker: Search for existing problems or report new ones on the GitHub issue tracker.
    • IRC: Join the #pytest channel on the libera.chat network to ask questions in real-time.
    • pytest Documentation: Since testinfra is implemented as a pytest plugin, it is highly recommended to consult the official pytest documentation to understand the underlying testing framework and maximize your usage of testinfra.
  3. Install pytest-testinfra

    main

    You can install the stable version of pytest-testinfra via pip, or install the latest development version directly from GitHub.

    To install the stable version:

    pip install pytest-testinfra

    To install the development version:

    pip install 'git+https://github.com/pytest-dev/pytest-testinfra@main#egg=pytest-testinfra'
    pip install pytest-testinfra
  4. Quick start with pytest-testinfra

    main

    Testinfra allows you to write Python unit tests to verify the actual state of your servers. It works as a plugin for pytest. In your test functions, you use the host fixture to interact with the system under test.

    Common modules used via the host fixture include:

    • host.file(path): To inspect files and their attributes (e.g., contains, user, group, mode).
    • host.package(name): To check package status (e.g., is_installed, version).
    • host.service(name): To check service status (e.g., is_running, is_enabled).

    To run your tests, use the pytest command pointing to your test file.

    def test_passwd_file(host):
        passwd = host.file("/etc/passwd")
        assert passwd.contains("root")
        assert passwd.user == "root"
        assert passwd.group == "root"
        assert passwd.mode == 0o644
    
    
    def test_nginx_is_installed(host):
        nginx = host.package("nginx")
        assert nginx.is_installed
        assert nginx.version.startswith("1.2")
    
    
    def test_nginx_running_and_enabled(host):
        nginx = host.service("nginx")
        assert nginx.is_running
        assert nginx.is_enabled
    
    # Run with:
    # $ pytest -v test_myinfra.py
  5. Access Testinfra modules via the host fixture

    main
    Testinfra modules are accessed through the host fixture. To use them in your tests, declare host as an argument in your test function. The host object provides access to various infrastructure modules (e.g., file, service, user, package) to inspect and verify the state of your system.
  6. Install Testinfra with specific connection backends

    main

    Testinfra uses Python extras to manage dependencies for different connection backends. To ensure you have the necessary dependencies for your target environments, install the specific backend you need. For example, to install support for Ansible and Salt, use:

    $ pip install pytest-testinfra[ansible,salt]

    Note that some backends may still require system-packaged tools to be installed on your machine.

  7. Integrate Testinfra with Jenkins

    main

    In a Jenkins environment where Vagrant is available, you can automate the testing lifecycle by installing dependencies, bringing up the Vagrant machine, and running pytest with the --junit-xml flag to generate reports for Jenkins to consume.

    pip install pytest-testinfra paramiko
    vagrant up
    vagrant ssh-config > .vagrant/ssh-config
    pytest --hosts=default --ssh-config=.vagrant/ssh-config --junit-xml junit.xml tests.py