modules: dynamic import
Dynamically import modules, thus we can create new modules that require third party modules without using try/import/except.
This commit is contained in:
Родитель
2e7ef4bf56
Коммит
e59f608537
|
@ -15,7 +15,7 @@ Note that you can also :ref:`make modules`.
|
|||
Command
|
||||
~~~~~~~
|
||||
|
||||
.. autoclass:: testinfra.modules.Command(command, *args)
|
||||
.. autoclass:: testinfra.modules.command.Command(command, *args)
|
||||
:members: check_output, run_expect, run_test, exists
|
||||
|
||||
|
||||
|
@ -36,22 +36,22 @@ TestinfraBackend
|
|||
Sudo
|
||||
~~~~
|
||||
|
||||
.. autoclass:: testinfra.modules.Sudo(user=None)
|
||||
.. autoclass:: testinfra.modules.sudo.Sudo(user=None)
|
||||
|
||||
|
||||
File
|
||||
~~~~
|
||||
|
||||
.. autoclass:: testinfra.modules.File
|
||||
.. autoclass:: testinfra.modules.file.File
|
||||
:members:
|
||||
:undoc-members:
|
||||
:exclude-members: as_fixture, get_module_class
|
||||
:exclude-members: get_module_class
|
||||
|
||||
|
||||
User
|
||||
~~~~
|
||||
|
||||
.. autoclass:: testinfra.modules.User
|
||||
.. autoclass:: testinfra.modules.user.User
|
||||
:members:
|
||||
:undoc-members:
|
||||
|
||||
|
@ -59,7 +59,7 @@ User
|
|||
Group
|
||||
~~~~~
|
||||
|
||||
.. autoclass:: testinfra.modules.Group
|
||||
.. autoclass:: testinfra.modules.group.Group
|
||||
:members:
|
||||
:undoc-members:
|
||||
|
||||
|
@ -67,7 +67,7 @@ Group
|
|||
Interface
|
||||
~~~~~~~~~
|
||||
|
||||
.. autoclass:: testinfra.modules.Interface
|
||||
.. autoclass:: testinfra.modules.interface.Interface
|
||||
:members:
|
||||
:undoc-members:
|
||||
:exclude-members: get_module_class
|
||||
|
@ -76,40 +76,40 @@ Interface
|
|||
Package
|
||||
~~~~~~~
|
||||
|
||||
.. autoclass:: testinfra.modules.Package
|
||||
.. autoclass:: testinfra.modules.package.Package
|
||||
:members:
|
||||
|
||||
|
||||
PipPackage
|
||||
~~~~~~~~~~
|
||||
|
||||
.. autoclass:: testinfra.modules.PipPackage
|
||||
.. autoclass:: testinfra.modules.pip.PipPackage
|
||||
:members:
|
||||
|
||||
Process
|
||||
~~~~~~~
|
||||
|
||||
.. autoclass:: testinfra.modules.Process
|
||||
.. autoclass:: testinfra.modules.process.Process
|
||||
:members:
|
||||
|
||||
Service
|
||||
~~~~~~~
|
||||
|
||||
.. autoclass:: testinfra.modules.Service
|
||||
.. autoclass:: testinfra.modules.service.Service
|
||||
:members:
|
||||
|
||||
|
||||
Supervisor
|
||||
~~~~~~~~~~
|
||||
|
||||
.. autoclass:: testinfra.modules.Supervisor
|
||||
.. autoclass:: testinfra.modules.supervisor.Supervisor
|
||||
:members:
|
||||
|
||||
|
||||
Socket
|
||||
~~~~~~
|
||||
|
||||
.. autoclass:: testinfra.modules.Socket
|
||||
.. autoclass:: testinfra.modules.socket.Socket
|
||||
:members:
|
||||
|
||||
|
||||
|
@ -117,7 +117,7 @@ SystemInfo
|
|||
~~~~~~~~~~
|
||||
|
||||
|
||||
.. autoclass:: testinfra.modules.SystemInfo
|
||||
.. autoclass:: testinfra.modules.systeminfo.SystemInfo
|
||||
:members:
|
||||
|
||||
|
||||
|
@ -125,13 +125,13 @@ Salt
|
|||
~~~~
|
||||
|
||||
|
||||
.. autoclass:: testinfra.modules.Salt(function, args=None)
|
||||
.. autoclass:: testinfra.modules.salt.Salt(function, args=None)
|
||||
:members:
|
||||
|
||||
Ansible
|
||||
~~~~~~~
|
||||
|
||||
.. autoclass:: testinfra.modules.Ansible(module_name, module_args=None, check=True)
|
||||
.. autoclass:: testinfra.modules.ansible.Ansible(module_name, module_args=None, check=True)
|
||||
:members:
|
||||
|
||||
|
||||
|
@ -139,7 +139,7 @@ PuppetResource
|
|||
~~~~~~~~~~~~~~
|
||||
|
||||
|
||||
.. autoclass:: testinfra.modules.PuppetResource(type, name=None)
|
||||
.. autoclass:: testinfra.modules.puppet.PuppetResource(type, name=None)
|
||||
:members:
|
||||
|
||||
|
||||
|
@ -147,20 +147,20 @@ Facter
|
|||
~~~~~~
|
||||
|
||||
|
||||
.. autoclass:: testinfra.modules.Facter(*facts)
|
||||
.. autoclass:: testinfra.modules.puppet.Facter(*facts)
|
||||
:members:
|
||||
|
||||
Sysctl
|
||||
~~~~~~
|
||||
|
||||
.. autoclass:: testinfra.modules.Sysctl(name)
|
||||
.. autoclass:: testinfra.modules.sysctl.Sysctl(name)
|
||||
:members:
|
||||
|
||||
|
||||
MountPoint
|
||||
~~~~~~~~~~
|
||||
|
||||
.. autoclass:: testinfra.modules.MountPoint(path)
|
||||
.. autoclass:: testinfra.modules.mountpoint.MountPoint(path)
|
||||
:members:
|
||||
|
||||
.. _pytest fixtures: https://pytest.org/latest/fixture.html
|
||||
|
|
|
@ -250,6 +250,6 @@ class BaseBackend(object):
|
|||
try:
|
||||
module = self._module_cache[name]
|
||||
except KeyError:
|
||||
module = getattr(testinfra.modules, name).get_module(self)
|
||||
module = testinfra.modules.get_module_class(name).get_module(self)
|
||||
self._module_cache[name] = module
|
||||
return module
|
||||
|
|
|
@ -13,30 +13,33 @@
|
|||
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from testinfra.modules.ansible import Ansible
|
||||
from testinfra.modules.command import Command
|
||||
from testinfra.modules.file import File
|
||||
from testinfra.modules.group import Group
|
||||
from testinfra.modules.interface import Interface
|
||||
from testinfra.modules.mountpoint import MountPoint
|
||||
from testinfra.modules.package import Package
|
||||
from testinfra.modules.pip import PipPackage
|
||||
from testinfra.modules.process import Process
|
||||
from testinfra.modules.puppet import Facter
|
||||
from testinfra.modules.puppet import PuppetResource
|
||||
from testinfra.modules.salt import Salt
|
||||
from testinfra.modules.service import Service
|
||||
from testinfra.modules.socket import Socket
|
||||
from testinfra.modules.sudo import Sudo
|
||||
from testinfra.modules.supervisor import Supervisor
|
||||
from testinfra.modules.sysctl import Sysctl
|
||||
from testinfra.modules.systeminfo import SystemInfo
|
||||
from testinfra.modules.user import User
|
||||
import importlib
|
||||
|
||||
modules = {
|
||||
'Ansible': 'ansible:Ansible',
|
||||
'Command': 'command:Command',
|
||||
'File': 'file:File',
|
||||
'Group': 'group:Group',
|
||||
'Interface': 'interface:Interface',
|
||||
'MountPoint': 'mountpoint:MountPoint',
|
||||
'Package': 'package:Package',
|
||||
'PipPackage': 'pip:PipPackage',
|
||||
'Process': 'process:Process',
|
||||
'PuppetResource': 'puppet:PuppetResource',
|
||||
'Facter': 'puppet:Facter',
|
||||
'Salt': 'salt:Salt',
|
||||
'Service': 'service:Service',
|
||||
'Socket': 'socket:Socket',
|
||||
'Sudo': 'sudo:Sudo',
|
||||
'Supervisor': 'supervisor:Supervisor',
|
||||
'Sysctl': 'sysctl:Sysctl',
|
||||
'SystemInfo': 'systeminfo:SystemInfo',
|
||||
'User': 'user:User',
|
||||
}
|
||||
|
||||
|
||||
__all__ = [
|
||||
"Command", "File", "Package", "Group", "Interface",
|
||||
"Service", "SystemInfo", "User", "Salt", "PuppetResource",
|
||||
"Facter", "Sysctl", "Socket", "Ansible", "Process",
|
||||
"Supervisor", "MountPoint", "Sudo", "PipPackage",
|
||||
]
|
||||
def get_module_class(name):
|
||||
modname, classname = modules[name].split(':')
|
||||
modname = '.'.join([__name__, modname])
|
||||
module = importlib.import_module(modname)
|
||||
return getattr(module, classname)
|
||||
|
|
|
@ -64,14 +64,6 @@ class Module(object):
|
|||
def get_module_class(cls, _backend):
|
||||
return cls
|
||||
|
||||
@classmethod
|
||||
def as_fixture(cls):
|
||||
@pytest.fixture()
|
||||
def f(TestinfraBackend):
|
||||
return TestinfraBackend.get_module(cls.__name__)
|
||||
f.__doc__ = cls.__doc__
|
||||
return f
|
||||
|
||||
|
||||
class InstanceModule(Module):
|
||||
|
||||
|
|
|
@ -15,31 +15,27 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import logging
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
import testinfra
|
||||
from testinfra import modules
|
||||
import testinfra.modules
|
||||
|
||||
File = modules.File.as_fixture()
|
||||
Command = modules.Command.as_fixture()
|
||||
Package = modules.Package.as_fixture()
|
||||
Group = modules.Group.as_fixture()
|
||||
Interface = modules.Interface.as_fixture()
|
||||
Command = modules.Command.as_fixture()
|
||||
Service = modules.Service.as_fixture()
|
||||
SystemInfo = modules.SystemInfo.as_fixture()
|
||||
User = modules.User.as_fixture()
|
||||
Salt = modules.Salt.as_fixture()
|
||||
PuppetResource = modules.PuppetResource.as_fixture()
|
||||
Facter = modules.Facter.as_fixture()
|
||||
Sysctl = modules.Sysctl.as_fixture()
|
||||
Socket = modules.Socket.as_fixture()
|
||||
Ansible = modules.Ansible.as_fixture()
|
||||
Process = modules.Process.as_fixture()
|
||||
Supervisor = modules.Supervisor.as_fixture()
|
||||
MountPoint = modules.MountPoint.as_fixture()
|
||||
Sudo = modules.Sudo.as_fixture()
|
||||
PipPackage = modules.PipPackage.as_fixture()
|
||||
|
||||
def _generate_fixtures():
|
||||
self = sys.modules[__name__]
|
||||
for modname in testinfra.modules.modules:
|
||||
def get_fixture(name):
|
||||
@pytest.fixture()
|
||||
def f(TestinfraBackend):
|
||||
return TestinfraBackend.get_module(name)
|
||||
f.__name__ = str(name)
|
||||
f.__doc__ = ('https://testinfra.readthedocs.io/en/latest/'
|
||||
'modules.html#{0}'.format(name.lower()))
|
||||
return f
|
||||
setattr(self, modname, get_fixture(modname))
|
||||
|
||||
_generate_fixtures()
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
|
|
Загрузка…
Ссылка в новой задаче