From 6887b8201751880ca349df7e30ba9d66dcb50560 Mon Sep 17 00:00:00 2001 From: Drew Bailey Date: Wed, 12 May 2021 09:08:59 -0400 Subject: [PATCH] check and return error from parsing var-files (#10569) * check and return error from parsing var-files * changelog entry for 1.1.0 and 1.0.5 --- CHANGELOG.md | 2 ++ jobspec2/parse.go | 4 ++++ jobspec2/parse_test.go | 14 +++++++++++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f77968c4129..91b792ed5ad9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,6 +55,7 @@ BUG FIXES: * cli: Remove extra linefeeds in monitor.log files written by `nomad operator debug`. [[GH-10252](https://github.com/hashicorp/nomad/issues/10252)] * cli: Fixed a bug where parsing HCLv2 may panic on some variable interpolation syntax [[GH-10326](https://github.com/hashicorp/nomad/issues/10326)] [[GH-10419](https://github.com/hashicorp/nomad/issues/10419)] * cli: Fixed a bug where `nomad operator debug` incorrectly parsed https Consul API URLs. [[GH-10082](https://github.com/hashicorp/nomad/pull/10082)] + * cli: Fixed a panic where `nomad job run` or `plan` would crash when supplied with non-existent `-var-file` files. [[GH-10569](https://github.com/hashicorp/nomad/issues/10569)] * client: Fixed log formatting when killing tasks. [[GH-10135](https://github.com/hashicorp/nomad/issues/10135)] * client: Added handling for cgroup-v2 memory metrics [[GH-10286](https://github.com/hashicorp/nomad/issues/10286)] * client: Only publish measured allocation memory metrics [[GH-10376](https://github.com/hashicorp/nomad/issues/10376)] @@ -84,6 +85,7 @@ BUG FIXES: * cli: Remove extra linefeeds in monitor.log files written by `nomad operator debug`. [[GH-10252](https://github.com/hashicorp/nomad/issues/10252)] * cli: Fixed a bug where parsing HCLv2 may panic on some variable interpolation syntax [[GH-10326](https://github.com/hashicorp/nomad/issues/10326)] [[GH-10419](https://github.com/hashicorp/nomad/issues/10419)] * cli: Fixed a bug where `nomad operator debug` incorrectly parsed https Consul API URLs. [[GH-10082](https://github.com/hashicorp/nomad/pull/10082)] + * cli: Fixed a panic where `nomad job run` or `plan` would crash when supplied with non-existent `-var-file` files. [[GH-10569](https://github.com/hashicorp/nomad/issues/10569)] * client: Fixed log formatting when killing tasks. [[GH-10135](https://github.com/hashicorp/nomad/issues/10135)] * client: Added handling for cgroup-v2 memory metrics [[GH-10286](https://github.com/hashicorp/nomad/issues/10286)] * client: Only publish measured allocation memory metrics [[GH-10376](https://github.com/hashicorp/nomad/issues/10376)] diff --git a/jobspec2/parse.go b/jobspec2/parse.go index a05e1943dc7d..1a5599fac998 100644 --- a/jobspec2/parse.go +++ b/jobspec2/parse.go @@ -88,6 +88,10 @@ func decode(c *jobConfig) error { for _, varFile := range config.VarFiles { parsedVarFile, ds := parseFile(varFile) + if parsedVarFile == nil || ds.HasErrors() { + return fmt.Errorf("unable to parse var file: %v", ds.Error()) + } + config.parsedVarFiles = append(config.parsedVarFiles, parsedVarFile) diags = append(diags, ds...) } diff --git a/jobspec2/parse_test.go b/jobspec2/parse_test.go index a815cd0da9e7..13e877325cad 100644 --- a/jobspec2/parse_test.go +++ b/jobspec2/parse_test.go @@ -146,7 +146,7 @@ job "example" { defer os.Remove(varFile.Name()) content := `dc_var = "set_dc" -region_var = "set_region"` + region_var = "set_region"` _, err = varFile.WriteString(content) require.NoError(t, err) @@ -162,6 +162,18 @@ region_var = "set_region"` require.NotNil(t, out.Region) require.Equal(t, "set_region", *out.Region) }) + + t.Run("var-file does not exist", func(t *testing.T) { + + out, err := ParseWithConfig(&ParseConfig{ + Path: "input.hcl", + Body: []byte(hcl), + VarFiles: []string{"does-not-exist.hcl"}, + AllowFS: true, + }) + require.Error(t, err) + require.Nil(t, out) + }) } // TestParse_UnknownVariables asserts that unknown variables are left intact for further processing