diff --git a/cmd/zapret-autotune/blockcheck_test.go b/cmd/zapret-autotune/blockcheck_test.go index 04f8ace..d7c2c23 100644 --- a/cmd/zapret-autotune/blockcheck_test.go +++ b/cmd/zapret-autotune/blockcheck_test.go @@ -1,6 +1,7 @@ package main import ( + "bufio" "bytes" "context" "encoding/json" @@ -29,15 +30,24 @@ func init() { if mode == "orphan" { if len(os.Args) == 2 && os.Args[1] == "--child" { signal.Ignore(syscall.SIGTERM) + fmt.Fprintln(os.Stdout, os.Getpid()) for { time.Sleep(time.Hour) } } child := exec.Command(os.Args[0], "--child") + ready, err := child.StdoutPipe() + if err != nil { + os.Exit(3) + } if err := child.Start(); err != nil { os.Exit(3) } - fmt.Fprintln(os.Stdout, child.Process.Pid) + line, err := bufio.NewReader(ready).ReadString('\n') + if err != nil { + os.Exit(3) + } + fmt.Fprint(os.Stdout, line) os.Exit(0) } if len(os.Args) == 2 && os.Args[1] == "--child" { @@ -380,6 +390,22 @@ func TestBlockcheckCleansWorkersAfterSuccessfulLeader(t *testing.T) { } } +func TestDaemonCommandRetainsSuccessfulChild(t *testing.T) { + binary := fakeBlockcheck(t, "orphan") + var stdout bytes.Buffer + if err := runBlockcheckCommand(context.Background(), binary, "", nil, &stdout, io.Discard); err != nil { + t.Fatal(err) + } + pid, err := strconv.Atoi(strings.TrimSpace(stdout.String())) + if err != nil { + t.Fatal(err) + } + defer syscall.Kill(pid, syscall.SIGKILL) + if err := syscall.Kill(pid, 0); err != nil { + t.Fatalf("daemon unexpectedly killed: %v", err) + } +} + func (w processReady) Write(p []byte) (int, error) { pid, err := strconv.Atoi(strings.TrimSpace(string(p))) if err != nil {