| 150 | } |
| 151 | |
| 152 | func binCmd(ctx context.Context, t *testing.T, bin string, oom, nice int) (*exec.Cmd, string) { |
| 153 | var ( |
| 154 | args = execArgs(oom, nice) |
| 155 | dir = t.TempDir() |
| 156 | file = filepath.Join(dir, "sentinel") |
| 157 | ) |
| 158 | |
| 159 | args = append(args, "sh", "-c", fmt.Sprintf("touch %s && sleep 10m", file)) |
| 160 | //nolint:gosec |
| 161 | cmd := exec.CommandContext(ctx, bin, args...) |
| 162 | |
| 163 | // We set this so we can also easily kill the sleep process the shell spawns. |
| 164 | cmd.SysProcAttr = &syscall.SysProcAttr{ |
| 165 | Setpgid: true, |
| 166 | } |
| 167 | |
| 168 | cmd.Env = os.Environ() |
| 169 | var buf bytes.Buffer |
| 170 | cmd.Stdout = &buf |
| 171 | cmd.Stderr = &buf |
| 172 | t.Cleanup(func() { |
| 173 | // Print output of a command if the test fails. |
| 174 | if t.Failed() { |
| 175 | t.Logf("cmd %q output: %s", cmd.Args, buf.String()) |
| 176 | } |
| 177 | if cmd.Process != nil { |
| 178 | // We use -cmd.Process.Pid to kill the whole process group. |
| 179 | _ = syscall.Kill(-cmd.Process.Pid, syscall.SIGINT) |
| 180 | } |
| 181 | }) |
| 182 | return cmd, file |
| 183 | } |
| 184 | |
| 185 | func cmd(ctx context.Context, t *testing.T, oom, nice int) (*exec.Cmd, string) { |
| 186 | return binCmd(ctx, t, TestBin, oom, nice) |