Improve error handling and diagnostics for the audita subprocess

This commit is contained in:
2026-05-04 11:46:57 -05:00
parent 89d7bc75c3
commit 4550987bc0
7 changed files with 124 additions and 18 deletions

View File

@@ -109,6 +109,67 @@ func TestRunTimeout(t *testing.T) {
}
}
func TestRunInheritsParentEnvironmentByDefault(t *testing.T) {
exe, err := os.Executable()
if err != nil {
t.Fatalf("os.Executable() error = %v", err)
}
t.Setenv("GO_WANT_SUBPROCESS_HELPER", "1")
t.Setenv("SUBPROCESS_HELPER_ENV_KEY", "SUBPROCESS_PARENT_VALUE")
t.Setenv("SUBPROCESS_PARENT_VALUE", "inherited-value")
dir := t.TempDir()
stdoutPath := filepath.Join(dir, "stdout.log")
req := RunRequest{
Executable: exe,
Args: []string{"-test.run=TestSubprocessHelper", "--", "printenv"},
StdoutLogPath: stdoutPath,
}
if _, err := Run(context.Background(), req); err != nil {
t.Fatalf("Run() error = %v", err)
}
data, err := os.ReadFile(stdoutPath)
if err != nil {
t.Fatalf("read stdout log: %v", err)
}
if strings.TrimSpace(string(data)) != "inherited-value" {
t.Fatalf("stdout = %q, want inherited-value", strings.TrimSpace(string(data)))
}
}
func TestRunEnvOverridesWinOverInheritedValues(t *testing.T) {
exe, err := os.Executable()
if err != nil {
t.Fatalf("os.Executable() error = %v", err)
}
t.Setenv("GO_WANT_SUBPROCESS_HELPER", "1")
t.Setenv("SUBPROCESS_HELPER_ENV_KEY", "SUBPROCESS_PARENT_VALUE")
t.Setenv("SUBPROCESS_PARENT_VALUE", "parent-value")
dir := t.TempDir()
stdoutPath := filepath.Join(dir, "stdout.log")
req := RunRequest{
Executable: exe,
Args: []string{"-test.run=TestSubprocessHelper", "--", "printenv"},
EnvOverrides: map[string]string{"SUBPROCESS_PARENT_VALUE": "override-value"},
StdoutLogPath: stdoutPath,
}
if _, err := Run(context.Background(), req); err != nil {
t.Fatalf("Run() error = %v", err)
}
data, err := os.ReadFile(stdoutPath)
if err != nil {
t.Fatalf("read stdout log: %v", err)
}
if strings.TrimSpace(string(data)) != "override-value" {
t.Fatalf("stdout = %q, want override-value", strings.TrimSpace(string(data)))
}
}
func TestWriteYAMLAtomic(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "config.generated.yml")
@@ -190,6 +251,10 @@ func TestSubprocessHelper(t *testing.T) {
case "sleep":
time.Sleep(500 * time.Millisecond)
os.Exit(0)
case "printenv":
key := os.Getenv("SUBPROCESS_HELPER_ENV_KEY")
_, _ = os.Stdout.WriteString(os.Getenv(key) + "\n")
os.Exit(0)
default:
os.Exit(2)
}