Skip to content

Commit ccffea2

Browse files
authored
refac(tests): Use Ginkgo/Gomega for workspace tests (#15)
Migrate workspace tests to Ginkgo/Gomega, making this the default choice for the repo.
1 parent 4bdfd54 commit ccffea2

File tree

11 files changed

+309
-207
lines changed

11 files changed

+309
-207
lines changed

.mockery.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ packages:
2121
config:
2222
all: true
2323
interfaces:
24+
github.com/codesphere-cloud/cs-go/api:
25+
config:
26+
all: true
27+
interfaces:

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ lint: install-build-deps
1010
golangci-lint run
1111

1212
test:
13+
# -count=1 to disable caching test results
1314
go test ./... -count=1
1415

1516
generate:

api/api_suite_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package api_test
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestApi(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "Api Suite")
13+
}

api/client.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import (
1111
)
1212

1313
type Client struct {
14-
ctx context.Context
15-
api *openapi_client.APIClient
14+
ctx context.Context
15+
api *openapi_client.APIClient
16+
time Time
1617
}
1718

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

3738
// For use in tests
38-
func NewClientWithCustomApi(ctx context.Context, opts Configuration, api *openapi_client.APIClient) *Client {
39+
func NewClientWithCustomDeps(ctx context.Context, opts Configuration, api *openapi_client.APIClient, time Time) *Client {
3940
return &Client{
40-
ctx: context.WithValue(ctx, openapi_client.ContextAccessToken, opts.Token),
41-
api: api,
41+
ctx: context.WithValue(ctx, openapi_client.ContextAccessToken, opts.Token),
42+
api: api,
43+
time: time,
4244
}
4345
}
4446

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

5355
func (c *Client) ListDataCenters() ([]DataCenter, error) {

api/mocks.go

Lines changed: 115 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/types.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package api
55

66
import (
77
openapi "github.com/codesphere-cloud/cs-go/api/openapi_client"
8+
"time"
89
)
910

1011
type DataCenter = openapi.MetadataGetDatacenters200ResponseInner
@@ -33,3 +34,18 @@ func ConvertToTeam(t *openapi.TeamsGetTeam200Response) *Team {
3334
Role: 0,
3435
}
3536
}
37+
38+
type Time interface {
39+
Sleep(time.Duration)
40+
Now() time.Time
41+
}
42+
43+
type RealTime struct{}
44+
45+
func (r *RealTime) Now() time.Time {
46+
return time.Now()
47+
}
48+
49+
func (r *RealTime) Sleep(t time.Duration) {
50+
time.Sleep(t)
51+
}

api/workspace.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (c *Client) SetEnvVarOnWorkspace(workspaceId int, envVars map[string]string
4444
func (client *Client) WaitForWorkspaceRunning(workspace *Workspace, timeout time.Duration) error {
4545
delay := 5 * time.Second
4646

47-
maxWaitTime := time.Now().Add(timeout)
47+
maxWaitTime := client.time.Now().Add(timeout)
4848
for {
4949
status, err := client.WorkspaceStatus(workspace.Id)
5050

@@ -55,10 +55,10 @@ func (client *Client) WaitForWorkspaceRunning(workspace *Workspace, timeout time
5555
if status.IsRunning {
5656
return nil
5757
}
58-
if time.Now().After(maxWaitTime) {
58+
if client.time.Now().After(maxWaitTime) {
5959
break
6060
}
61-
time.Sleep(delay)
61+
client.time.Sleep(delay)
6262
}
6363

6464
return errors.TimedOut(

0 commit comments

Comments
 (0)