mirror of
https://github.com/hashicorp/packer.git
synced 2026-09-19 14:31:40 -04:00
Having only one test suite for the whole of Packer makes it harder to segregate between test types, and makes for a longer runtime as no tests run in parallel by default. This commit splits the packer_test suite into several components in order to make extension easier. First we have `lib`: this package embeds the core for running Packer test suites. This ships facilities to build your own test suite for Packer core, and exposes convenience methods and structures for building plugins, packer core, and use it to run a test suite in a temporary directory. Then we have two separate test suites: one for plugins, and one for core itself, the latter of which does not depend on plugins being compiled at all. This sets the stage for more specialised test suites in the future, each of which can run in parallel on different parts of the code.
37 lines
924 B
Go
37 lines
924 B
Go
package lib
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"testing"
|
|
)
|
|
|
|
// BuildTestPacker builds a new Packer binary based on the current state of the repository.
|
|
//
|
|
// If for some reason the binary cannot be built, we will immediately exit with an error.
|
|
func BuildTestPacker(t *testing.T) (string, error) {
|
|
testDir, err := currentDir()
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to compile packer binary: %s", err)
|
|
}
|
|
|
|
packerCoreDir := filepath.Dir(filepath.Dir(testDir))
|
|
|
|
outBin := filepath.Join(os.TempDir(), fmt.Sprintf("packer_core-%d", rand.Int()))
|
|
if runtime.GOOS == "windows" {
|
|
outBin = fmt.Sprintf("%s.exe", outBin)
|
|
}
|
|
|
|
compileCommand := exec.Command("go", "build", "-C", packerCoreDir, "-o", outBin)
|
|
logs, err := compileCommand.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("failed to compile Packer core: %s\ncompilation logs: %s", err, logs)
|
|
}
|
|
|
|
return outBin, nil
|
|
}
|