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

103 lines
3.2 KiB
Go

package compose
import (
"context"
"errors"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"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 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
}