From 1e7e57baf79cf92fd2f5dc7b7bd182d768b84aca Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Tue, 6 Aug 2024 11:00:08 -0400 Subject: [PATCH] packer_test: add gob/pb test suite With the draft to support both gob and protobuf as serialisation formats for Packer, along with the SDK changes that propel them, we add a series of tests that make sure the logic that picks which protocol is solid and functional. These tests rely on building several versions of the tester plugin, with and without protobuf support, to then install them in the tests as needed to test the logic of Packer using packer build with them, and templates that require multiple plugins. --- packer_test/gob_test/gob_pb_test.go | 179 ++++++++++++++++++ packer_test/gob_test/suite_test.go | 38 ++++ .../gob_test/templates/internal_only.pkr.hcl | 7 + .../templates/test_both_plugins.pkr.hcl | 22 +++ .../templates/test_one_pinned_plugin.pkr.hcl | 16 ++ .../templates/test_one_plugin.pkr.hcl | 16 ++ 6 files changed, 278 insertions(+) create mode 100644 packer_test/gob_test/gob_pb_test.go create mode 100644 packer_test/gob_test/suite_test.go create mode 100644 packer_test/gob_test/templates/internal_only.pkr.hcl create mode 100644 packer_test/gob_test/templates/test_both_plugins.pkr.hcl create mode 100644 packer_test/gob_test/templates/test_one_pinned_plugin.pkr.hcl create mode 100644 packer_test/gob_test/templates/test_one_plugin.pkr.hcl diff --git a/packer_test/gob_test/gob_pb_test.go b/packer_test/gob_test/gob_pb_test.go new file mode 100644 index 00000000000..59dabc8dc71 --- /dev/null +++ b/packer_test/gob_test/gob_pb_test.go @@ -0,0 +1,179 @@ +package gob_test + +import "github.com/hashicorp/packer/packer_test/common/check" + +const pbPluginName = "github.com/hashicorp/pbtester" + +func CheckPBUsed(expect bool) check.Checker { + const strToLookFor = "protobuf for communication with plugins" + + var opts []check.GrepOpts + if !expect { + opts = append(opts, check.GrepInvert) + } + + return check.Grep(strToLookFor, opts...) +} + +// Two different plugins installed locally, one with gob, one with protobuf. +// Both should have different sources so Packer will discover and fallback to using only gob. +func (ts *PackerGobTestSuite) TestTwoPluginsDifferentPB() { + pluginDir := ts.MakePluginDir().InstallPluginVersions("1.0.0+gob") + defer pluginDir.Cleanup() + + ts.PackerCommand().UsePluginDir(pluginDir). + SetArgs("plugins", "install", "--path", ts.GetPluginPath(ts.T(), "1.0.0+pb"), pbPluginName). + Assert(check.MustSucceed()) + + ts.PackerCommand().UsePluginDir(pluginDir). + SetArgs("build", "./templates/test_both_plugins.pkr.hcl"). + Assert(CheckPBUsed(false)) + + ts.PackerCommand().UsePluginDir(pluginDir). + SetArgs("build", "./templates/test_one_plugin.pkr.hcl"). + Assert(CheckPBUsed(false)) +} + +// Two plugins, both with protobuf supported +// Both installed plugins will support protobuf, so Packer will use Protobuf for all its communications. +func (ts *PackerGobTestSuite) TestTwoPluginsBothPB() { + pluginDir := ts.MakePluginDir().InstallPluginVersions("1.0.0+pb") + defer pluginDir.Cleanup() + + ts.PackerCommand().UsePluginDir(pluginDir). + SetArgs("plugins", "install", "--path", ts.GetPluginPath(ts.T(), "1.0.0+pb"), pbPluginName). + Assert(check.MustSucceed()) + + ts.PackerCommand().UsePluginDir(pluginDir). + SetArgs("build", "./templates/test_both_plugins.pkr.hcl"). + Assert(CheckPBUsed(true)) + + ts.PackerCommand().UsePluginDir(pluginDir). + SetArgs("build", "./templates/test_one_plugin.pkr.hcl"). + Assert(CheckPBUsed(true)) +} + +// Two plugins, both with protobuf supported, force gob +// Both installed plugins support protobuf, but the environment variable PACKER_FORCE_GOB is +// set to 1 (or on), so Packer must use gob despite protobuf being supported all around. +func (ts *PackerGobTestSuite) TestTwoPluginsBothPBForceGob() { + pluginDir := ts.MakePluginDir().InstallPluginVersions("1.0.0+pb") + defer pluginDir.Cleanup() + + ts.PackerCommand().UsePluginDir(pluginDir). + SetArgs("plugins", "install", "--path", ts.GetPluginPath(ts.T(), "1.0.0+pb"), pbPluginName). + Assert(check.MustSucceed()) + + ts.PackerCommand().UsePluginDir(pluginDir). + AddEnv("PACKER_FORCE_GOB", "1"). + SetArgs("build", "./templates/test_both_plugins.pkr.hcl"). + Assert(check.MustSucceed(), CheckPBUsed(false)) + + ts.PackerCommand().UsePluginDir(pluginDir). + AddEnv("PACKER_FORCE_GOB", "1"). + SetArgs("build", "./templates/test_one_plugin.pkr.hcl"). + Assert(check.MustSucceed(), CheckPBUsed(false)) +} + +// Two plugins installed, one with two versions: one version supporting pb, +// one older with gob only. The other with only protobuf. +// The template used pins the older version of the first plugin. +// In this case, gob should be the one used, as the selected version supports +// gob only, despite a newer version supporting protobuf, and the other plugin +// also being compatible. +func (ts *PackerGobTestSuite) TestTwoPluginsLatestPBOlderGob_OlderPinned() { + pluginDir := ts.MakePluginDir().InstallPluginVersions("1.0.0+gob", "1.1.0+pb") + defer pluginDir.Cleanup() + + ts.PackerCommand().UsePluginDir(pluginDir). + SetArgs("plugins", "install", "--path", ts.GetPluginPath(ts.T(), "1.1.0+pb"), pbPluginName). + Assert(check.MustSucceed(), check.MustSucceed()) + + ts.PackerCommand().UsePluginDir(pluginDir). + SetArgs("build", "./templates/test_one_pinned_plugin.pkr.hcl"). + Assert(check.MustSucceed(), CheckPBUsed(false)) +} + +// One plugin installed, one version supporting pb, one older with gob only +// The template used pins the older version. +// In this case, gob should be the one used, as the selected version supports +// gob only, despite a newer version supporting protobuf. +func (ts *PackerGobTestSuite) TestOnePluginLatestPBOlderGob_OlderPinned() { + pluginDir := ts.MakePluginDir().InstallPluginVersions("1.0.0+gob", "1.1.0+pb") + defer pluginDir.Cleanup() + + ts.PackerCommand().UsePluginDir(pluginDir). + SetArgs("build", "./templates/test_one_pinned_plugin.pkr.hcl"). + Assert(check.MustSucceed(), CheckPBUsed(false)) +} + +// One plugin, with latest version supporting gob, but the older supporting protobuf +// In this case, Packer will default to using the latest version, and should +// default to using gob. +func (ts *PackerGobTestSuite) TestOnePluginWithLatestOnlyGob() { + pluginDir := ts.MakePluginDir().InstallPluginVersions("1.0.0+pb", "1.1.0+gob") + defer pluginDir.Cleanup() + + ts.PackerCommand().UsePluginDir(pluginDir). + SetArgs("build", "./templates/test_one_plugin.pkr.hcl"). + Assert(check.MustSucceed(), CheckPBUsed(false)) +} + +// One plugin, gob only supported +// Packer will load the only plugin available there, and will use it, and use gob for comms +func (ts PackerGobTestSuite) TestOnePluginWithOnlyGob() { + pluginDir := ts.MakePluginDir().InstallPluginVersions("1.0.0+gob") + defer pluginDir.Cleanup() + + ts.PackerCommand().UsePluginDir(pluginDir). + SetArgs("build", "./templates/test_one_plugin.pkr.hcl"). + Assert(check.MustSucceed(), CheckPBUsed(false)) +} + +// One plugin, protobuf supported +// Packer will load the only plugin available there, and use protobuf for comms +func (ts PackerGobTestSuite) TestOnePluginWithPB() { + pluginDir := ts.MakePluginDir().InstallPluginVersions("1.0.0+pb") + defer pluginDir.Cleanup() + + ts.PackerCommand().UsePluginDir(pluginDir). + SetArgs("build", "./templates/test_one_plugin.pkr.hcl"). + Assert(check.MustSucceed(), CheckPBUsed(true)) +} + +// No plugin installed, only internal components +// In this test, Packer must use Protobuf for internal components as nothing installed will prevent it. +func (ts PackerGobTestSuite) TestInternalOnly() { + pluginDir := ts.MakePluginDir().InstallPluginVersions() + defer pluginDir.Cleanup() + + ts.PackerCommand().UsePluginDir(pluginDir). + SetArgs("build", "./templates/internal_only.pkr.hcl"). + Assert(check.MustSucceed(), CheckPBUsed(true)) +} + +// One plugin with gob only installed, use only internal components +// +// Packer in this case will fallback to Gob, even if the template uses internal +// components only, as this is determined at loading time. +func (ts PackerGobTestSuite) TestInternalOnlyWithGobPluginInstalled() { + pluginDir := ts.MakePluginDir().InstallPluginVersions("1.0.0+gob") + defer pluginDir.Cleanup() + + ts.PackerCommand().UsePluginDir(pluginDir). + SetArgs("build", "./templates/internal_only.pkr.hcl"). + Assert(check.MustSucceed(), CheckPBUsed(false)) +} + +// One plugin with pb support installed, use only internal components +// +// Packer in this case will fallback to Gob, even if the template uses internal +// components only, as this is determined at loading time. +func (ts PackerGobTestSuite) TestInternalOnlyWithPBPluginInstalled() { + pluginDir := ts.MakePluginDir().InstallPluginVersions("1.0.0+pb") + defer pluginDir.Cleanup() + + ts.PackerCommand().UsePluginDir(pluginDir). + SetArgs("build", "./templates/internal_only.pkr.hcl"). + Assert(check.MustSucceed(), CheckPBUsed(true)) +} diff --git a/packer_test/gob_test/suite_test.go b/packer_test/gob_test/suite_test.go new file mode 100644 index 00000000000..4bef9f8424e --- /dev/null +++ b/packer_test/gob_test/suite_test.go @@ -0,0 +1,38 @@ +package gob_test + +import ( + "testing" + + "github.com/hashicorp/packer/packer_test/common" + "github.com/stretchr/testify/suite" +) + +type PackerGobTestSuite struct { + *common.PackerTestSuite +} + +func Test_PackerPluginSuite(t *testing.T) { + baseSuite, cleanup := common.InitBaseSuite(t) + defer cleanup() + + ts := &PackerGobTestSuite{ + baseSuite, + } + + var compilationJobs []chan common.CompilationResult + + // Build two versions of each plugin, one with gob only, one with protobuf only + // + // We'll install them manually in tests, as they'll need to be installed as + // different plugin sources in order for discovery to trigger the + // gob-only/pb-supported behaviours we want to test. + compilationJobs = append(compilationJobs, ts.CompilePlugin("1.1.0+pb", common.UseDependency(common.SDKModule, "grpc_base"))) + compilationJobs = append(compilationJobs, ts.CompilePlugin("1.0.0+pb", common.UseDependency(common.SDKModule, "grpc_base"))) + + compilationJobs = append(compilationJobs, ts.CompilePlugin("1.0.0+gob")) + compilationJobs = append(compilationJobs, ts.CompilePlugin("1.1.0+gob")) + + common.Ready(t, compilationJobs) + + suite.Run(t, ts) +} diff --git a/packer_test/gob_test/templates/internal_only.pkr.hcl b/packer_test/gob_test/templates/internal_only.pkr.hcl new file mode 100644 index 00000000000..e005230b49d --- /dev/null +++ b/packer_test/gob_test/templates/internal_only.pkr.hcl @@ -0,0 +1,7 @@ +source "null" "test" { + communicator = "none" +} + +build { + sources = ["null.test"] +} diff --git a/packer_test/gob_test/templates/test_both_plugins.pkr.hcl b/packer_test/gob_test/templates/test_both_plugins.pkr.hcl new file mode 100644 index 00000000000..36c148e4bf1 --- /dev/null +++ b/packer_test/gob_test/templates/test_both_plugins.pkr.hcl @@ -0,0 +1,22 @@ +packer { + required_plugins { + tester = { + source = "github.com/hashicorp/tester", + version = ">= 1.0.0" + } + pbtester = { + source = "github.com/hashicorp/pbtester", + version = ">= 1.0.0" + } + } +} + +source "tester-dynamic" "test" {} +source "pbtester-dynamic" "test" {} + +build { + sources = [ + "tester-dynamic.test", + "pbtester-dynamic.test" + ] +} diff --git a/packer_test/gob_test/templates/test_one_pinned_plugin.pkr.hcl b/packer_test/gob_test/templates/test_one_pinned_plugin.pkr.hcl new file mode 100644 index 00000000000..735c0947c2f --- /dev/null +++ b/packer_test/gob_test/templates/test_one_pinned_plugin.pkr.hcl @@ -0,0 +1,16 @@ +packer { + required_plugins { + tester = { + source = "github.com/hashicorp/tester", + version = "= 1.0.0" + } + } +} + +source "tester-dynamic" "test" {} + +build { + sources = [ + "tester-dynamic.test", + ] +} diff --git a/packer_test/gob_test/templates/test_one_plugin.pkr.hcl b/packer_test/gob_test/templates/test_one_plugin.pkr.hcl new file mode 100644 index 00000000000..1e063e27d34 --- /dev/null +++ b/packer_test/gob_test/templates/test_one_plugin.pkr.hcl @@ -0,0 +1,16 @@ +packer { + required_plugins { + tester = { + source = "github.com/hashicorp/tester", + version = ">= 1.0.0" + } + } +} + +source "tester-dynamic" "test" {} + +build { + sources = [ + "tester-dynamic.test", + ] +}