mirror of
https://github.com/hashicorp/packer.git
synced 2026-05-19 08:25:21 -04:00
The lib name for the common components for writing packer_test suites was not clear, and did not follow the convention established in Packer core and plugins. Therefore this commit does two things: first the lib is renamed into common as to follow this convention, and clearly document which components are common to all tests. Also checkers are placed in a subpackage of common, common/check, so that it is clearer what is meant to be used as checks for a command's execution status after it's been run, as part of Assert.
36 lines
927 B
Go
36 lines
927 B
Go
package common
|
|
|
|
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
|
|
}
|