From de25a7b5619e2c590e2b542670e382424cfb95cf Mon Sep 17 00:00:00 2001 From: Jasmine Dahilig Date: Fri, 3 May 2019 06:48:02 -0700 Subject: [PATCH 1/2] add non-empty string validation for datacenters --- nomad/structs/structs.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index 46a79e3e642b..9a1c29a5c833 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -3382,6 +3382,12 @@ func (j *Job) Validate() error { } if len(j.Datacenters) == 0 { mErr.Errors = append(mErr.Errors, errors.New("Missing job datacenters")) + } else { + for _, v := range j.Datacenters { + if v == "" { + mErr.Errors = append(mErr.Errors, errors.New("Job datacenter must be non-empty string")) + } + } } if len(j.TaskGroups) == 0 { mErr.Errors = append(mErr.Errors, errors.New("Missing job task groups")) From b40c69b7c07588cdaca65f20f6af9d83e47cd954 Mon Sep 17 00:00:00 2001 From: Jasmine Dahilig Date: Wed, 8 May 2019 11:51:52 -0700 Subject: [PATCH 2/2] add unit tests for datacenter non-empty string validation --- nomad/structs/structs_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/nomad/structs/structs_test.go b/nomad/structs/structs_test.go index 2b5751dbe9fd..186e1268eeb3 100644 --- a/nomad/structs/structs_test.go +++ b/nomad/structs/structs_test.go @@ -110,6 +110,16 @@ func TestJob_Validate(t *testing.T) { if !strings.Contains(mErr.Errors[2].Error(), "Task group web validation failed") { t.Fatalf("err: %s", err) } + + // test for empty datacenters + j = &Job{ + Datacenters: []string{""}, + } + err = j.Validate() + mErr = err.(*multierror.Error) + if !strings.Contains(mErr.Error(), "datacenter must be non-empty string") { + t.Fatalf("err: %s", err) + } } func TestJob_Warnings(t *testing.T) {