mirror of
https://github.com/kemko/nomad.git
synced 2026-01-06 10:25:42 +03:00
cli: set -hcl2-strict to false if -hcl1 is defined (#14426)
These options are mutually exclusive but, since `-hcl2-strict` defaults to `true` users had to explicitily set it to `false` when using `-hcl1`. Also return `255` when job plan fails validation as this is the expected code in this situation.
This commit is contained in:
@@ -87,12 +87,12 @@ Plan Options:
|
||||
used as the job.
|
||||
|
||||
-hcl1
|
||||
Parses the job file as HCLv1.
|
||||
Parses the job file as HCLv1. Takes precedence over "-hcl2-strict".
|
||||
|
||||
-hcl2-strict
|
||||
Whether an error should be produced from the HCL2 parser where a variable
|
||||
has been supplied which is not defined within the root variables. Defaults
|
||||
to true.
|
||||
to true, but ignored if "-hcl1" is also defined.
|
||||
|
||||
-policy-override
|
||||
Sets the flag to force override any soft mandatory Sentinel policies.
|
||||
@@ -183,9 +183,13 @@ func (c *JobPlanCommand) Run(args []string) int {
|
||||
return 255
|
||||
}
|
||||
|
||||
if c.JobGetter.HCL1 {
|
||||
c.JobGetter.Strict = false
|
||||
}
|
||||
|
||||
if err := c.JobGetter.Validate(); err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Invalid job options: %s", err))
|
||||
return 1
|
||||
return 255
|
||||
}
|
||||
|
||||
path := args[0]
|
||||
|
||||
@@ -114,6 +114,25 @@ job "job1" {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanCommand_hcl1_hcl2_strict(t *testing.T) {
|
||||
ci.Parallel(t)
|
||||
|
||||
_, _, addr := testServer(t, false, nil)
|
||||
|
||||
t.Run("-hcl1 implies -hcl2-strict is false", func(t *testing.T) {
|
||||
ui := cli.NewMockUi()
|
||||
cmd := &JobPlanCommand{Meta: Meta{Ui: ui}}
|
||||
got := cmd.Run([]string{
|
||||
"-hcl1", "-hcl2-strict",
|
||||
"-address", addr,
|
||||
"assets/example-short.nomad",
|
||||
})
|
||||
// Exit code 1 here means that an alloc will be created, which is
|
||||
// expected.
|
||||
require.Equal(t, 1, got)
|
||||
})
|
||||
}
|
||||
|
||||
func TestPlanCommand_From_STDIN(t *testing.T) {
|
||||
ci.Parallel(t)
|
||||
stdinR, stdinW, err := os.Pipe()
|
||||
|
||||
@@ -95,12 +95,12 @@ Run Options:
|
||||
used as the job.
|
||||
|
||||
-hcl1
|
||||
Parses the job file as HCLv1.
|
||||
Parses the job file as HCLv1. Takes precedence over "-hcl2-strict".
|
||||
|
||||
-hcl2-strict
|
||||
Whether an error should be produced from the HCL2 parser where a variable
|
||||
has been supplied which is not defined within the root variables. Defaults
|
||||
to true.
|
||||
to true, but ignored if "-hcl1" is also defined.
|
||||
|
||||
-output
|
||||
Output the JSON that would be submitted to the HTTP API without submitting
|
||||
@@ -223,6 +223,10 @@ func (c *JobRunCommand) Run(args []string) int {
|
||||
return 1
|
||||
}
|
||||
|
||||
if c.JobGetter.HCL1 {
|
||||
c.JobGetter.Strict = false
|
||||
}
|
||||
|
||||
if err := c.JobGetter.Validate(); err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Invalid job options: %s", err))
|
||||
return 1
|
||||
|
||||
@@ -54,6 +54,24 @@ job "job1" {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunCommand_hcl1_hcl2_strict(t *testing.T) {
|
||||
ci.Parallel(t)
|
||||
|
||||
_, _, addr := testServer(t, false, nil)
|
||||
|
||||
t.Run("-hcl1 implies -hcl2-strict is false", func(t *testing.T) {
|
||||
ui := cli.NewMockUi()
|
||||
cmd := &JobRunCommand{Meta: Meta{Ui: ui}}
|
||||
got := cmd.Run([]string{
|
||||
"-hcl1", "-hcl2-strict",
|
||||
"-address", addr,
|
||||
"-detach",
|
||||
"assets/example-short.nomad",
|
||||
})
|
||||
require.Equal(t, 0, got, ui.ErrorWriter.String())
|
||||
})
|
||||
}
|
||||
|
||||
func TestRunCommand_Fails(t *testing.T) {
|
||||
ci.Parallel(t)
|
||||
|
||||
|
||||
@@ -48,12 +48,12 @@ Validate Options:
|
||||
used as the job.
|
||||
|
||||
-hcl1
|
||||
Parses the job file as HCLv1.
|
||||
Parses the job file as HCLv1. Takes precedence over "-hcl2-strict".
|
||||
|
||||
-hcl2-strict
|
||||
Whether an error should be produced from the HCL2 parser where a variable
|
||||
has been supplied which is not defined within the root variables. Defaults
|
||||
to true.
|
||||
to true, but ignored if "-hcl1" is also defined.
|
||||
|
||||
-vault-token
|
||||
Used to validate if the user submitting the job has permission to run the job
|
||||
@@ -130,6 +130,10 @@ func (c *JobValidateCommand) Run(args []string) int {
|
||||
return 1
|
||||
}
|
||||
|
||||
if c.JobGetter.HCL1 {
|
||||
c.JobGetter.Strict = false
|
||||
}
|
||||
|
||||
if err := c.JobGetter.Validate(); err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Invalid job options: %s", err))
|
||||
return 1
|
||||
|
||||
@@ -68,6 +68,22 @@ func TestValidateCommand_Files(t *testing.T) {
|
||||
require.Equal(t, 1, code)
|
||||
})
|
||||
}
|
||||
func TestValidateCommand_hcl1_hcl2_strict(t *testing.T) {
|
||||
ci.Parallel(t)
|
||||
|
||||
_, _, addr := testServer(t, false, nil)
|
||||
|
||||
t.Run("-hcl1 implies -hcl2-strict is false", func(t *testing.T) {
|
||||
ui := cli.NewMockUi()
|
||||
cmd := &JobValidateCommand{Meta: Meta{Ui: ui}}
|
||||
got := cmd.Run([]string{
|
||||
"-hcl1", "-hcl2-strict",
|
||||
"-address", addr,
|
||||
"assets/example-short.nomad",
|
||||
})
|
||||
require.Equal(t, 0, got, ui.ErrorWriter.String())
|
||||
})
|
||||
}
|
||||
|
||||
func TestValidateCommand_Fails(t *testing.T) {
|
||||
ci.Parallel(t)
|
||||
|
||||
Reference in New Issue
Block a user