From 76c10596800311f34495a914ec4d7834ea354c08 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Mon, 11 Oct 2021 10:29:36 -0700 Subject: [PATCH] add tests, and simplify code --- .../testdata/complete/sources.pkr.hcl | 2 +- hcl2template/types.packer_config_test.go | 36 ++++++++++++++++++- hcl2template/types.source.go | 5 +-- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/hcl2template/testdata/complete/sources.pkr.hcl b/hcl2template/testdata/complete/sources.pkr.hcl index 3c7b735d5..7b713568a 100644 --- a/hcl2template/testdata/complete/sources.pkr.hcl +++ b/hcl2template/testdata/complete/sources.pkr.hcl @@ -13,7 +13,7 @@ source "amazon-ebs" "ubuntu-1604" { } source "virtualbox-iso" "ubuntu-1204" { - string = "string" + string = "${source.name}-${source.type}" int = 42 int64 = 43 bool = true diff --git a/hcl2template/types.packer_config_test.go b/hcl2template/types.packer_config_test.go index 891cccb8b..2ce476c0f 100644 --- a/hcl2template/types.packer_config_test.go +++ b/hcl2template/types.packer_config_test.go @@ -3,9 +3,11 @@ package hcl2template import ( "path/filepath" "testing" + "time" "github.com/hashicorp/go-version" packersdk "github.com/hashicorp/packer-plugin-sdk/packer" + "github.com/hashicorp/packer-plugin-sdk/template/config" "github.com/hashicorp/packer/hcl2template/addrs" . "github.com/hashicorp/packer/hcl2template/internal" hcl2template "github.com/hashicorp/packer/hcl2template/internal" @@ -204,7 +206,39 @@ func TestParser_complete(t *testing.T) { &packer.CoreBuild{ Type: "virtualbox-iso.ubuntu-1204", Prepared: true, - Builder: basicMockBuilder, + Builder: &MockBuilder{ + Config: MockConfig{ + NestedMockConfig: NestedMockConfig{ + // interpolates source and type in builder + String: "ubuntu-1204-virtualbox-iso", + Int: 42, + Int64: 43, + Bool: true, + Trilean: config.TriTrue, + Duration: 10 * time.Second, + MapStringString: map[string]string{ + "a": "b", + "c": "d", + }, + SliceString: []string{ + "a", + "b", + "c", + }, + SliceSliceString: [][]string{ + {"a", "b"}, + {"c", "d"}, + }, + Tags: []MockTag{}, + Datasource: "string", + }, + Nested: builderBasicNestedMockConfig, + NestedSlice: []NestedMockConfig{ + builderBasicNestedMockConfig, + builderBasicNestedMockConfig, + }, + }, + }, Provisioners: []packer.CoreBuildProvisioner{ { PType: "shell", diff --git a/hcl2template/types.source.go b/hcl2template/types.source.go index e7ee39dae..6c8d7bd9c 100644 --- a/hcl2template/types.source.go +++ b/hcl2template/types.source.go @@ -111,10 +111,7 @@ func (cfg *PackerConfig) startBuilder(source SourceUseBlock, ectx *hcl.EvalConte body := source.Body // Add known values to source accessor in eval context. - ectx.Variables[sourcesAccessor] = cty.ObjectVal(map[string]cty.Value{ - "type": cty.StringVal(source.Type), - "name": cty.StringVal(source.Name), - }) + ectx.Variables[sourcesAccessor] = cty.ObjectVal(source.ctyValues()) decoded, moreDiags := decodeHCL2Spec(body, ectx, builder) diags = append(diags, moreDiags...)