|
|
|
|
@ -4,182 +4,71 @@ import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
|
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
|
|
|
"github.com/hashicorp/go-version"
|
|
|
|
|
"github.com/hashicorp/packer/hcl2template/addrs"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestPackerConfig_decodeImplicitRequiredPluginsBlocks(t *testing.T) {
|
|
|
|
|
type fields struct {
|
|
|
|
|
PackerConfig
|
|
|
|
|
}
|
|
|
|
|
type args struct {
|
|
|
|
|
block *hcl.Block
|
|
|
|
|
}
|
|
|
|
|
func TestPackerConfig_required_plugin_parse(t *testing.T) {
|
|
|
|
|
defaultParser := getBasicParser()
|
|
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
fields fields
|
|
|
|
|
args args
|
|
|
|
|
wantDiags bool
|
|
|
|
|
wantConfig PackerConfig
|
|
|
|
|
name string
|
|
|
|
|
cfg PackerConfig
|
|
|
|
|
requirePlugins string
|
|
|
|
|
restOfTemplate string
|
|
|
|
|
wantDiags bool
|
|
|
|
|
wantConfig PackerConfig
|
|
|
|
|
}{
|
|
|
|
|
{"invalid block", fields{PackerConfig: PackerConfig{}}, args{block: &hcl.Block{}}, false, PackerConfig{}},
|
|
|
|
|
{"invalid block name", fields{PackerConfig: PackerConfig{}}, args{block: &hcl.Block{Labels: []string{""}}}, false, PackerConfig{}},
|
|
|
|
|
{"implicitly require amazon plugin through datasource",
|
|
|
|
|
fields{PackerConfig: PackerConfig{}},
|
|
|
|
|
args{block: &hcl.Block{Labels: []string{"amazon-ami"}}},
|
|
|
|
|
false,
|
|
|
|
|
PackerConfig{
|
|
|
|
|
Packer: struct {
|
|
|
|
|
VersionConstraints []VersionConstraint
|
|
|
|
|
RequiredPlugins []*RequiredPlugins
|
|
|
|
|
}{
|
|
|
|
|
RequiredPlugins: []*RequiredPlugins{
|
|
|
|
|
{
|
|
|
|
|
RequiredPlugins: map[string]*RequiredPlugin{
|
|
|
|
|
"amazon": {
|
|
|
|
|
Name: "amazon",
|
|
|
|
|
Source: "github.com/hashicorp/amazon",
|
|
|
|
|
Type: &addrs.Plugin{"github.com", "hashicorp", "amazon"},
|
|
|
|
|
PluginDependencyReason: PluginDependencyImplicit,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}},
|
|
|
|
|
{"don't replace explicitly imported amazon plugin",
|
|
|
|
|
fields{PackerConfig: PackerConfig{
|
|
|
|
|
Packer: struct {
|
|
|
|
|
VersionConstraints []VersionConstraint
|
|
|
|
|
RequiredPlugins []*RequiredPlugins
|
|
|
|
|
}{
|
|
|
|
|
RequiredPlugins: []*RequiredPlugins{
|
|
|
|
|
{
|
|
|
|
|
RequiredPlugins: map[string]*RequiredPlugin{
|
|
|
|
|
"amazon": {
|
|
|
|
|
Name: "amazon",
|
|
|
|
|
Source: "github.com/hashicorp/amazon",
|
|
|
|
|
Type: &addrs.Plugin{"github.com", "hashicorp", "amazon"},
|
|
|
|
|
PluginDependencyReason: PluginDependencyExplicit,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}},
|
|
|
|
|
args{block: &hcl.Block{Labels: []string{"amazon-ami"}}},
|
|
|
|
|
false,
|
|
|
|
|
PackerConfig{
|
|
|
|
|
Packer: struct {
|
|
|
|
|
VersionConstraints []VersionConstraint
|
|
|
|
|
RequiredPlugins []*RequiredPlugins
|
|
|
|
|
}{
|
|
|
|
|
RequiredPlugins: []*RequiredPlugins{
|
|
|
|
|
{
|
|
|
|
|
RequiredPlugins: map[string]*RequiredPlugin{
|
|
|
|
|
"amazon": {
|
|
|
|
|
Name: "amazon",
|
|
|
|
|
Source: "github.com/hashicorp/amazon",
|
|
|
|
|
Type: &addrs.Plugin{"github.com", "hashicorp", "amazon"},
|
|
|
|
|
PluginDependencyReason: PluginDependencyExplicit,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}},
|
|
|
|
|
{"implict import of a plugin without a dash",
|
|
|
|
|
fields{PackerConfig: PackerConfig{}},
|
|
|
|
|
args{block: &hcl.Block{Labels: []string{"google"}}},
|
|
|
|
|
false,
|
|
|
|
|
PackerConfig{
|
|
|
|
|
Packer: struct {
|
|
|
|
|
VersionConstraints []VersionConstraint
|
|
|
|
|
RequiredPlugins []*RequiredPlugins
|
|
|
|
|
}{
|
|
|
|
|
RequiredPlugins: []*RequiredPlugins{
|
|
|
|
|
{
|
|
|
|
|
RequiredPlugins: map[string]*RequiredPlugin{
|
|
|
|
|
"google": {
|
|
|
|
|
Name: "google",
|
|
|
|
|
Source: "github.com/hashicorp/google",
|
|
|
|
|
Type: &addrs.Plugin{"github.com", "hashicorp", "google"},
|
|
|
|
|
PluginDependencyReason: PluginDependencyImplicit,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}},
|
|
|
|
|
{"ignore already imported google plugin",
|
|
|
|
|
fields{PackerConfig: PackerConfig{
|
|
|
|
|
Packer: struct {
|
|
|
|
|
VersionConstraints []VersionConstraint
|
|
|
|
|
RequiredPlugins []*RequiredPlugins
|
|
|
|
|
}{
|
|
|
|
|
RequiredPlugins: []*RequiredPlugins{
|
|
|
|
|
{
|
|
|
|
|
RequiredPlugins: map[string]*RequiredPlugin{
|
|
|
|
|
"google": {
|
|
|
|
|
Name: "google",
|
|
|
|
|
Source: "github.com/hashicorp/google",
|
|
|
|
|
Type: &addrs.Plugin{"github.com", "hashicorp", "google"},
|
|
|
|
|
PluginDependencyReason: PluginDependencyExplicit,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}},
|
|
|
|
|
args{block: &hcl.Block{Labels: []string{"google"}}},
|
|
|
|
|
false,
|
|
|
|
|
PackerConfig{
|
|
|
|
|
Packer: struct {
|
|
|
|
|
VersionConstraints []VersionConstraint
|
|
|
|
|
RequiredPlugins []*RequiredPlugins
|
|
|
|
|
}{
|
|
|
|
|
RequiredPlugins: []*RequiredPlugins{
|
|
|
|
|
{
|
|
|
|
|
RequiredPlugins: map[string]*RequiredPlugin{
|
|
|
|
|
"google": {
|
|
|
|
|
Name: "google",
|
|
|
|
|
Source: "github.com/hashicorp/google",
|
|
|
|
|
Type: &addrs.Plugin{"github.com", "hashicorp", "google"},
|
|
|
|
|
PluginDependencyReason: PluginDependencyExplicit,
|
|
|
|
|
},
|
|
|
|
|
// {"empty source labels", PackerConfig{parser: defaultParser}, ``, `source "" "" {}`, false, PackerConfig{}},
|
|
|
|
|
{"add required_plugin", PackerConfig{parser: defaultParser}, `
|
|
|
|
|
packer {
|
|
|
|
|
required_plugins {
|
|
|
|
|
amazon = {
|
|
|
|
|
source = "github.com/hashicorp/amazon"
|
|
|
|
|
version = "~> v1.2.3"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`, ``, false, PackerConfig{
|
|
|
|
|
Packer: struct {
|
|
|
|
|
VersionConstraints []VersionConstraint
|
|
|
|
|
RequiredPlugins []*RequiredPlugins
|
|
|
|
|
}{
|
|
|
|
|
RequiredPlugins: []*RequiredPlugins{
|
|
|
|
|
{RequiredPlugins: map[string]*RequiredPlugin{
|
|
|
|
|
"amazon": {
|
|
|
|
|
Name: "amazon",
|
|
|
|
|
Source: "github.com/hashicorp/amazon",
|
|
|
|
|
Type: &addrs.Plugin{"github.com", "hashicorp", "amazon"},
|
|
|
|
|
Requirement: VersionConstraint{
|
|
|
|
|
Required: mustVersionConstraints(version.NewConstraint("~> v1.2.3")),
|
|
|
|
|
},
|
|
|
|
|
PluginDependencyReason: PluginDependencyExplicit,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}},
|
|
|
|
|
},
|
|
|
|
|
}},
|
|
|
|
|
},
|
|
|
|
|
}},
|
|
|
|
|
}
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
cfg := PackerConfig{
|
|
|
|
|
Packer: tt.fields.Packer,
|
|
|
|
|
Basedir: tt.fields.Basedir,
|
|
|
|
|
CorePackerVersionString: tt.fields.CorePackerVersionString,
|
|
|
|
|
Cwd: tt.fields.Cwd,
|
|
|
|
|
Sources: tt.fields.Sources,
|
|
|
|
|
InputVariables: tt.fields.InputVariables,
|
|
|
|
|
LocalVariables: tt.fields.LocalVariables,
|
|
|
|
|
Datasources: tt.fields.Datasources,
|
|
|
|
|
LocalBlocks: tt.fields.LocalBlocks,
|
|
|
|
|
ValidationOptions: tt.fields.ValidationOptions,
|
|
|
|
|
Builds: tt.fields.Builds,
|
|
|
|
|
parser: tt.fields.parser,
|
|
|
|
|
files: tt.fields.files,
|
|
|
|
|
except: tt.fields.except,
|
|
|
|
|
only: tt.fields.only,
|
|
|
|
|
force: tt.fields.force,
|
|
|
|
|
debug: tt.fields.debug,
|
|
|
|
|
onError: tt.fields.onError,
|
|
|
|
|
cfg := tt.cfg
|
|
|
|
|
file, diags := cfg.parser.ParseHCL([]byte(tt.requirePlugins), "required_plugins.pkr.hcl")
|
|
|
|
|
if len(diags) > 0 {
|
|
|
|
|
t.Fatal(diags)
|
|
|
|
|
}
|
|
|
|
|
if diags := cfg.decodeRequiredPluginsBlock(file); len(diags) > 0 {
|
|
|
|
|
t.Fatal(diags)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rest, diags := cfg.parser.ParseHCL([]byte(tt.restOfTemplate), "rest.pkr.hcl")
|
|
|
|
|
if len(diags) > 0 {
|
|
|
|
|
t.Fatal(diags)
|
|
|
|
|
}
|
|
|
|
|
if gotDiags := cfg.inferImplicitRequiredPluginFromBlocks(tt.args.block); (len(gotDiags) > 0) != tt.wantDiags {
|
|
|
|
|
t.Errorf("PackerConfig.inferImplicitRequiredPluginFromBlocks() = %v", gotDiags)
|
|
|
|
|
if gotDiags := cfg.decodeImplicitRequiredPluginsBlocks(rest); (len(gotDiags) > 0) != tt.wantDiags {
|
|
|
|
|
t.Fatal(gotDiags)
|
|
|
|
|
}
|
|
|
|
|
if diff := cmp.Diff(tt.wantConfig, cfg, cmpopts.IgnoreUnexported(PackerConfig{})); diff != "" {
|
|
|
|
|
if diff := cmp.Diff(tt.wantConfig, cfg, cmpOpts...); diff != "" {
|
|
|
|
|
t.Errorf("PackerConfig.inferImplicitRequiredPluginFromBlocks() unexpected PackerConfig: %v", diff)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|