Files
ThothII/tools/tht/internal/compose/runner_test.go
T

143 lines
4.6 KiB
Go

package compose
import (
"context"
"errors"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"github.com/aritmolab/thothii/tools/tht/internal/config"
)
func TestRunnerPassesEachArgumentWithoutShellSplitting(t *testing.T) {
t.Parallel()
runner := NewRunner(writeExecutable(t, "#!/bin/sh\nprintf '<%s>\\n' \"$@\"\ncat\n"))
result, err := runner.Run(context.Background(), []string{"compose", "--project-directory", "/tmp/a project with spaces", "config"}, strings.NewReader("stdin value\n"))
if err != nil {
t.Fatalf("Run() error = %v", err)
}
want := "<compose>\n<--project-directory>\n</tmp/a project with spaces>\n<config>\nstdin value\n"
if result.Stdout != want {
t.Errorf("stdout = %q, want %q", result.Stdout, want)
}
if result.ExitCode != 0 {
t.Errorf("ExitCode = %d, want 0", result.ExitCode)
}
}
func TestRunnerBoundsFloodingOutputDuringCapture(t *testing.T) {
t.Parallel()
runner := NewRunner(writeExecutable(t, "#!/bin/sh\nwhile :; do printf '0123456789abcdef'; printf 'fedcba9876543210' >&2; done\n"))
started := time.Now()
result, err := RunBounded(runner, context.Background(), []string{"compose", "run", "--rm", "core"}, nil, CaptureLimits{
StdoutBytes: 1024,
StderrBytes: 1024,
})
if !errors.Is(err, ErrOutputLimit) {
t.Fatalf("RunBounded() error = %v, want ErrOutputLimit", err)
}
if len(result.Stdout) > 1024 || len(result.Stderr) > 1024 {
t.Fatalf("captured output exceeded limits: stdout=%d stderr=%d", len(result.Stdout), len(result.Stderr))
}
if elapsed := time.Since(started); elapsed > 5*time.Second {
t.Fatalf("overflow teardown took %s", elapsed)
}
}
func TestRunnerCancelsAndReapsAHangingChildWithinFinalBound(t *testing.T) {
t.Parallel()
runner := NewRunner(writeExecutable(t, "#!/bin/sh\ntrap '' TERM INT\nwhile :; do sleep 1; done\n"))
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
started := time.Now()
_, err := RunBounded(runner, ctx, []string{"compose", "run", "--rm", "core"}, nil, CaptureLimits{
StdoutBytes: 1024,
StderrBytes: 1024,
})
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("RunBounded() error = %v, want deadline exceeded", err)
}
if elapsed := time.Since(started); elapsed > 5*time.Second {
t.Fatalf("cancellation teardown took %s", elapsed)
}
}
func TestRunnerReturnsTheChildExitCode(t *testing.T) {
t.Parallel()
runner := NewRunner(writeExecutable(t, "#!/bin/sh\necho unavailable >&2\nexit 42\n"))
result, err := runner.Run(context.Background(), []string{"compose", "ps"}, nil)
if err == nil {
t.Fatal("Run() error = nil, want child exit error")
}
if result.ExitCode != 42 {
t.Errorf("ExitCode = %d, want 42", result.ExitCode)
}
if result.Stderr != "unavailable\n" {
t.Errorf("stderr = %q, want unavailable output", result.Stderr)
}
}
func TestRunnerReportsMissingDocker(t *testing.T) {
t.Parallel()
runner := NewRunner(filepath.Join(t.TempDir(), "docker-does-not-exist"))
result, err := runner.Run(context.Background(), []string{"compose", "version"}, nil)
if !errors.Is(err, exec.ErrNotFound) {
t.Fatalf("Run() error = %v, want exec.ErrNotFound", err)
}
if result.ExitCode != 127 {
t.Errorf("ExitCode = %d, want 127", result.ExitCode)
}
}
func TestInstallationRunnerAppliesComposeArgumentsWithoutRewritingHostDockerCommands(t *testing.T) {
t.Parallel()
recorded := &recordingRunner{}
installation := config.Installation{
Path: "/tmp/thothii/deploy/local/thothii-installation.yaml",
Profile: "local",
ProjectDirectory: "/tmp/thothii",
EnvFile: "/tmp/thothii/deploy/local/operator.env",
}
runner := InstallationRunner{Installation: installation, Runner: recorded}
if _, err := runner.Run(context.Background(), []string{"compose", "ps"}, nil); err != nil {
t.Fatal(err)
}
if _, err := runner.Run(context.Background(), []string{"inspect", "core-id"}, nil); err != nil {
t.Fatal(err)
}
if got, want := strings.Join(recorded.calls[0], " "), strings.Join(installation.ComposeArgs("ps"), " "); got != want {
t.Fatalf("Compose call = %q, want %q", got, want)
}
if got, want := strings.Join(recorded.calls[1], " "), "inspect core-id"; got != want {
t.Fatalf("host Docker call = %q, want %q", got, want)
}
}
type recordingRunner struct{ calls [][]string }
func (r *recordingRunner) Run(_ context.Context, args []string, _ io.Reader) (Result, error) {
r.calls = append(r.calls, append([]string(nil), args...))
return Result{}, nil
}
func writeExecutable(t *testing.T, contents string) string {
t.Helper()
path := filepath.Join(t.TempDir(), "fake-docker")
if err := os.WriteFile(path, []byte(contents), 0o700); err != nil {
t.Fatal(err)
}
return path
}