Ensure templates have different destinations

This commit is contained in:
Alex Dadgar
2016-10-10 15:19:00 -07:00
parent 9ceccaa70a
commit 4c6a83a160
2 changed files with 21 additions and 0 deletions

View File

@@ -2129,11 +2129,19 @@ func (t *Task) Validate(ephemeralDisk *EphemeralDisk) error {
}
}
destinations := make(map[string]int, len(t.Templates))
for idx, tmpl := range t.Templates {
if err := tmpl.Validate(); err != nil {
outer := fmt.Errorf("Template %d validation failed: %s", idx+1, err)
mErr.Errors = append(mErr.Errors, outer)
}
if other, ok := destinations[tmpl.DestPath]; ok {
outer := fmt.Errorf("Template %d has same destination as %d", idx+1, other)
mErr.Errors = append(mErr.Errors, outer)
} else {
destinations[tmpl.DestPath] = idx + 1
}
}
return mErr.ErrorOrNil()

View File

@@ -526,6 +526,19 @@ func TestTask_Validate_Template(t *testing.T) {
if !strings.Contains(err.Error(), "Template 1 validation failed") {
t.Fatalf("err: %s", err)
}
// Have two templates that share the same destination
good := &Template{
SourcePath: "foo",
DestPath: "local/foo",
ChangeMode: "noop",
}
task.Templates = []*Template{good, good}
err = task.Validate(ephemeralDisk)
if !strings.Contains(err.Error(), "same destination as") {
t.Fatalf("err: %s", err)
}
}
func TestTemplate_Validate(t *testing.T) {