-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from avirshup/better_tests
Improved tests
- Loading branch information
Showing
10 changed files
with
154 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
target: | ||
target_include: | ||
FROM: debian:jessie | ||
build_directory: ../ | ||
build: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
unmatched-line-1 | ||
c | ||
**/unmatched-line-2 | ||
|
||
# comment | ||
c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']) | ||
|
||
|