tests: integration suite

This commit is contained in:
IliaDenisov
2026-04-09 15:27:14 +02:00
parent e04fc663f0
commit 1c8e0ca48e
20 changed files with 2748 additions and 10 deletions
+71
View File
@@ -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)
}