Better tests

pull/1/head
Christophe Mehay 8 years ago
parent 013751ff72
commit 026c0b6369

@ -4,13 +4,12 @@ FROM python:2
RUN pip install pytest twiggy six pyyaml jinja2
ENV PYTHONPATH /opt/pyentrypoint/
ADD pyentrypoint /opt/pyentrypoint/
ADD tests /opt/
ENV PYTHONPATH /opt/
ADD tests/test_template.yml.tpl /tmp/test_template.yml
WORKDIR /opt/
WORKDIR /opt/tests
ENV SECRET nothing
CMD ["py.test", "-s", "."]

@ -4,14 +4,12 @@ FROM python:3
RUN pip3 install pytest twiggy six pyyaml jinja2
ENV PYTHONPATH /opt/pyentrypoint/
ADD pyentrypoint /opt/pyentrypoint/
ADD tests /opt/
ENV PYTHONPATH /opt/
ADD tests/test_template.yml.tpl /tmp/test_template.yml
WORKDIR /opt/
WORKDIR /opt/tests
ENV SECRET nothing
CMD ["py.test", "-s", "."]

@ -7,6 +7,9 @@ testpython3:
- test2
- test3
- test4
volumes:
- ./pyentrypoint:/opt/pyentrypoint:ro
- ./tests:/opt/tests
testpython2:
build: .
@ -17,6 +20,9 @@ testpython2:
- test2
- test3
- test4
volumes:
- ./pyentrypoint:/opt/pyentrypoint:ro
- ./tests:/opt/tests
test1:
image: busybox

@ -1,6 +1,7 @@
from __future__ import absolute_import
from __future__ import unicode_literals
from .constants import *
from .docker_links import DockerLinks
from .entrypoint import Entrypoint

@ -49,4 +49,4 @@ class Command(object):
if not self.args or \
[p for p in subcom if fnmatch(self.args[0], p)]:
self.args.insert(0, self.command)
os.execvpe(self.args[0], self.args, os.environ)
os.execvpe(self.args[0], self.args, self.env)

@ -14,6 +14,7 @@ from yaml import load
from yaml import Loader
from .command import Command
from .constants import ENTRYPOINT_FILE
from .docker_links import DockerLinks
from .links import Links
@ -30,8 +31,6 @@ class Config(object):
Config file should always be in WORKDIR and named entrypoint-config.yml
"""
_config_file = 'entrypoint-config.yml'
def _return_item_lst(self, item):
"""Return item as a list"""
if item in self._config:
@ -40,10 +39,11 @@ class Config(object):
return self._config[item]
return []
def __init__(self, args=[]):
def __init__(self, conf=ENTRYPOINT_FILE, args=[]):
self._config = {}
self._args = []
self._links = None
self._config_file = conf
if not os.path.isfile(self._config_file):
return
with open(self._config_file) as f:

@ -0,0 +1 @@
ENTRYPOINT_FILE = 'entrypoint-config.yml'

@ -9,12 +9,7 @@ class Container(object):
"""Container handles a single container link"""
ip = None
environ = None
names = None
links = None
def __init__(self, ip, env, names, links=None):
def __init__(self, ip, env, names, links=[]):
self.ip = ip
self.environ = env
self.names = names

@ -18,6 +18,7 @@ from twiggy import log
from twiggy import quickSetup
from .config import Config
from .constants import ENTRYPOINT_FILE
from .docker_links import DockerLinks
__all__ = ['Entrypoint', 'main']
@ -31,10 +32,10 @@ class Entrypoint(object):
quickSetup(min_level=levels.INFO)
self.log = log.name('entrypoint')
def __init__(self, args=[]):
def __init__(self, conf=ENTRYPOINT_FILE, args=[]):
self._set_logguer()
try:
self.config = Config(args)
self.config = Config(conf=conf, args=args)
except Exception as err:
self.log.error(err)
if self.config.debug:

@ -7,7 +7,7 @@ config_files:
- /tmp/test_template.yml
secret_env:
- SSHKEY
- SECRET
links:
test1:

@ -0,0 +1 @@
command: bash

@ -68,7 +68,7 @@ def test_ports():
def test_entrypoint_links():
entry = Entrypoint()
entry = Entrypoint(conf='configs/base.yml')
links = entry.config.links
assert len(links.all) == 4
@ -85,12 +85,20 @@ def test_containers():
assert len(ctns) == 4
for ctn in ctns:
if 'test1' in ctn.names or 'test3' in ctn.names:
if 'test1' in ctn.names:
assert ctn.environ['FOO'] == 'bar'
assert len(ctn.links) == 2
if 'test2' in ctn.names:
assert len(ctn.links) == 2
if 'test3' in ctn.names:
assert ctn.environ['FOO'] == 'bar'
assert len(ctn.links) == 0
if 'test4' in ctn.names:
assert len(ctn.links) == 0
def test_templates():
entry = Entrypoint()
entry = Entrypoint(conf='configs/base.yml')
conf = entry.config
@ -122,7 +130,7 @@ def test_templates():
def test_conf_commands():
entry = Entrypoint()
entry = Entrypoint(conf='configs/base.yml')
for cmd in entry.config.pre_conf_commands:
entry.run_conf_cmd(cmd)
@ -143,9 +151,12 @@ def test_command():
run = [
# ((Process instance), (file to check))
(Process(target=Entrypoint(
['-c', 'echo OK > /tmp/CMD']).launch), '/tmp/CMD'),
conf='configs/base.yml',
args=['-c', 'echo OK > /tmp/CMD']).launch), '/tmp/CMD'),
(Process(target=Entrypoint(
['bash', '-c', 'echo OK > /tmp/CMD2']).launch), '/tmp/CMD2'),
conf='configs/base.yml',
args=['bash', '-c', 'echo ${SECRET}OK > /tmp/CMD2']).launch),
'/tmp/CMD2'),
]
for proc, test in run:

Loading…
Cancel
Save