| 1 | |
| 2 | |
| 3 | package workbench |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "errors" |
| 8 | "os" |
| 9 | "os/exec" |
| 10 | "path/filepath" |
| 11 | "runtime" |
| 12 | "strconv" |
| 13 | "strings" |
| 14 | "sync" |
| 15 | "sync/atomic" |
| 16 | "syscall" |
| 17 | "testing" |
| 18 | "time" |
| 19 | ) |
| 20 | |
| 21 | |
| 22 | func localGuestSession(t *testing.T) (run func(context.Context, string) (guestSessionReply, bool, error), session *guestSession, starts *atomic.Int32) { |
| 23 | t.Helper() |
| 24 | if runtime.GOOS == "windows" { |
| 25 | t.Skip("the control session serves Linux guests") |
| 26 | } |
| 27 | for _, name := range []string{"python3", "bash"} { |
| 28 | if _, err := exec.LookPath(name); err != nil { |
| 29 | t.Skip(name + " is unavailable") |
| 30 | } |
| 31 | } |
| 32 | lifetime, cancel := context.WithCancel(context.Background()) |
| 33 | session, starts = &guestSession{}, &atomic.Int32{} |
| 34 | t.Cleanup(func() { session.close(); cancel() }) |
| 35 | command := func(ctx context.Context) *exec.Cmd { |
| 36 | starts.Add(1) |
| 37 | return exec.CommandContext(ctx, "python3", "-I", "-u", "-c", guestSessionServer) |
| 38 | } |
| 39 | return func(ctx context.Context, script string) (guestSessionReply, bool, error) { |
| 40 | return session.run(ctx, lifetime, command, script) |
| 41 | }, session, starts |
| 42 | } |
| 43 | |
| 44 | func TestGuestSessionRunsScriptsOverOneConnection(t *testing.T) { |
| 45 | run, _, starts := localGuestSession(t) |
| 46 | ctx := context.Background() |
| 47 | reply, sent, err := run(ctx, "printf 'out %s' \"$0\"; printf problem >&2; exit 7") |
| 48 | if err != nil || !sent || reply.code != 7 || string(reply.stdout) != "out /bin/bash" || string(reply.stderr) != "problem" { |
| 49 | t.Fatalf("reply %+v sent=%v err=%v", reply, sent, err) |
| 50 | } |
| 51 | |
| 52 | if reply, _, err = run(ctx, "cd /tmp; export LEFT=1"); err != nil || reply.code != 0 { |
| 53 | t.Fatal(reply, err) |
| 54 | } |
| 55 | if reply, _, err = run(ctx, "printf '%s' \"${LEFT:-unset}\"; test \"$PWD\" != /tmp"); err != nil || reply.code != 0 || string(reply.stdout) != "unset" { |
| 56 | t.Fatalf("state leaked between scripts: %+v %v", reply, err) |
| 57 | } |
| 58 | |
| 59 | if reply, _, err = run(ctx, "cat; printf done"); err != nil || string(reply.stdout) != "done" { |
| 60 | t.Fatalf("stdin was not empty: %+v %v", reply, err) |
| 61 | } |
| 62 | if reply, _, err = run(ctx, "printf '多字节 ✓\\n\\0binary'"); err != nil || string(reply.stdout) != "多字节 ✓\n\x00binary" { |
| 63 | t.Fatalf("output changed in transit: %q %v", reply.stdout, err) |
| 64 | } |
| 65 | if starts.Load() != 1 { |
| 66 | t.Fatalf("%d connections were opened for consecutive scripts", starts.Load()) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func TestGuestSessionSlowScriptDoesNotDelayOthers(t *testing.T) { |
| 71 | run, _, starts := localGuestSession(t) |
| 72 | slow := make(chan error, 1) |
| 73 | go func() { |
| 74 | reply, _, err := run(context.Background(), "sleep 1; printf slow") |
| 75 | if err == nil && string(reply.stdout) != "slow" { |
| 76 | err = errors.New("slow script lost its output") |
| 77 | } |
| 78 | slow <- err |
| 79 | }() |
| 80 | time.Sleep(100 * time.Millisecond) |
| 81 | started := time.Now() |
| 82 | var group sync.WaitGroup |
| 83 | for i := 0; i < 8; i++ { |
| 84 | group.Add(1) |
| 85 | go func(i int) { |
| 86 | defer group.Done() |
| 87 | reply, _, err := run(context.Background(), "printf "+strconv.Itoa(i)) |
| 88 | if err != nil || string(reply.stdout) != strconv.Itoa(i) { |
| 89 | t.Errorf("script %d: %+v %v", i, reply, err) |
| 90 | } |
| 91 | }(i) |
| 92 | } |
| 93 | group.Wait() |
| 94 | if time.Since(started) > 700*time.Millisecond { |
| 95 | t.Fatal("quick scripts waited for the slow one") |
| 96 | } |
| 97 | if err := <-slow; err != nil { |
| 98 | t.Fatal(err) |
| 99 | } |
| 100 | if starts.Load() != 1 { |
| 101 | t.Fatal("concurrent scripts opened more connections") |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestGuestSessionCancellationStopsTheScript(t *testing.T) { |
| 106 | run, _, _ := localGuestSession(t) |
| 107 | file := filepath.Join(t.TempDir(), "pid") |
| 108 | ctx, cancel := context.WithCancel(context.Background()) |
| 109 | done := make(chan error, 1) |
| 110 | go func() { |
| 111 | _, _, err := run(ctx, "sleep 60 & echo $! > "+file+"; wait") |
| 112 | done <- err |
| 113 | }() |
| 114 | var pid int |
| 115 | for deadline := time.Now().Add(3 * time.Second); time.Now().Before(deadline); time.Sleep(10 * time.Millisecond) { |
| 116 | if data, err := os.ReadFile(file); err == nil && len(data) > 1 { |
| 117 | pid, _ = strconv.Atoi(strings.TrimSpace(string(data))) |
| 118 | break |
| 119 | } |
| 120 | } |
| 121 | if pid == 0 { |
| 122 | t.Fatal("script did not start") |
| 123 | } |
| 124 | cancel() |
| 125 | if err := <-done; !errors.Is(err, context.Canceled) { |
| 126 | t.Fatal(err) |
| 127 | } |
| 128 | for deadline := time.Now().Add(3 * time.Second); time.Now().Before(deadline); time.Sleep(10 * time.Millisecond) { |
| 129 | if syscall.Kill(pid, 0) != nil { |
| 130 | break |
| 131 | } |
| 132 | } |
| 133 | if syscall.Kill(pid, 0) == nil { |
| 134 | _ = syscall.Kill(pid, syscall.SIGKILL) |
| 135 | t.Fatal("a cancelled script kept its background process") |
| 136 | } |
| 137 | |
| 138 | if reply, _, err := run(context.Background(), "printf ok"); err != nil || string(reply.stdout) != "ok" { |
| 139 | t.Fatal(reply, err) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | func TestGuestSessionBoundsOutput(t *testing.T) { |
| 144 | run, _, _ := localGuestSession(t) |
| 145 | started := time.Now() |
| 146 | reply, _, err := run(context.Background(), "yes | head -c 100000000; yes x >&2 | true; head -c 100000 /dev/zero >&2") |
| 147 | if err != nil || len(reply.stdout) <= 64<<10 || len(reply.stdout) > 64<<10+256 || len(reply.stderr) > 4096 || reply.code == 0 { |
| 148 | t.Fatalf("stdout=%d stderr=%d code=%d err=%v", len(reply.stdout), len(reply.stderr), reply.code, err) |
| 149 | } |
| 150 | if time.Since(started) > 5*time.Second { |
| 151 | t.Fatal("an oversized answer was not stopped") |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | func TestGuestSessionFallsBackOnlyBeforeTheScriptIsSent(t *testing.T) { |
| 156 | if runtime.GOOS == "windows" { |
| 157 | t.Skip("the control session serves Linux guests") |
| 158 | } |
| 159 | session, starts := &guestSession{}, 0 |
| 160 | missing := func(ctx context.Context) *exec.Cmd { |
| 161 | starts++ |
| 162 | return exec.CommandContext(ctx, filepath.Join(t.TempDir(), "missing")) |
| 163 | } |
| 164 | for i := 0; i < 3; i++ { |
| 165 | if _, sent, err := session.run(context.Background(), context.Background(), missing, "true"); sent || err == nil { |
| 166 | t.Fatal("an unavailable session claimed to run the script") |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | if starts != 1 { |
| 171 | t.Fatalf("an unavailable session was started %d times", starts) |
| 172 | } |
| 173 | |
| 174 | run, session, opened := localGuestSession(t) |
| 175 | done := make(chan error, 1) |
| 176 | go func() { |
| 177 | _, sent, err := run(context.Background(), "sleep 30") |
| 178 | if !sent { |
| 179 | err = errors.New("a script cut off by the connection was reported as unsent") |
| 180 | } |
| 181 | done <- err |
| 182 | }() |
| 183 | time.Sleep(200 * time.Millisecond) |
| 184 | session.close() |
| 185 | if err := <-done; !errors.Is(err, errGuestSessionEnded) { |
| 186 | t.Fatal(err) |
| 187 | } |
| 188 | if reply, sent, err := run(context.Background(), "printf again"); err != nil || !sent || string(reply.stdout) != "again" || opened.Load() != 2 { |
| 189 | t.Fatalf("a closed session did not reopen: %+v %v %v %d", reply, sent, err, opened.Load()) |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | func TestGuestSessionToleratesLoginShellOutputAndRejectsOtherServers(t *testing.T) { |
| 194 | if runtime.GOOS == "windows" { |
| 195 | t.Skip("the control session serves Linux guests") |
| 196 | } |
| 197 | if _, err := exec.LookPath("python3"); err != nil { |
| 198 | t.Skip("python3 is unavailable") |
| 199 | } |
| 200 | noisy := func(ctx context.Context) *exec.Cmd { |
| 201 | return exec.CommandContext(ctx, "bash", "-c", "echo 'Welcome to the guest'; exec python3 -I -u -c \"$0\"", guestSessionServer) |
| 202 | } |
| 203 | session := &guestSession{} |
| 204 | defer session.close() |
| 205 | if reply, sent, err := session.run(context.Background(), context.Background(), noisy, "printf ok"); err != nil || !sent || string(reply.stdout) != "ok" { |
| 206 | t.Fatalf("%+v %v %v", reply, sent, err) |
| 207 | } |
| 208 | other := &guestSession{} |
| 209 | defer other.close() |
| 210 | silent := func(ctx context.Context) *exec.Cmd { |
| 211 | return exec.CommandContext(ctx, "bash", "-c", "echo not-a-session; sleep 30") |
| 212 | } |
| 213 | started := time.Now() |
| 214 | if _, sent, err := other.run(context.Background(), context.Background(), silent, "true"); sent || err == nil { |
| 215 | t.Fatal("an unknown server was used") |
| 216 | } |
| 217 | if time.Since(started) > guestSessionStartup+2*time.Second { |
| 218 | t.Fatal("startup was not bounded") |
| 219 | } |
| 220 | } |