Skip to content

Commit

Permalink
Tests with docker-compose
Browse files Browse the repository at this point in the history
  • Loading branch information
philpep committed Mar 8, 2016
1 parent 71eeb18 commit 41d3b98
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ coverage.xml
flake8.report
junit*.xml
doc/build
.vagrant
.cache
19 changes: 15 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
sudo: required
services:
- docker
env:
global:
- DOCKER_VERSION=1.10.2-0~trusty
- DOCKER_COMPOSE_VERSION=1.6.2
language: python
python: 2.7
before_install:
- wget https://releases.hashicorp.com/vagrant/1.8.1/vagrant_1.8.1_x86_64.deb
- sudo dpkg -i vagrant_1.8.1_x86_64.deb
- for image in debian_wheezy debian_jessie ubuntu_trusty centos_7 fedora_21; do docker pull philpep/testinfra:$image; done
- vagrant up --provider=docker
- apt-cache madison docker-engine
- sudo apt-get -o Dpkg::Options::="--force-confnew" install -y docker-engine=${DOCKER_VERSION}
- curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > docker-compose
- chmod +x docker-compose
- sudo mv docker-compose /usr/local/bin
- docker version
- docker-compose version
- for image in debian_wheezy debian_jessie ubuntu_trusty centos_7; do docker pull philpep/testinfra:$image; done
- docker-compose up -d
install:
- pip install tox
script:
- tox
after_script:
- docker-compose kill
14 changes: 7 additions & 7 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ You're encouraged to setup a full test environment, to add tests and check if
all the tests pass *before* submitting your pull request. To run the complete
test suite you must install:

- `Vagrant <https://vagrantup.com>`_ version 1.7 or greater
- `Docker <https://www.docker.com>`_ version 1.7 or greater
- `Docker <https://www.docker.com>`_ version 1.10 or greater
- `Docker compose <https://docs.docker.com/compose/>`_ version 1.6 or greater
- `tox <https://tox.readthedocs.org/en/latest/>`_

To run all tests run::

vagrant up --provider=docker
docker-compose up -d
tox

To run only some selected tests::
Expand All @@ -32,11 +32,11 @@ To run only some selected tests::
tox -e py27 -- -v --pdb testinfra/test/integration/test_jessie.py


To speedup the docker images generation during `vagrant up` you can download
them before running the tests (docker will detect they are the same and use the
cache)::
To speedup the docker images generation during `docker-compose up` you can
download them before running the tests (docker will detect they are the same
and use the cache)::

for tag in debian_jessie debian_wheezy centos_7 fedora_21 ubuntu_trusty; do
for tag in debian_jessie debian_wheezy centos_7 fedora ubuntu_trusty; do
docker pull philpep/testinfra:$tag
done

Expand Down
25 changes: 0 additions & 25 deletions Vagrantfile

This file was deleted.

24 changes: 24 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: '2'
services:
debian_wheezy:
build: images/debian_wheezy
hostname: debian_wheezy

debian_jessie:
build: images/debian_jessie
privileged: true
hostname: debian_jessie

centos_7:
build: images/centos_7
privileged: true
hostname: centos_7

fedora:
build: images/fedora
privileged: true
hostname: fedora

ubuntu_trusty:
build: images/ubuntu_trusty
hostname: ubuntu_trusty
4 changes: 2 additions & 2 deletions images/fedora_21/Dockerfile → images/fedora/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM fedora:21
FROM fedora:23

RUN yum -y install systemd openssh-server && yum clean all &&\
RUN dnf -y install which procps python systemd openssh-server && dnf clean all &&\
(cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done) && \
rm -f /lib/systemd/system/multi-user.target.wants/* && \
rm -f /etc/systemd/system/*.wants/* && \
Expand Down
27 changes: 27 additions & 0 deletions insecure_private_key
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzI
w+niNltGEFHzD8+v1I2YJ6oXevct1Y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Bo2+
dBEjSBwLk5q5tJqHmy/jKMJL4n9ROlx93XS+njxgibTvU6Fp9w+NOFD/HvxB3Tcz
6+jJF85D5BNAG3DBMKBjAoGBAOAxZvgsKN+JuENXsST7F89Tck2iTcQIT8g5rwWC
P9Vt74yboe2kDT531w8+egz7nAmRBKNM751U/95P9t88EDacDI/Z2OwnuFQHCPDF
llYOUI+SpLJ6/vURRbHSnnn8a/XG+nzedGH5JGqEJNQsz+xT2axM0/W/CRknmGaJ
kda/AoGANWrLCz708y7VYgAtW2Uf1DPOIYMdvo6fxIB5i9ZfISgcJ/bbCUkFrhoH
+vq/5CIWxCPp0f85R4qxxQ5ihxJ0YDQT9Jpx4TMss4PSavPaBH3RXow5Ohe+bYoQ
NE5OgEXk2wVfZczCZpigBKbKZHNYcelXtTt/nP3rsCuGcM4h53s=
-----END RSA PRIVATE KEY-----
8 changes: 6 additions & 2 deletions testinfra/test/integration/test_centos_7.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@

import pytest


from testinfra.utils.docker_compose_to_ssh_config import DockerComposeService


pytestmark = pytest.mark.integration
testinfra_hosts = [
"%s://centos_7" % (b_type,)
for b_type in ("ssh", "paramiko", "safe-ssh", "docker")
]
for b_type in ("ssh", "paramiko", "safe-ssh")
] + ["docker://" + DockerComposeService.get("centos_7")["name"]]

if sys.version_info == 2:
testinfra_hosts.append("ansible://centos_7")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,22 @@

import pytest

from testinfra.utils.docker_compose_to_ssh_config import DockerComposeService

pytestmark = pytest.mark.integration
testinfra_hosts = [
"%s://fedora_21" % (b_type,)
for b_type in ("ssh", "paramiko", "safe-ssh", "docker")
]
"%s://fedora" % (b_type,)
for b_type in ("ssh", "paramiko", "safe-ssh")
] + ["docker://" + DockerComposeService.get("fedora")["name"]]

if sys.version_info[0] == 2:
testinfra_hosts.append("ansible://fedora_21")
testinfra_hosts.append("ansible://fedora")


def test_ssh_package(Package):
ssh = Package("openssh-server")
assert ssh.is_installed
assert ssh.version.startswith("6.6")
assert ssh.version.startswith("7.1")


def test_ssh_service(Service):
Expand All @@ -43,7 +45,7 @@ def test_ssh_service(Service):

def test_systeminfo(SystemInfo):
assert SystemInfo.type == "linux"
assert SystemInfo.release == "21"
assert SystemInfo.release == "23"
assert SystemInfo.distribution == "fedora"
assert SystemInfo.codename is None

Expand Down
6 changes: 4 additions & 2 deletions testinfra/test/integration/test_jessie.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@

import pytest

from testinfra.utils.docker_compose_to_ssh_config import DockerComposeService

pytestmark = pytest.mark.integration
testinfra_hosts = [
"%s://debian_jessie" % (b_type,)
for b_type in ("ssh", "paramiko", "safe-ssh", "docker")
]
for b_type in ("ssh", "paramiko", "safe-ssh")
] + ["docker://" + DockerComposeService.get("debian_jessie")["name"]]

if sys.version_info[0] == 2:
testinfra_hosts.append("ansible://debian_jessie")
Expand Down
8 changes: 7 additions & 1 deletion testinfra/test/integration/test_trusty.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@

import pytest

from testinfra.utils.docker_compose_to_ssh_config import DockerComposeService

_docker_name = DockerComposeService.get("ubuntu_trusty")["name"]
pytestmark = pytest.mark.integration
testinfra_hosts = [
"%s://ubuntu_trusty?sudo=%s" % (b_type, sudo)
for b_type, sudo in itertools.product(
["ssh", "paramiko", "safe-ssh", "docker"],
["ssh", "paramiko", "safe-ssh"],
["true", "false"],
)
] + [
"docker://%s?sudo=%s" % (_docker_name, sudo,)
for sudo in ["true", "false"]
]

if sys.version_info == 2:
Expand Down
6 changes: 4 additions & 2 deletions testinfra/test/integration/test_wheezy.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@

import pytest

from testinfra.utils.docker_compose_to_ssh_config import DockerComposeService

pytestmark = pytest.mark.integration
testinfra_hosts = [
"%s://debian_wheezy" % (b_type,)
for b_type in ("ssh", "paramiko", "safe-ssh", "docker")
]
for b_type in ("ssh", "paramiko", "safe-ssh")
] + ["docker://" + DockerComposeService.get("debian_wheezy")["name"]]

if sys.version_info[0] == 2:
testinfra_hosts.append("ansible://debian_wheezy")
Expand Down
70 changes: 70 additions & 0 deletions testinfra/utils/docker_compose_to_ssh_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# -*- coding: utf8 -*-
# Copyright © 2016 Philippe Pepiot
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function

import testinfra

template = """
Host %s
HostName %s
User root
Port 22
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile .cache/insecure_private_key
IdentitiesOnly yes
LogLevel FATAL
"""


class DockerComposeService(object):
_services = None

@classmethod
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

@classmethod
def get(cls, service):
return cls.get_services()[service]


def docker_compose_to_ssh_config():
for service, values in DockerComposeService.get_services().items():
print(template % (service, values["ip"]))


if __name__ == "__main__":
docker_compose_to_ssh_config()
6 changes: 3 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ deps=
-rtest-requirements.txt
py27: ansible>=2
commands=
/bin/sh -c 'test -f .vagrant/ssh-config || vagrant ssh-config > .vagrant/ssh-config'
py27: /bin/sh -c 'python -m testinfra.utils.ssh_config_to_ansible_inventory .vagrant/ssh-config > .vagrant/ansible-inventory'
testinfra --integration --ansible-inventory=.vagrant/ansible-inventory --ssh-config=.vagrant/ssh-config {posargs:-v -n 5 --cov testinfra --cov-report xml --cov-report term testinfra}
/bin/sh -c 'mkdir -p .cache && install -m 600 insecure_private_key .cache/ && python -m testinfra.utils.docker_compose_to_ssh_config > .cache/ssh-config'
py27: /bin/sh -c 'python -m testinfra.utils.ssh_config_to_ansible_inventory .cache/ssh-config > .cache/ansible-inventory'
testinfra --integration --ansible-inventory=.cache/ansible-inventory --ssh-config=.cache/ssh-config {posargs:-v -n 5 --cov testinfra --cov-report xml --cov-report term testinfra}
usedevelop=True
passenv=HOME TRAVIS

Expand Down

0 comments on commit 41d3b98

Please sign in to comment.