package template import ( "os" "reflect" "testing" "time" ) func TestParse(t *testing.T) { cases := []struct { File string Result *Template Err bool }{ /* * Builders */ { "parse-basic.json", &Template{ Builders: map[string]*Builder{ "something": &Builder{ Name: "something", Type: "something", }, }, }, false, }, { "parse-builder-no-type.json", nil, true, }, { "parse-builder-repeat.json", nil, true, }, /* * Provisioners */ { "parse-provisioner-basic.json", &Template{ Provisioners: []*Provisioner{ &Provisioner{ Type: "something", }, }, }, false, }, { "parse-provisioner-pause-before.json", &Template{ Provisioners: []*Provisioner{ &Provisioner{ Type: "something", PauseBefore: 1 * time.Second, }, }, }, false, }, { "parse-provisioner-only.json", &Template{ Provisioners: []*Provisioner{ &Provisioner{ Type: "something", OnlyExcept: OnlyExcept{ Only: []string{"foo"}, }, }, }, }, false, }, { "parse-provisioner-except.json", &Template{ Provisioners: []*Provisioner{ &Provisioner{ Type: "something", OnlyExcept: OnlyExcept{ Except: []string{"foo"}, }, }, }, }, false, }, { "parse-provisioner-override.json", &Template{ Provisioners: []*Provisioner{ &Provisioner{ Type: "something", Override: map[string]interface{}{ "foo": map[string]interface{}{}, }, }, }, }, false, }, { "parse-provisioner-no-type.json", nil, true, }, { "parse-variable-default.json", &Template{ Variables: map[string]*Variable{ "foo": &Variable{ Default: "foo", }, }, }, false, }, { "parse-variable-required.json", &Template{ Variables: map[string]*Variable{ "foo": &Variable{ Required: true, }, }, }, false, }, } for _, tc := range cases { f, err := os.Open(fixtureDir(tc.File)) if err != nil { t.Fatalf("err: %s", err) } tpl, err := Parse(f) f.Close() if (err != nil) != tc.Err { t.Fatalf("err: %s", err) } if !reflect.DeepEqual(tpl, tc.Result) { t.Fatalf("bad: %s\n\n%#v\n\n%#v", tc.File, tpl, tc.Result) } } }