From 4c6a83a1603d67ba397a2ce08da73296da230018 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Mon, 10 Oct 2016 15:19:00 -0700 Subject: [PATCH] Ensure templates have different destinations --- nomad/structs/structs.go | 8 ++++++++ nomad/structs/structs_test.go | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index fff8b3e82..a4975e19f 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -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() diff --git a/nomad/structs/structs_test.go b/nomad/structs/structs_test.go index 3cfecc4a6..62e40de2c 100644 --- a/nomad/structs/structs_test.go +++ b/nomad/structs/structs_test.go @@ -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) {