Skip to content

Commit

Permalink
Merge pull request #56 from avirshup/fix_all_flag
Browse files Browse the repository at this point in the history
Fix --all flag
  • Loading branch information
avirshup authored Jun 8, 2018
2 parents 5a480ac + b347ab0 commit a85c3e0
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 21 deletions.
20 changes: 13 additions & 7 deletions dockermake/imagedefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,34 +43,40 @@ def __init__(self, makefile_path):
print('Working directory: %s' % os.path.abspath(os.curdir))
print('Copy cache directory: %s' % staging.TMPDIR)
try:
self.ymldefs = self.parse_yaml(self.makefile_path)
ymldefs, alltargets = self.parse_yaml(self.makefile_path)
except errors.UserException:
raise
except Exception as exc:
raise errors.ParsingFailure('Failed to read file %s:\n' % self.makefile_path +
str(exc))
self.all_targets = self.ymldefs.pop('_ALL_', [])

self.ymldefs = ymldefs
self.all_targets = alltargets
self._external_dockerfiles = {}

def parse_yaml(self, filename):
# locate and verify the DockerMake.yml file
fname = os.path.expanduser(filename)
print('READING %s' % os.path.expanduser(fname))
if fname in self._sources:
raise errors.CircularSourcesError('Circular _SOURCES_ in %s' % self.makefile_path)
self._sources.add(fname)

with open(fname, 'r') as yaml_file:
yamldefs = yaml.load(yaml_file)

self._check_yaml_and_paths(filename, yamldefs)

# Recursively read all steps in included files from the _SOURCES_ field and
# store them in sourcedefs
sourcedefs = {}
for s in yamldefs.get('_SOURCES_', []):
src = self.parse_yaml(s)
for s in yamldefs.pop('_SOURCES_', []):
src, _ = self.parse_yaml(s) # ignore source's _ALL_ targets
sourcedefs.update(src)

# Now add the steps defined in this file
sourcedefs.update(yamldefs)
return sourcedefs
alltargets = sourcedefs.pop('_ALL_', [])

return sourcedefs, alltargets

@staticmethod
def _check_yaml_and_paths(ymlfilepath, yamldefs):
Expand Down
16 changes: 16 additions & 0 deletions test/data/explicit_all.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
_ALL_:
- t1
- t3

_SOURCES_:
- included_all.yml

t1:
FROM: alpine
build: |
RUN mkdir /opt && echo t1 > /opt/t1
t2:
FROM: alpine
build: |
RUN mkdir /opt && echo t2 > /opt/t2
12 changes: 12 additions & 0 deletions test/data/implicit_all.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
_SOURCES_:
- included_all.yml

t1:
FROM: alpine
build: |
RUN mkdir /opt && echo t1 > /opt/t1
t2:
FROM: alpine
build: |
RUN mkdir /opt && echo t2 > /opt/t2
9 changes: 9 additions & 0 deletions test/data/included_all.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
t3:
FROM: alpine
build: |
RUN mkdir /opt && echo t3 > /opt/t3
t4:
FROM: alpine
build: |
RUN mkdir /opt && echo t4 > /opt/t4
10 changes: 5 additions & 5 deletions test/test_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ def test_push_dockerhub_with_login():
if 'DOCKERUSER' not in os.environ or 'DOCKERTOKEN' not in os.environ:
pytest.skip("Can't test dockerhub push - no login info available")

USER = os.environ['DOCKERUSER']
TOKEN = os.environ['DOCKERTOKEN']
user = os.environ['DOCKERUSER']
token = os.environ['DOCKERTOKEN']

subprocess.check_call(['docker-make','testimage','--repo',
'docker.io/%s/docker-make-test-push:' % USER,
'docker.io/%s/docker-make-test-push:' % user,
'--tag', customtag, '--push',
'--user', USER,
'--token', TOKEN],
'--user', user,
'--token', token],
cwd=THISDIR)

subprocess.check_call(['docker','pull',
Expand Down
36 changes: 27 additions & 9 deletions test/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ def test_paths_relative_interpreted_relative_to_definition_file(img2):
'd': {'content': 'd', 'path': '/opt/d/d'}}


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


img3 = helpers.creates_images('target_ignore_string')
def test_ignore_string(img3):
run_docker_make('-f data/ignores.yml target_ignore_string')
Expand Down Expand Up @@ -171,21 +180,13 @@ def test_build_fails_if_secrets_already_exist(experimental_daemon, secretfail):
with pytest.raises(dockermake.errors.BuildError):
run_docker_make('-f data/secret-squash.yml secretfail')


copy_with_secrets = helpers.creates_images('copy_with_secrets')
def test_error_if_copy_with_secrets(copy_with_secrets):
with pytest.raises(dockermake.errors.ParsingFailure):
run_docker_make('-f data/copy_with_secrets.yml copy_with_secrets')


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


twostep = helpers.creates_images('target-twostep',
'dmkbuild_target-twostep_2',
'dmkbuild_target-twostep_1')
Expand All @@ -195,6 +196,23 @@ def test_keep_build_tags(twostep, docker_client):
docker_client.images.get('dmkbuild_target-twostep_2')


alltest = helpers.creates_images('t1', 't2', 't3', 't4')
def test_implicit_all(alltest):
run_docker_make('-f data/implicit_all.yml --all')
for s in 't1 t2 t3 t4'.split():
helpers.assert_file_content(s, '/opt/%s' % s, s)


def test_explicit_all(alltest):
run_docker_make('-f data/explicit_all.yml --all')
for s in 't1 t3'.split():
helpers.assert_file_content(s, '/opt/%s' % s, s)
client = helpers.get_client()
for s in 't2 t4'.split():
with pytest.raises(docker.errors.ImageNotFound):
client.images.get(s)


buildargs = helpers.creates_images('target-buildargs')
def test_build_args(buildargs):
run_docker_make('-f data/build-args.yml --build-arg FILENAME=hello-world.txt target-buildargs')
Expand Down

0 comments on commit a85c3e0

Please sign in to comment.