|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/github/ghcs/cmd/ghcs/output" |
| 11 | + "github.com/github/ghcs/internal/api" |
| 12 | +) |
| 13 | + |
| 14 | +type mockAPIClient struct { |
| 15 | + getCodespaceToken func(context.Context, string, string) (string, error) |
| 16 | + getCodespace func(context.Context, string, string, string) (*api.Codespace, error) |
| 17 | +} |
| 18 | + |
| 19 | +func (m *mockAPIClient) GetCodespaceToken(ctx context.Context, userLogin, codespaceName string) (string, error) { |
| 20 | + if m.getCodespaceToken == nil { |
| 21 | + return "", errors.New("mock api client GetCodespaceToken not implemented") |
| 22 | + } |
| 23 | + |
| 24 | + return m.getCodespaceToken(ctx, userLogin, codespaceName) |
| 25 | +} |
| 26 | + |
| 27 | +func (m *mockAPIClient) GetCodespace(ctx context.Context, token, userLogin, codespaceName string) (*api.Codespace, error) { |
| 28 | + if m.getCodespace == nil { |
| 29 | + return nil, errors.New("mock api client GetCodespace not implemented") |
| 30 | + } |
| 31 | + |
| 32 | + return m.getCodespace(ctx, token, userLogin, codespaceName) |
| 33 | +} |
| 34 | + |
| 35 | +func TestPollForCodespace(t *testing.T) { |
| 36 | + logger := output.NewLogger(nil, nil, false) |
| 37 | + user := &api.User{Login: "test"} |
| 38 | + tmpCodespace := &api.Codespace{Name: "tmp-codespace"} |
| 39 | + codespaceToken := "codespace-token" |
| 40 | + |
| 41 | + ctxTimeout := 1 * time.Second |
| 42 | + exceedTime := 2 * time.Second |
| 43 | + exceedProvisioningTime := false |
| 44 | + |
| 45 | + api := &mockAPIClient{ |
| 46 | + getCodespaceToken: func(ctx context.Context, userLogin, codespace string) (string, error) { |
| 47 | + if exceedProvisioningTime { |
| 48 | + ticker := time.NewTicker(exceedTime) |
| 49 | + defer ticker.Stop() |
| 50 | + <-ticker.C |
| 51 | + } |
| 52 | + if userLogin != user.Login { |
| 53 | + return "", fmt.Errorf("user does not match, got: %s, expected: %s", userLogin, user.Login) |
| 54 | + } |
| 55 | + if codespace != tmpCodespace.Name { |
| 56 | + return "", fmt.Errorf("codespace does not match, got: %s, expected: %s", codespace, tmpCodespace.Name) |
| 57 | + } |
| 58 | + return codespaceToken, nil |
| 59 | + }, |
| 60 | + getCodespace: func(ctx context.Context, token, userLogin, codespace string) (*api.Codespace, error) { |
| 61 | + if token != codespaceToken { |
| 62 | + return nil, fmt.Errorf("token does not match, got: %s, expected: %s", token, codespaceToken) |
| 63 | + } |
| 64 | + if userLogin != user.Login { |
| 65 | + return nil, fmt.Errorf("user does not match, got: %s, expected: %s", userLogin, user.Login) |
| 66 | + } |
| 67 | + if codespace != tmpCodespace.Name { |
| 68 | + return nil, fmt.Errorf("codespace does not match, got: %s, expected: %s", codespace, tmpCodespace.Name) |
| 69 | + } |
| 70 | + return tmpCodespace, nil |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + ctx, cancel := context.WithTimeout(context.Background(), ctxTimeout) |
| 75 | + defer cancel() |
| 76 | + |
| 77 | + codespace, err := pollForCodespace(ctx, api, logger, user, tmpCodespace) |
| 78 | + if err != nil { |
| 79 | + t.Error(err) |
| 80 | + } |
| 81 | + if tmpCodespace.Name != codespace.Name { |
| 82 | + t.Errorf("returned codespace does not match, got: %s, expected: %s", codespace.Name, tmpCodespace.Name) |
| 83 | + } |
| 84 | + |
| 85 | + exceedProvisioningTime = true |
| 86 | + ctx, cancel = context.WithTimeout(ctx, ctxTimeout) |
| 87 | + defer cancel() |
| 88 | + |
| 89 | + _, err = pollForCodespace(ctx, api, logger, user, tmpCodespace) |
| 90 | + if err == nil { |
| 91 | + t.Error("expected context deadline exceeded error, got nil") |
| 92 | + } |
| 93 | +} |
0 commit comments