Skip to content

Commit b9038ea

Browse files
committed
Modify Waiters tests for Create and Delete git instances
1 parent 0ba3edf commit b9038ea

File tree

2 files changed

+88
-90
lines changed

2 files changed

+88
-90
lines changed

services/git/wait/wait.go

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

1212
const (
13-
CreateSuccess = "Ready"
14-
Creating = "Creating"
15-
CreateFail = "Error"
13+
InstanceStateReady = "Ready"
14+
InstanceStateCreating = "Creating"
15+
InstanceStateError = "Error"
1616
)
1717

1818
// APIClientInterface Interfaces needed for tests
@@ -26,13 +26,13 @@ func CreateGitInstanceWaitHandler(ctx context.Context, a APIClientInterface, pro
2626
if err != nil {
2727
return false, nil, err
2828
}
29-
if *instance.Id == "" || *instance.State == "" {
29+
if instance.Id == nil || instance.State == nil {
3030
return false, nil, fmt.Errorf("could not get Instance id or State from response for project %s and instanceId %s", projectId, instanceId)
3131
}
32-
if *instance.Id == instanceId && *instance.State == CreateSuccess {
32+
if *instance.Id == instanceId && *instance.State == InstanceStateReady {
3333
return true, instance, nil
3434
}
35-
if *instance.Id == instanceId && *instance.State == CreateFail {
35+
if *instance.Id == instanceId && *instance.State == InstanceStateError {
3636
return true, instance, fmt.Errorf("create failed for Instance with id %s", instanceId)
3737
}
3838
return false, nil, nil
@@ -45,10 +45,10 @@ func DeleteGitInstanceWaitHandler(ctx context.Context, a APIClientInterface, pro
4545
handler := wait.New(func() (waitFinished bool, response *git.Instance, err error) {
4646
instance, err := a.GetGitExecute(ctx, projectId, instanceId)
4747
if err != nil {
48-
return true, nil, err
48+
return false, nil, err
4949
}
5050
if instance != nil {
51-
return true, instance, nil
51+
return false, instance, nil
5252
}
5353
return true, nil, nil
5454
})

services/git/wait/wait_test.go

Lines changed: 80 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,9 @@ type apiClientMocked struct {
1818
getFails bool
1919
returnInstance bool
2020
wantErr bool
21-
created time.Time
22-
id string
2321
projectId string
24-
name string
25-
state string
26-
url string
27-
version string
22+
instanceId string
23+
getGitResponse *git.Instance
2824
}
2925

3026
func (a *apiClientMocked) GetGitExecute(_ context.Context, _, _ string) (*git.Instance, error) {
@@ -37,98 +33,112 @@ func (a *apiClientMocked) GetGitExecute(_ context.Context, _, _ string) (*git.In
3733
return nil, nil
3834
}
3935
return &git.Instance{
40-
Created: utils.Ptr(a.created),
41-
Id: utils.Ptr(a.id),
42-
Name: utils.Ptr(a.name),
43-
State: utils.Ptr(a.state),
44-
Url: utils.Ptr(a.url),
45-
Version: utils.Ptr(a.version),
36+
Created: a.getGitResponse.Created,
37+
Id: a.getGitResponse.Id,
38+
Name: a.getGitResponse.Name,
39+
State: a.getGitResponse.State,
40+
Url: a.getGitResponse.Url,
41+
Version: a.getGitResponse.Version,
4642
}, nil
4743
}
4844

45+
var PROJECT_ID = uuid.New().String()
46+
var INSTANCE_ID = uuid.New().String()
47+
4948
func TestCreateGitInstanceWaitHandler(t *testing.T) {
5049
tests := []struct {
5150
desc string
5251
getFails bool
5352
wantErr bool
5453
wantResp bool
55-
created time.Time
56-
id string
5754
projectId string
58-
name string
59-
state string
60-
url string
61-
version string
55+
instanceId string
6256
returnInstance bool
57+
getGitResponse *git.Instance
6358
}{
6459
{
6560
desc: "Creation of an instance succeeded",
6661
getFails: false,
6762
wantErr: false,
6863
wantResp: true,
69-
created: time.Now(),
70-
id: uuid.New().String(),
7164
projectId: uuid.New().String(),
72-
name: "instance-test",
73-
state: CreateSuccess,
74-
url: "https://testing.git.onstackit.cloud",
75-
version: "v1.6.0",
65+
instanceId: INSTANCE_ID,
7666
returnInstance: true,
67+
getGitResponse: &git.Instance{
68+
Created: utils.Ptr(time.Now()),
69+
Id: utils.Ptr(INSTANCE_ID),
70+
Name: utils.Ptr("instance-test"),
71+
State: utils.Ptr(InstanceStateReady),
72+
Url: utils.Ptr("https://testing.git.onstackit.cloud"),
73+
Version: utils.Ptr("v1.6.0"),
74+
},
7775
},
7876
{
7977
desc: "Creation of an instance Failed With Error",
8078
getFails: true,
8179
wantErr: true,
8280
wantResp: false,
83-
created: time.Now(),
84-
id: uuid.New().String(),
8581
projectId: uuid.New().String(),
86-
name: "instance-test",
87-
state: CreateFail,
88-
url: "https://testing.git.onstackit.cloud",
89-
version: "v1.6.0",
82+
instanceId: INSTANCE_ID,
9083
returnInstance: true,
84+
getGitResponse: &git.Instance{
85+
Created: utils.Ptr(time.Now()),
86+
Id: utils.Ptr(INSTANCE_ID),
87+
Name: utils.Ptr("instance-test"),
88+
State: utils.Ptr(InstanceStateReady),
89+
Url: utils.Ptr("https://testing.git.onstackit.cloud"),
90+
Version: utils.Ptr("v1.6.0"),
91+
},
9192
},
9293
{
9394
desc: "Creation of an instance with response failed and without error",
9495
getFails: false,
95-
wantErr: true,
96+
wantErr: false,
9697
wantResp: true,
97-
created: time.Now(),
98-
id: uuid.New().String(),
9998
projectId: uuid.New().String(),
100-
name: "instance-test",
101-
state: CreateFail,
102-
url: "https://testing.git.onstackit.cloud",
103-
version: "v1.6.0",
99+
instanceId: INSTANCE_ID,
104100
returnInstance: true,
101+
getGitResponse: &git.Instance{
102+
Created: utils.Ptr(time.Now()),
103+
Id: utils.Ptr(INSTANCE_ID),
104+
Name: utils.Ptr("instance-test"),
105+
State: utils.Ptr(InstanceStateReady),
106+
Url: utils.Ptr("https://testing.git.onstackit.cloud"),
107+
Version: utils.Ptr("v1.6.0"),
108+
},
105109
},
106110
{
107111
desc: "Creation of an instance failed without id on the response",
108112
getFails: false,
109113
wantErr: true,
110114
wantResp: false,
111-
created: time.Now(),
112-
projectId: uuid.New().String(),
113-
name: "instance-test",
114-
state: CreateFail,
115-
url: "https://testing.git.onstackit.cloud",
116-
version: "v1.6.0",
115+
projectId: PROJECT_ID,
116+
instanceId: INSTANCE_ID,
117117
returnInstance: true,
118+
getGitResponse: &git.Instance{
119+
Created: utils.Ptr(time.Now()),
120+
Name: utils.Ptr("instance-test"),
121+
State: utils.Ptr(InstanceStateError),
122+
Url: utils.Ptr("https://testing.git.onstackit.cloud"),
123+
Version: utils.Ptr("v1.6.0"),
124+
},
118125
},
119126

120127
{
121128
desc: "Creation of an instance without state on the response",
122129
getFails: false,
123130
wantErr: true,
124131
wantResp: false,
125-
created: time.Now(),
126-
id: uuid.New().String(),
127-
projectId: uuid.New().String(),
128-
name: "instance-test",
129-
url: "https://testing.git.onstackit.cloud",
130-
version: "v1.6.0",
132+
projectId: PROJECT_ID,
133+
instanceId: INSTANCE_ID,
131134
returnInstance: true,
135+
getGitResponse: &git.Instance{
136+
Created: utils.Ptr(time.Now()),
137+
Id: utils.Ptr(INSTANCE_ID),
138+
Name: utils.Ptr("instance-test"),
139+
Url: utils.Ptr("https://testing.git.onstackit.cloud"),
140+
Version: utils.Ptr("v1.6.0"),
141+
},
132142
},
133143
}
134144
for _, tt := range tests {
@@ -137,28 +147,24 @@ func TestCreateGitInstanceWaitHandler(t *testing.T) {
137147
desc: tt.desc,
138148
getFails: tt.getFails,
139149
wantErr: tt.wantErr,
140-
created: tt.created,
141-
id: tt.id,
142150
projectId: tt.projectId,
143-
name: tt.name,
144-
state: tt.state,
145-
url: tt.url,
146-
version: tt.version,
151+
instanceId: tt.instanceId,
152+
getGitResponse: tt.getGitResponse,
147153
returnInstance: tt.returnInstance,
148154
}
149155
var instanceWanted *git.Instance
150156
if tt.wantResp {
151157
instanceWanted = &git.Instance{
152-
Created: utils.Ptr(tt.created),
153-
Id: utils.Ptr(tt.id),
154-
Name: utils.Ptr(tt.name),
155-
State: utils.Ptr(tt.state),
156-
Url: utils.Ptr(tt.url),
157-
Version: utils.Ptr(tt.version),
158+
Created: tt.getGitResponse.Created,
159+
Id: tt.getGitResponse.Id,
160+
Name: tt.getGitResponse.Name,
161+
State: tt.getGitResponse.State,
162+
Url: tt.getGitResponse.Url,
163+
Version: tt.getGitResponse.Version,
158164
}
159165
}
160166

161-
handler := CreateGitInstanceWaitHandler(context.Background(), apiClient, apiClient.projectId, apiClient.id)
167+
handler := CreateGitInstanceWaitHandler(context.Background(), apiClient, apiClient.projectId, apiClient.instanceId)
162168

163169
response, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
164170

@@ -179,12 +185,7 @@ func TestDeleteGitInstanceWaitHandler(t *testing.T) {
179185
wantReturnedInstance bool
180186
getFails bool
181187
returnInstance bool
182-
created time.Time
183-
id string
184-
name string
185-
state string
186-
url string
187-
version string
188+
getGitResponse *git.Instance
188189
}{
189190
{
190191
desc: "Instance deletion failed with error",
@@ -193,16 +194,18 @@ func TestDeleteGitInstanceWaitHandler(t *testing.T) {
193194
},
194195
{
195196
desc: "Instance deletion failed returning existing instance",
196-
wantErr: false,
197+
wantErr: true,
197198
getFails: false,
198199
wantReturnedInstance: true,
199-
created: time.Now(),
200-
id: uuid.New().String(),
201-
name: "instance-test",
202-
state: CreateSuccess,
203200
returnInstance: true,
204-
url: "https://testing.git.onstackit.cloud",
205-
version: "v1.6.0",
201+
getGitResponse: &git.Instance{
202+
Created: utils.Ptr(time.Now()),
203+
Id: utils.Ptr(INSTANCE_ID),
204+
Name: utils.Ptr("instance-test"),
205+
State: utils.Ptr(InstanceStateReady),
206+
Url: utils.Ptr("https://testing.git.onstackit.cloud"),
207+
Version: utils.Ptr("v1.6.0"),
208+
},
206209
},
207210
{
208211
desc: "Instance deletion succesfull",
@@ -218,16 +221,11 @@ func TestDeleteGitInstanceWaitHandler(t *testing.T) {
218221

219222
projectId: uuid.New().String(),
220223
getFails: tt.getFails,
221-
created: tt.created,
222-
id: tt.id,
223-
name: tt.name,
224-
state: tt.state,
225-
url: tt.url,
226-
version: tt.version,
227224
returnInstance: tt.returnInstance,
225+
getGitResponse: tt.getGitResponse,
228226
}
229227

230-
handler := DeleteGitInstanceWaitHandler(context.Background(), apiClient, apiClient.projectId, apiClient.id)
228+
handler := DeleteGitInstanceWaitHandler(context.Background(), apiClient, apiClient.projectId, apiClient.instanceId)
231229
response, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
232230

233231
if (err != nil) != tt.wantErr {

0 commit comments

Comments
 (0)