From 3052e3c5d5678376864aea609c935654484a911a Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 11 Feb 2021 14:44:12 +0100 Subject: [PATCH 1/8] tests --- command/init_test.go | 53 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/command/init_test.go b/command/init_test.go index a77002401..e691d66fc 100644 --- a/command/init_test.go +++ b/command/init_test.go @@ -14,7 +14,7 @@ import ( ) type testCaseInit struct { - checkSkip func(*testing.T) + init []func(*testing.T, testCaseInit) name string Meta Meta inPluginFolder map[string]string @@ -94,10 +94,8 @@ func TestInitCommand_Run(t *testing.T) { }, }, { - func(t *testing.T) { - if os.Getenv(acctest.TestEnvVar) == "" { - t.Skipf("Acceptance test skipped unless env '%s' set", acctest.TestEnvVar) - } + []func(t *testing.T, tc testCaseInit){ + skipInitTestUnlessEnVar(acctest.TestEnvVar).fn, }, // here we pre-write plugins with valid checksums, Packer will // see those as valid installations it did. @@ -171,10 +169,34 @@ func TestInitCommand_Run(t *testing.T) { }, }, { - func(t *testing.T) { - if os.Getenv(acctest.TestEnvVar) == "" { - t.Skipf("Acceptance test skipped unless env '%s' set", acctest.TestEnvVar) - } + []func(t *testing.T, tc testCaseInit){ + skipInitTestUnlessEnVar(acctest.TestEnvVar).fn, + }, + "release-with-no-binary", + testMetaFile(t), + nil, + "h1:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=", + map[string]string{ + `cfg.pkr.hcl`: ` + packer { + required_plugins { + comment = { + source = "github.com/sylviamoss/comment" + version = "v0.2.20" + } + } + }`, + }, + cfg.dir("3_pkr_config"), + cfg.dir("3_pkr_user_folder"), + 1, + nil, + "h1:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=", + nil, + }, + { + []func(t *testing.T, tc testCaseInit){ + skipInitTestUnlessEnVar(acctest.TestEnvVar).fn, }, "release-with-no-binary", testMetaFile(t), @@ -201,8 +223,8 @@ func TestInitCommand_Run(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if tt.checkSkip != nil { - tt.checkSkip(t) + for _, init := range tt.init { + init(t, tt) if t.Skipped() { return } @@ -264,3 +286,12 @@ func TestInitCommand_Run(t *testing.T) { }) } } + +type skipInitTestUnlessEnVar string + +func (key skipInitTestUnlessEnVar) fn(t *testing.T, tc testCaseInit) { + return // always run acc tests for now + if os.Getenv(string(key)) == "" { + t.Skipf("Acceptance test skipped unless env '%s' set", key) + } +} From ba8484ee6b1dd77f0725852d916c1c839672b3b2 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 11 Feb 2021 14:49:52 +0100 Subject: [PATCH 2/8] tests: pre make dir in case nothing happens --- command/utils_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/command/utils_test.go b/command/utils_test.go index f381b2e98..81dcf9e05 100644 --- a/command/utils_test.go +++ b/command/utils_test.go @@ -15,6 +15,9 @@ func mustString(s string, e error) string { } func createFiles(dir string, content map[string]string) { + if err := os.MkdirAll(dir, 0777); err != nil { + panic(err) + } for relPath, content := range content { contentPath := filepath.Join(dir, relPath) if err := os.MkdirAll(filepath.Dir(contentPath), 0777); err != nil { From 021044e963d5bb8d117352ec54dacadc0cf5d3b0 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 11 Feb 2021 14:56:42 +0100 Subject: [PATCH 3/8] put name first --- command/init_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/command/init_test.go b/command/init_test.go index e691d66fc..c00a5a054 100644 --- a/command/init_test.go +++ b/command/init_test.go @@ -14,8 +14,8 @@ import ( ) type testCaseInit struct { - init []func(*testing.T, testCaseInit) name string + init []func(*testing.T, testCaseInit) Meta Meta inPluginFolder map[string]string expectedPackerConfigDirHashBeforeInit string @@ -54,13 +54,13 @@ func TestInitCommand_Run(t *testing.T) { tests := []testCaseInit{ { - nil, // here we pre-write plugins with valid checksums, Packer will // see those as valid installations it did. // the directory hash before and after init should be the same, // that's a no-op. This also should do no GH query, so it is best // to always run it. "already-installed-no-op", + nil, testMetaFile(t), map[string]string{ "github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_darwin_amd64": "1", @@ -94,13 +94,13 @@ func TestInitCommand_Run(t *testing.T) { }, }, { - []func(t *testing.T, tc testCaseInit){ - skipInitTestUnlessEnVar(acctest.TestEnvVar).fn, - }, // here we pre-write plugins with valid checksums, Packer will // see those as valid installations it did. // But because we require version 0.2.19, we will upgrade. "already-installed-upgrade", + []func(t *testing.T, tc testCaseInit){ + skipInitTestUnlessEnVar(acctest.TestEnvVar).fn, + }, testMetaFile(t), map[string]string{ "github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_darwin_amd64": "1", @@ -169,10 +169,10 @@ func TestInitCommand_Run(t *testing.T) { }, }, { + "release-with-no-binary", []func(t *testing.T, tc testCaseInit){ skipInitTestUnlessEnVar(acctest.TestEnvVar).fn, }, - "release-with-no-binary", testMetaFile(t), nil, "h1:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=", @@ -195,10 +195,10 @@ func TestInitCommand_Run(t *testing.T) { nil, }, { + "release-with-no-binary", []func(t *testing.T, tc testCaseInit){ skipInitTestUnlessEnVar(acctest.TestEnvVar).fn, }, - "release-with-no-binary", testMetaFile(t), nil, "h1:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=", From ad0ce49cec1c998fe4fbb9921385964e164a59ab Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 11 Feb 2021 15:17:05 +0100 Subject: [PATCH 4/8] fix tmpdir creation --- command/init_test.go | 16 ++++++++-------- command/utils_test.go | 3 --- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/command/init_test.go b/command/init_test.go index c00a5a054..ff3791371 100644 --- a/command/init_test.go +++ b/command/init_test.go @@ -213,8 +213,8 @@ func TestInitCommand_Run(t *testing.T) { } }`, }, - cfg.dir("3_pkr_config"), - cfg.dir("3_pkr_user_folder"), + cfg.dir("4_pkr_config"), + cfg.dir("4_pkr_user_folder"), 1, nil, "h1:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=", @@ -223,6 +223,12 @@ func TestInitCommand_Run(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + t.Cleanup(func() { + _ = os.RemoveAll(tt.packerConfigDir) + }) + t.Cleanup(func() { + _ = os.RemoveAll(tt.packerUserFolder) + }) for _, init := range tt.init { init(t, tt) if t.Skipped() { @@ -231,13 +237,7 @@ func TestInitCommand_Run(t *testing.T) { } log.Printf("starting %s", tt.name) createFiles(tt.packerConfigDir, tt.inPluginFolder) - t.Cleanup(func() { - _ = os.RemoveAll(tt.packerConfigDir) - }) createFiles(tt.packerUserFolder, tt.inConfigFolder) - t.Cleanup(func() { - _ = os.RemoveAll(tt.packerUserFolder) - }) hash, err := dirhash.HashDir(tt.packerConfigDir, "", dirhash.DefaultHash) if err != nil { diff --git a/command/utils_test.go b/command/utils_test.go index 81dcf9e05..f381b2e98 100644 --- a/command/utils_test.go +++ b/command/utils_test.go @@ -15,9 +15,6 @@ func mustString(s string, e error) string { } func createFiles(dir string, content map[string]string) { - if err := os.MkdirAll(dir, 0777); err != nil { - panic(err) - } for relPath, content := range content { contentPath := filepath.Join(dir, relPath) if err := os.MkdirAll(filepath.Dir(contentPath), 0777); err != nil { From b321c64c1e74ae633a248f0bea178840af497bd7 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 11 Feb 2021 15:26:33 +0100 Subject: [PATCH 5/8] Update init_test.go --- command/init_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/command/init_test.go b/command/init_test.go index ff3791371..27ddf0e54 100644 --- a/command/init_test.go +++ b/command/init_test.go @@ -223,6 +223,8 @@ func TestInitCommand_Run(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + log.Printf("starting %s", tt.name) + log.Printf("%#v", tt) t.Cleanup(func() { _ = os.RemoveAll(tt.packerConfigDir) }) @@ -235,7 +237,6 @@ func TestInitCommand_Run(t *testing.T) { return } } - log.Printf("starting %s", tt.name) createFiles(tt.packerConfigDir, tt.inPluginFolder) createFiles(tt.packerUserFolder, tt.inConfigFolder) From 367706901085c1cf0d951d50aa50961873ea3359 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 11 Feb 2021 16:07:24 +0100 Subject: [PATCH 6/8] test: manually-installed-single-component-plugin-works --- command/config_file_unix.go | 7 ++++ command/config_file_windows.go | 7 ++++ command/init_test.go | 74 ++++++++++++++++++++++++++++++++-- packer/plugin.go | 1 + 4 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 command/config_file_unix.go create mode 100644 command/config_file_windows.go diff --git a/command/config_file_unix.go b/command/config_file_unix.go new file mode 100644 index 000000000..b1eba3aed --- /dev/null +++ b/command/config_file_unix.go @@ -0,0 +1,7 @@ +// +build darwin freebsd linux netbsd openbsd solaris + +package command + +const ( + defaultConfigDir = ".packer.d" +) diff --git a/command/config_file_windows.go b/command/config_file_windows.go new file mode 100644 index 000000000..5d8350a63 --- /dev/null +++ b/command/config_file_windows.go @@ -0,0 +1,7 @@ +// +build windows + +package command + +const ( + defaultConfigDir = "packer.d" +) diff --git a/command/init_test.go b/command/init_test.go index 27ddf0e54..3d8296b4b 100644 --- a/command/init_test.go +++ b/command/init_test.go @@ -1,14 +1,17 @@ package command import ( + "context" "fmt" "log" "os" + "path/filepath" "runtime" "sort" "testing" "github.com/google/go-cmp/cmp" + "github.com/hashicorp/go-getter/v2" "github.com/hashicorp/packer-plugin-sdk/acctest" "golang.org/x/mod/sumdb/dirhash" ) @@ -220,6 +223,53 @@ func TestInitCommand_Run(t *testing.T) { "h1:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=", nil, }, + { + "manually-installed-single-component-plugin-works", + []func(t *testing.T, tc testCaseInit){ + skipInitTestUnlessEnVar(acctest.TestEnvVar).fn, + initTestGoGetPlugin{ + Src: "https://github.com/azr/packer-provisioner-comment/releases/download/v1.0.0/" + + "packer-provisioner-comment_v1.0.0_" + runtime.GOOS + "_" + runtime.GOARCH + ".zip", + Dst: filepath.Join(cfg.dir("5_pkr_config"), defaultConfigDir, "plugins"), + }.fn, + }, + testMetaFile(t), + nil, + map[string]string{ + "darwin": "h1:nVebbXToeehPUASRbvV9M4qaA9+UgoR5AMp7LjTrSBk=", + "linux": "h1:/U5vdeMtOpRKNu0ld8+qf4t6WC+BsfCQ6JRo9Dh/khI=", + "windows": "h1:0nkdNCjtTHTgBNkzVKG++/VYmWAvq/o236GGTxrIf/Q=", + }[runtime.GOOS], + map[string]string{ + `source.pkr.hcl`: ` + source "null" "test" { + communicator = "none" + } + `, + `build.pkr.hcl`: ` + build { + sources = ["source.null.test"] + provisioner "comment" { + comment = "Begin ¡" + ui = true + bubble_text = true + } + } + `, + }, + cfg.dir("5_pkr_config"), + cfg.dir("5_pkr_user_folder"), + 0, + nil, + map[string]string{ + "darwin": "h1:nVebbXToeehPUASRbvV9M4qaA9+UgoR5AMp7LjTrSBk=", + "linux": "h1:/U5vdeMtOpRKNu0ld8+qf4t6WC+BsfCQ6JRo9Dh/khI=", + "windows": "h1:0nkdNCjtTHTgBNkzVKG++/VYmWAvq/o236GGTxrIf/Q=", + }[runtime.GOOS], + []func(*testing.T, testCaseInit){ + testBuild{want: 0}.fn, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -231,6 +281,7 @@ func TestInitCommand_Run(t *testing.T) { t.Cleanup(func() { _ = os.RemoveAll(tt.packerUserFolder) }) + os.Setenv("PACKER_CONFIG_DIR", tt.packerConfigDir) for _, init := range tt.init { init(t, tt) if t.Skipped() { @@ -245,7 +296,7 @@ func TestInitCommand_Run(t *testing.T) { t.Fatalf("HashDir: %v", err) } if diff := cmp.Diff(tt.expectedPackerConfigDirHashBeforeInit, hash); diff != "" { - t.Errorf("unexpected dir hash before init: %s", diff) + t.Errorf("unexpected dir hash before init: +found -expected %s", diff) } args := []string{tt.packerUserFolder} @@ -254,6 +305,10 @@ func TestInitCommand_Run(t *testing.T) { Meta: tt.Meta, } + if err := c.CoreConfig.Components.PluginConfig.Discover(); err != nil { + t.Fatalf("Failed to discover plugins: %s", err) + } + c.CoreConfig.Components.PluginConfig.KnownPluginFolders = []string{tt.packerConfigDir} if got := c.Run(args); got != tt.want { t.Errorf("InitCommand.Run() = %v, want %v", got, tt.want) @@ -291,8 +346,19 @@ func TestInitCommand_Run(t *testing.T) { type skipInitTestUnlessEnVar string func (key skipInitTestUnlessEnVar) fn(t *testing.T, tc testCaseInit) { - return // always run acc tests for now - if os.Getenv(string(key)) == "" { - t.Skipf("Acceptance test skipped unless env '%s' set", key) + // always run acc tests for now + // if os.Getenv(string(key)) == "" { + // t.Skipf("Acceptance test skipped unless env '%s' set", key) + // } +} + +type initTestGoGetPlugin struct { + Src string + Dst string +} + +func (opts initTestGoGetPlugin) fn(t *testing.T, _ testCaseInit) { + if _, err := getter.Get(context.Background(), opts.Dst, opts.Src); err != nil { + t.Fatalf("get: %v", err) } } diff --git a/packer/plugin.go b/packer/plugin.go index 8f7397934..1d8bcd7ba 100644 --- a/packer/plugin.go +++ b/packer/plugin.go @@ -106,6 +106,7 @@ func (c *PluginConfig) Discover() error { func (c *PluginConfig) discoverExternalComponents(path string) error { var err error + log.Printf("[TRACE] discovering plugins in %s", path) if !filepath.IsAbs(path) { path, err = filepath.Abs(path) From 40c2b2a15340cc0c2496537debc130b25310fe8d Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 11 Feb 2021 16:45:04 +0100 Subject: [PATCH 7/8] add manually-installed-single-component-plugin-old-api-fails --- command/init_test.go | 79 ++++++++++++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 29 deletions(-) diff --git a/command/init_test.go b/command/init_test.go index 3d8296b4b..b68460e3a 100644 --- a/command/init_test.go +++ b/command/init_test.go @@ -197,32 +197,6 @@ func TestInitCommand_Run(t *testing.T) { "h1:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=", nil, }, - { - "release-with-no-binary", - []func(t *testing.T, tc testCaseInit){ - skipInitTestUnlessEnVar(acctest.TestEnvVar).fn, - }, - testMetaFile(t), - nil, - "h1:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=", - map[string]string{ - `cfg.pkr.hcl`: ` - packer { - required_plugins { - comment = { - source = "github.com/sylviamoss/comment" - version = "v0.2.20" - } - } - }`, - }, - cfg.dir("4_pkr_config"), - cfg.dir("4_pkr_user_folder"), - 1, - nil, - "h1:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=", - nil, - }, { "manually-installed-single-component-plugin-works", []func(t *testing.T, tc testCaseInit){ @@ -230,7 +204,7 @@ func TestInitCommand_Run(t *testing.T) { initTestGoGetPlugin{ Src: "https://github.com/azr/packer-provisioner-comment/releases/download/v1.0.0/" + "packer-provisioner-comment_v1.0.0_" + runtime.GOOS + "_" + runtime.GOARCH + ".zip", - Dst: filepath.Join(cfg.dir("5_pkr_config"), defaultConfigDir, "plugins"), + Dst: filepath.Join(cfg.dir("4_pkr_config"), defaultConfigDir, "plugins"), }.fn, }, testMetaFile(t), @@ -257,8 +231,8 @@ func TestInitCommand_Run(t *testing.T) { } `, }, - cfg.dir("5_pkr_config"), - cfg.dir("5_pkr_user_folder"), + cfg.dir("4_pkr_config"), + cfg.dir("4_pkr_user_folder"), 0, nil, map[string]string{ @@ -270,6 +244,53 @@ func TestInitCommand_Run(t *testing.T) { testBuild{want: 0}.fn, }, }, + { + "manually-installed-single-component-plugin-old-api-fails", + []func(t *testing.T, tc testCaseInit){ + skipInitTestUnlessEnVar(acctest.TestEnvVar).fn, + initTestGoGetPlugin{ + Src: "https://github.com/azr/packer-provisioner-comment/releases/download/v0.0.0/" + + "packer-provisioner-comment_v0.0.0_" + runtime.GOOS + "_" + runtime.GOARCH + ".zip", + Dst: filepath.Join(cfg.dir("5_pkr_config"), defaultConfigDir, "plugins"), + }.fn, + }, + testMetaFile(t), + nil, + map[string]string{ + "darwin": "h1:gW4gzpDXeu3cDrXgHJj9iWAN7Pyak626Gq8Bu2LG1kY=", + "linux": "h1:wQ2H5+J7VXwQzqR9DgpWtjhw9OVEFbcKQL6dgm/+zwo=", + "windows": "h1:BqRdW3c5H1PZ2Q4DOaKWja21v3nDlY5Nn8kqahhHGSw=", + }[runtime.GOOS], + map[string]string{ + `source.pkr.hcl`: ` + source "null" "test" { + communicator = "none" + } + `, + `build.pkr.hcl`: ` + build { + sources = ["source.null.test"] + provisioner "comment" { + comment = "Begin ¡" + ui = true + bubble_text = true + } + } + `, + }, + cfg.dir("5_pkr_config"), + cfg.dir("5_pkr_user_folder"), + 0, + nil, + map[string]string{ + "darwin": "h1:gW4gzpDXeu3cDrXgHJj9iWAN7Pyak626Gq8Bu2LG1kY=", + "linux": "h1:wQ2H5+J7VXwQzqR9DgpWtjhw9OVEFbcKQL6dgm/+zwo=", + "windows": "h1:BqRdW3c5H1PZ2Q4DOaKWja21v3nDlY5Nn8kqahhHGSw=", + }[runtime.GOOS], + []func(*testing.T, testCaseInit){ + testBuild{want: 1}.fn, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From 7089e0854a6c3c873ee728b7f6ed712fedefc561 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 11 Feb 2021 18:00:18 +0100 Subject: [PATCH 8/8] rename setup func to avoid name conflict --- command/init_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/command/init_test.go b/command/init_test.go index b68460e3a..0b86379c7 100644 --- a/command/init_test.go +++ b/command/init_test.go @@ -18,7 +18,7 @@ import ( type testCaseInit struct { name string - init []func(*testing.T, testCaseInit) + setup []func(*testing.T, testCaseInit) Meta Meta inPluginFolder map[string]string expectedPackerConfigDirHashBeforeInit string @@ -303,7 +303,7 @@ func TestInitCommand_Run(t *testing.T) { _ = os.RemoveAll(tt.packerUserFolder) }) os.Setenv("PACKER_CONFIG_DIR", tt.packerConfigDir) - for _, init := range tt.init { + for _, init := range tt.setup { init(t, tt) if t.Skipped() { return