11package api_test
22
33import (
4+ "context"
45 "testing"
56 "time"
67
7- "context"
8-
98 "github.com/codesphere-cloud/cs-go/api"
109 "github.com/codesphere-cloud/cs-go/api/errors"
1110 "github.com/codesphere-cloud/cs-go/api/openapi_client"
11+ tu "github.com/codesphere-cloud/cs-go/pkg/testutils"
1212 "github.com/stretchr/testify/assert"
1313 "github.com/stretchr/testify/mock"
1414)
@@ -18,9 +18,27 @@ func getTestingClient(t *testing.T) (*api.Client, *openapi_client.MockWorkspaces
1818 apis := openapi_client.APIClient {
1919 WorkspacesAPI : wsApiMock ,
2020 }
21+ tu .PatchTimeFuncs (t )
2122 return api .NewClientWithCustomApi (context .TODO (), api.Configuration {}, & apis ), wsApiMock
2223}
2324
25+ func mockWorkspaceStatus (wsApiMock * openapi_client.MockWorkspacesAPI , workspaceId int , isRunning ... bool ) {
26+ wsApiMock .EXPECT ().WorkspacesGetWorkspaceStatus (mock .Anything , float32 (workspaceId )).
27+ Return (openapi_client.ApiWorkspacesGetWorkspaceStatusRequest {ApiService : wsApiMock })
28+ for _ , running := range isRunning {
29+ wsApiMock .EXPECT ().WorkspacesGetWorkspaceStatusExecute (mock .Anything ).Once ().Return (& api.WorkspaceStatus {
30+ IsRunning : running ,
31+ }, nil , nil )
32+ }
33+ mock .InOrder (wsApiMock .ExpectedCalls ... )
34+ }
35+
36+ func mockCreateWorkspace (wsApiMock * openapi_client.MockWorkspacesAPI , ws api.Workspace ) {
37+ wsApiMock .EXPECT ().WorkspacesCreateWorkspace (mock .Anything ).
38+ Return (openapi_client.ApiWorkspacesCreateWorkspaceRequest {ApiService : wsApiMock })
39+ wsApiMock .EXPECT ().WorkspacesCreateWorkspaceExecute (mock .Anything ).Return (& ws , nil , nil )
40+ }
41+
2442func TestListWorkspaces (t * testing.T ) {
2543 client , wsApiMock := getTestingClient (t )
2644
@@ -45,16 +63,9 @@ func TestWaitForWorkspaceRunningSuccess(t *testing.T) {
4563 Id : 0 , Name : "fakeWorkspace" ,
4664 }
4765
48- wsApiMock .EXPECT ().WorkspacesGetWorkspaceStatus (mock .Anything , float32 (0 )).
49- Return (openapi_client.ApiWorkspacesGetWorkspaceStatusRequest {ApiService : wsApiMock })
50- wsApiMock .EXPECT ().WorkspacesGetWorkspaceStatusExecute (mock .Anything ).Return (& api.WorkspaceStatus {
51- IsRunning : true ,
52- }, nil , nil )
66+ mockWorkspaceStatus (wsApiMock , ws .Id , true )
5367
54- err := client .WaitForWorkspaceRunning (
55- & ws ,
56- api.WaitForWorkspaceRunningOptions {Timeout : 1 * time .Millisecond , Delay : 1 * time .Millisecond },
57- )
68+ err := client .WaitForWorkspaceRunning (& ws , 1 * time .Millisecond )
5869
5970 assert .Nil (t , err , "should be nil" )
6071}
@@ -66,16 +77,9 @@ func TestWaitForWorkspaceRunningTimeout(t *testing.T) {
6677 Id : 0 , Name : "fakeWorkspace" ,
6778 }
6879
69- wsApiMock .EXPECT ().WorkspacesGetWorkspaceStatus (mock .Anything , float32 (0 )).
70- Return (openapi_client.ApiWorkspacesGetWorkspaceStatusRequest {ApiService : wsApiMock })
71- wsApiMock .EXPECT ().WorkspacesGetWorkspaceStatusExecute (mock .Anything ).Return (& api.WorkspaceStatus {
72- IsRunning : false ,
73- }, nil , nil )
80+ mockWorkspaceStatus (wsApiMock , ws .Id , false , false )
7481
75- err := client .WaitForWorkspaceRunning (
76- & ws ,
77- api.WaitForWorkspaceRunningOptions {Timeout : 1 * time .Millisecond , Delay : 1 * time .Millisecond },
78- )
82+ err := client .WaitForWorkspaceRunning (& ws , 1 * time .Second )
7983
8084 assert .IsType (t , err , & errors.TimedOutError {}, "expected timeout error" )
8185}
@@ -87,19 +91,8 @@ func TestWaitForWorkspaceRunningOnRetry(t *testing.T) {
8791 Id : 42 , Name : "fakeWorkspace" ,
8892 }
8993
90- wsApiMock .EXPECT ().WorkspacesGetWorkspaceStatus (mock .Anything , float32 (42 )).
91- Return (openapi_client.ApiWorkspacesGetWorkspaceStatusRequest {ApiService : wsApiMock })
92- wsApiMock .EXPECT ().WorkspacesGetWorkspaceStatusExecute (mock .Anything ).Return (& api.WorkspaceStatus {
93- IsRunning : false ,
94- }, nil , nil ).Once ()
95- wsApiMock .EXPECT ().WorkspacesGetWorkspaceStatusExecute (mock .Anything ).Return (& api.WorkspaceStatus {
96- IsRunning : true ,
97- }, nil , nil ).Once ()
98-
99- err := client .WaitForWorkspaceRunning (
100- & ws ,
101- api.WaitForWorkspaceRunningOptions {Timeout : 10 * time .Millisecond , Delay : 1 * time .Millisecond },
102- )
94+ mockWorkspaceStatus (wsApiMock , ws .Id , false , true )
95+ err := client .WaitForWorkspaceRunning (& ws , 1 * time .Millisecond )
10396
10497 assert .Nil (t , err , "should be nil" )
10598}
@@ -115,17 +108,14 @@ func TestDeployWorkspace(t *testing.T) {
115108 Return (openapi_client.ApiWorkspacesCreateWorkspaceRequest {ApiService : wsApiMock })
116109 wsApiMock .EXPECT ().WorkspacesCreateWorkspaceExecute (mock .Anything ).Return (& ws , nil , nil )
117110
118- wsApiMock .EXPECT ().WorkspacesGetWorkspaceStatus (mock .Anything , float32 (42 )).
119- Return (openapi_client.ApiWorkspacesGetWorkspaceStatusRequest {ApiService : wsApiMock })
120- wsApiMock .EXPECT ().WorkspacesGetWorkspaceStatusExecute (mock .Anything ).Return (& api.WorkspaceStatus {
121- IsRunning : true ,
122- }, nil , nil )
111+ mockWorkspaceStatus (wsApiMock , ws .Id , true )
123112
124- err := client .DeployWorkspace (
113+ newWs , err := client .DeployWorkspace (
125114 api.DeployWorkspaceArgs {Timeout : 1 * time .Millisecond },
126115 )
127116
128117 assert .Nil (t , err , "should be nil" )
118+ assert .Equal (t , newWs .Name , ws .Name , "Should have the same name" )
129119}
130120
131121func TestDeployWorkspaceWithEnvVars (t * testing.T ) {
@@ -142,21 +132,15 @@ func TestDeployWorkspaceWithEnvVars(t *testing.T) {
142132 },
143133 }
144134
145- wsApiMock .EXPECT ().WorkspacesCreateWorkspace (mock .Anything ).
146- Return (openapi_client.ApiWorkspacesCreateWorkspaceRequest {ApiService : wsApiMock })
147- wsApiMock .EXPECT ().WorkspacesCreateWorkspaceExecute (mock .Anything ).Return (& ws , nil , nil )
148-
149- wsApiMock .EXPECT ().WorkspacesGetWorkspaceStatus (mock .Anything , float32 (42 )).
150- Return (openapi_client.ApiWorkspacesGetWorkspaceStatusRequest {ApiService : wsApiMock })
151- wsApiMock .EXPECT ().WorkspacesGetWorkspaceStatusExecute (mock .Anything ).Return (& api.WorkspaceStatus {
152- IsRunning : true ,
153- }, nil , nil )
135+ mockCreateWorkspace (wsApiMock , ws )
136+ mockWorkspaceStatus (wsApiMock , ws .Id , true )
154137
155138 wsApiMock .EXPECT ().WorkspacesSetEnvVar (mock .Anything , float32 (42 )).
156139 Return (openapi_client.ApiWorkspacesSetEnvVarRequest {ApiService : wsApiMock })
157140 wsApiMock .EXPECT ().WorkspacesSetEnvVarExecute (mock .Anything ).Return (nil , nil ).Once ()
158141
159- err := client .DeployWorkspace (args )
142+ newWs , err := client .DeployWorkspace (args )
160143
161144 assert .Nil (t , err , "should be nil" )
145+ assert .Equal (t , newWs .Name , ws .Name , "Should have the same name" )
162146}
0 commit comments