| @@ -0,0 +1,220 @@ |
| 1 | +新增://go:build !windows |
| 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 | +新增:// The server that runs on the guest, started here as a local process. |
| 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 | +新增: // Each script is a separate shell: no state passes between them. |
| 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 | +新增: // stdin is empty, as on a connection of its own. |
| 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 | +新增: // The connection stays usable. |
| 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 | +新增: // It is not started again for every poll. |
| 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 | +新增:} |