github.com/hashicorp/packer@v1.14.3/packer_test/common/suite.go (about) 1 package common 2 3 import ( 4 "fmt" 5 "os" 6 "sync" 7 "testing" 8 9 "github.com/stretchr/testify/suite" 10 ) 11 12 type PackerTestSuite struct { 13 suite.Suite 14 // pluginsDirectory is the directory in which plugins are compiled. 15 // 16 // Those binaries are not necessarily meant to be used as-is, but 17 // instead should be used for composing plugin installation directories. 18 pluginsDirectory string 19 // packerPath is the location in which the Packer executable is compiled 20 // 21 // Since we don't necessarily want to manually compile Packer beforehand, 22 // we compile it on demand, and use this executable for the tests. 23 packerPath string 24 // compiledPlugins is the map of each compiled plugin to its path. 25 // 26 // This used to be global, but should be linked to the suite instead, as 27 // we may have multiple suites that exist, each with its own repo of 28 // plugins compiled for the purposes of the test, so as they all run 29 // within the same process space, they should be separate instances. 30 compiledPlugins sync.Map 31 } 32 33 // CompileTestPluginVersions batch compiles a series of plugins 34 func (ts *PackerTestSuite) CompileTestPluginVersions(t *testing.T, versions ...string) { 35 results := []chan CompilationResult{} 36 for _, ver := range versions { 37 results = append(results, ts.CompilePlugin(ver)) 38 } 39 40 Ready(t, results) 41 } 42 43 // SkipNoAcc is a pre-condition that skips the test if the PACKER_ACC environment 44 // variable is unset, or set to "0". 45 // 46 // This allows us to build tests with a potential for long runs (or errors like 47 // rate-limiting), so we can still test them, but only in a longer timeouted 48 // context. 49 func (ts *PackerTestSuite) SkipNoAcc() { 50 acc := os.Getenv("PACKER_ACC") 51 if acc == "" || acc == "0" { 52 ts.T().Logf("Skipping test as `PACKER_ACC` is unset.") 53 ts.T().Skip() 54 } 55 } 56 57 func InitBaseSuite(t *testing.T) (*PackerTestSuite, func()) { 58 ts := &PackerTestSuite{ 59 compiledPlugins: sync.Map{}, 60 } 61 62 tempDir, err := os.MkdirTemp("", "packer-core-acc-test-") 63 if err != nil { 64 panic(fmt.Sprintf("failed to create temporary directory for compiled plugins: %s", err)) 65 } 66 ts.pluginsDirectory = tempDir 67 68 packerPath := os.Getenv("PACKER_CUSTOM_PATH") 69 if packerPath == "" { 70 var err error 71 t.Logf("Building test packer binary...") 72 packerPath, err = BuildTestPacker(t) 73 if err != nil { 74 t.Fatalf("failed to build Packer binary: %s", err) 75 } 76 } 77 ts.packerPath = packerPath 78 t.Logf("Done") 79 80 return ts, func() { 81 err := os.RemoveAll(ts.pluginsDirectory) 82 if err != nil { 83 t.Logf("failed to cleanup directory %q: %s. This will need manual action", ts.pluginsDirectory, err) 84 } 85 86 if os.Getenv("PACKER_CUSTOM_PATH") != "" { 87 return 88 } 89 90 err = os.Remove(ts.packerPath) 91 if err != nil { 92 t.Logf("failed to cleanup compiled packer binary %q: %s. This will need manual action", packerPath, err) 93 } 94 } 95 }