|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License 2.0; |
| 3 | +// you may not use this file except in compliance with the Elastic License 2.0. |
| 4 | + |
| 5 | +package process |
| 6 | + |
| 7 | +import ( |
| 8 | + "bufio" |
| 9 | + "context" |
| 10 | + "io" |
| 11 | + "os" |
| 12 | + "path/filepath" |
| 13 | + "runtime" |
| 14 | + "strings" |
| 15 | + "syscall" |
| 16 | + "testing" |
| 17 | + "time" |
| 18 | +) |
| 19 | + |
| 20 | +// TestGetCmdAndTerminateCmd ensures the process started by this package |
| 21 | +// can be gracefully terminated. |
| 22 | +// |
| 23 | +// This requires two things: |
| 24 | +// 1. getCmd sets the correct SysProcAttr according to the OS |
| 25 | +// 2. terminateCmd sends the correct signal according to the OS |
| 26 | +// |
| 27 | +// On Linux and MacOS, no attribute needs to be set on SysProcAttr |
| 28 | +// and terminateCmd send SIGTERM. |
| 29 | +// On Windows the process needs the CREATE_NEW_PROCESS_GROUP flag |
| 30 | +// and terminateCmd needs to send the CTRL+BREAK event, which the |
| 31 | +// Go runtime translates into a syscall.SIGINT. |
| 32 | +// |
| 33 | +// If this test is failing, run go test with -v so all the output |
| 34 | +// of the child process is sent to stdout |
| 35 | +func TestGetCmdAndTerminateCmd(t *testing.T) { |
| 36 | + wd, err := os.Getwd() |
| 37 | + if err != nil { |
| 38 | + t.Fatalf("cannot get working directory: %s", err) |
| 39 | + } |
| 40 | + testBinary := filepath.Join(wd, "testsignal", "testsignal") |
| 41 | + |
| 42 | + if runtime.GOOS == "windows" { |
| 43 | + testBinary += ".exe" |
| 44 | + } |
| 45 | + |
| 46 | + ctx, cancel := context.WithTimeout(t.Context(), 15*time.Second) |
| 47 | + t.Cleanup(cancel) |
| 48 | + |
| 49 | + cmd, err := getCmd(ctx, testBinary, nil, os.Getuid(), os.Getgid(), t.Name()) |
| 50 | + if err != nil { |
| 51 | + t.Fatalf("'getCmd' failed: %s", err) |
| 52 | + } |
| 53 | + |
| 54 | + out, err := cmd.StderrPipe() |
| 55 | + if err != nil { |
| 56 | + t.Fatalf("cannot get stderr pipe for child process: %s", err) |
| 57 | + } |
| 58 | + sc := bufio.NewScanner(out) |
| 59 | + |
| 60 | + if err := cmd.Start(); err != nil { |
| 61 | + t.Fatalf("cannot start child process: %s", err) |
| 62 | + } |
| 63 | + |
| 64 | + for sc.Scan() { |
| 65 | + line := sc.Text() |
| 66 | + if testing.Verbose() { |
| 67 | + t.Log("Child process output:", line) |
| 68 | + } |
| 69 | + if strings.Contains(line, "Wait for signal") { |
| 70 | + break |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if err := terminateCmd(cmd.Process); err != nil { |
| 75 | + t.Fatalf("did not expect an error from 'terminateCmd': %s", err) |
| 76 | + } |
| 77 | + |
| 78 | + expectedMsg := "Got signal: " |
| 79 | + if runtime.GOOS == "windows" { |
| 80 | + expectedMsg += syscall.SIGINT.String() |
| 81 | + } else { |
| 82 | + expectedMsg += syscall.SIGTERM.String() |
| 83 | + } |
| 84 | + |
| 85 | + for sc.Scan() { |
| 86 | + line := sc.Text() |
| 87 | + if testing.Verbose() { |
| 88 | + t.Log("Child process output:", line) |
| 89 | + } |
| 90 | + if strings.Contains(line, expectedMsg) { |
| 91 | + break |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Drain the stdout and wait for the child process to exit. |
| 96 | + // Because we called cmd.StderrPipe() we need to drain the |
| 97 | + // pipe before calling cmd.Wait. |
| 98 | + if testing.Verbose() { |
| 99 | + for sc.Scan() { |
| 100 | + line := sc.Text() |
| 101 | + t.Log("Process Output:", line) |
| 102 | + } |
| 103 | + return |
| 104 | + } else { |
| 105 | + _, _ = io.Copy(io.Discard, out) |
| 106 | + } |
| 107 | + |
| 108 | + // Wait to ensure the child process exits successfully |
| 109 | + if err := cmd.Wait(); err != nil { |
| 110 | + t.Fatalf("cmd did not finish successfully: %s", err) |
| 111 | + } |
| 112 | + |
| 113 | + if cmd.ProcessState.ExitCode() != 0 { |
| 114 | + t.Fatalf("process did not finish successfully, exit code: %d", cmd.ProcessState.ExitCode()) |
| 115 | + } |
| 116 | +} |
0 commit comments