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

templater: fail rendering if a variable is not set #12

Merged
merged 1 commit into from
Oct 6, 2017
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
11 changes: 8 additions & 3 deletions levant/templater.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func renderTFTemplte(src, variableFile string, flagVars *map[string]string) (tpl
mergedVars := helper.VariableMerge(&variables, flagVars)

// Setup the template file for rendering
t := template.New("jobTemplate").Delims("[[", "]]")
t := newTemplate()

if t, err = t.Parse(src); err != nil {
return
Expand Down Expand Up @@ -117,7 +117,7 @@ func renderYAMLVarsTemplate(src, variableFile string, flagVars *map[string]strin
mergedVars := helper.VariableMerge(&variables, flagVars)

// Setup the template file for rendering
t := template.New("jobTemplate").Delims("[[", "]]")
t := newTemplate()
if t, err = t.Parse(string(src)); err != nil {
return
}
Expand All @@ -134,7 +134,7 @@ func readJobFile(src string, flagVars *map[string]string) (tpl *bytes.Buffer, er
tpl = &bytes.Buffer{}

// Setup the template file for rendering
t := template.New("jobTemplate").Delims("[[", "]]")
t := newTemplate()
if t, err = t.Parse(src); err != nil {
return
}
Expand All @@ -145,3 +145,8 @@ func readJobFile(src string, flagVars *map[string]string) (tpl *bytes.Buffer, er

return tpl, nil
}

// newTemplate returns an empty template with default options set
func newTemplate() *template.Template {
return template.New("jobTemplate").Delims("[[", "]]").Option("missingkey=error")
}
11 changes: 11 additions & 0 deletions levant/templater_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package levant

import (
"strings"
"testing"

nomad "github.com/hashicorp/nomad/api"
Expand Down Expand Up @@ -69,4 +70,14 @@ func TestTemplater_RenderTemplate(t *testing.T) {
if job.Datacenters[0] != testDCName {
t.Fatalf("expected %s but got %v", testDCName, job.Datacenters[0])
}

// Test var-args only render.
fVars["job_name"] = testJobName
job, err = RenderTemplate("test-fixtures/missing_var.nomad", "", &fVars)
if err == nil {
t.Fatal("expected err to not be nil")
}
if !strings.Contains(err.Error(), "binary_url") {
t.Fatal("expected err to mention missing var (binary_url)")
}
}
56 changes: 56 additions & 0 deletions levant/test-fixtures/missing_var.nomad
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
job "[[.job_name]]" {
datacenters = ["dc1"]
type = "service"
update {
max_parallel = 1
min_healthy_time = "10s"
healthy_deadline = "1m"
auto_revert = true
}

group "cache" {
count = 1
restart {
attempts = 10
interval = "5m"
delay = "25s"
mode = "delay"
}
ephemeral_disk {
size = 300
}
task "redis" {
artifact {
# .binary_url is not set
source = "[[ .binary_url ]]"
}

driver = "docker"
config {
image = "redis:3.2"
port_map {
db = 6379
}
}
resources {
cpu = 500
memory = 256
network {
mbits = 10
port "db" {}
}
}
service {
name = "global-redis-check"
tags = ["global", "cache"]
port = "db"
check {
name = "alive"
type = "tcp"
interval = "10s"
timeout = "2s"
}
}
}
}
}