From 029729225dad9c296c8dc369aa88f2ea778e8bdf Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Mon, 15 Feb 2021 12:21:10 +0100 Subject: [PATCH] tests and fixes --- .../duplicate_required_plugins/packer.pkr.hcl | 10 ++- .../testdata/init/imports/build.pkr.hcl | 19 +++-- .../init/invalid_inexplicit_source.pkr.hcl | 8 +++ .../init/invalid_inexplicit_source_2.pkr.hcl | 8 +++ .../init/invalid_short_source.pkr.hcl | 5 ++ hcl2template/types.packer_config_test.go | 70 +++++++++++++++++-- hcl2template/types.required_plugins.go | 13 +++- hcl2template/types.variables.go | 3 +- 8 files changed, 114 insertions(+), 22 deletions(-) create mode 100644 hcl2template/testdata/init/invalid_inexplicit_source.pkr.hcl create mode 100644 hcl2template/testdata/init/invalid_inexplicit_source_2.pkr.hcl create mode 100644 hcl2template/testdata/init/invalid_short_source.pkr.hcl diff --git a/hcl2template/testdata/init/duplicate_required_plugins/packer.pkr.hcl b/hcl2template/testdata/init/duplicate_required_plugins/packer.pkr.hcl index b60e10ac0..1cf3f291e 100644 --- a/hcl2template/testdata/init/duplicate_required_plugins/packer.pkr.hcl +++ b/hcl2template/testdata/init/duplicate_required_plugins/packer.pkr.hcl @@ -1,7 +1,13 @@ packer { required_plugins { - amazon = ">= v0" - amazon = ">= v4" + amazon = { + source = "github.com/hashicorp/amazon" + version = ">= v0" + } + amazon = { + source = "github.com/hashicorp/amazon" + version = ">= v4" + } } } \ No newline at end of file diff --git a/hcl2template/testdata/init/imports/build.pkr.hcl b/hcl2template/testdata/init/imports/build.pkr.hcl index 453fa045e..7220f3b93 100644 --- a/hcl2template/testdata/init/imports/build.pkr.hcl +++ b/hcl2template/testdata/init/imports/build.pkr.hcl @@ -3,29 +3,26 @@ packer { required_version = ">= v1" required_plugins { - amazon = ">= v0" - + amazon = { + source = "github.com/hashicorp/amazon" + version = ">= v0" + } amazon-v1 = { - source = "amazon" + source = "github.com/hashicorp/amazon" version = ">= v1" } - amazon-v2 = { - source = "amazon" + source = "github.com/hashicorp/amazon" version = ">= v2" } - - amazon-v3 = { - source = "hashicorp/amazon" + source = "github.com/hashicorp/amazon" version = ">= v3" } - amazon-v3-azr = { - source = "azr/amazon" + source = "github.com/azr/amazon" version = ">= v3" } - amazon-v4 = { source = "github.com/hashicorp/amazon" version = ">= v4" diff --git a/hcl2template/testdata/init/invalid_inexplicit_source.pkr.hcl b/hcl2template/testdata/init/invalid_inexplicit_source.pkr.hcl new file mode 100644 index 000000000..3c84ffc87 --- /dev/null +++ b/hcl2template/testdata/init/invalid_inexplicit_source.pkr.hcl @@ -0,0 +1,8 @@ +packer { + required_plugins { + amazon = { + source = "amazon" + version = ">= v0" + } + } +} \ No newline at end of file diff --git a/hcl2template/testdata/init/invalid_inexplicit_source_2.pkr.hcl b/hcl2template/testdata/init/invalid_inexplicit_source_2.pkr.hcl new file mode 100644 index 000000000..3e71b828e --- /dev/null +++ b/hcl2template/testdata/init/invalid_inexplicit_source_2.pkr.hcl @@ -0,0 +1,8 @@ +packer { + required_plugins { + amazon = { + source = "hashicorp/amazon" + version = ">= v0" + } + } +} \ No newline at end of file diff --git a/hcl2template/testdata/init/invalid_short_source.pkr.hcl b/hcl2template/testdata/init/invalid_short_source.pkr.hcl new file mode 100644 index 000000000..13ea61a42 --- /dev/null +++ b/hcl2template/testdata/init/invalid_short_source.pkr.hcl @@ -0,0 +1,5 @@ +packer { + required_plugins { + amazon = ">= v0" + } +} \ No newline at end of file diff --git a/hcl2template/types.packer_config_test.go b/hcl2template/types.packer_config_test.go index 87c7825bf..a9a0488e6 100644 --- a/hcl2template/types.packer_config_test.go +++ b/hcl2template/types.packer_config_test.go @@ -431,7 +431,7 @@ func TestParser_no_init(t *testing.T) { RequiredPlugins: map[string]*RequiredPlugin{ "amazon": { Name: "amazon", - Source: "", + Source: "github.com/hashicorp/amazon", Type: &addrs.Plugin{ Type: "amazon", Namespace: "hashicorp", @@ -443,7 +443,7 @@ func TestParser_no_init(t *testing.T) { }, "amazon-v1": { Name: "amazon-v1", - Source: "amazon", + Source: "github.com/hashicorp/amazon", Type: &addrs.Plugin{ Type: "amazon", Namespace: "hashicorp", @@ -455,7 +455,7 @@ func TestParser_no_init(t *testing.T) { }, "amazon-v2": { Name: "amazon-v2", - Source: "amazon", + Source: "github.com/hashicorp/amazon", Type: &addrs.Plugin{ Type: "amazon", Namespace: "hashicorp", @@ -467,7 +467,7 @@ func TestParser_no_init(t *testing.T) { }, "amazon-v3": { Name: "amazon-v3", - Source: "hashicorp/amazon", + Source: "github.com/hashicorp/amazon", Type: &addrs.Plugin{ Type: "amazon", Namespace: "hashicorp", @@ -479,7 +479,7 @@ func TestParser_no_init(t *testing.T) { }, "amazon-v3-azr": { Name: "amazon-v3-azr", - Source: "azr/amazon", + Source: "github.com/azr/amazon", Type: &addrs.Plugin{ Type: "amazon", Namespace: "azr", @@ -610,6 +610,66 @@ func TestParser_no_init(t *testing.T) { []packersdk.Build{}, false, }, + {"invalid_inexplicit_source.pkr.hcl", + defaultParser, + parseTestArgs{"testdata/init/invalid_inexplicit_source.pkr.hcl", nil, nil}, + &PackerConfig{ + Packer: struct { + VersionConstraints []VersionConstraint + RequiredPlugins []*RequiredPlugins + }{ + VersionConstraints: nil, + RequiredPlugins: []*RequiredPlugins{ + {}, + }, + }, + CorePackerVersionString: lockedVersion, + Basedir: "testdata/init", + }, + true, true, + []packersdk.Build{}, + false, + }, + {"invalid_short_source.pkr.hcl", + defaultParser, + parseTestArgs{"testdata/init/invalid_short_source.pkr.hcl", nil, nil}, + &PackerConfig{ + Packer: struct { + VersionConstraints []VersionConstraint + RequiredPlugins []*RequiredPlugins + }{ + VersionConstraints: nil, + RequiredPlugins: []*RequiredPlugins{ + {}, + }, + }, + CorePackerVersionString: lockedVersion, + Basedir: "testdata/init", + }, + true, true, + []packersdk.Build{}, + false, + }, + {"invalid_short_source_2.pkr.hcl", + defaultParser, + parseTestArgs{"testdata/init/invalid_inexplicit_source_2.pkr.hcl", nil, nil}, + &PackerConfig{ + Packer: struct { + VersionConstraints []VersionConstraint + RequiredPlugins []*RequiredPlugins + }{ + VersionConstraints: nil, + RequiredPlugins: []*RequiredPlugins{ + {}, + }, + }, + CorePackerVersionString: lockedVersion, + Basedir: "testdata/init", + }, + true, true, + []packersdk.Build{}, + false, + }, } testParse_only_Parse(t, tests) } diff --git a/hcl2template/types.required_plugins.go b/hcl2template/types.required_plugins.go index 8fc66064b..3e2ae4b3f 100644 --- a/hcl2template/types.required_plugins.go +++ b/hcl2template/types.required_plugins.go @@ -62,7 +62,11 @@ func (cfg *PackerConfig) decodeImplicitRequiredPluginsBlocks(f *hcl.File) hcl.Di // RequiredPlugin represents a declaration of a dependency on a particular // Plugin version or source. type RequiredPlugin struct { - Name string + Name string + // Source used to be able to tell how the template referenced this source, + // for example, "awesomecloud" instead of github.com/awesome/awesomecloud. + // This one is left here in case we want to go back to allowing inexplicit + // source url definitions. Source string Type *addrs.Plugin Requirement VersionConstraint @@ -77,7 +81,7 @@ type RequiredPlugins struct { func decodeRequiredPluginsBlock(block *hcl.Block) (*RequiredPlugins, hcl.Diagnostics) { attrs, diags := block.Body.JustAttributes() ret := &RequiredPlugins{ - RequiredPlugins: make(map[string]*RequiredPlugin), + RequiredPlugins: nil, DeclRange: block.DefRange, } for name, attr := range attrs { @@ -112,6 +116,7 @@ func decodeRequiredPluginsBlock(block *hcl.Block) (*RequiredPlugins, hcl.Diagnos name, c), Subject: attr.Range.Ptr(), }) + continue case expr.Type().IsObjectType(): if !expr.Type().HasAttribute("version") { @@ -186,6 +191,7 @@ func decodeRequiredPluginsBlock(block *hcl.Block) (*RequiredPlugins, hcl.Diagnos } } diags = append(diags, sourceDiags...) + continue } else { rp.Type = p } @@ -214,6 +220,9 @@ func decodeRequiredPluginsBlock(block *hcl.Block) (*RequiredPlugins, hcl.Diagnos }) } + if ret.RequiredPlugins == nil { + ret.RequiredPlugins = make(map[string]*RequiredPlugin) + } ret.RequiredPlugins[rp.Name] = rp } diff --git a/hcl2template/types.variables.go b/hcl2template/types.variables.go index 217ead998..750869f9b 100644 --- a/hcl2template/types.variables.go +++ b/hcl2template/types.variables.go @@ -82,10 +82,9 @@ func (v *Variable) GoString() string { // validateValue ensures that all of the configured custom validations for a // variable value are passing. -// func (v *Variable) validateValue(val VariableAssignment) (diags hcl.Diagnostics) { if len(v.Validations) == 0 { - log.Printf("[TRACE] validateValue: not active for %s, so skipping", v.Name) + // log.Printf("[TRACE] validateValue: not active for %s, so skipping", v.Name) return nil }