Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
Bryan Frimin
2025-12-01 12:17:54 +01:00
parent db3c3882d1
commit 4410fd0950
6 changed files with 0 additions and 183 deletions

View File

@@ -12,33 +12,6 @@
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
// Package testutil provides end-to-end testing infrastructure for the Probo API.
//
// This package runs an external probod binary for realistic e2e testing.
// It supports coverage collection when using a coverage-instrumented binary.
//
// Required environment variables:
// - PROBO_E2E_BINARY: Path to the probod binary
// - PROBO_E2E_CONFIG: Path to the config file
//
// Optional environment variables:
// - PROBO_E2E_COVERDIR: Directory for coverage data (enables coverage collection)
// - PROBO_E2E_VERBOSE: If set, outputs binary stdout/stderr for debugging
//
// Example usage:
//
// # Build the binary (with coverage)
// go build -cover -o bin/probod-coverage ./cmd/probod
//
// # Run e2e tests
// PROBO_E2E_BINARY=./bin/probod-coverage \
// PROBO_E2E_COVERDIR=./coverage/e2e \
// PROBO_E2E_CONFIG=./e2e/console/testdata/config.yaml \
// go test -v ./e2e/console/...
//
// # Generate coverage report
// go tool covdata textfmt -i=./coverage/e2e -o=coverage-e2e.out
// go tool cover -html=coverage-e2e.out -o=coverage-e2e.html
package testutil
import (
@@ -58,24 +31,12 @@ var (
setupOnce sync.Once
)
// TestEnv holds the test environment state
type TestEnv struct {
BaseURL string
cmd *exec.Cmd
done chan error
}
// Setup initializes the test environment. Call this from TestMain.
// It starts probod with the provided configuration and waits for it to be ready.
//
// Example:
//
// func TestMain(m *testing.M) {
// testutil.Setup()
// code := m.Run()
// testutil.Teardown()
// os.Exit(code)
// }
func Setup() {
setupOnce.Do(func() {
binaryPath := os.Getenv("PROBO_E2E_BINARY")
@@ -104,11 +65,6 @@ func Setup() {
done: make(chan error, 1),
}
// Start the external binary
// Note: We use exec.Command instead of exec.CommandContext because
// CommandContext sends SIGKILL on context cancel, which prevents the
// binary from writing coverage data. We manage the process lifecycle
// manually in Teardown() using SIGTERM for graceful shutdown.
cmd := exec.Command(binaryPath, "-cfg-file", configPath)
if coverDir != "" {
cmd.Env = append(os.Environ(), "GOCOVERDIR="+coverDir)
@@ -130,16 +86,13 @@ func Setup() {
os.Exit(1)
}
// Wait for process to exit in background
go func() {
err := cmd.Wait()
testEnv.done <- err
}()
// TODO: Parse config file to get actual port
testEnv.BaseURL = "http://localhost:18080"
// Wait for server to be ready
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := waitForServer(ctx, testEnv.BaseURL, 30*time.Second); err != nil {
@@ -179,17 +132,14 @@ func waitForServer(ctx context.Context, baseURL string, timeout time.Duration) e
return fmt.Errorf("server did not become ready within %v", timeout)
}
// Teardown shuts down the test environment. Call this after m.Run() in TestMain.
func Teardown() {
if testEnv == nil {
return
}
if testEnv.cmd != nil && testEnv.cmd.Process != nil {
// Send SIGTERM for graceful shutdown (allows coverage data to be written)
testEnv.cmd.Process.Signal(syscall.SIGTERM)
// Wait for graceful shutdown with timeout
select {
case <-testEnv.done:
case <-time.After(10 * time.Second):
@@ -199,7 +149,6 @@ func Teardown() {
}
}
// GetBaseURL returns the base URL of the test server
func GetBaseURL() string {
if testEnv == nil {
return "http://localhost:8080"