Files
mesh-sdk/go/forgekey/runner_test.go
T

73 lines
2.4 KiB
Go

//go:build integration
package forgekey
import (
"context"
"os"
"strings"
"testing"
"time"
)
func lowered(t *testing.T) {
was := CallTimeout
CallTimeout = 50 * time.Millisecond
t.Cleanup(func() { CallTimeout = was })
}
// 1497/AC1: ExecRunnerFor(0), no deadline: a command longer than the lowered CallTimeout is not cut.
func TestExecRunnerForZeroIsNotCutByCallTimeout(t *testing.T) {
lowered(t)
r := ExecRunnerFor(0)(context.Background(), nil, "sleep", "0.2")
if r.Status != 0 || r.Err != "" {
t.Errorf("ran = %+v, want status 0", r)
}
}
// 1497/AC2: ExecRunner is unchanged: cut at CallTimeout with status 124.
func TestExecRunnerIsStillCutAtCallTimeout(t *testing.T) {
lowered(t)
r := ExecRunner(context.Background(), nil, "sleep", "0.2")
if r.Status != 124 {
t.Errorf("ran = %+v, want status 124", r)
}
}
// 1497/AC3: ExecRunnerFor(0) is cut when its context is cancelled.
func TestExecRunnerForZeroIsCutByItsContext(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
began := time.Now()
r := ExecRunnerFor(0)(ctx, nil, "sleep", "2")
if r.Err != "ended by its step's deadline" {
t.Errorf("Err = %q, want the step's deadline named, not a bound that was never set", r.Err)
}
if r.Status == 0 || time.Since(began) > time.Second {
t.Errorf("ran = %+v after %v, want it cut", r, time.Since(began))
}
}
// Issue 734: run from a process whose own directory was deleted under it (a module whose build a new one replaced), a
// command starts in the temporary directory rather than failing to find where it is.
func TestExecRunnerForStartsInTheTemporaryDirectoryWhenItsOwnIsGone(t *testing.T) {
gone := t.TempDir()
t.Chdir(gone)
if err := os.Remove(gone); err != nil {
t.Fatal(err)
}
r := ExecRunnerFor(0)(context.Background(), nil, "pwd")
if r.Status != 0 || strings.TrimSpace(r.Stdout) != os.TempDir() {
t.Errorf("a command run from a deleted directory = %+v, want it started in %s", r, os.TempDir())
}
}
// 1497/AC5: ExecRunnerFor(0) is unchanged: the env argument is added to the process's environment.
func TestExecRunnerForAddsToTheProcessEnvironment(t *testing.T) {
t.Setenv("MESH_TEST_SECRET", "x")
r := ExecRunnerFor(0)(context.Background(), []string{"HOME=/h"}, "env")
if !strings.Contains(r.Stdout, "MESH_TEST_SECRET=x") || !strings.Contains(r.Stdout, "HOME=/h") {
t.Errorf("ran = %+v", r)
}
}