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 host(request, tmpdir_factory): if not has_docker(): pytest.skip() return image, kw = parse_hostspec(request.param) spec = BaseBackend.parse_hostspec(image) for marker in getattr(request.function, "pytestmark", []): if marker.name == "destructive": scope = "function" break else: scope = "session" fname = "_docker_container_{}_{}".format(spec.name, scope) docker_id, docker_host, port = request.getfixturevalue(fname) if kw["connection"] == "docker": hostname = docker_id elif kw["connection"] in ("ansible", "ssh", "paramiko", "safe-ssh"): hostname = spec.name 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": setup_ansible_config(tmpdir, hostname, docker_host, spec.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(hostname, docker_host, port, str(key))) kw["ssh_config"] = str(ssh_config) # Wait ssh to be up service = testinfra.get_host(docker_id, connection="docker").service images_with_sshd = ("centos_7", "alpine", "archlinux") if image in images_with_sshd: service_name = "sshd" else: service_name = "ssh" while not service(service_name).is_running: time.sleep(0.5) if kw["connection"] != "ansible": hostspec = (spec.user or "root") + "@" + hostname else: hostspec = spec.name b = testinfra.host.get_host(hostspec, **kw) b.backend.get_hostname = lambda: image return b
def test_parse_hostspec(hostspec, expected): assert BaseBackend.parse_hostspec(hostspec) == expected
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
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 test_parse_hostspec(hostspec, expected): assert BaseBackend.parse_hostspec(hostspec) == expected
def host(request, tmpdir_factory): if not has_docker(): pytest.skip() return image, kw = parse_hostspec(request.param) spec = BaseBackend.parse_hostspec(image) if getattr(request.function, "destructive", None) is not None: scope = "function" else: scope = "session" fname = "_docker_container_%s_%s" % (spec.name, scope) docker_id, docker_host, port = request.getfixturevalue(fname) if kw["connection"] == "docker": hostname = docker_id elif kw["connection"] in ("ansible", "ssh", "paramiko", "safe-ssh"): hostname = spec.name 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, hostname, docker_host, spec.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(hostname, docker_host, port, str(key))) kw["ssh_config"] = str(ssh_config) # Wait ssh to be up service = testinfra.get_host( docker_id, connection='docker').service images_with_sshd = ( "centos_6", "centos_7", "alpine_38", "archlinux" ) if image in images_with_sshd: service_name = "sshd" else: service_name = "ssh" while not service(service_name).is_running: time.sleep(.5) if kw["connection"] != "ansible": hostspec = (spec.user or "root") + "@" + hostname else: hostspec = spec.name b = testinfra.host.get_host(hostspec, **kw) b.backend.get_hostname = lambda: image return b