Skip to content

Commit

Permalink
WIP: Integration tests using vagrant & docker
Browse files Browse the repository at this point in the history
  • Loading branch information
philpep committed May 15, 2015
1 parent b2909fe commit 5ea634f
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 43 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ coverage.xml
flake8.report
junit.xml
doc/build
.vagrant
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ test:
testinfra -vs --cov testinfra --cov-report term testinfra
flake8 testinfra

integration-test:
vagrant up
vagrant ssh-config > .vagrant/ssh_config
testinfra -vs --cov testinfra --cov-report term --integration --hosts=debian_wheezy,debian_jessie,centos_7,ubuntu_trusty,fedora_21 --ssh-config=.vagrant/ssh_config -n 4 testinfra

doc:
$(MAKE) -C doc html


.PHONY: all test doc
.PHONY: all test doc integration-test
17 changes: 17 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# vim: ts=2 sw=2 et ft=ruby

Vagrant.configure("2") do |config|
config.vm.synced_folder ".", "/vagrant", disabled: true
config.ssh.username = "root"
[
"debian_wheezy", "debian_jessie", "ubuntu_trusty",
"centos_7", "fedora_21",
].each do |vmname|
config.vm.define vmname do |vm|
vm.vm.provider "docker" do |docker|
docker.build_dir = "images/#{vmname}"
docker.has_ssh = true
end
end
end
end
13 changes: 13 additions & 0 deletions images/centos_7/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM centos:7

RUN yum -y update; \
yum -y install openssh-server && \
rm -f /etc/ssh/ssh_host_ecdsa_key /etc/ssh/ssh_host_rsa_key && \
ssh-keygen -q -N "" -t dsa -f /etc/ssh/ssh_host_ecdsa_key && \
ssh-keygen -q -N "" -t rsa -f /etc/ssh/ssh_host_rsa_key && \
sed -i "s/#UsePrivilegeSeparation.*/UsePrivilegeSeparation no/g" /etc/ssh/sshd_config


ADD https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub /root/.ssh/authorized_keys
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
9 changes: 9 additions & 0 deletions images/debian_jessie/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM debian:jessie

RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get -y install python openssh-server && \
mkdir -p /var/run/sshd

ADD https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub /root/.ssh/authorized_keys
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
9 changes: 9 additions & 0 deletions images/debian_wheezy/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM debian:wheezy

RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get -y install python openssh-server && \
mkdir -p /var/run/sshd

ADD https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub /root/.ssh/authorized_keys
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
13 changes: 13 additions & 0 deletions images/fedora_21/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM fedora:21

RUN yum -y update; \
yum -y install openssh-server && \
rm -f /etc/ssh/ssh_host_ecdsa_key /etc/ssh/ssh_host_rsa_key && \
ssh-keygen -q -N "" -t dsa -f /etc/ssh/ssh_host_ecdsa_key && \
ssh-keygen -q -N "" -t rsa -f /etc/ssh/ssh_host_rsa_key && \
sed -i "s/#UsePrivilegeSeparation.*/UsePrivilegeSeparation no/g" /etc/ssh/sshd_config


ADD https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub /root/.ssh/authorized_keys
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
9 changes: 9 additions & 0 deletions images/ubuntu_trusty/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM ubuntu:trusty

RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get -y install python openssh-server && \
mkdir -p /var/run/sshd

ADD https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub /root/.ssh/authorized_keys
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
30 changes: 24 additions & 6 deletions testinfra/backend/paramiko.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from __future__ import absolute_import

import logging
import os

from testinfra.backend import base

Expand All @@ -31,8 +32,9 @@

class ParamikoBakend(base.BaseBackend):

def __init__(self, hostspec, *args, **kwargs):
def __init__(self, hostspec, ssh_config=None, *args, **kwargs):
self.host, self.user, self.port = self.parse_hostspec(hostspec)
self.ssh_config = ssh_config
self._client = None
super(ParamikoBakend, self).__init__(*args, **kwargs)

Expand All @@ -45,11 +47,27 @@ def client(self):
"to use the paramiko backend"))
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.WarningPolicy())
client.connect(
self.host,
port=int(self.port) if self.port else 22,
username=self.user,
)
cfg = {
"hostname": self.host,
"port": int(self.port) if self.port else 22,
"username": self.user,
}
if self.ssh_config:
ssh_config = paramiko.SSHConfig()
with open(self.ssh_config) as f:
ssh_config.parse(f)

for key, value in ssh_config.lookup(self.host).items():
if key == "hostname":
cfg[key] = value
elif key == "user":
cfg["username"] = value
elif key == "port":
cfg[key] = int(value)
elif key == "identityfile":
cfg["key_filename"] = os.path.expanduser(value[0])

client.connect(**cfg)
self._client = client
return self._client

Expand Down
6 changes: 5 additions & 1 deletion testinfra/backend/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@
class SshBackend(local.LocalBackend):
"""Run command through ssh command"""

def __init__(self, hostspec, *args, **kwargs):
def __init__(self, hostspec, ssh_config=None, *args, **kwargs):
self.host, self.user, self.port = self.parse_hostspec(hostspec)
self.ssh_config = ssh_config
super(SshBackend, self).__init__(*args, **kwargs)

def run(self, command, *args, **kwargs):
cmd = ["ssh"]
cmd_args = []
if self.ssh_config:
cmd.append("-F %s")
cmd_args.append(self.ssh_config)
if self.user:
cmd.append("-o User=%s")
cmd_args.append(self.user)
Expand Down
12 changes: 11 additions & 1 deletion testinfra/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@
def testinfra_backend(pytestconfig, _testinfra_host):
if _testinfra_host is not None:
backend_type = pytestconfig.option.connection or "paramiko"
testinfra.set_backend(backend_type, _testinfra_host)
testinfra.set_backend(
backend_type,
_testinfra_host,
ssh_config=pytestconfig.option.ssh_config,
)
else:
testinfra.set_backend("local")

Expand All @@ -53,6 +57,12 @@ def pytest_addoption(parser):
dest="hosts",
help="Hosts list (comma separated)",
)
group._addoption(
"--ssh-config",
action="store",
dest="ssh_config",
help="SSH config file",
)
group._addoption(
"--nagios",
action="store_true",
Expand Down
33 changes: 11 additions & 22 deletions testinfra/test/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,14 @@


@pytest.mark.integration
def test_package(Package, SystemInfo):
assert Package("zsh").is_installed
if (
SystemInfo.type, SystemInfo.distribution, SystemInfo.codename,
) == ("linux", "debian", "wheezy"):
version = "4.3.17-1"
elif (
SystemInfo.type, SystemInfo.release,
) == ("freebsd", "10.1-RELEASE"):
version = "5.0.7_2"
elif (
SystemInfo.type, SystemInfo.release,
) == ("netbsd", "6.1.5"):
version = "5.0.7nb1"
elif (
SystemInfo.type, SystemInfo.release,
) == ("openbsd", "5.6"):
version = "5.0.5p0"
else:
pytest.skip()

assert Package("zsh").version == version
def test_package_bash(_testinfra_host, Package):
bash = Package("bash")
version = {
"debian_wheezy": "4.2+dfsg-0.1+deb7u3",
"debian_jessie": "4.3-11+b1",
"ubuntu_trusty": "4.3-7ubuntu1.5",
"centos_7": "4.2.46",
"fedora_21": "4.3.33",
}[_testinfra_host]
assert bash.is_installed
assert bash.version == version
20 changes: 8 additions & 12 deletions testinfra/test/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@


@pytest.mark.integration
def test_ssh_service(Service, SystemInfo):
if SystemInfo.type == "linux":
name = "ssh"
elif SystemInfo.type.endswith("bsd"):
name = "sshd"
def test_ssh_service(_testinfra_host, Service, SystemInfo):
if _testinfra_host in (
"ubuntu_trusty", "debian_wheezy", "debian_jessie",
):
ssh = Service("ssh")
assert ssh.is_running
assert ssh.is_enabled
else:
pytest.skip("unsupported system")
assert Service(name).is_running
if SystemInfo.type in ("netbsd", "openbsd"):
with pytest.raises(NotImplementedError):
assert Service(name).is_enabled
else:
assert Service(name).is_enabled
pytest.skip()

0 comments on commit 5ea634f

Please sign in to comment.