Skip to main content

A py.test plugin providing fixtures to simplify inmanta modules testing.

Project description

pytest-inmanta

A pytest plugin to test inmanta modules

Installation

pip install pytest-inmanta

Usage

This plugin provides a test fixture that can compile, export and deploy code without running an actual inmanta server.

def test_compile(project):
    """
        Test compiling a simple model that uses std
    """
    project.compile("""
host = std::Host(name="server", os=std::linux)
file = std::ConfigFile(host=host, path="/tmp/test", content="1234")
        """)

The fixture also provides access to the model internals

    assert len(project.get_instances("std::Host")) == 1
    assert project.get_instances("std::Host")[0].name == "server"

To the exported resources

    f = project.get_resource("std::ConfigFile")
    assert f.permissions == 644

To compiler output and mock filesystem

def test_template(project):
    """
        Test the evaluation of a template
    """
    project.add_mock_file("templates", "test.tmpl", "{{ value }}")
    project.compile("""import unittest
value = "1234"
std::print(std::template("unittest/test.tmpl"))
    """)

    assert project.get_stdout() == "1234\n"

And allows deploy

    project.deploy_resource("std::ConfigFile")

And dryrun

    changes = project.dryrun_resource("testmodule::Resource")
    assert changes == {"value": {'current': 'read', 'desired': 'write'}}

Testing functions and classes defined in a module is also possible using the inmanta_plugins fixture. The fixture exposes inmanta modules as its attributes and imports them dynamically when accessed.

    def test_example(inmanta_plugins):
        inmanta_plugins.testmodule.regular_function("example")

This dynamism is required because the compiler resets module imports when project.compile is called. As a result, if you store a module in a local variable, it will not survive a compilation. Therefore you are advised to access modules in the inmanta_plugins package in a fully qualified manner (using the fixture). The following example demonstrates this.

    def test_module_inequality(project, inmanta_plugins):
        cached_module = inmanta_plugins.testmodule
        assert cached_module is inmanta_plugins.testmodule

        project.compile("import testmodule")

        assert cached_module is not inmanta_plugins.testmodule

While you could import from the inmanta_plugins package directly, the fixture makes abstraction of module reloading. Without the fixture you would be required to reimport after project.compile.

Testing plugins

Take the following plugin as an example:

    # <module-name>/plugins/__init__.py

    from inmanta.plugins import plugin

    @plugin
    def hostname(fqdn: "string") -> "string":
        """
            Return the hostname part of the fqdn
        """
        return fqdn.split(".")[0]

A test case, to test this plugin looks like this:

    # <module-name>/tests/test_hostname.py

    def test_hostname(project):
        host = "test"
        fqdn = f"{host}.something.com"
        assert project.get_plugin_function("hostname")(fqdn) == host
  • Line 3: Creates a pytest test case, which requires the project fixture.
  • Line 6: Calls the function project.get_plugin_function(plugin_name: str): FunctionType, which returns the plugin function named plugin_name. As such, this line tests whether host is returned when the plugin function hostname is called with the parameter fqdn.

Options

The following options are available.

  • --venv: folder in which to place the virtual env for tests (will be shared by all tests), overrides INMANTA_TEST_ENV. This options depends on symlink support. This does not work on all windows versions. On windows 10 you need to run pytest in an admin shell. Using a fixed virtual environment can speed up running the tests.
  • --use-module-in-place: makes inmanta add the parent directory of your module directory to it's directory path, instead of copying your module to a temporary libs directory. It allows testing the current module against specific versions of dependent modules. Using this option can speed up the tests, because the module dependencies are not downloaded multiple times.
  • --module_repo: location to download modules from, overrides INMANTA_MODULE_REPO. The default value is the inmanta github organisation.
  • --install_mode: install mode to use for modules downloaded during this test, overrides INMANTA_INSTALL_MODE.
  • --no_load-plugins: Don't load plugins in the Project class. Overrides INMANTA_TEST_NO_LOAD_PLUGINS. The value of INMANTA_TEST_NO_LOAD_PLUGINS environment variable has to be a non-empty string to not load plugins. When not using this option during the testing of plugins with the project.get_plugin_function method, it's possible that the module's plugin/__init__.py is loaded multiple times, which can cause issues when it has side effects, as they are executed multiple times as well.

Use the generic pytest options --log-cli-level to show Inmanta logger to see any setup or cleanup warnings. For example, --log-cli-level=INFO

Compatibility with pytest-cov

The --use-module-in-place option should be set when pytest-inmanta is used in combination with the pytest-cov pytest plugin. Without the --use-module-in-place option, the reported test coverage will be incorrect.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-inmanta-1.5.2.tar.gz (12.8 kB view details)

Uploaded Source

File details

Details for the file pytest-inmanta-1.5.2.tar.gz.

File metadata

  • Download URL: pytest-inmanta-1.5.2.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.6.8

File hashes

Hashes for pytest-inmanta-1.5.2.tar.gz
Algorithm Hash digest
SHA256 a2b2eb5200886b37d1708b9ed83785a8dffea229f5237b8b6bc4e509a59d9577
MD5 ec694e8b8d15ea63e4308c42c58ed7b2
BLAKE2b-256 d24be8c46ef11fb4f164d85f99c9a771639566954e56d51fa140101a9d0c0503

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page