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)) } } // 1497/AC4: ExecRunnerClean(0) runs env with only the given words: PATH and HOME, not the process's secret. func TestExecRunnerCleanGivesTheCommandOnlyTheEnvArgument(t *testing.T) { t.Setenv("MESH_TEST_SECRET", "x") r := ExecRunnerClean(0)(context.Background(), []string{"PATH=" + os.Getenv("PATH"), "HOME=/h"}, "env") if r.Status != 0 || !strings.Contains(r.Stdout, "HOME=/h") || !strings.Contains(r.Stdout, "PATH=") || strings.Contains(r.Stdout, "MESH_TEST_SECRET") { t.Errorf("ran = %+v", r) } } // 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) } }