Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .mockery.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ packages:
config:
all: true
interfaces:
github.com/codesphere-cloud/cs-go/api:
config:
all: true
interfaces:
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ lint: install-build-deps
golangci-lint run

test:
# -count=1 to disable caching test results
go test ./... -count=1

generate:
Expand Down
13 changes: 13 additions & 0 deletions api/api_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package api_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestApi(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Api Suite")
}
14 changes: 8 additions & 6 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
)

type Client struct {
ctx context.Context
api *openapi_client.APIClient
ctx context.Context
api *openapi_client.APIClient
time Time
}

type Configuration struct {
Expand All @@ -35,10 +36,11 @@ func (c Configuration) GetApiUrl() *url.URL {
}

// For use in tests
func NewClientWithCustomApi(ctx context.Context, opts Configuration, api *openapi_client.APIClient) *Client {
func NewClientWithCustomDeps(ctx context.Context, opts Configuration, api *openapi_client.APIClient, time Time) *Client {
return &Client{
ctx: context.WithValue(ctx, openapi_client.ContextAccessToken, opts.Token),
api: api,
ctx: context.WithValue(ctx, openapi_client.ContextAccessToken, opts.Token),
api: api,
time: time,
}
}

Expand All @@ -47,7 +49,7 @@ func NewClient(ctx context.Context, opts Configuration) *Client {
cfg.Servers = []openapi_client.ServerConfiguration{{
URL: opts.BaseUrl.String(),
}}
return NewClientWithCustomApi(ctx, opts, openapi_client.NewAPIClient(cfg))
return NewClientWithCustomDeps(ctx, opts, openapi_client.NewAPIClient(cfg), &RealTime{})
}

func (c *Client) ListDataCenters() ([]DataCenter, error) {
Expand Down
115 changes: 115 additions & 0 deletions api/mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package api

import (
openapi "github.com/codesphere-cloud/cs-go/api/openapi_client"
"time"
)

type DataCenter = openapi.MetadataGetDatacenters200ResponseInner
Expand Down Expand Up @@ -33,3 +34,18 @@ func ConvertToTeam(t *openapi.TeamsGetTeam200Response) *Team {
Role: 0,
}
}

type Time interface {
Sleep(time.Duration)
Now() time.Time
}

type RealTime struct{}

func (r *RealTime) Now() time.Time {
return time.Now()
}

func (r *RealTime) Sleep(t time.Duration) {
time.Sleep(t)
}
6 changes: 3 additions & 3 deletions api/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (c *Client) SetEnvVarOnWorkspace(workspaceId int, envVars map[string]string
func (client *Client) WaitForWorkspaceRunning(workspace *Workspace, timeout time.Duration) error {
delay := 5 * time.Second

maxWaitTime := time.Now().Add(timeout)
maxWaitTime := client.time.Now().Add(timeout)
for {
status, err := client.WorkspaceStatus(workspace.Id)

Expand All @@ -55,10 +55,10 @@ func (client *Client) WaitForWorkspaceRunning(workspace *Workspace, timeout time
if status.IsRunning {
return nil
}
if time.Now().After(maxWaitTime) {
if client.time.Now().After(maxWaitTime) {
break
}
time.Sleep(delay)
client.time.Sleep(delay)
}

return errors.TimedOut(
Expand Down
Loading