def _testinfra_backend(request, pytestconfig, _testinfra_host): kwargs = {} if pytestconfig.option.ssh_config is not None: kwargs["ssh_config"] = pytestconfig.option.ssh_config if pytestconfig.option.sudo is not None: kwargs["sudo"] = pytestconfig.option.sudo if _testinfra_host is not None: if "://" in _testinfra_host: url = urllib.parse.urlparse(_testinfra_host) backend_type = url.scheme host = url.netloc query = urllib.parse.parse_qs(url.query) if query.get("sudo", ["false"])[0].lower() == "true": kwargs["sudo"] = True if "ssh_config" in query: kwargs["ssh_config"] = query.get("ssh_config")[0] else: backend_type = pytestconfig.option.connection or "paramiko" host = _testinfra_host backend = testinfra.get_backend( backend_type, host, **kwargs) else: backend = testinfra.get_backend("local", **kwargs) return backend
def docker(request, LocalCommand): # Build image and get the right tag try: tag = request.module.IMAGE_TAG except AttributeError: raise AssertionError( 'Missing IMAGE_TAG in test module' ) builder = Builder(tag=tag) builder.build() if builder.error: raise AssertionError( 'Dockerfile failed to build: %s' % builder.error ) # Run a new container docker_id = LocalCommand.check_output( "docker run -d %s" % tag ) print('Running container: %s' % docker_id) def teardown(): print('\nRemoving container: %s' % docker_id) LocalCommand.check_output("docker kill %s", docker_id) LocalCommand.check_output("docker rm %s", docker_id) # At the end of all tests, we destroy the container request.addfinalizer(teardown) return testinfra.get_backend("docker://%s" % (docker_id,))
def DockerGeneric(request, args, test_args, image, cmd, entrypoint): assert 'docker' in check_output('id'), "Are you in the docker group?" # Always appended PYTEST arg to tell pihole we're testing if 'pihole' in image: args = '{} -e PYTEST=1'.format(args) docker_run = 'docker run -d -t {args} {test_args} {entry} {image} {cmd}'\ .format(args=args, test_args=test_args, entry=entrypoint, image=image, cmd=cmd) print docker_run docker_id = check_output(docker_run) def teardown(): check_output("docker logs {}".format(docker_id)) check_output("docker rm -f {}".format(docker_id)) request.addfinalizer(teardown) docker_container = testinfra.get_backend("docker://" + docker_id) docker_container.id = docker_id def run_bash(self, command, *args, **kwargs): cmd = self.get_command(command, *args) if self.user is not None: out = self.run_local("docker exec -u %s %s /bin/bash -c %s", self.user, self.name, cmd) else: out = self.run_local("docker exec %s /bin/bash -c %s", self.name, cmd) out.command = self.encode(cmd) return out funcType = type(docker_container.run) docker_container.run = funcType(run_bash, docker_container, testinfra.backend.docker.DockerBackend) return docker_container
def TestinfraBackend(request): # Override the TestinfraBackend fixture, # all testinfra fixtures (i.e. modules) depend on it. docker_command = '' docker_image = request.param docker_sleeptime = 5 # Check for custom command in docker image name if "#" in docker_image: docker_image, docker_command = docker_image.split("#", 2) if docker_command == "loop": docker_command = 'tail -f /dev/null' docker_sleeptime = 0 docker_id = check_output( "docker run -d -v \"%s:/app:ro\" %s " + docker_command, test_conf_app_path, docker_image ) def teardown(): check_output("docker rm -f %s", docker_id) # Destroy the container at the end of the fixture life request.addfinalizer(teardown) # wait for getting the image up if docker_sleeptime: time.sleep(docker_sleeptime) # Return a dynamic created backend return testinfra.get_backend("docker://" + docker_id)
def DockerGeneric(request, args, image, cmd, entrypoint=''): assert 'docker' in check_output('id'), "Are you in the docker group?" if 'pihole' in image: args += " --dns 127.0.0.1 --dns 1.1.1.1 -v /dev/null:/etc/pihole/adlists.default -e PYTEST=1" docker_run = "docker run -d -t {args} {entry} {image} {cmd}".format( args=args, entry=entrypoint, image=image, cmd=cmd) print docker_run docker_id = check_output(docker_run) def teardown(): check_output("docker logs {}".format(docker_id)) check_output("docker rm -f {}".format(docker_id)) request.addfinalizer(teardown) docker_container = testinfra.get_backend("docker://" + docker_id) docker_container.id = docker_id def run_bash(self, command, *args, **kwargs): cmd = self.get_command(command, *args) if self.user is not None: out = self.run_local("docker exec -u %s %s /bin/bash -c %s", self.user, self.name, cmd) else: out = self.run_local("docker exec %s /bin/bash -c %s", self.name, cmd) out.command = self.encode(cmd) return out funcType = type(docker_container.run) docker_container.run = funcType(run_bash, docker_container, testinfra.backend.docker.DockerBackend) return docker_container
def DockerGeneric(request, args, image, cmd): assert 'docker' in check_output('id'), "Are you in the docker group?" if 'diginc/pi-hole' in image: args += " -v /dev/null:/etc/pihole/adlists.default -e PYTEST=\"True\"" docker_run = "docker run -d {} {} {}".format(args, image, cmd) docker_id = check_output(docker_run) def teardown(): check_output("docker rm -f %s", docker_id) request.addfinalizer(teardown) docker_container = testinfra.get_backend("docker://" + docker_id) docker_container.id = docker_id def run_bash(self, command, *args, **kwargs): cmd = self.get_command(command, *args) if self.user is not None: out = self.run_local("docker exec -u %s %s /bin/bash -c %s", self.user, self.name, cmd) else: out = self.run_local("docker exec %s /bin/bash -c %s", self.name, cmd) out.command = self.encode(cmd) return out funcType = type(docker_container.run) docker_container.run = funcType(run_bash, docker_container, testinfra.backend.docker.DockerBackend) return docker_container
def build(docker_repo, arch, args): run_local = testinfra.get_backend("local://").get_module("Command").run dockerfile = 'Dockerfile_{}'.format(arch) repo_tag = '{}:{}_{}'.format(docker_repo, __version__, arch) cached_image = '{}/{}'.format('pihole', repo_tag) time = '' if args['-t']: time = 'time ' no_cache = '' if args['--no-cache']: no_cache = '--no-cache' build_command = '{time}docker build {no_cache} --pull --cache-from="{cache},{create_tag}" -f {dockerfile} -t {create_tag} .'\ .format(time=time, no_cache=no_cache, cache=cached_image, dockerfile=dockerfile, create_tag=repo_tag) print(" ::: Building {} into {}".format(dockerfile, repo_tag)) if args['-v']: print(build_command, '\n') build_result = run_local(build_command) if args['-v']: print(build_result.stdout) print(build_result.stderr) if build_result.rc != 0: print(" ::: Building {} encountered an error".format(dockerfile)) print(build_result.stderr) assert build_result.rc == 0
def DockerGeneric(request, args, image, cmd): assert 'docker' in check_output('id'), "Are you in the docker group?" if 'diginc/pi-hole' in image: args += " -v /dev/null:/etc/pihole/adlists.default -e PYTEST=\"True\"" docker_run = "docker run -d {} {} {}".format(args, image, cmd) docker_id = check_output(docker_run) def teardown(): check_output("docker rm -f %s", docker_id) request.addfinalizer(teardown) docker_container = testinfra.get_backend("docker://" + docker_id) docker_container.id = docker_id def run_bash(self, command, *args, **kwargs): cmd = self.get_command(command, *args) if self.user is not None: out = self.run_local( "docker exec -u %s %s /bin/bash -c %s", self.user, self.name, cmd) else: out = self.run_local( "docker exec %s /bin/bash -c %s", self.name, cmd) out.command = self.encode(cmd) return out funcType = type(docker_container.run) docker_container.run = funcType(run_bash, docker_container, testinfra.backend.docker.DockerBackend) return docker_container
def TestinfraBackend(request): # Override the TestinfraBackend fixture, # all testinfra fixtures (i.e. modules) depend on it. docker_command = '' docker_image = request.param docker_sleeptime = 5 # Check for custom command in docker image name if "#" in docker_image: docker_image, docker_command = docker_image.split("#", 2) if docker_command == "loop": docker_command = 'tail -f /dev/null' docker_sleeptime = 0 docker_id = check_output( "docker run -d -v \"%s:/app:ro\" %s " + docker_command, test_conf_app_path, docker_image) def teardown(): check_output("docker rm -f %s", docker_id) # Destroy the container at the end of the fixture life request.addfinalizer(teardown) # wait for getting the image up if docker_sleeptime: time.sleep(docker_sleeptime) # Return a dynamic created backend return testinfra.get_backend("docker://" + docker_id)
def _testinfra_backend(request, pytestconfig, _testinfra_host): kwargs = {"connection": pytestconfig.option.connection or "paramiko"} if pytestconfig.option.ssh_config is not None: kwargs["ssh_config"] = pytestconfig.option.ssh_config if pytestconfig.option.sudo is not None: kwargs["sudo"] = pytestconfig.option.sudo return testinfra.get_backend(_testinfra_host, **kwargs)
def TestinfraBackend(request, tmpdir_factory): image, kw = parse_hostspec(request.param) docker_id, docker_host, port = start_container( request, image, tmpdir_factory) if kw["connection"] == "docker": host = docker_id elif kw["connection"] in ("ansible", "ssh", "paramiko", "safe-ssh"): host, user, _ = BaseBackend.parse_hostspec(image) tmpdir = tmpdir_factory.mktemp(str(id(request))) key = tmpdir.join("ssh_key") key.write(open(os.path.join(BASETESTDIR, "ssh_key")).read()) key.chmod(384) # octal 600 if kw["connection"] == "ansible": if ansible is None: pytest.skip() return inventory = tmpdir.join("inventory") inventory.write(get_ansible_inventory( host, docker_host, user or "root", port, str(key))) kw["ansible_inventory"] = str(inventory) else: ssh_config = tmpdir.join("ssh_config") ssh_config.write(( "Host {}\n" " Hostname {}\n" " User {}\n" " Port {}\n" " UserKnownHostsFile /dev/null\n" " StrictHostKeyChecking no\n" " IdentityFile {}\n" " IdentitiesOnly yes\n" " LogLevel FATAL\n" ).format(image, docker_host, user or "root", port, str(key))) kw["ssh_config"] = str(ssh_config) # Wait ssh to be up service = testinfra.get_backend( docker_id, connection="docker" ).get_module("Service") while not service("ssh").is_running: time.sleep(.5) backend = testinfra.get_backend(host, **kw) backend.get_hostname = lambda: image return backend
def LocalCommand(TestinfraBackend): """Run commands locally Same as `Command` but run commands locally with subprocess even when the connection backend is not "local". Note: `LocalCommand` does NOT respect ``--sudo`` option """ return testinfra.get_backend("local://").get_module("Command")
def Docker(_docker_build, request, LocalCommand): docker_id = LocalCommand.check_output("docker run -d testinfra-nginx") def teardown(): LocalCommand.check_output("docker kill %s", docker_id) LocalCommand.check_output("docker rm %s", docker_id) request.addfinalizer(teardown) return testinfra.get_backend("docker://" + docker_id)
def test_same_website_root(): tree = {} for name in ("default", "production"): conn = testinfra.get_backend( name, connection="paramiko", ssh_config=".vagrant-ssh-config") Command = conn.get_module("Command") tree[name] = Command.check_output("tree /srv/website") assert tree["default"] == tree["production"]
def LocalCommand(testinfra_backend): """Run commands locally Same as `Command` but run commands locally with subprocess even when the connection backend is not "local". Note: `LocalCommand` does NOT respect ``--sudo`` option """ return testinfra.get_backend("local://").get_module("Command")
def _get_module(module_name, backend=default_backend): """Retrieve the correct module implementation determined by the backend being used. :param module_name: TestInfra module to retrieve :param backend: string representing backend for TestInfra :returns: desired TestInfra module object :rtype: object """ backend_instance = testinfra.get_backend(backend) return backend_instance.get_module(_to_pascal_case(module_name))
def TestinfraBackend(request): docker_run = "docker run -d {}".format(request.param) print docker_run docker_id = check_output(docker_run) check_output("docker exec %s sed -i 's/^gravity_spinup/#donotcurl/g' /usr/local/bin/gravity.sh", docker_id) def teardown(): check_output("docker rm -f %s", docker_id) request.addfinalizer(teardown) return testinfra.get_backend("docker://" + docker_id)
def Docker(request, args, image, cmd): ''' combine our fixtures into a docker run command and setup finalizer to cleanup ''' assert 'docker' in check_output('id'), "Are you in the docker group?" docker_run = "docker run {} {} {}".format(args, image, cmd) docker_id = check_output(docker_run) def teardown(): check_output("docker rm -f %s", docker_id) request.addfinalizer(teardown) docker_container = testinfra.get_backend("docker://" + docker_id) docker_container.id = docker_id return docker_container
def __call__(self, function, args=None, local=False): args = args or [] if isinstance(args, six.string_types): args = [args] backend = get_backend() if backend.get_backend_type() == "salt": return backend.run_salt(function, args) else: cmd = "salt-call --out=json" if local: cmd += " --local" cmd += " %s" + len(args) * " %s" cmd_args = [function] + args return json.loads(self.check_output(cmd, *cmd_args))["local"]
def DockerGeneric(request, args, image, cmd): assert 'docker' in check_output('id'), "Are you in the docker group?" if 'diginc/pi-hole' in image: args += " -e PYTEST=\"True\"" docker_run = "docker run -d {} {} {}".format(args, image, cmd) docker_id = check_output(docker_run) def teardown(): check_output("docker rm -f %s", docker_id) request.addfinalizer(teardown) docker_container = testinfra.get_backend("docker://" + docker_id) docker_container.id = docker_id return docker_container
def TestinfraBackend(request): # Override the TestinfraBackend fixture, # all testinfra fixtures (i.e. modules) depend on it. docker_id = subprocess.check_output([ "docker", "run", "-d", request.param, ]).strip() def teardown(): subprocess.check_output(["docker", "rm", "-f", docker_id]) # Destroy the container at the end of the fixture life request.addfinalizer(teardown) # Return a dynamic created backend return testinfra.get_backend("docker://" + docker_id)
def __init__(self, request): env = '-e "ZONES_DNSSEC=example.invalid"' if hasattr(request, 'param'): self.args = request.param keys = [] if 'tsig_slave' in request.param: keys += request.param['tsig_slave'] if 'tsig_update' in request.param: keys += request.param['tsig_update'] self.keys = keys env += ' -e "TSIG_KEYS=' i = 1 tmp_env = "" for key in keys: tmp_env += "{index}:{key} ".format(index=i, key=key) i +=1 env += tmp_env.strip() env += '"' i = 1 if 'tsig_slave' in request.param: env += ' -e "TSIG_SLAVES=' tmp_env = "" for key in request.param['tsig_slave']: tmp_env+= "{index}:0.0.0.0/0:{index} ".format(index=i) i +=1 env += tmp_env.strip() env += '"' if 'tsig_update' in request.param: env += ' -e "TSIG_UPDATES=' i = 1 tmp_env = "" for key in request.param['tsig_update']: tmp_env+= "{index}:0.0.0.0/0:{index} ".format(index=i) i +=1 env += tmp_env.strip() env += '"' docker_id = check_output("docker run --health-interval=2s -d %s nsmaster" % env) self.docker_id = docker_id while True: health =json.loads(check_output("docker inspect --format='{{json .State.Health}}' %s" % docker_id)) time.sleep(1) if health['Status'] == 'healthy': break self.backend = testinfra.get_backend("docker://" + docker_id)
def AnsibleDockerTestinfraBackend(request): """ Boot and stop a docker image. """ logger = logging.getLogger('container-management') # Run a new container. Run in privileged mode, so systemd will start docker_id = local_command.check_output( "docker run --privileged -v /sys/fs/cgroup:/sys/fs/cgroup:ro -d -P %s", request.param ) logger.info('Test container id: %s', docker_id) def teardown(): """ Actions to execute on fixture end of life """ local_command.check_output("docker kill %s", docker_id) local_command.check_output("docker rm %s", docker_id) local_command.check_output("rm /tmp/%s", docker_id) # At the end of each test, we destroy the container request.addfinalizer(teardown) # Get Docker host mapping for container SSH port expose host_ssh_port = local_command.check_output( "docker inspect --format" " '{{ (index (index .NetworkSettings.Ports \"22/tcp\") 0).HostPort }}'" " %s" % docker_id) # Create inventory file _manage_inventory_file(request, docker_id, host_ssh_port) # Get container instance container = testinfra.get_backend("docker://%s" % docker_id) # Add user public key to authorized connection _set_authorized_keys(request, container) _manage_ansible_provisionning(request, container) return container
def Docker(request, docker_image, LocalCommand): """ Boot and stop a docker image. The image is primed with salt-minion. """ from os.path import dirname root_dir = dirname(dirname(__file__)) print 'Project root dir is:', root_dir # Run a new container. Run in privileged mode, so systemd will start docker_id = LocalCommand.check_output("docker run --privileged -d -v %s/salt/:/srv/salt -v %s/pillar/:/srv/pillar/ %s", root_dir, root_dir, docker_image) def teardown(): LocalCommand("docker kill %s", docker_id) LocalCommand("docker rm %s", docker_id) # At the end of each test, we destroy the container request.addfinalizer(teardown) return testinfra.get_backend("docker://%s" % (docker_id,))
def __init__(self, request): env = '' if hasattr(request, 'param'): self.args = request.param if 'users' in request.param: env += '-e "SEED_USERS=' tmp_env = '' for user, domain, password in request.param.get('users'): tmp_env += '%s:%s:%s ' % (domain, user, password) tmp_env.strip() tmp_env += '"' env += tmp_env docker_id = check_output("docker run --health-interval=2s -d %s nsmaster" % env) self.docker_id = docker_id while True: health =json.loads(check_output("docker inspect --format='{{json .State.Health}}' %s" % docker_id)) time.sleep(1) if health['Status'] == 'healthy': break self.backend = testinfra.get_backend("docker://" + docker_id)
def get_services(cls): if cls._services is not None: return cls._services cls._services = {} Command = testinfra.get_backend("local://").get_module("Command") for line in Command.check_output(( "docker-compose ps -q | xargs docker inspect --format " """'{{ index .Config.Labels "com.docker.compose.service" }} """ """{{ .Name }} """ """{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'""" )).splitlines(): service, name, ip = line.split() cls._services[service] = { "name": name[1:] if name[0] == "/" else name, "ip": ip, } return cls._services
def build(docker_repo, os, arch, args): run_local = testinfra.get_backend("local://").get_module("Command").run dockerfile = 'Dockerfile_{}_{}'.format(os, arch) repo_tag = '{}:{}_{}'.format(docker_repo, os, arch) cached_image = '{}/{}'.format('diginc', repo_tag) no_cache = '' if args['--no-cache']: no_cache = '--no-cache' build_command = 'docker build {no_cache} --pull --cache-from="{cache},{create_tag}" -f {dockerfile} -t {create_tag} .'\ .format(no_cache=no_cache, cache=cached_image, dockerfile=dockerfile, create_tag=repo_tag) print " ::: Building {} into {}".format(dockerfile, repo_tag) if args['-v']: print build_command, '\n' build_result = run_local(build_command) if args['-v']: print build_result.stdout if build_result.rc != 0: print " ::: Building {} encountered an error".format(dockerfile) print build_result.stderr assert build_result.rc == 0
def DockerGeneric(request, _test_args, _args, _image, _cmd, _entrypoint): assert 'docker' in check_output('id'), "Are you in the docker group?" # Always appended PYTEST arg to tell pihole we're testing if 'pihole' in _image and 'PYTEST=1' not in _args: _args = '{} -e PYTEST=1'.format(_args) docker_run = 'docker run -d -t {args} {test_args} {entry} {image} {cmd}'\ .format(args=_args, test_args=_test_args, entry=_entrypoint, image=_image, cmd=_cmd) # Print a human runable version of the container run command for faster debugging print( docker_run.replace('-d -t', '--rm -it').replace('tail -f /dev/null', 'bash')) docker_id = check_output(docker_run) def teardown(): check_output("docker logs {}".format(docker_id)) check_output("docker rm -f {}".format(docker_id)) request.addfinalizer(teardown) docker_container = testinfra.get_backend("docker://" + docker_id) docker_container.id = docker_id def run_bash(self, command, *args, **kwargs): cmd = self.get_command(command, *args) if self.user is not None: out = self.run_local("docker exec -u %s %s /bin/bash -c %s", self.user, self.name, cmd) else: out = self.run_local("docker exec %s /bin/bash -c %s", self.name, cmd) out.command = self.encode(cmd) return out funcType = type(docker_container.run) # override run function to use bash not sh docker_container.run = funcType(run_bash, docker_container, testinfra.backend.docker.DockerBackend) return docker_container
import testinfra run_local = testinfra.get_backend("local://").get_module("Command").run def test_scripts_pass_shellcheck(): ''' Make sure shellcheck does not find anything wrong with our shell scripts ''' shellcheck = ("find . -type f -name 'update.sh' " "| while read file; do " "shellcheck -x \"$file\" -e SC1090,SC1091; " "done;") results = run_local(shellcheck) print results.stdout assert '' == results.stdout
def TestinfraBackend(request, tmpdir_factory): if not has_docker(): pytest.skip() return image, kw = parse_hostspec(request.param) host, user, _ = BaseBackend.parse_hostspec(image) if getattr(request.function, "destructive", None) is not None: scope = "function" else: scope = "session" fname = "_docker_container_%s_%s" % (host, scope) docker_id, docker_host, port = request.getfuncargvalue(fname) if kw["connection"] == "docker": host = docker_id elif kw["connection"] in ("ansible", "ssh", "paramiko", "safe-ssh"): tmpdir = tmpdir_factory.mktemp(str(id(request))) key = tmpdir.join("ssh_key") key.write(open(os.path.join(BASETESTDIR, "ssh_key")).read()) key.chmod(384) # octal 600 if kw["connection"] == "ansible": if ansible is None: pytest.skip() return setup_ansible_config(tmpdir, host, docker_host, user or "root", port, str(key)) os.environ["ANSIBLE_CONFIG"] = str(tmpdir.join("ansible.cfg")) # this force backend cache reloading kw["ansible_inventory"] = str(tmpdir.join("inventory")) else: ssh_config = tmpdir.join("ssh_config") ssh_config.write( ("Host {}\n" " Hostname {}\n" " Port {}\n" " UserKnownHostsFile /dev/null\n" " StrictHostKeyChecking no\n" " IdentityFile {}\n" " IdentitiesOnly yes\n" " LogLevel FATAL\n").format(host, docker_host, port, str(key))) kw["ssh_config"] = str(ssh_config) # Wait ssh to be up service = testinfra.get_backend( docker_id, connection="docker").get_module("Service") if image in ("centos_7", "fedora"): service_name = "sshd" else: service_name = "ssh" while not service(service_name).is_running: time.sleep(.5) if kw["connection"] != "ansible": hostspec = (user or "root") + "@" + host else: hostspec = host backend = testinfra.get_backend(hostspec, **kw) backend.get_hostname = lambda: image return backend
documentation for the main python project: http://testinfra.readthedocs.org/ ''' try: import testinfra HAS_TESTINFRA = True except ImportError: HAS_TESTINFRA = False import logging LOG = logging.getLogger(__name__) __virtualname__ = 'infratest' conn = testinfra.get_backend('local://') File = conn.get_module("File") Package = conn.get_module("Package") Service = conn.get_module("Service") Socket = conn.get_module("Socket") Process = conn.get_module("Process") Group = conn.get_module("Group") User = conn.get_module("User") Interface = conn.get_module("Interface") SystemInfo = conn.get_module("SystemInfo") Sysctl = conn.get_module("Sysctl") INFRATEST = {} INFRATEST['Passed'] = [] INFRATEST['Failed'] = []
import pytest import testinfra WEB_SERVER = {'alpine': 'nginx', 'debian': 'lighttpd'} check_output = testinfra.get_backend("local://").get_module( "Command").check_output def DockerGeneric(request, args, image, cmd): assert 'docker' in check_output('id'), "Are you in the docker group?" if 'diginc/pi-hole' in image: args += " -v /dev/null:/etc/pihole/adlists.default -e PYTEST=\"True\"" docker_run = "docker run -d {} {} {}".format(args, image, cmd) docker_id = check_output(docker_run) def teardown(): check_output("docker rm -f %s", docker_id) request.addfinalizer(teardown) docker_container = testinfra.get_backend("docker://" + docker_id) docker_container.id = docker_id def run_bash(self, command, *args, **kwargs): cmd = self.get_command(command, *args) if self.user is not None: out = self.run_local("docker exec -u %s %s /bin/bash -c %s", self.user, self.name, cmd) else: out = self.run_local("docker exec %s /bin/bash -c %s", self.name,
def run(command, *args, **kwargs): return get_backend().run(command, *args, **kwargs)
# -*- coding: utf-8 -*- """ Pytest plugin entry point """ import logging import logging.config import os import re import testinfra import pytest # Use testinfra to get a handy function to run commands locally local_command = testinfra.get_backend('local://').get_module('Command') def pytest_addoption(parser): """ Manage plugin command line arguments """ group = parser.getgroup('ansible-docker') group.addoption( '--no-logging-overload', action='store_true', dest='no_logging_overload', default=False, help='Not overload logging, warning output is unreadeable.' )
def TestinfraBackend(request, tmpdir_factory): if not has_docker(): pytest.skip() return image, kw = parse_hostspec(request.param) host, user, _ = BaseBackend.parse_hostspec(image) if getattr(request.function, "destructive", None) is not None: scope = "function" else: scope = "session" fname = "_docker_container_%s_%s" % (host, scope) docker_id, docker_host, port = request.getfuncargvalue(fname) if kw["connection"] == "docker": host = docker_id elif kw["connection"] in ("ansible", "ssh", "paramiko", "safe-ssh"): tmpdir = tmpdir_factory.mktemp(str(id(request))) key = tmpdir.join("ssh_key") key.write(open(os.path.join(BASETESTDIR, "ssh_key")).read()) key.chmod(384) # octal 600 if kw["connection"] == "ansible": if ansible is None: pytest.skip() return inventory = tmpdir.join("inventory") inventory.write(get_ansible_inventory( host, docker_host, user or "root", port, str(key))) kw["ansible_inventory"] = str(inventory) else: ssh_config = tmpdir.join("ssh_config") ssh_config.write(( "Host {}\n" " Hostname {}\n" " Port {}\n" " UserKnownHostsFile /dev/null\n" " StrictHostKeyChecking no\n" " IdentityFile {}\n" " IdentitiesOnly yes\n" " LogLevel FATAL\n" ).format(host, docker_host, port, str(key))) kw["ssh_config"] = str(ssh_config) # Wait ssh to be up service = testinfra.get_backend( docker_id, connection="docker" ).get_module("Service") if image in ("centos_7", "fedora"): service_name = "sshd" else: service_name = "ssh" while not service(service_name).is_running: time.sleep(.5) if kw["connection"] != "ansible": hostspec = (user or "root") + "@" + host else: hostspec = host backend = testinfra.get_backend(hostspec, **kw) backend.get_hostname = lambda: image return backend
def LocalCommand(): return testinfra.get_backend("local").get_module("Command")
def LocalCommand(testinfra_backend): return testinfra.get_backend("local://").get_module("Command")
import pytest import testinfra run_local = testinfra.get_backend("local://").get_module("Command").run def test_scripts_pass_shellcheck(): """ Make sure shellcheck does not find anything wrong with our shell scripts """ shellcheck = "find . ! -path './pi-hole/*' -name '*.sh' -a ! -name 'gravity.sh' | while read file; do shellcheck $file; done;" results = run_local(shellcheck) print results.stdout assert "" == results.stdout
import pytest import testinfra check_output = testinfra.get_backend( "local://" ).get_module("Command").check_output @pytest.fixture def Pihole(Docker): ''' used to contain some script stubbing, now pretty much an alias. Also provides bash as the default run function shell ''' def run_bash(self, command, *args, **kwargs): cmd = self.get_command(command, *args) if self.user is not None: out = self.run_local( "docker exec -u %s %s /bin/bash -c %s", self.user, self.name, cmd) else: out = self.run_local( "docker exec %s /bin/bash -c %s", self.name, cmd) out.command = self.encode(cmd) return out funcType = type(Docker.run) Docker.run = funcType(run_bash, Docker, testinfra.backend.docker.DockerBackend) return Docker @pytest.fixture def Docker(request, args, image, cmd): ''' combine our fixtures into a docker run command and setup finalizer to cleanup ''' assert 'docker' in check_output('id'), "Are you in the docker group?"