Skip to content

Commit

Permalink
Fix #230 (#234)
Browse files Browse the repository at this point in the history
* fix dependency in validate json

* add circular dependency detect

* add tests for circular dependency.

* fix dependency test
  • Loading branch information
iberryful authored and ib-steffen committed Dec 13, 2018
1 parent be3b50d commit 27e1ec6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/pyinfrabox/infrabox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,8 @@ def validate_json(d):
return True

jobs = {}
all_job_names = set([j['name'] for j in d['jobs']])
all_deps = {}
for i in range(0, len(d['jobs'])):
job = d['jobs'][i]
job_name = job['name']
Expand Down Expand Up @@ -537,12 +539,23 @@ def validate_json(d):
if job_name == parent_name:
raise ValidationError(path, "Job '%s' may not depend on itself" % parent_name)

if parent_name not in jobs:
if parent_name not in all_job_names:
raise ValidationError(path + ".depends_on", "Job '%s' not found" % parent_name)

if parent_name in deps:
raise ValidationError(path + ".depends_on", "'%s' duplicate dependencies" % parent_name)

deps[parent_name] = True

if deps:
all_deps[job_name] = deps

for job_name, deps in all_deps.items():
queue = list(deps.keys())
for dep_job in queue:
if dep_job == job_name:
raise ValidationError("Jobs", "Circular dependency detected.")
if dep_job in all_deps:
queue.extend(all_deps[dep_job].keys())

return True
31 changes: 30 additions & 1 deletion src/pyinfrabox/tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,36 @@ def test_dep_defined_later(self):
}]
}

self.raises_expect(d, "#jobs[0].depends_on: Job 'compile' not found")
validate_json(d)

def test_dep_detect_circular_dependency(self):
d = {
"version": 1,
"jobs": [{
"type": "docker",
"name": "a",
"docker_file": "Dockerfile",
"resources": {"limits": {"cpu": 1, "memory": 1024}},
"build_only": False,
"depends_on": ["b"]
}, {
"type": "docker",
"name": "b",
"docker_file": "Dockerfile",
"build_only": False,
"depends_on": ["c"],
"resources": {"limits": {"cpu": 1, "memory": 1024}},
}, {
"type": "docker",
"name": "c",
"docker_file": "Dockerfile",
"build_only": False,
"depends_on": ["a"],
"resources": {"limits": {"cpu": 1, "memory": 1024}},
}]
}

self.raises_expect(d, "Jobs: Circular dependency detected.")

def test_dep_not_found(self):
d = {
Expand Down

0 comments on commit 27e1ec6

Please sign in to comment.