tests: integration suite
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
// Package harness provides reusable black-box integration helpers shared by
|
||||
// inter-service suites.
|
||||
package harness
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var binaryCache struct {
|
||||
mu sync.Mutex
|
||||
paths map[string]string
|
||||
}
|
||||
|
||||
// BuildBinary builds packagePath once per test process and returns the
|
||||
// resulting executable path.
|
||||
func BuildBinary(t testing.TB, name string, packagePath string) string {
|
||||
t.Helper()
|
||||
|
||||
root := repositoryRoot(t)
|
||||
key := name + ":" + packagePath
|
||||
|
||||
binaryCache.mu.Lock()
|
||||
if binaryCache.paths == nil {
|
||||
binaryCache.paths = make(map[string]string)
|
||||
}
|
||||
if path, ok := binaryCache.paths[key]; ok {
|
||||
binaryCache.mu.Unlock()
|
||||
return path
|
||||
}
|
||||
|
||||
outputDir := filepath.Join(os.TempDir(), "galaxy-integration-binaries")
|
||||
if err := os.MkdirAll(outputDir, 0o755); err != nil {
|
||||
binaryCache.mu.Unlock()
|
||||
t.Fatalf("create integration binary directory: %v", err)
|
||||
}
|
||||
|
||||
outputPath := filepath.Join(outputDir, sanitizeBinaryName(key))
|
||||
cmd := exec.Command("go", "build", "-o", outputPath, packagePath)
|
||||
cmd.Dir = root
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
binaryCache.mu.Unlock()
|
||||
t.Fatalf("build %s: %v\n%s", packagePath, err, output)
|
||||
}
|
||||
|
||||
binaryCache.paths[key] = outputPath
|
||||
binaryCache.mu.Unlock()
|
||||
return outputPath
|
||||
}
|
||||
|
||||
func repositoryRoot(t testing.TB) string {
|
||||
t.Helper()
|
||||
|
||||
_, file, _, ok := runtime.Caller(0)
|
||||
if !ok {
|
||||
t.Fatal("resolve harness repository root: runtime caller is unavailable")
|
||||
}
|
||||
|
||||
return filepath.Clean(filepath.Join(filepath.Dir(file), "..", "..", ".."))
|
||||
}
|
||||
|
||||
func sanitizeBinaryName(value string) string {
|
||||
replacer := strings.NewReplacer("/", "_", "\\", "_", ":", "_", ".", "_")
|
||||
return replacer.Replace(value)
|
||||
}
|
||||
Reference in New Issue
Block a user