Skip to content

Commit

Permalink
Merge pull request #34 from avirshup/better_tests
Browse files Browse the repository at this point in the history
Improved tests
  • Loading branch information
avirshup authored Oct 18, 2017
2 parents 94c9d80 + 6569454 commit 1ff4dcf
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 15 deletions.
6 changes: 3 additions & 3 deletions dockermake/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ def run(args):
return

if not os.path.exists(args.makefile):
print('No docker makefile found at path "%s"'%args.makefile)
msg = 'No docker makefile found at path "%s"' % args.makefile
if args.makefile == 'DockerMake.yml':
print('Type `docker-make --help` to see usage.')
sys.exit(1)
msg += '\nType `docker-make --help` to see usage.'
raise errors.MissingFileError(msg)

defs = ImageDefs(args.makefile)

Expand Down
2 changes: 1 addition & 1 deletion dockermake/step.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def _get_ignorefile(img_def):
elif img_def.get('ignorefile', None) is not None:
assert 'ignore' not in img_def
with open(img_def['ignorefile'], 'r') as igfile:
lines = list(igfile)
lines = igfile.read().splitlines()
else:
return None

Expand Down
Empty file added test/__init__.py
Empty file.
26 changes: 25 additions & 1 deletion test/data/ignores.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
target:
target_ignore_string:
FROM: alpine
build_directory: ./test_build
ignore: |
# comment
b
unmatched_line
build: |
ADD . /opt
target_ignorefile:
FROM: alpine
build_directory: ./test_build
ignorefile: test_build/custom_ignore_file.txt
build: |
ADD . /opt
target_regular_ignore:
FROM: alpine
build_directory: ./test_build
build: |
ADD . /opt
target_ignore_directory:
FROM: alpine
ignore: |
d
unmatched_line
build_directory: ./test_build
build: |
ADD . /opt
6 changes: 4 additions & 2 deletions test/data/multibase.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ install_something:
build: |
RUN mkdir -p /opt/java8
RUN touch /opt/java8/java
RUN echo success2 > /opt/success
target3:
target3_bases:
requires:
- install_something
FROM: continuumio/miniconda3
build: RUN echo success3 > /opt/success

target2:
target2_bases:
requires:
- install_something
- base2
Expand Down
2 changes: 1 addition & 1 deletion test/data/relative_path_test_dir/include_target.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target:
target_include:
FROM: debian:jessie
build_directory: ../
build:
Expand Down
5 changes: 5 additions & 0 deletions test/data/test_build/custom_ignore_file.txt
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
unmatched-line-1
c
**/unmatched-line-2

# comment
c
1 change: 1 addition & 0 deletions test/data/test_build/d/d
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
d
60 changes: 60 additions & 0 deletions test/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import os
import io
import tarfile
import pytest
import docker.errors

__client = None

def _get_client():
"""
Returns:
docker.APIClient
"""
global __client
if __client is None:
__client = docker.from_env()
return __client


def creates_images(*imgnames):
""" Creates fixtures to make sure to remove named images after (and before, if necessary)
running a test
"""
@pytest.fixture
def fixture():
client = _get_client()
for name in imgnames:
try:
client.images.remove(name, force=True)
except docker.errors.ImageNotFound:
pass

yield

for name in imgnames: # force it to also remove the containers
client.images.remove(name, force=True)
return fixture


def assert_file_content(imgname, path, content):
""" Asserts that an image exists with a file at the
specified path containing the specified content
"""
client = _get_client()
try:
image = client.images.get(imgname)
except (docker.errors.ImageNotFound, docker.errors.APIError) as exc:
assert False, "Image %s not found: %s" % (imgname, exc)

container = client.containers.create(image)

try:
tarstream, stat = container.get_archive(path)
except docker.errors.NotFound:
assert False, 'File %s not found' % path
container.remove()

tf = tarfile.open(fileobj=io.BytesIO(tarstream.read()))
val = tf.extractfile(os.path.basename(path)).read().decode('utf-8')
assert val.strip() == content
61 changes: 54 additions & 7 deletions test/test_features.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,61 @@
import subprocess
import pytest
from dockermake.__main__ import _runargs as run_docker_make

from .helpers import assert_file_content, creates_images

def test_multiple_bases():
subprocess.check_call(['docker-make', '-f', 'data/multibase.yml', 'target2', 'target3'])

# note: these tests MUST be run with CWD REPO_ROOT/tests

def test_paths_relative_interpreted_relative_to_definition_file():
subprocess.check_call(['docker-make', '-f', 'data/include.yml', 'target'])
img1 = creates_images(*'target2_bases target3_bases'.split())
def test_multiple_bases(img1):
run_docker_make('-f data/multibase.yml target2_bases target3_bases')
assert_file_content('target2_bases', '/opt/success', 'success2')
assert_file_content('target3_bases', '/opt/success', 'success3')


def test_ignore_string():
subprocess.check_call(['docker-make', '-f', 'data/ignores.yml', 'target'])
img2 = creates_images('target_include')
def test_paths_relative_interpreted_relative_to_definition_file(img2):
run_docker_make('-f data/include.yml target_include')
assert_file_content('target_include', '/opt/testfile.txt',
'this is a file used in tests for relative path resolution')


_FILES = {'a': {'content': 'a', 'path': '/opt/a'},
'b': {'content': 'b', 'path': '/opt/b'},
'c': {'content': 'c', 'path': '/opt/c'},
'd': {'content': 'd', 'path': '/opt/d/d'}}


img3 = creates_images('target_ignore_string')
def test_ignore_string(img3):
run_docker_make('-f data/ignores.yml target_ignore_string')
_check_files('target_ignore_string', b=False)


img4 = creates_images('target_ignorefile')
def test_ignorefile(img4):
run_docker_make('-f data/ignores.yml target_ignorefile')
_check_files('target_ignorefile', c=False)


img5 = creates_images('target_regular_ignore')
def test_regular_ignore(img5):
run_docker_make('-f data/ignores.yml target_regular_ignore')
_check_files('target_regular_ignore', a=False, b=False)


img6 = creates_images('target_ignore_directory')
def test_ignore_directory(img6):
run_docker_make('-f data/ignores.yml target_ignore_directory')
_check_files('target_ignore_directory', d=False)


def _check_files(img, **present):
for f, record in _FILES.items():
if not present.get(f, True):
with pytest.raises(AssertionError):
assert_file_content(img, record['path'], record['content'])
else:
assert_file_content(img, record['path'], record['content'])


0 comments on commit 1ff4dcf

Please sign in to comment.