pytest-testinfra icon indicating copy to clipboard operation
pytest-testinfra copied to clipboard

How to extend a testinfra module?

Open conorsch opened this issue 9 years ago • 5 comments

Hello @philpep, thanks for the great project. I'm struggling to write custom testinfra modules that extend the builtins, e.g. File. Right now I'm trying to write a module for checking attributes for X509 SSL certs, similar to what serverspec already provides. The example docs are too simple for this use case:

  • https://testinfra.readthedocs.io/en/latest/examples.html#make-your-own-modules
  • https://github.com/philpep/testinfra-echo

Here's the class-based approach I'm taking:

$ cat tests/test_x509_certificates.py 
from testinfra.modules.file import File
from testinfra.modules.command import Command
import pytest


class _X509Certificate(File):
    """
    Inspect X509 SSL certificates and expose their attributes.
    """

    def _run_openssl_command(self, params):
        """
        Private method for running openssl against certificate file.
        Used by most class properties as a helper method.
        The `.path` attribute is inherited from File().
        """
        c = Command("openssl -in {} %s".format(self.path), params)
        return c


    @property
    def email(self):
        """
        Return `email` field from X509 certificate.
        """
        return self._run_openssl_command("-email -noout").stdout


@pytest.fixture()
def X509Certificate():
    x = _X509Certificate
    return x


def test_x509_debugging(X509Certificate):
    path = '/usr/share/ca-certificates/mozilla/thawte_Primary_Root_CA.crt'
    c = X509Certificate(path)
    assert c.email == 'foo'

Can you point me in the right direction here? I've been banging my head against this for a while now. Happy to submit a docs PR once I have a working example!

conorsch avatar Jan 27 '17 01:01 conorsch

Hi,

Instead of

@pytest.fixture()
def X509Certificate():
    x = _X509Certificate
    return x

Try

X509Certificate = _X509Certificate.as_fixture()

And instead of c = Command(...): c = self.run(...)

Agree that extending modules is not simple, maybe a monkeypatch could be even simple ?

philpep avatar Jan 27 '17 08:01 philpep

Have you got it working?

mihai-satmarean avatar Dec 11 '18 13:12 mihai-satmarean

Never did cobble together an implementation to my liking, currently using a hodge-podge of other tools to get the job done. Very interested in your results if you're successful, @mihai-satmarean !

conorsch avatar Dec 11 '18 16:12 conorsch

Given the discussion in https://github.com/philpep/testinfra/issues/233#issue-252744553, it seems like this is a duplicate and somehow outdated since the recommended solution API has been removed?

decentral1se avatar May 29 '19 09:05 decentral1se

I came across this today. I followed your example and got this working. https://github.com/awalker125/testinfra-extension-poc/blob/master/tests/fixtures/kubectl_binary.py

Its not exactly the same usecase but it does the job for me.

awalker125 avatar Jul 23 '19 19:07 awalker125