Skip to content

Commit

Permalink
Merge pull request #9310 from hashicorp/f-warn-against-hcl2
Browse files Browse the repository at this point in the history
Custom message when job file is HCL2 incompatible
  • Loading branch information
Mahmood Ali committed Nov 11, 2020
2 parents e8b8864 + 73df83f commit 86882df
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
7 changes: 7 additions & 0 deletions command/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,14 @@ func (j *JobGetter) ApiJobWithArgs(jpath string, vars []string, varfiles []strin
ArgVars: vars,
AllowFS: true,
})

if err != nil {
if _, merr := jobspec.Parse(&buf); merr == nil {
return nil, fmt.Errorf("Failed to parse using HCL 2. Use the HCL 1 parser with `nomad run -hcl1`, or address the following issues:\n%v", err)
}
}
}

if err != nil {
return nil, fmt.Errorf("Error parsing job file from %s:\n%v", jpath, err)
}
Expand Down
48 changes: 48 additions & 0 deletions command/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,54 @@ func TestJobGetter_LocalFile(t *testing.T) {
}
}

// TestJobGetter_LocalFile_InvalidHCL2 asserts that a custom message is emited
// if the file is a valid HCL1 but not HCL2
func TestJobGetter_LocalFile_InvalidHCL2(t *testing.T) {
t.Parallel()

cases := []struct {
name string
hcl string
expectHCL1Message bool
}{
{
"invalid HCL",
"nothing",
false,
},
{
"invalid HCL2",
`job "example" {
meta = { "a" = "b" }
}`,
true,
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
fh, err := ioutil.TempFile("", "nomad")
require.NoError(t, err)
defer os.Remove(fh.Name())
defer fh.Close()

_, err = fh.WriteString(c.hcl)
require.NoError(t, err)

j := &JobGetter{}
_, err = j.ApiJob(fh.Name())
require.Error(t, err)

exptMessage := "Failed to parse using HCL 2. Use the HCL 1"
if c.expectHCL1Message {
require.Contains(t, err.Error(), exptMessage)
} else {
require.NotContains(t, err.Error(), exptMessage)
}
})
}
}

// Test StructJob with jobfile from HTTP Server
func TestJobGetter_HTTPServer(t *testing.T) {
t.Parallel()
Expand Down

0 comments on commit 86882df

Please sign in to comment.