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 -strict to error if there is a key missing #308

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion command/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func (c *DeployCommand) Run(args []string) int {

var err error
var level, format string
var strictMode bool

config := &levant.DeployConfig{
Client: &structs.ClientConfig{},
Expand All @@ -125,6 +126,7 @@ func (c *DeployCommand) Run(args []string) int {
flags.StringVar(&format, "log-format", "HUMAN", "")
flags.StringVar(&config.Deploy.VaultToken, "vault-token", "", "")
flags.BoolVar(&config.Deploy.EnvVault, "vault", false, "")
flags.BoolVar(&strictMode, "strict", false, "")

flags.Var((*helper.FlagStringSlice)(&config.Template.VariableFiles), "var-file", "")

Expand Down Expand Up @@ -159,7 +161,7 @@ func (c *DeployCommand) Run(args []string) int {
}

config.Template.Job, err = template.RenderJob(config.Template.TemplateFile,
config.Template.VariableFiles, config.Client.ConsulAddr, &c.Meta.flagVars)
config.Template.VariableFiles, config.Client.ConsulAddr, strictMode, &c.Meta.flagVars)
if err != nil {
c.UI.Error(fmt.Sprintf("[ERROR] levant/command: %v", err))
return 1
Expand Down
4 changes: 2 additions & 2 deletions command/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestDeploy_checkCanaryAutoPromote(t *testing.T) {
}

for i, c := range cases {
job, err := template.RenderJob(c.File, []string{}, "", &fVars)
job, err := template.RenderJob(c.File, []string{}, "", false, &fVars)
if err != nil {
t.Fatalf("case %d failed: %v", i, err)
}
Expand Down Expand Up @@ -61,7 +61,7 @@ func TestDeploy_checkForceBatch(t *testing.T) {
}

for i, c := range cases {
job, err := template.RenderJob(c.File, []string{}, "", &fVars)
job, err := template.RenderJob(c.File, []string{}, "", false, &fVars)
if err != nil {
t.Fatalf("case %d failed: %v", i, err)
}
Expand Down
4 changes: 3 additions & 1 deletion command/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (c *PlanCommand) Run(args []string) int {
Template: &structs.TemplateConfig{},
}

var strictMode bool
flags := c.Meta.FlagSet("plan", FlagSetVars)
flags.Usage = func() { c.UI.Output(c.Help()) }

Expand All @@ -98,6 +99,7 @@ func (c *PlanCommand) Run(args []string) int {
flags.StringVar(&level, "log-level", "INFO", "")
flags.StringVar(&format, "log-format", "HUMAN", "")
flags.Var((*helper.FlagStringSlice)(&config.Template.VariableFiles), "var-file", "")
flags.BoolVar(&strictMode, "strict", false, "")

if err = flags.Parse(args); err != nil {
return 1
Expand All @@ -124,7 +126,7 @@ func (c *PlanCommand) Run(args []string) int {
}

config.Template.Job, err = template.RenderJob(config.Template.TemplateFile,
config.Template.VariableFiles, config.Client.ConsulAddr, &c.Meta.flagVars)
config.Template.VariableFiles, config.Client.ConsulAddr, strictMode, &c.Meta.flagVars)

if err != nil {
c.UI.Error(fmt.Sprintf("[ERROR] levant/command: %v", err))
Expand Down
4 changes: 3 additions & 1 deletion command/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (c *RenderCommand) Run(args []string) int {

var addr, outPath, templateFile string
var variables []string
var strictMode bool
var err error
var tpl *bytes.Buffer
var level, format string
Expand All @@ -81,6 +82,7 @@ func (c *RenderCommand) Run(args []string) int {
flags.StringVar(&format, "log-format", "HUMAN", "")
flags.Var((*helper.FlagStringSlice)(&variables), "var-file", "")
flags.StringVar(&outPath, "out", "", "")
flags.BoolVar(&strictMode, "strict", false, "")

if err = flags.Parse(args); err != nil {
return 1
Expand All @@ -106,7 +108,7 @@ func (c *RenderCommand) Run(args []string) int {
return 1
}

tpl, err = template.RenderTemplate(templateFile, variables, addr, &c.Meta.flagVars)
tpl, err = template.RenderTemplate(templateFile, variables, addr, strictMode, &c.Meta.flagVars)
if err != nil {
c.UI.Error(fmt.Sprintf("[ERROR] levant/command: %v", err))
return 1
Expand Down
7 changes: 4 additions & 3 deletions template/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (

// RenderJob takes in a template and variables performing a render of the
// template followed by Nomad jobspec parse.
func RenderJob(templateFile string, variableFiles []string, addr string, flagVars *map[string]interface{}) (job *nomad.Job, err error) {
func RenderJob(templateFile string, variableFiles []string, addr string, strictMode bool, flagVars *map[string]interface{}) (job *nomad.Job, err error) {
var tpl *bytes.Buffer
tpl, err = RenderTemplate(templateFile, variableFiles, addr, flagVars)
tpl, err = RenderTemplate(templateFile, variableFiles, addr, strictMode, flagVars)
if err != nil {
return
}
Expand All @@ -31,12 +31,13 @@ func RenderJob(templateFile string, variableFiles []string, addr string, flagVar

// RenderTemplate is the main entry point to render the template based on the
// passed variables file.
func RenderTemplate(templateFile string, variableFiles []string, addr string, flagVars *map[string]interface{}) (tpl *bytes.Buffer, err error) {
func RenderTemplate(templateFile string, variableFiles []string, addr string, strictMode bool, flagVars *map[string]interface{}) (tpl *bytes.Buffer, err error) {

t := &tmpl{}
t.flagVariables = flagVars
t.jobTemplateFile = templateFile
t.variableFiles = variableFiles
t.errMissingKey = strictMode

c, err := client.NewConsulClient(addr)
if err != nil {
Expand Down
57 changes: 49 additions & 8 deletions template/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestTemplater_RenderTemplate(t *testing.T) {
fVars := make(map[string]interface{})

// Test basic TF template render.
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{"test-fixtures/test.tf"}, "", &fVars)
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{"test-fixtures/test.tf"}, "", false, &fVars)
if err != nil {
t.Fatal(err)
}
Expand All @@ -37,7 +37,7 @@ func TestTemplater_RenderTemplate(t *testing.T) {
}

// Test basic YAML template render.
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{"test-fixtures/test.yaml"}, "", &fVars)
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{"test-fixtures/test.yaml"}, "", false, &fVars)
if err != nil {
t.Fatal(err)
}
Expand All @@ -49,7 +49,7 @@ func TestTemplater_RenderTemplate(t *testing.T) {
}

// Test multiple var-files
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{"test-fixtures/test.yaml", "test-fixtures/test-overwrite.yaml"}, "", &fVars)
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{"test-fixtures/test.yaml", "test-fixtures/test-overwrite.yaml"}, "", false, &fVars)
if err != nil {
t.Fatal(err)
}
Expand All @@ -58,7 +58,7 @@ func TestTemplater_RenderTemplate(t *testing.T) {
}

// Test multiple var-files of different types
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{"test-fixtures/test.tf", "test-fixtures/test-overwrite.yaml"}, "", &fVars)
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{"test-fixtures/test.tf", "test-fixtures/test-overwrite.yaml"}, "", false, &fVars)
if err != nil {
t.Fatal(err)
}
Expand All @@ -68,7 +68,7 @@ func TestTemplater_RenderTemplate(t *testing.T) {

// Test multiple var-files with var-args
fVars["job_name"] = testJobNameOverwrite2
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{"test-fixtures/test.tf", "test-fixtures/test-overwrite.yaml"}, "", &fVars)
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{"test-fixtures/test.tf", "test-fixtures/test-overwrite.yaml"}, "", false, &fVars)
if err != nil {
t.Fatal(err)
}
Expand All @@ -77,7 +77,7 @@ func TestTemplater_RenderTemplate(t *testing.T) {
}

// Test empty var-args and empty variable file render.
job, err = RenderJob("test-fixtures/none_templated.nomad", []string{}, "", &fVars)
job, err = RenderJob("test-fixtures/none_templated.nomad", []string{}, "", false, &fVars)
if err != nil {
t.Fatal(err)
}
Expand All @@ -87,7 +87,7 @@ func TestTemplater_RenderTemplate(t *testing.T) {

// Test var-args only render.
fVars = map[string]interface{}{"job_name": testJobName, "task_resource_cpu": "1313"}
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{}, "", &fVars)
job, err = RenderJob("test-fixtures/single_templated.nomad", []string{}, "", false, &fVars)
if err != nil {
t.Fatal(err)
}
Expand All @@ -102,7 +102,7 @@ func TestTemplater_RenderTemplate(t *testing.T) {
delete(fVars, "job_name")
fVars["datacentre"] = testDCName
os.Setenv(testEnvName, testEnvValue)
job, err = RenderJob("test-fixtures/multi_templated.nomad", []string{"test-fixtures/test.yaml"}, "", &fVars)
job, err = RenderJob("test-fixtures/multi_templated.nomad", []string{"test-fixtures/test.yaml"}, "", false, &fVars)
if err != nil {
t.Fatal(err)
}
Expand All @@ -116,3 +116,44 @@ func TestTemplater_RenderTemplate(t *testing.T) {
t.Fatalf("expected %s but got %v", testEnvValue, *job.TaskGroups[0].Name)
}
}

func TestTemplater_RenderTemplate_strict(t *testing.T) {
testCase := []struct {
description string
flagVars map[string]interface{}
errExpected bool
strictMode bool
}{
{
description: "non-strict mode with job_name missing",
flagVars: map[string]interface{}{"task_resource_cpu": "1313"},
strictMode: false,
errExpected: false,
},
{
description: "strict mode with missing vars",
flagVars: map[string]interface{}{},
strictMode: true,
errExpected: true,
},
{
description: "strict mode with all vars",
flagVars: map[string]interface{}{"job_name": testJobName, "task_resource_cpu": "1313"},
strictMode: true,
errExpected: false,
},
}

for _, tc := range testCase {
t.Run(tc.description, func(t *testing.T) {
_, err := RenderJob("test-fixtures/single_templated.nomad", []string{}, "", tc.strictMode, &tc.flagVars)

if tc.errExpected && err == nil {
t.Fatalf("expected error but got nil")
}
if !tc.errExpected && err != nil {
t.Fatalf("expected no error but got %v", err)
}
})
}
}
9 changes: 8 additions & 1 deletion template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type tmpl struct {
flagVariables *map[string]interface{}
jobTemplateFile string
variableFiles []string
errMissingKey bool
}

const (
Expand All @@ -28,7 +29,13 @@ const (
func (t *tmpl) newTemplate() *template.Template {
tmpl := template.New("jobTemplate")
tmpl.Delims(leftDelim, rightDelim)
tmpl.Option("missingkey=zero")

if t.errMissingKey {
tmpl.Option("missingkey=error")
} else {
tmpl.Option("missingkey=zero")
}

tmpl.Funcs(funcMap(t.consulClient))
return tmpl
}
2 changes: 1 addition & 1 deletion test/acctest/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (c DeployTestStepRunner) Run(s *TestState) error {
}
c.Vars["job_name"] = s.JobName

job, err := template.RenderJob("fixtures/"+c.FixtureName, []string{}, "", &c.Vars)
job, err := template.RenderJob("fixtures/"+c.FixtureName, []string{}, "", false, &c.Vars)
if err != nil {
return fmt.Errorf("error rendering template: %s", err)
}
Expand Down