Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add non-empty string validation for datacenters #5665

Merged
merged 2 commits into from
May 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
10 changes: 10 additions & 0 deletions nomad/structs/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use testify require here instead of using Fatalf directly

Suggested change
if !strings.Contains(mErr.Error(), "datacenter must be non-empty string") {
require.Contains(t, mErr.Error(), "datacenter must be non-empty string")

t.Fatalf("err: %s", err)
}
}

func TestJob_Warnings(t *testing.T) {
Expand Down