add tests for info sharing
This commit is contained in:
parent
cd7abf1f9e
commit
8490bbc45c
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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}}`
|
||||
|
||||
|
|
Loading…
Reference in New Issue