From 8490bbc45cea542076159b845dfe733fde43307c Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Tue, 17 Dec 2019 13:41:48 -0800 Subject: [PATCH] add tests for info sharing --- packer/build.go | 2 +- packer/build_test.go | 30 +++++++++++ packer/builder_mock.go | 4 +- template/interpolate/funcs_test.go | 85 ++++++++++++++++++++++++++++++ 4 files changed, 119 insertions(+), 2 deletions(-) diff --git a/packer/build.go b/packer/build.go index ec10fec3c..9403adcd8 100644 --- a/packer/build.go +++ b/packer/build.go @@ -163,7 +163,7 @@ func (b *CoreBuild) Prepare() (warn []string, err error) { generatedPlaceholderMap := BasicPlaceholderData() if generatedVars != nil { for _, k := range generatedVars { - generatedPlaceholderMap[k] = fmt.Sprintf("Generated_%s. "+ + generatedPlaceholderMap[k] = fmt.Sprintf("Build_%s. "+ common.PlaceholderMsg, k) } } diff --git a/packer/build_test.go b/packer/build_test.go index ad286e447..3f657f63b 100644 --- a/packer/build_test.go +++ b/packer/build_test.go @@ -4,6 +4,8 @@ import ( "context" "reflect" "testing" + + "github.com/hashicorp/packer/helper/common" ) func boolPointer(tf bool) *bool { @@ -176,6 +178,34 @@ func TestBuildPrepare_variables_default(t *testing.T) { } } +func TestBuildPrepare_ProvisionerGetsGeneratedMap(t *testing.T) { + packerConfig := testDefaultPackerConfig() + + build := testBuild() + builder := build.Builder.(*MockBuilder) + builder.GeneratedVars = []string{"PartyVar"} + + build.Prepare() + if !builder.PrepareCalled { + t.Fatalf("should be called") + } + if !reflect.DeepEqual(builder.PrepareConfig, []interface{}{42, packerConfig}) { + t.Fatalf("bad: %#v", builder.PrepareConfig) + } + + coreProv := build.Provisioners[0] + prov := coreProv.Provisioner.(*MockProvisioner) + if !prov.PrepCalled { + t.Fatal("prepare should be called") + } + + generated := BasicPlaceholderData() + generated["PartyVar"] = "Build_PartyVar. " + common.PlaceholderMsg + if !reflect.DeepEqual(prov.PrepConfigs, []interface{}{42, packerConfig, generated}) { + t.Fatalf("bad: %#v", prov.PrepConfigs) + } +} + func TestBuild_Run(t *testing.T) { ui := testUi() diff --git a/packer/builder_mock.go b/packer/builder_mock.go index f995c1b8c..06aa87291 100644 --- a/packer/builder_mock.go +++ b/packer/builder_mock.go @@ -25,6 +25,8 @@ type MockBuilder struct { RunUi Ui CancelCalled bool RunFn func(ctx context.Context) + + GeneratedVars []string } func (tb *MockBuilder) ConfigSpec() hcldec.ObjectSpec { return tb.FlatMapstructure().HCL2Spec() } @@ -34,7 +36,7 @@ func (tb *MockBuilder) FlatConfig() interface{} { return tb.FlatMapstructure() } func (tb *MockBuilder) Prepare(config ...interface{}) ([]string, []string, error) { tb.PrepareCalled = true tb.PrepareConfig = config - return nil, tb.PrepareWarnings, nil + return tb.GeneratedVars, tb.PrepareWarnings, nil } func (tb *MockBuilder) Run(ctx context.Context, ui Ui, h Hook) (Artifact, error) { diff --git a/template/interpolate/funcs_test.go b/template/interpolate/funcs_test.go index f02e5678e..b9a586641 100644 --- a/template/interpolate/funcs_test.go +++ b/template/interpolate/funcs_test.go @@ -9,6 +9,7 @@ import ( "time" "github.com/google/go-cmp/cmp" + "github.com/hashicorp/packer/helper/common" "github.com/hashicorp/packer/version" ) @@ -318,6 +319,90 @@ func TestFuncUser(t *testing.T) { } } +func TestFuncPackerBuild(t *testing.T) { + type cases struct { + DataMap interface{} + ErrExpected bool + Template string + OutVal string + } + + testCases := []cases{ + // Data map is empty; there should be an error. + { + DataMap: nil, + ErrExpected: true, + Template: "{{ build `PartyVar` }}", + OutVal: "", + }, + // Data map is a map[string]string and contains value + { + DataMap: map[string]string{"PartyVar": "PartyVal"}, + ErrExpected: false, + Template: "{{ build `PartyVar` }}", + OutVal: "PartyVal", + }, + // Data map is a map[string]string and contains value + { + DataMap: map[string]string{"PartyVar": "PartyVal"}, + ErrExpected: false, + Template: "{{ build `PartyVar` }}", + OutVal: "PartyVal", + }, + // Data map is a map[string]string and contains value with placeholder. + { + DataMap: map[string]string{"PartyVar": "PartyVal" + common.PlaceholderMsg}, + ErrExpected: false, + Template: "{{ build `PartyVar` }}", + OutVal: "{{.PartyVar}}", + }, + // Data map is a map[interface{}]interface{} and contains value + { + DataMap: map[interface{}]interface{}{"PartyVar": "PartyVal"}, + ErrExpected: false, + Template: "{{ build `PartyVar` }}", + OutVal: "PartyVal", + }, + // Data map is a map[interface{}]interface{} and contains value + { + DataMap: map[interface{}]interface{}{"PartyVar": "PartyVal"}, + ErrExpected: false, + Template: "{{ build `PartyVar` }}", + OutVal: "PartyVal", + }, + // Data map is a map[interface{}]interface{} and contains value with placeholder. + { + DataMap: map[interface{}]interface{}{"PartyVar": "PartyVal" + common.PlaceholderMsg}, + ErrExpected: false, + Template: "{{ build `PartyVar` }}", + OutVal: "{{.PartyVar}}", + }, + // Data map is a map[interface{}]interface{} and doesn't have value. + { + DataMap: map[interface{}]interface{}{"BadVar": "PartyVal" + common.PlaceholderMsg}, + ErrExpected: true, + Template: "{{ build `MissingVar` }}", + OutVal: "", + }, + } + + for _, tc := range testCases { + ctx := &Context{} + ctx.Data = tc.DataMap + i := &I{Value: tc.Template} + + result, err := i.Render(ctx) + if (err != nil) != tc.ErrExpected { + t.Fatalf("Input: %s\n\nerr: %s", tc.Template, err) + } + + if ok := strings.Compare(result, tc.OutVal); ok != 0 { + t.Fatalf("Expected input to include: %s\n\nGot: %s", + tc.OutVal, result) + } + } +} + func TestFuncPackerVersion(t *testing.T) { template := `{{packer_version}}`