github.com/hashicorp/packer@v1.14.3/packer_test/plugin_tests/loading_test.go (about) 1 package plugin_tests 2 3 import ( 4 "crypto/sha256" 5 "fmt" 6 "os" 7 "path/filepath" 8 9 "github.com/hashicorp/packer/packer_test/common" 10 "github.com/hashicorp/packer/packer_test/common/check" 11 ) 12 13 func (ts *PackerPluginTestSuite) TestLoadingOrder() { 14 pluginDir := ts.MakePluginDir().InstallPluginVersions("1.0.9", "1.0.10") 15 defer pluginDir.Cleanup() 16 17 for _, command := range []string{"build", "validate"} { 18 tests := []struct { 19 name string 20 templatePath string 21 grepStr string 22 }{ 23 { 24 "HCL2 " + command + " - With required_plugins, 1.0.10 is the most recent and should load", 25 "./templates/simple.pkr.hcl", 26 "packer-plugin-tester_v1\\.0\\.10[^\n]+ plugin:", 27 }, 28 { 29 "JSON " + command + " - No required_plugins, 1.0.10 is the most recent and should load", 30 "./templates/simple.json", 31 "packer-plugin-tester_v1\\.0\\.10[^\n]+ plugin:", 32 }, 33 { 34 "HCL2 " + command + " - With required_plugins, 1.0.9 is pinned, so 1.0.9 should be used", 35 "./templates/pin_1.0.9.pkr.hcl", 36 "packer-plugin-tester_v1\\.0\\.9[^\n]+ plugin:", 37 }, 38 } 39 40 for _, tt := range tests { 41 ts.Run(tt.name, func() { 42 ts.PackerCommand(). 43 SetArgs(command, tt.templatePath). 44 UsePluginDir(pluginDir). 45 Assert(check.MustSucceed(), check.Grep(tt.grepStr)) 46 }) 47 } 48 } 49 } 50 51 func (ts *PackerPluginTestSuite) TestLoadWithLegacyPluginName() { 52 pluginDir := ts.MakePluginDir() 53 defer pluginDir.Cleanup() 54 55 plugin := ts.GetPluginPath(ts.T(), "1.0.10") 56 57 common.CopyFile(ts.T(), filepath.Join(pluginDir.Dir(), "packer-plugin-tester"), plugin) 58 59 ts.Run("only legacy plugins installed: expect build to fail", func() { 60 ts.Run("with required_plugins - expect prompt for packer init", func() { 61 ts.PackerCommand().UsePluginDir(pluginDir). 62 SetArgs("build", "templates/simple.pkr.hcl"). 63 Assert(check.MustFail(), 64 check.Grep("Did you run packer init for this project", check.GrepStdout), 65 check.Grep("following plugins are required", check.GrepStdout)) 66 }) 67 68 ts.Run("JSON template, without required_plugins: should say the component is unknown", func() { 69 ts.PackerCommand().UsePluginDir(pluginDir). 70 SetArgs("build", "templates/simple.json"). 71 Assert(check.MustFail(), 72 check.Grep("The builder tester-dynamic is unknown by Packer", check.GrepStdout)) 73 }) 74 }) 75 76 pluginDir = ts.MakePluginDir().InstallPluginVersions("1.0.0") 77 defer pluginDir.Cleanup() 78 79 common.CopyFile(ts.T(), filepath.Join(pluginDir.Dir(), "packer-plugin-tester"), plugin) 80 81 ts.Run("multiple plugins installed: one with no version in path, one with qualified name. Should pick-up the qualified one only.", func() { 82 ts.PackerCommand().UsePluginDir(pluginDir). 83 SetArgs("build", "templates/simple.pkr.hcl"). 84 Assert(check.MustSucceed(), check.Grep("packer-plugin-tester_v1\\.0\\.0[^\\n]+ plugin:", check.GrepStderr)) 85 }) 86 87 wd, cleanup := common.TempWorkdir(ts.T(), "./templates/simple.pkr.hcl") 88 defer cleanup() 89 90 common.CopyFile(ts.T(), filepath.Join(wd, "packer-plugin-tester"), plugin) 91 92 ts.Run("multiple plugins installed: 1.0.0 in plugin dir with sum, one in workdir (no version). Should load 1.0.0", func() { 93 ts.PackerCommand().UsePluginDir(pluginDir).SetWD(wd). 94 SetArgs("build", "simple.pkr.hcl"). 95 Assert(check.MustSucceed(), check.Grep("packer-plugin-tester_v1\\.0\\.0[^\\n]+ plugin:", check.GrepStderr)) 96 }) 97 } 98 99 func (ts *PackerPluginTestSuite) TestLoadWithSHAMismatches() { 100 plugin := ts.GetPluginPath(ts.T(), "1.0.10") 101 102 ts.Run("move plugin with right name, but no SHA256SUM, should reject", func() { 103 pluginDir := ts.MakePluginDir().InstallPluginVersions("1.0.9") 104 defer pluginDir.Cleanup() 105 106 pluginDestName := common.ExpectedInstalledName("1.0.10") 107 108 common.CopyFile(ts.T(), filepath.Join(pluginDir.Dir(), "github.com", "hashicorp", "tester", pluginDestName), plugin) 109 110 ts.PackerCommand().UsePluginDir(pluginDir). 111 SetArgs("plugins", "installed"). 112 Assert(check.MustSucceed(), 113 check.Grep("packer-plugin-tester_v1\\.0\\.9[^\\n]+", check.GrepStdout), 114 check.GrepInverted("packer-plugin-tester_v1.0.10", check.GrepStdout), 115 check.Grep("v1.0.10[^\\n]+ignoring possibly unsafe binary", check.GrepStderr)) 116 }) 117 118 ts.Run("move plugin with right name, invalid SHA256SUM, should reject", func() { 119 pluginDir := ts.MakePluginDir().InstallPluginVersions("1.0.9") 120 defer pluginDir.Cleanup() 121 122 pluginDestName := common.ExpectedInstalledName("1.0.10") 123 common.CopyFile(ts.T(), filepath.Join(pluginDir.Dir(), "github.com", "hashicorp", "tester", pluginDestName), plugin) 124 125 common.WriteFile(ts.T(), 126 filepath.Join(pluginDir.Dir(), "github.com", "hashicorp", "tester", fmt.Sprintf("%s_SHA256SUM", pluginDestName)), 127 fmt.Sprintf("%x", sha256.New().Sum([]byte("Not the plugin's contents for sure.")))) 128 129 ts.PackerCommand().UsePluginDir(pluginDir). 130 SetArgs("plugins", "installed"). 131 Assert(check.MustSucceed(), 132 check.Grep("packer-plugin-tester_v1\\.0\\.9[^\\n]+", check.GrepStdout), 133 check.GrepInverted("packer-plugin-tester_v1.0.10", check.GrepStdout), 134 check.Grep("v1.0.10[^\\n]+ignoring possibly unsafe binary", check.GrepStderr), 135 check.Grep(`Checksums \(\*sha256\.[dD]igest\) did not match.`, check.GrepStderr)) 136 }) 137 } 138 139 func (ts *PackerPluginTestSuite) TestPluginPathEnvvarWithMultiplePaths() { 140 pluginDirOne := ts.MakePluginDir().InstallPluginVersions("1.0.10") 141 defer pluginDirOne.Cleanup() 142 143 pluginDirTwo := ts.MakePluginDir().InstallPluginVersions("1.0.9") 144 defer pluginDirTwo.Cleanup() 145 146 pluginDirVal := fmt.Sprintf("%s%c%s", pluginDirOne.Dir(), os.PathListSeparator, pluginDirTwo.Dir()) 147 ts.Run("load plugin with two dirs - not supported anymore, should error", func() { 148 ts.PackerCommand().UseRawPluginDir(pluginDirVal). 149 SetArgs("plugins", "installed"). 150 Assert(check.MustFail(), 151 check.Grep("Multiple paths are no longer supported for PACKER_PLUGIN_PATH"), 152 check.MkPipeCheck("All envvars are suggested", 153 check.PipeGrep(`\* PACKER_PLUGIN_PATH=`), 154 check.LineCount()). 155 SetStream(check.OnlyStderr). 156 SetTester(check.IntCompare(check.Eq, 2))) 157 }) 158 } 159 160 func (ts *PackerPluginTestSuite) TestInstallNonCanonicalPluginVersion() { 161 pluginPath := ts.MakePluginDir() 162 defer pluginPath.Cleanup() 163 164 common.ManualPluginInstall(ts.T(), 165 filepath.Join(pluginPath.Dir(), "github.com", "hashicorp", "tester"), 166 ts.GetPluginPath(ts.T(), "1.0.10"), 167 "001.00.010") 168 169 ts.Run("try listing plugins with non-canonical version installed - report none", func() { 170 ts.PackerCommand().UsePluginDir(pluginPath). 171 SetArgs("plugins", "installed"). 172 Assert(check.MustSucceed(), 173 check.Grep(`version .* in path is non canonical`, check.GrepStderr), 174 check.MkPipeCheck("no output in stdout").SetTester(check.ExpectEmptyInput()).SetStream(check.OnlyStdout)) 175 }) 176 } 177 178 func (ts *PackerPluginTestSuite) TestLoadPluginWithMetadataInName() { 179 pluginPath := ts.MakePluginDir() 180 defer pluginPath.Cleanup() 181 182 common.ManualPluginInstall(ts.T(), 183 filepath.Join(pluginPath.Dir(), "github.com", "hashicorp", "tester"), 184 ts.GetPluginPath(ts.T(), "1.0.10+metadata"), 185 "1.0.10+metadata") 186 187 ts.Run("try listing plugins with metadata in name - report none", func() { 188 ts.PackerCommand().UsePluginDir(pluginPath). 189 SetArgs("plugins", "installed"). 190 Assert(check.MustSucceed(), 191 check.Grep("found version .* with metadata in the name", check.GrepStderr), 192 check.MkPipeCheck("no output in stdout").SetTester(check.ExpectEmptyInput()).SetStream(check.OnlyStdout)) 193 }) 194 } 195 196 func (ts *PackerPluginTestSuite) TestLoadWithOnlyReleaseFlag() { 197 pluginPath := ts.MakePluginDir().InstallPluginVersions("1.0.0", "1.0.1-dev") 198 defer pluginPath.Cleanup() 199 200 for _, cmd := range []string{"validate", "build"} { 201 ts.Run(fmt.Sprintf("run %s without --ignore-prerelease flag - pick 1.0.1-dev by default", cmd), func() { 202 ts.PackerCommand().UsePluginDir(pluginPath). 203 SetArgs(cmd, "./templates/simple.pkr.hcl"). 204 Assert(check.MustSucceed(), 205 check.Grep("packer-plugin-tester_v1.0.1-dev.*: plugin process exited", check.GrepStderr)) 206 }) 207 208 ts.Run(fmt.Sprintf("run %s with --ignore-prerelease flag - pick 1.0.0", cmd), func() { 209 ts.PackerCommand().UsePluginDir(pluginPath). 210 SetArgs(cmd, "--ignore-prerelease-plugins", "./templates/simple.pkr.hcl"). 211 Assert(check.MustSucceed(), 212 check.Grep("packer-plugin-tester_v1.0.0.*: plugin process exited", check.GrepStderr)) 213 }) 214 } 215 } 216 217 func (ts *PackerPluginTestSuite) TestWithLegacyConfigAndComponents() { 218 pluginDir := ts.MakePluginDir().InstallPluginVersions("1.0.0") 219 defer pluginDir.Cleanup() 220 221 workdir, cleanup := common.TempWorkdir(ts.T(), "./sample_config.json", "./templates/simple.json", "./templates/simple.pkr.hcl") 222 defer cleanup() 223 224 for _, cmd := range []string{"validate", "build"} { 225 ts.Run(fmt.Sprintf("%s simple JSON template with config.json and components defined", cmd), func() { 226 ts.PackerCommand().UsePluginDir(pluginDir).SetWD(workdir). 227 SetArgs(cmd, "simple.json"). 228 AddEnv("PACKER_CONFIG", filepath.Join(workdir, "sample_config.json")). 229 Assert(check.MustFail(), 230 check.Grep("Your configuration file describes some legacy components", check.GrepStderr), 231 check.Grep("packer-provisioner-super-shell", check.GrepStderr)) 232 }) 233 234 ts.Run(fmt.Sprintf("%s simple HCL2 template with config.json and components defined", cmd), func() { 235 ts.PackerCommand().UsePluginDir(pluginDir).SetWD(workdir). 236 SetArgs(cmd, "simple.pkr.hcl"). 237 AddEnv("PACKER_CONFIG", filepath.Join(workdir, "sample_config.json")). 238 Assert(check.MustFail(), 239 check.Grep("Your configuration file describes some legacy components", check.GrepStderr), 240 check.Grep("packer-provisioner-super-shell", check.GrepStderr)) 241 }) 242 } 243 }