Moar debug and main tests

pull/3/head
Christophe Mehay 8 years ago
parent 01cd8137cc
commit 0bfa834b71

@ -13,4 +13,4 @@ WORKDIR /opt/tests
ENV SECRET nothing
CMD ["py.test", "--verbose", "-s", "."]
CMD ["py.test", "--verbose", "-rw", "."]

@ -13,4 +13,4 @@ WORKDIR /opt/tests
ENV SECRET nothing
CMD ["py.test", "--verbose", "-s", "."]
CMD ["py.test", "--verbose", "-rw", "."]

@ -6,5 +6,8 @@ build:
test: build
@docker-compose up --force-recreate testpython2 testpython3
test_debug: build
@docker-compose up --force-recreate testpython2_debug testpython3_debug
publish:
@python setup.py register && python setup.py sdist upload

@ -1,7 +1,6 @@
testpython3:
build: .
dockerfile: Dockerfile.py3
#command: python3 docker_links.py
links:
- test1
- test2
@ -14,7 +13,32 @@ testpython3:
testpython2:
build: .
dockerfile: Dockerfile.py2
#command: python docker_links.py
links:
- test1
- test2
- test3
- test4
volumes:
- ./pyentrypoint:/opt/pyentrypoint:ro
- ./tests:/opt/tests
testpython3_debug:
build: .
dockerfile: Dockerfile.py3
command: ["py.test", "--verbose", "-s", "-rw", "."]
links:
- test1
- test2
- test3
- test4
volumes:
- ./pyentrypoint:/opt/pyentrypoint:ro
- ./tests:/opt/tests
testpython2_debug:
build: .
dockerfile: Dockerfile.py2
command: ["py.test", "--verbose", "-s", "-rw", "."]
links:
- test1
- test2

@ -5,6 +5,8 @@ from __future__ import unicode_literals
import os
from fnmatch import fnmatch
from .logs import Logs
class Command(object):
"""This object handle command in dockerfile"""
@ -14,6 +16,11 @@ class Command(object):
self.args = args
self.command = command
self.env = os.environ
self.log = Logs().log
self.log.debug('Handled command is: {cmd}'.format(cmd=self.command))
self.log.debug('Arguments are: "{args}"'.format(
args='" "'.join(self.args)
))
def _clean_links_env(self):
# TODO: that Looks too much complicated
@ -41,12 +48,20 @@ class Command(object):
subcom = self.config.subcommands
if not self.args or self.args[0] == self.command or \
[p for p in subcom if fnmatch(self.args[0], p)]:
self.log.debug("Command is handled")
return True
self.log.debug("Command is not handled")
return False
def _exec(self):
self.log.debug('Now running: "{args}"'.format(
args='" "'.join(self.args)
))
os.execvpe(self.args[0], self.args, self.env)
def run(self):
if not self.is_handled:
os.execvpe(self.args[0], self.args, self.env)
self._exec()
if os.getuid() is 0:
os.setgid(self.config.group)
os.setuid(self.config.user)
@ -59,4 +74,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, self.env)
self._exec()

@ -111,7 +111,9 @@ def main(argv):
entry = Entrypoint(args=argv)
try:
if not entry.is_handled and not entry.should_config:
entry.log.warning("Running command without config")
entry.launch()
entry.log.debug("Starting config")
entry.run_pre_conf_cmds()
entry.apply_conf()
entry.run_post_conf_cmds()

@ -5,7 +5,7 @@ from setuptools import setup
# Thanks Sam and Max
__version__ = '0.3.2'
__version__ = '0.3.3'
if __name__ == '__main__':
setup(

@ -33,3 +33,5 @@ post_conf_commands:
- echo TEST3 > /tmp/OKOKOK
- echo "INFO IS DISPLAYED\nON TWO LINES"
- echo "WARNING IS DISPLAYED" 1>&2
debug: true

@ -10,3 +10,5 @@ pre_conf_commands:
post_conf_commands:
- echo NOPE >> /tmp/CMD4
debug: true

@ -0,0 +1,9 @@
command: cat
user: 1000
group: 1000
pre_conf_commands:
- echo OK > /tmp/CMD_FORCE
debug: true

@ -0,0 +1,88 @@
# Tests using pytest
from __future__ import absolute_import
from __future__ import unicode_literals
import os
from multiprocessing import Process
from pyentrypoint.entrypoint import main
class ProxyMain(object):
def __init__(self, args, env):
self.args = args
self.env = env
def run(self):
for key, val in self.env.items():
os.environ[key] = val
main(self.args)
def test_main():
run = [
# ((Process instance), (file to check), (uid), (gid))
(
Process(target=ProxyMain(
args=['pyentrypoint', '-c', 'echo OK > /tmp/CMD6'],
env={'ENTRYPOINT_CONFIG': 'configs/base.yml'}
).run),
'/tmp/CMD6',
1000,
1000,
), (
Process(target=ProxyMain(
args=['pyentrypoint',
'bash',
'-c',
'echo ${SECRET}OK > /tmp/CMD7'],
env={'ENTRYPOINT_CONFIG': 'configs/base.yml'}
).run),
'/tmp/CMD7',
1000,
1000,
), (
Process(target=ProxyMain(
args=['pyentrypoint', 'bash', '-c', 'echo OK > /tmp/CMD8'],
env={'ENTRYPOINT_CONFIG': 'configs/usernames.yml'}
).run),
'/tmp/CMD8',
33,
33,
), (
Process(target=ProxyMain(
args=['pyentrypoint', 'bash', '-c', 'echo OK > /tmp/CMD9'],
env={'ENTRYPOINT_CONFIG': 'configs/unhandled.yml'}
).run),
'/tmp/CMD9',
0,
0,
), (
Process(target=ProxyMain(
args=['pyentrypoint', 'bash', '-c', 'echo OK > /tmp/CMD10'],
env={'ENTRYPOINT_CONFIG': 'configs/unhandled_force.yml',
'ENTRYPOINT_FORCE': 'true'}
).run),
'/tmp/CMD_FORCE',
0,
0,
), (
Process(target=ProxyMain(
args=['pyentrypoint', 'bash', '-c', 'echo OK > /tmp/CMD11'],
env={'ENTRYPOINT_CONFIG': '/dontexist'}
).run),
'/tmp/CMD11',
0,
0,
)
]
for proc, test, uid, gid in run:
proc.start()
proc.join()
with open(test) as f:
assert f.readline().startswith('OK')
assert os.stat(test).st_uid == uid
assert os.stat(test).st_gid == gid

@ -183,7 +183,7 @@ def test_command():
args=['bash', '-c', 'echo OK > /tmp/CMD4']).launch),
'/tmp/CMD4', 0, 0),
(Process(target=Entrypoint(
conf='/dontexit',
conf='/dontexist',
args=['bash', '-c', 'echo OK > /tmp/CMD5']).launch),
'/tmp/CMD5', 0, 0),
]
@ -207,18 +207,20 @@ def test_config_file():
def test_force_config():
os.environ['ENTRYPOINT_FORCE'] = 'True'
entry = Entrypoint(conf='configs/base.yml')
assert not entry.should_config
os.environ['ENTRYPOINT_FORCE'] = 'True'
assert entry.should_config
del os.environ['ENTRYPOINT_FORCE']
def test_display_raw():
os.environ['ENTRYPOINT_RAW'] = 'True'
entry = Entrypoint(conf='configs/base.yml')
assert not entry.raw_output
os.environ['ENTRYPOINT_RAW'] = 'True'
assert entry.raw_output
del os.environ['ENTRYPOINT_RAW']

Loading…
Cancel
Save