Test running plugins with fake and fresh plugins (#10595)

* show error as to why plugin discovery failed
* allow to run more manual tests after a plugin installation
* test that a freshly installed external plugin can run
This commit is contained in:
Adrien Delorme 2021-02-09 17:07:59 +01:00 committed by GitHub
parent 9afaa5a21f
commit ffbb110167
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 112 additions and 57 deletions

View File

@ -1,10 +1,9 @@
package command package command
import ( import (
"io/ioutil" "fmt"
"log" "log"
"os" "os"
"path/filepath"
"runtime" "runtime"
"sort" "sort"
"testing" "testing"
@ -14,27 +13,46 @@ import (
"golang.org/x/mod/sumdb/dirhash" "golang.org/x/mod/sumdb/dirhash"
) )
type testCaseInit struct {
checkSkip func(*testing.T)
name string
Meta Meta
inPluginFolder map[string]string
expectedPackerConfigDirHashBeforeInit string
inConfigFolder map[string]string
packerConfigDir string
packerUserFolder string
want int
dirFiles []string
expectedPackerConfigDirHashAfterInit string
moreTests []func(*testing.T, testCaseInit)
}
type testBuild struct {
want int
}
func (tb testBuild) fn(t *testing.T, tc testCaseInit) {
bc := BuildCommand{
Meta: tc.Meta,
}
args := []string{tc.packerUserFolder}
want := tb.want
if got := bc.Run(args); got != want {
t.Errorf("BuildCommand.Run() = %v, want %v", got, want)
}
}
func TestInitCommand_Run(t *testing.T) { func TestInitCommand_Run(t *testing.T) {
// These tests will try to optimise for doing the least amount of github api // These tests will try to optimise for doing the least amount of github api
// requests whilst testing the max amount of things at once. Hopefully they // requests whilst testing the max amount of things at once. Hopefully they
// don't require a GH token just yet. Acc tests are run on linux, darwin and // don't require a GH token just yet. Acc tests are run on linux, darwin and
// windows, so requests are done 3 times. // windows, so requests are done 3 times.
type testCase struct {
checkSkip func(*testing.T)
name string
inPluginFolder map[string]string
expectedPackerConfigDirHashBeforeInit string
hclFile string
packerConfigDir string
want int
dirFiles []string
expectedPackerConfigDirHashAfterInit string
}
cfg := &configDirSingleton{map[string]string{}} cfg := &configDirSingleton{map[string]string{}}
tests := []testCase{ tests := []testCaseInit{
{ {
nil, nil,
// here we pre-write plugins with valid checksums, Packer will // here we pre-write plugins with valid checksums, Packer will
@ -43,6 +61,7 @@ func TestInitCommand_Run(t *testing.T) {
// that's a no-op. This also should do no GH query, so it is best // that's a no-op. This also should do no GH query, so it is best
// to always run it. // to always run it.
"already-installed-no-op", "already-installed-no-op",
testMetaFile(t),
map[string]string{ map[string]string{
"github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_darwin_amd64": "1", "github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_darwin_amd64": "1",
"github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_darwin_amd64_SHA256SUM": "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b", "github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_darwin_amd64_SHA256SUM": "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b",
@ -52,7 +71,8 @@ func TestInitCommand_Run(t *testing.T) {
"github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_linux_amd64_SHA256SUM": "59031c50e0dfeedfde2b4e9445754804dce3f29e4efa737eead0ca9b4f5b85a5", "github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_linux_amd64_SHA256SUM": "59031c50e0dfeedfde2b4e9445754804dce3f29e4efa737eead0ca9b4f5b85a5",
}, },
"h1:Q5qyAOdD43hL3CquQdVfaHpOYGf0UsZ/+wVA9Ry6cbA=", "h1:Q5qyAOdD43hL3CquQdVfaHpOYGf0UsZ/+wVA9Ry6cbA=",
`# cfg.pkr.hcl map[string]string{
`cfg.pkr.hcl`: `
packer { packer {
required_plugins { required_plugins {
comment = { comment = {
@ -61,10 +81,17 @@ func TestInitCommand_Run(t *testing.T) {
} }
} }
}`, }`,
cfg.dir("1"), },
cfg.dir("1_pkr_config"),
cfg.dir("1_pkr_user_folder"),
0, 0,
nil, nil,
"h1:Q5qyAOdD43hL3CquQdVfaHpOYGf0UsZ/+wVA9Ry6cbA=", "h1:Q5qyAOdD43hL3CquQdVfaHpOYGf0UsZ/+wVA9Ry6cbA=",
[]func(t *testing.T, tc testCaseInit){
// test that a build will not work since plugins are broken for
// this tests (they are not binaries).
testBuild{want: 1}.fn,
},
}, },
{ {
func(t *testing.T) { func(t *testing.T) {
@ -76,6 +103,7 @@ func TestInitCommand_Run(t *testing.T) {
// see those as valid installations it did. // see those as valid installations it did.
// But because we require version 0.2.19, we will upgrade. // But because we require version 0.2.19, we will upgrade.
"already-installed-upgrade", "already-installed-upgrade",
testMetaFile(t),
map[string]string{ map[string]string{
"github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_darwin_amd64": "1", "github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_darwin_amd64": "1",
"github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_darwin_amd64_SHA256SUM": "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b", "github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_darwin_amd64_SHA256SUM": "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b",
@ -85,7 +113,8 @@ func TestInitCommand_Run(t *testing.T) {
"github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_linux_amd64_SHA256SUM": "59031c50e0dfeedfde2b4e9445754804dce3f29e4efa737eead0ca9b4f5b85a5", "github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_linux_amd64_SHA256SUM": "59031c50e0dfeedfde2b4e9445754804dce3f29e4efa737eead0ca9b4f5b85a5",
}, },
"h1:Q5qyAOdD43hL3CquQdVfaHpOYGf0UsZ/+wVA9Ry6cbA=", "h1:Q5qyAOdD43hL3CquQdVfaHpOYGf0UsZ/+wVA9Ry6cbA=",
`# cfg.pkr.hcl map[string]string{
`cfg.pkr.hcl`: `
packer { packer {
required_plugins { required_plugins {
comment = { comment = {
@ -94,7 +123,24 @@ func TestInitCommand_Run(t *testing.T) {
} }
} }
}`, }`,
cfg.dir("2"), `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("2_pkr_config"),
cfg.dir("2_pkr_user_folder"),
0, 0,
[]string{ []string{
"github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_darwin_amd64", "github.com/sylviamoss/comment/packer-plugin-comment_v0.2.18_x5.0_darwin_amd64",
@ -119,6 +165,10 @@ func TestInitCommand_Run(t *testing.T) {
"linux": "h1:CGym0+Nd0LEANgzqL0wx/LDjRL8bYwlpZ0HajPJo/hs=", "linux": "h1:CGym0+Nd0LEANgzqL0wx/LDjRL8bYwlpZ0HajPJo/hs=",
"windows": "h1:ag0/C1YjP7KoEDYOiJHE0K+lhFgs0tVgjriWCXVT1fg=", "windows": "h1:ag0/C1YjP7KoEDYOiJHE0K+lhFgs0tVgjriWCXVT1fg=",
}[runtime.GOOS], }[runtime.GOOS],
[]func(t *testing.T, tc testCaseInit){
// test that a build will work as the plugin was just installed
testBuild{want: 0}.fn,
},
}, },
{ {
func(t *testing.T) { func(t *testing.T) {
@ -127,9 +177,11 @@ func TestInitCommand_Run(t *testing.T) {
} }
}, },
"release-with-no-binary", "release-with-no-binary",
testMetaFile(t),
nil, nil,
"h1:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=", "h1:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=",
`# cfg.pkr.hcl map[string]string{
`cfg.pkr.hcl`: `
packer { packer {
required_plugins { required_plugins {
comment = { comment = {
@ -138,10 +190,13 @@ func TestInitCommand_Run(t *testing.T) {
} }
} }
}`, }`,
cfg.dir("3"), },
cfg.dir("3_pkr_config"),
cfg.dir("3_pkr_user_folder"),
1, 1,
nil, nil,
"h1:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=", "h1:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=",
nil,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
@ -157,6 +212,10 @@ func TestInitCommand_Run(t *testing.T) {
t.Cleanup(func() { t.Cleanup(func() {
_ = os.RemoveAll(tt.packerConfigDir) _ = os.RemoveAll(tt.packerConfigDir)
}) })
createFiles(tt.packerUserFolder, tt.inConfigFolder)
t.Cleanup(func() {
_ = os.RemoveAll(tt.packerUserFolder)
})
hash, err := dirhash.HashDir(tt.packerConfigDir, "", dirhash.DefaultHash) hash, err := dirhash.HashDir(tt.packerConfigDir, "", dirhash.DefaultHash)
if err != nil { if err != nil {
@ -166,21 +225,10 @@ func TestInitCommand_Run(t *testing.T) {
t.Errorf("unexpected dir hash before init: %s", diff) t.Errorf("unexpected dir hash before init: %s", diff)
} }
cfgDir, err := ioutil.TempDir("", "pkr-test-init-file-folder") args := []string{tt.packerUserFolder}
if err != nil {
t.Fatalf("TempDir: %v", err)
}
if err := ioutil.WriteFile(filepath.Join(cfgDir, "cfg.pkr.hcl"), []byte(tt.hclFile), 0666); err != nil {
t.Fatalf("WriteFile: %v", err)
}
t.Cleanup(func() {
_ = os.RemoveAll(cfgDir)
})
args := []string{cfgDir}
c := &InitCommand{ c := &InitCommand{
Meta: testMetaFile(t), Meta: tt.Meta,
} }
c.CoreConfig.Components.PluginConfig.KnownPluginFolders = []string{tt.packerConfigDir} c.CoreConfig.Components.PluginConfig.KnownPluginFolders = []string{tt.packerConfigDir}
@ -207,6 +255,12 @@ func TestInitCommand_Run(t *testing.T) {
if diff := cmp.Diff(tt.expectedPackerConfigDirHashAfterInit, hash); diff != "" { if diff := cmp.Diff(tt.expectedPackerConfigDirHashAfterInit, hash); diff != "" {
t.Errorf("unexpected dir hash after init: %s", diff) t.Errorf("unexpected dir hash after init: %s", diff)
} }
for i, subTest := range tt.moreTests {
t.Run(fmt.Sprintf("-subtest-%d", i), func(t *testing.T) {
subTest(t, tt)
})
}
}) })
} }
} }

View File

@ -97,7 +97,8 @@ func (cfg *PackerConfig) detectPluginBinaries() hcl.Diagnostics {
if err != nil { if err != nil {
diags = append(diags, &hcl.Diagnostic{ diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError, Severity: hcl.DiagError,
Summary: fmt.Sprintf("Failed to discover plugin %s", pluginRequirement.Identifier.ForDisplay()), Summary: fmt.Sprintf("Error discovering plugin %s", pluginRequirement.Identifier.ForDisplay()),
Detail: err.Error(),
}) })
continue continue
} }