fix first pass rendering crash on syntax error (#374)

This commit is contained in:
David Genest 2018-09-27 21:27:11 -04:00 committed by KUOKA Yusuke
parent 429c42d429
commit 942f9a20b0
3 changed files with 36 additions and 8 deletions

View File

@ -402,7 +402,7 @@ environments:
releases:
{{ if (eq .Environment.Name "production" }}
{{ if eq .Environment.Name "production" }}
- name: newrelic-agent
# snip
{{ end }}

14
main.go
View File

@ -3,16 +3,15 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"os/signal"
"path/filepath"
"sort"
"syscall"
"io/ioutil"
"strings"
"syscall"
"github.com/roboll/helmfile/args"
"github.com/roboll/helmfile/environment"
@ -22,7 +21,6 @@ import (
"github.com/urfave/cli"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os/exec"
)
const (
@ -726,8 +724,12 @@ func (r *twoPassRenderer) renderEnvironment(content []byte) environment.Environm
// parse as much as we can, tolerate errors, this is a preparse
yamlBuf, err := firstPassRenderer.RenderTemplateContentToBuffer(content)
if err != nil && logger != nil {
if err != nil && r.logger != nil {
r.logger.Debugf("first-pass rendering input of \"%s\":\n%s", r.filename, prependLineNumbers(string(content)))
if yamlBuf == nil { // we have a template syntax error, let the second parse report
r.logger.Debugf("template syntax error: %v", err)
return firstPassEnv
}
}
c := state.NewCreator(r.logger, r.reader, r.abs)
c.Strict = false

View File

@ -2,10 +2,12 @@ package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/roboll/helmfile/helmexec"
"github.com/roboll/helmfile/state"
"gopkg.in/yaml.v2"
)
@ -16,7 +18,7 @@ func makeRenderer(readFile func(string) ([]byte, error), env string) *twoPassRen
env: env,
namespace: "namespace",
filename: "",
logger: logger,
logger: helmexec.NewLogger(os.Stdout, "debug"),
abs: filepath.Abs,
}
}
@ -228,3 +230,27 @@ func TestReadFromYaml_RenderTemplateWithNamespace(t *testing.T) {
t.Errorf("release name should be namespace-myrelease")
}
}
func TestReadFromYaml_HelfileShouldBeResilentToTemplateErrors(t *testing.T) {
yamlContent := []byte(`environments:
staging:
production:
releases:
{{ if (eq .Environment.Name "production" }} # notice syntax error: unclosed left paren
- name: prod-myrelease
{{ else }}
- name: myapp
{{ end }}
chart: mychart
`)
fileReader := func(filename string) ([]byte, error) {
return yamlContent, nil
}
r := makeRenderer(fileReader, "staging")
_, err := r.renderTemplate(yamlContent)
if err == nil {
t.Fatalf("wanted error, none returned")
}
}