github.com/hashicorp/packer@v1.14.3/packer_test/plugin_tests/gob_pb_test.go (about) 1 package plugin_tests 2 3 import "github.com/hashicorp/packer/packer_test/common/check" 4 5 const pbPluginName = "github.com/hashicorp/pbtester" 6 7 func CheckPBUsed(expect bool) check.Checker { 8 const strToLookFor = "protobuf for communication with plugins" 9 10 var opts []check.GrepOpts 11 if !expect { 12 opts = append(opts, check.GrepInvert) 13 } 14 15 return check.Grep(strToLookFor, opts...) 16 } 17 18 // Two different plugins installed locally, one with gob, one with protobuf. 19 // Both should have different sources so Packer will discover and fallback to using only gob. 20 func (ts *PackerGobTestSuite) TestTwoPluginsDifferentPB() { 21 pluginDir := ts.MakePluginDir().InstallPluginVersions("1.0.0+gob") 22 defer pluginDir.Cleanup() 23 24 ts.PackerCommand().UsePluginDir(pluginDir). 25 SetArgs("plugins", "install", "--path", ts.GetPluginPath(ts.T(), "1.0.0+pb"), pbPluginName). 26 Assert(check.MustSucceed()) 27 28 ts.PackerCommand().UsePluginDir(pluginDir). 29 SetArgs("build", "./templates/test_both_plugins.pkr.hcl"). 30 Assert(CheckPBUsed(false)) 31 32 ts.PackerCommand().UsePluginDir(pluginDir). 33 SetArgs("build", "./templates/test_one_plugin.pkr.hcl"). 34 Assert(CheckPBUsed(false)) 35 } 36 37 // Two plugins, both with protobuf supported 38 // Both installed plugins will support protobuf, so Packer will use Protobuf for all its communications. 39 func (ts *PackerGobTestSuite) TestTwoPluginsBothPB() { 40 pluginDir := ts.MakePluginDir().InstallPluginVersions("1.0.0+pb") 41 defer pluginDir.Cleanup() 42 43 ts.PackerCommand().UsePluginDir(pluginDir). 44 SetArgs("plugins", "install", "--path", ts.GetPluginPath(ts.T(), "1.0.0+pb"), pbPluginName). 45 Assert(check.MustSucceed()) 46 47 ts.PackerCommand().UsePluginDir(pluginDir). 48 SetArgs("build", "./templates/test_both_plugins.pkr.hcl"). 49 Assert(CheckPBUsed(true)) 50 51 ts.PackerCommand().UsePluginDir(pluginDir). 52 SetArgs("build", "./templates/test_one_plugin.pkr.hcl"). 53 Assert(CheckPBUsed(true)) 54 } 55 56 // Two plugins, both with protobuf supported, force gob 57 // Both installed plugins support protobuf, but the environment variable PACKER_FORCE_GOB is 58 // set to 1 (or on), so Packer must use gob despite protobuf being supported all around. 59 func (ts *PackerGobTestSuite) TestTwoPluginsBothPBForceGob() { 60 pluginDir := ts.MakePluginDir().InstallPluginVersions("1.0.0+pb") 61 defer pluginDir.Cleanup() 62 63 ts.PackerCommand().UsePluginDir(pluginDir). 64 SetArgs("plugins", "install", "--path", ts.GetPluginPath(ts.T(), "1.0.0+pb"), pbPluginName). 65 Assert(check.MustSucceed()) 66 67 ts.PackerCommand().UsePluginDir(pluginDir). 68 AddEnv("PACKER_FORCE_GOB", "1"). 69 SetArgs("build", "./templates/test_both_plugins.pkr.hcl"). 70 Assert(check.MustSucceed(), CheckPBUsed(false)) 71 72 ts.PackerCommand().UsePluginDir(pluginDir). 73 AddEnv("PACKER_FORCE_GOB", "1"). 74 SetArgs("build", "./templates/test_one_plugin.pkr.hcl"). 75 Assert(check.MustSucceed(), CheckPBUsed(false)) 76 } 77 78 // Two plugins installed, one with two versions: one version supporting pb, 79 // one older with gob only. The other with only protobuf. 80 // The template used pins the older version of the first plugin. 81 // In this case, gob should be the one used, as the selected version supports 82 // gob only, despite a newer version supporting protobuf, and the other plugin 83 // also being compatible. 84 func (ts *PackerGobTestSuite) TestTwoPluginsLatestPBOlderGob_OlderPinned() { 85 pluginDir := ts.MakePluginDir().InstallPluginVersions("1.0.0+gob", "1.1.0+pb") 86 defer pluginDir.Cleanup() 87 88 ts.PackerCommand().UsePluginDir(pluginDir). 89 SetArgs("plugins", "install", "--path", ts.GetPluginPath(ts.T(), "1.1.0+pb"), pbPluginName). 90 Assert(check.MustSucceed(), check.MustSucceed()) 91 92 ts.PackerCommand().UsePluginDir(pluginDir). 93 SetArgs("build", "./templates/test_one_pinned_plugin.pkr.hcl"). 94 Assert(check.MustSucceed(), CheckPBUsed(false)) 95 } 96 97 // One plugin installed, one version supporting pb, one older with gob only 98 // The template used pins the older version. 99 // In this case, gob should be the one used, as the selected version supports 100 // gob only, despite a newer version supporting protobuf. 101 func (ts *PackerGobTestSuite) TestOnePluginLatestPBOlderGob_OlderPinned() { 102 pluginDir := ts.MakePluginDir().InstallPluginVersions("1.0.0+gob", "1.1.0+pb") 103 defer pluginDir.Cleanup() 104 105 ts.PackerCommand().UsePluginDir(pluginDir). 106 SetArgs("build", "./templates/test_one_pinned_plugin.pkr.hcl"). 107 Assert(check.MustSucceed(), CheckPBUsed(false)) 108 } 109 110 // One plugin, with latest version supporting gob, but the older supporting protobuf 111 // In this case, Packer will default to using the latest version, and should 112 // default to using gob. 113 func (ts *PackerGobTestSuite) TestOnePluginWithLatestOnlyGob() { 114 pluginDir := ts.MakePluginDir().InstallPluginVersions("1.0.0+pb", "1.1.0+gob") 115 defer pluginDir.Cleanup() 116 117 ts.PackerCommand().UsePluginDir(pluginDir). 118 SetArgs("build", "./templates/test_one_plugin.pkr.hcl"). 119 Assert(check.MustSucceed(), CheckPBUsed(false)) 120 } 121 122 // One plugin, gob only supported 123 // Packer will load the only plugin available there, and will use it, and use gob for comms 124 func (ts PackerGobTestSuite) TestOnePluginWithOnlyGob() { 125 pluginDir := ts.MakePluginDir().InstallPluginVersions("1.0.0+gob") 126 defer pluginDir.Cleanup() 127 128 ts.PackerCommand().UsePluginDir(pluginDir). 129 SetArgs("build", "./templates/test_one_plugin.pkr.hcl"). 130 Assert(check.MustSucceed(), CheckPBUsed(false)) 131 } 132 133 // One plugin, protobuf supported 134 // Packer will load the only plugin available there, and use protobuf for comms 135 func (ts PackerGobTestSuite) TestOnePluginWithPB() { 136 pluginDir := ts.MakePluginDir().InstallPluginVersions("1.0.0+pb") 137 defer pluginDir.Cleanup() 138 139 ts.PackerCommand().UsePluginDir(pluginDir). 140 SetArgs("build", "./templates/test_one_plugin.pkr.hcl"). 141 Assert(check.MustSucceed(), CheckPBUsed(true)) 142 } 143 144 // No plugin installed, only internal components 145 // In this test, Packer must use Protobuf for internal components as nothing installed will prevent it. 146 func (ts PackerGobTestSuite) TestInternalOnly() { 147 pluginDir := ts.MakePluginDir().InstallPluginVersions() 148 defer pluginDir.Cleanup() 149 150 ts.PackerCommand().UsePluginDir(pluginDir). 151 SetArgs("build", "./templates/internal_only.pkr.hcl"). 152 Assert(check.MustSucceed(), CheckPBUsed(true)) 153 } 154 155 // One plugin with gob only installed, use only internal components 156 // 157 // Packer in this case will fallback to Gob, even if the template uses internal 158 // components only, as this is determined at loading time. 159 func (ts PackerGobTestSuite) TestInternalOnlyWithGobPluginInstalled() { 160 pluginDir := ts.MakePluginDir().InstallPluginVersions("1.0.0+gob") 161 defer pluginDir.Cleanup() 162 163 ts.PackerCommand().UsePluginDir(pluginDir). 164 SetArgs("build", "./templates/internal_only.pkr.hcl"). 165 Assert(check.MustSucceed(), CheckPBUsed(false)) 166 } 167 168 // One plugin with pb support installed, use only internal components 169 // 170 // Packer in this case will fallback to Gob, even if the template uses internal 171 // components only, as this is determined at loading time. 172 func (ts PackerGobTestSuite) TestInternalOnlyWithPBPluginInstalled() { 173 pluginDir := ts.MakePluginDir().InstallPluginVersions("1.0.0+pb") 174 defer pluginDir.Cleanup() 175 176 ts.PackerCommand().UsePluginDir(pluginDir). 177 SetArgs("build", "./templates/internal_only.pkr.hcl"). 178 Assert(check.MustSucceed(), CheckPBUsed(true)) 179 }