1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
package rpc
import (
"context"
"fmt"
"net"
"strconv"
"testing"
"github.com/cli/cli/v2/internal/codespaces/rpc/codespace"
"github.com/cli/cli/v2/internal/codespaces/rpc/jupyter"
"github.com/cli/cli/v2/internal/codespaces/rpc/ssh"
rpctest "github.com/cli/cli/v2/internal/codespaces/rpc/test"
"google.golang.org/grpc"
)
type mockServer struct {
jupyter.JupyterServerHostServerMock
codespace.CodespaceHostServerMock
ssh.SshServerHostServerMock
}
func newMockServer() *mockServer {
server := &mockServer{}
server.CodespaceHostServerMock.NotifyCodespaceOfClientActivityFunc = func(context.Context, *codespace.NotifyCodespaceOfClientActivityRequest) (*codespace.NotifyCodespaceOfClientActivityResponse, error) {
return &codespace.NotifyCodespaceOfClientActivityResponse{
Message: "",
Result: true,
}, nil
}
return server
}
// runTestGrpcServer serves grpc requests over the provided Listener using the mockServer for mocked callbacks.
// It does not return until the Context is cancelled and the server fully shuts down.
func runTestGrpcServer(ctx context.Context, listener net.Listener, server *mockServer) error {
s := grpc.NewServer()
jupyter.RegisterJupyterServerHostServer(s, server)
codespace.RegisterCodespaceHostServer(s, server)
ssh.RegisterSshServerHostServer(s, server)
ch := make(chan error, 1)
go func() { ch <- s.Serve(listener) }()
select {
case <-ctx.Done():
s.Stop()
<-ch
return nil
case err := <-ch:
return err
}
}
// createTestInvoker is the main test setup function. It returns an Invoker using the provided mockServer, as well as a shutdown function.
// The Invoker does not need to be closed directly, that will be handled by the shutdown function.
func createTestInvoker(t *testing.T, server *mockServer) (Invoker, func(), error) {
listener, err := net.Listen("tcp", "127.0.0.1:16634")
if err != nil {
return nil, nil, fmt.Errorf("failed to listen: %w", err)
}
ctx, cancel := context.WithCancel(context.Background())
ch := make(chan error)
go func() { ch <- runTestGrpcServer(ctx, listener, server) }()
close := func() {
cancel()
<-ch
listener.Close()
}
invoker, err := CreateInvoker(context.Background(), &rpctest.Session{})
if err != nil {
close()
return nil, nil, fmt.Errorf("error connecting to internal server: %w", err)
}
return invoker, func() {
invoker.Close()
close()
}, nil
}
// Test that the RPC invoker notifies the codespace of client activity on connection
func verifyNotifyCodespaceOfClientActivity(t *testing.T, server *mockServer) {
calls := server.CodespaceHostServerMock.NotifyCodespaceOfClientActivityCalls()
if len(calls) == 0 {
t.Fatalf("no client activity calls")
}
for _, call := range calls {
activities := call.NotifyCodespaceOfClientActivityRequest.GetClientActivities()
if activities[0] == connectedEventName {
return
}
}
t.Fatalf("no activity named %s", connectedEventName)
}
// Test that the RPC invoker returns the correct port and URL when the JupyterLab server starts successfully
func TestStartJupyterServerSuccess(t *testing.T) {
resp := jupyter.GetRunningServerResponse{
Port: strconv.Itoa(1234),
ServerUrl: "http://localhost:1234?token=1234",
Message: "",
Result: true,
}
server := newMockServer()
server.JupyterServerHostServerMock.GetRunningServerFunc = func(context.Context, *jupyter.GetRunningServerRequest) (*jupyter.GetRunningServerResponse, error) {
return &resp, nil
}
invoker, stop, err := createTestInvoker(t, server)
if err != nil {
t.Fatalf("error connecting to internal server: %v", err)
}
defer stop()
port, url, err := invoker.StartJupyterServer(context.Background())
if err != nil {
t.Fatalf("expected %v, got %v", nil, err)
}
if strconv.Itoa(port) != resp.Port {
t.Fatalf("expected %s, got %d", resp.Port, port)
}
if url != resp.ServerUrl {
t.Fatalf("expected %s, got %s", resp.ServerUrl, url)
}
verifyNotifyCodespaceOfClientActivity(t, server)
}
// Test that the RPC invoker returns an error when the JupyterLab server fails to start
func TestStartJupyterServerFailure(t *testing.T) {
resp := jupyter.GetRunningServerResponse{
Port: strconv.Itoa(1234),
ServerUrl: "http://localhost:1234?token=1234",
Message: "error message",
Result: false,
}
server := newMockServer()
server.JupyterServerHostServerMock.GetRunningServerFunc = func(context.Context, *jupyter.GetRunningServerRequest) (*jupyter.GetRunningServerResponse, error) {
return &resp, nil
}
invoker, stop, err := createTestInvoker(t, server)
if err != nil {
t.Fatalf("error connecting to internal server: %v", err)
}
defer stop()
errorMessage := fmt.Sprintf("failed to start JupyterLab: %s", resp.Message)
port, url, err := invoker.StartJupyterServer(context.Background())
if err.Error() != errorMessage {
t.Fatalf("expected %v, got %v", errorMessage, err)
}
if port != 0 {
t.Fatalf("expected %d, got %d", 0, port)
}
if url != "" {
t.Fatalf("expected %s, got %s", "", url)
}
verifyNotifyCodespaceOfClientActivity(t, server)
}
// Test that the RPC invoker doesn't throw an error when requesting an incremental rebuild
func TestRebuildContainerIncremental(t *testing.T) {
resp := codespace.RebuildContainerResponse{
RebuildContainer: true,
}
server := newMockServer()
server.RebuildContainerAsyncFunc = func(context.Context, *codespace.RebuildContainerRequest) (*codespace.RebuildContainerResponse, error) {
return &resp, nil
}
invoker, stop, err := createTestInvoker(t, server)
if err != nil {
t.Fatalf("error connecting to internal server: %v", err)
}
defer stop()
err = invoker.RebuildContainer(context.Background(), false)
if err != nil {
t.Fatalf("expected %v, got %v", nil, err)
}
verifyNotifyCodespaceOfClientActivity(t, server)
}
// Test that the RPC invoker doesn't throw an error when requesting a full rebuild
func TestRebuildContainerFull(t *testing.T) {
resp := codespace.RebuildContainerResponse{
RebuildContainer: true,
}
server := newMockServer()
server.RebuildContainerAsyncFunc = func(context.Context, *codespace.RebuildContainerRequest) (*codespace.RebuildContainerResponse, error) {
return &resp, nil
}
invoker, stop, err := createTestInvoker(t, server)
if err != nil {
t.Fatalf("error connecting to internal server: %v", err)
}
defer stop()
err = invoker.RebuildContainer(context.Background(), true)
if err != nil {
t.Fatalf("expected %v, got %v", nil, err)
}
verifyNotifyCodespaceOfClientActivity(t, server)
}
// Test that the RPC invoker throws an error when the rebuild fails
func TestRebuildContainerFailure(t *testing.T) {
resp := codespace.RebuildContainerResponse{
RebuildContainer: false,
}
server := newMockServer()
server.RebuildContainerAsyncFunc = func(context.Context, *codespace.RebuildContainerRequest) (*codespace.RebuildContainerResponse, error) {
return &resp, nil
}
invoker, stop, err := createTestInvoker(t, server)
if err != nil {
t.Fatalf("error connecting to internal server: %v", err)
}
defer stop()
errorMessage := "couldn't rebuild codespace"
err = invoker.RebuildContainer(context.Background(), true)
if err.Error() != errorMessage {
t.Fatalf("expected %v, got %v", errorMessage, err)
}
}
// Test that the RPC invoker returns the correct port and user when the SSH server starts successfully
func TestStartSSHServerSuccess(t *testing.T) {
resp := ssh.StartRemoteServerResponse{
ServerPort: strconv.Itoa(1234),
User: "test",
Message: "",
Result: true,
}
server := newMockServer()
server.StartRemoteServerAsyncFunc = func(context.Context, *ssh.StartRemoteServerRequest) (*ssh.StartRemoteServerResponse, error) {
return &resp, nil
}
invoker, stop, err := createTestInvoker(t, server)
if err != nil {
t.Fatalf("error connecting to internal server: %v", err)
}
defer stop()
port, user, err := invoker.StartSSHServer(context.Background())
if err != nil {
t.Fatalf("expected %v, got %v", nil, err)
}
if strconv.Itoa(port) != resp.ServerPort {
t.Fatalf("expected %s, got %d", resp.ServerPort, port)
}
if user != resp.User {
t.Fatalf("expected %s, got %s", resp.User, user)
}
verifyNotifyCodespaceOfClientActivity(t, server)
}
// Test that the RPC invoker returns an error when the SSH server fails to start
func TestStartSSHServerFailure(t *testing.T) {
resp := ssh.StartRemoteServerResponse{
ServerPort: strconv.Itoa(1234),
User: "test",
Message: "error message",
Result: false,
}
server := newMockServer()
server.StartRemoteServerAsyncFunc = func(context.Context, *ssh.StartRemoteServerRequest) (*ssh.StartRemoteServerResponse, error) {
return &resp, nil
}
invoker, stop, err := createTestInvoker(t, server)
if err != nil {
t.Fatalf("error connecting to internal server: %v", err)
}
defer stop()
errorMessage := fmt.Sprintf("failed to start SSH server: %s", resp.Message)
port, user, err := invoker.StartSSHServer(context.Background())
if err.Error() != errorMessage {
t.Fatalf("expected %v, got %v", errorMessage, err)
}
if port != 0 {
t.Fatalf("expected %d, got %d", 0, port)
}
if user != "" {
t.Fatalf("expected %s, got %s", "", user)
}
}
|