|
1 | 1 | package sandbox |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bufio" |
4 | 5 | "bytes" |
| 6 | + "context" |
5 | 7 | "encoding/json" |
| 8 | + "fmt" |
| 9 | + "io" |
6 | 10 | "net" |
7 | 11 | "os" |
| 12 | + "os/exec" |
8 | 13 | "path/filepath" |
9 | 14 | "runtime" |
10 | 15 | "strconv" |
11 | 16 | "testing" |
| 17 | + "time" |
12 | 18 |
|
13 | 19 | "github.com/stretchr/testify/assert" |
14 | 20 | "github.com/stretchr/testify/require" |
15 | 21 | ) |
16 | 22 |
|
| 23 | +func TestServerOutputInterruptsInheritedStdout(t *testing.T) { |
| 24 | + if runtime.GOOS != "linux" { |
| 25 | + t.Skip("Linux sandbox command output uses /proc/self/fd") |
| 26 | + } |
| 27 | + const marker = "server output write interrupted\n" |
| 28 | + if os.Getenv("DATABRICKS_TEST_SERVER_OUTPUT") == "1" { |
| 29 | + output, err := openServerOutput(os.Stdout) |
| 30 | + require.NoError(t, err) |
| 31 | + defer output.Close() |
| 32 | + file := output.(*os.File) |
| 33 | + require.NoError(t, file.SetWriteDeadline(time.Now().Add(100*time.Millisecond))) |
| 34 | + _, err = file.Write(make([]byte, 16<<20)) |
| 35 | + require.ErrorIs(t, err, os.ErrDeadlineExceeded) |
| 36 | + fmt.Fprint(os.Stderr, marker) |
| 37 | + return |
| 38 | + } |
| 39 | + executable, err := os.Executable() |
| 40 | + require.NoError(t, err) |
| 41 | + ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second) |
| 42 | + defer cancel() |
| 43 | + cmd := exec.CommandContext(ctx, executable, "-test.run=^TestServerOutputInterruptsInheritedStdout$") |
| 44 | + cmd.Env = append(os.Environ(), "DATABRICKS_TEST_SERVER_OUTPUT=1") |
| 45 | + stdout, err := cmd.StdoutPipe() |
| 46 | + require.NoError(t, err) |
| 47 | + stderr, err := cmd.StderrPipe() |
| 48 | + require.NoError(t, err) |
| 49 | + require.NoError(t, cmd.Start()) |
| 50 | + t.Cleanup(func() { |
| 51 | + _ = cmd.Process.Kill() |
| 52 | + _ = cmd.Wait() |
| 53 | + }) |
| 54 | + // Keep stdout full until the child's write has been interrupted. |
| 55 | + line, err := bufio.NewReader(stderr).ReadString('\n') |
| 56 | + require.NoError(t, err) |
| 57 | + require.Equal(t, marker, line) |
| 58 | + _, err = io.Copy(io.Discard, stdout) |
| 59 | + require.NoError(t, err) |
| 60 | + require.NoError(t, cmd.Wait()) |
| 61 | +} |
| 62 | + |
17 | 63 | func TestServerCommandValidatesArgumentsWithoutAuthentication(t *testing.T) { |
18 | 64 | for _, args := range [][]string{ |
19 | 65 | {"--sshd-config", "config", "--port", "22"}, |
|
0 commit comments