-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathlist_test.go
More file actions
executable file
·460 lines (431 loc) · 11.2 KB
/
list_test.go
File metadata and controls
executable file
·460 lines (431 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2025 STACKIT GmbH & Co. KG
package list
import (
"context"
"errors"
"testing"
"github.com/google/uuid"
"github.com/spf13/cobra"
cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/edge/client"
testUtils "github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
"github.com/stackitcloud/stackit-sdk-go/services/edge"
)
type testCtxKey struct{}
var (
testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
testProjectId = uuid.NewString()
testRegion = "eu01"
)
// mockExecutable is a mock for the Executable interface
type mockExecutable struct {
executeFails bool
executeResp *edge.InstanceList
}
func (m *mockExecutable) Execute() (*edge.InstanceList, error) {
if m.executeFails {
return nil, errors.New("API error")
}
if m.executeResp != nil {
return m.executeResp, nil
}
return &edge.InstanceList{
Instances: &[]edge.Instance{
{Id: utils.Ptr("instance-1"), DisplayName: utils.Ptr("namea")},
{Id: utils.Ptr("instance-2"), DisplayName: utils.Ptr("nameb")},
},
}, nil
}
// mockAPIClient is a mock for the edge.APIClient interface
type mockAPIClient struct {
getInstancesMock edge.ApiListInstancesRequest
}
func (m *mockAPIClient) ListInstances(_ context.Context, _, _ string) edge.ApiListInstancesRequest {
if m.getInstancesMock != nil {
return m.getInstancesMock
}
return &mockExecutable{}
}
// Unused methods to satisfy the interface
func (m *mockAPIClient) CreateInstance(_ context.Context, _, _ string) edge.ApiCreateInstanceRequest {
return nil
}
func (m *mockAPIClient) GetInstance(_ context.Context, _, _, _ string) edge.ApiGetInstanceRequest {
return nil
}
func (m *mockAPIClient) GetInstanceByName(_ context.Context, _, _, _ string) edge.ApiGetInstanceByNameRequest {
return nil
}
func (m *mockAPIClient) UpdateInstance(_ context.Context, _, _, _ string) edge.ApiUpdateInstanceRequest {
return nil
}
func (m *mockAPIClient) UpdateInstanceByName(_ context.Context, _, _, _ string) edge.ApiUpdateInstanceByNameRequest {
return nil
}
func (m *mockAPIClient) DeleteInstance(_ context.Context, _, _, _ string) edge.ApiDeleteInstanceRequest {
return nil
}
func (m *mockAPIClient) DeleteInstanceByName(_ context.Context, _, _, _ string) edge.ApiDeleteInstanceByNameRequest {
return nil
}
func (m *mockAPIClient) GetKubeconfigByInstanceId(_ context.Context, _, _, _ string) edge.ApiGetKubeconfigByInstanceIdRequest {
return nil
}
func (m *mockAPIClient) GetKubeconfigByInstanceName(_ context.Context, _, _, _ string) edge.ApiGetKubeconfigByInstanceNameRequest {
return nil
}
func (m *mockAPIClient) GetTokenByInstanceId(_ context.Context, _, _, _ string) edge.ApiGetTokenByInstanceIdRequest {
return nil
}
func (m *mockAPIClient) GetTokenByInstanceName(_ context.Context, _, _, _ string) edge.ApiGetTokenByInstanceNameRequest {
return nil
}
func (m *mockAPIClient) ListPlansProject(_ context.Context, _ string) edge.ApiListPlansProjectRequest {
return nil
}
func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
flagValues := map[string]string{
globalflags.ProjectIdFlag: testProjectId,
globalflags.RegionFlag: testRegion,
}
for _, mod := range mods {
mod(flagValues)
}
return flagValues
}
func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
model := &inputModel{
GlobalFlagModel: &globalflags.GlobalFlagModel{
ProjectId: testProjectId,
Region: testRegion,
Verbosity: globalflags.VerbosityDefault,
},
}
for _, mod := range mods {
mod(model)
}
return model
}
func TestParseInput(t *testing.T) {
type args struct {
flags map[string]string
cmpOpts []testUtils.ValueComparisonOption
}
tests := []struct {
name string
wantErr any
want *inputModel
args args
}{
{
name: "success",
want: fixtureInputModel(),
args: args{
flags: fixtureFlagValues(),
cmpOpts: []testUtils.ValueComparisonOption{
testUtils.WithAllowUnexported(inputModel{}),
},
},
},
{
name: "with limit",
want: fixtureInputModel(func(model *inputModel) {
model.Limit = utils.Ptr(int64(10))
}),
args: args{
flags: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[limitFlag] = "10"
}),
cmpOpts: []testUtils.ValueComparisonOption{
testUtils.WithAllowUnexported(inputModel{}),
},
},
},
{
name: "no flag values",
wantErr: true,
args: args{
flags: map[string]string{},
},
},
{
name: "project id missing",
wantErr: &cliErr.ProjectIdError{},
args: args{
flags: fixtureFlagValues(func(flagValues map[string]string) {
delete(flagValues, globalflags.ProjectIdFlag)
}),
},
},
{
name: "project id empty",
wantErr: "value cannot be empty",
args: args{
flags: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[globalflags.ProjectIdFlag] = ""
}),
},
},
{
name: "project id invalid",
wantErr: "invalid UUID length",
args: args{
flags: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[globalflags.ProjectIdFlag] = "invalid-uuid"
}),
},
},
{
name: "limit invalid",
wantErr: "invalid syntax",
args: args{
flags: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[limitFlag] = "invalid"
}),
},
},
{
name: "limit less than 1",
wantErr: &cliErr.FlagValidationError{},
args: args{
flags: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[limitFlag] = "0"
}),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
caseOpts := []testUtils.ParseInputCaseOption{}
if len(tt.args.cmpOpts) > 0 {
caseOpts = append(caseOpts, testUtils.WithParseInputCmpOptions(tt.args.cmpOpts...))
}
testUtils.RunParseInputCase(t, testUtils.ParseInputTestCase[*inputModel]{
Name: tt.name,
Flags: tt.args.flags,
WantModel: tt.want,
WantErr: tt.wantErr,
CmdFactory: NewCmd,
ParseInputFunc: func(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) {
return parseInput(p, cmd)
},
}, caseOpts...)
})
}
}
func TestRun(t *testing.T) {
type args struct {
model *inputModel
client client.APIClient
}
tests := []struct {
name string
wantErr error
want []edge.Instance
args args
}{
{
name: "list success",
want: []edge.Instance{
{Id: utils.Ptr("instance-1"), DisplayName: utils.Ptr("namea")},
{Id: utils.Ptr("instance-2"), DisplayName: utils.Ptr("nameb")},
},
args: args{
model: fixtureInputModel(),
client: &mockAPIClient{},
},
},
{
name: "list success with limit",
want: []edge.Instance{
{Id: utils.Ptr("instance-1"), DisplayName: utils.Ptr("namea")},
},
args: args{
model: fixtureInputModel(func(model *inputModel) {
model.Limit = utils.Ptr(int64(1))
}),
client: &mockAPIClient{},
},
},
{
name: "list success with limit greater than items",
want: []edge.Instance{
{Id: utils.Ptr("instance-1"), DisplayName: utils.Ptr("namea")},
{Id: utils.Ptr("instance-2"), DisplayName: utils.Ptr("nameb")},
},
args: args{
model: fixtureInputModel(func(model *inputModel) {
model.Limit = utils.Ptr(int64(5))
}),
client: &mockAPIClient{},
},
},
{
name: "list success with no items",
want: []edge.Instance{},
args: args{
model: fixtureInputModel(),
client: &mockAPIClient{
getInstancesMock: &mockExecutable{
executeResp: &edge.InstanceList{Instances: &[]edge.Instance{}},
},
},
},
},
{
name: "list API error",
wantErr: &cliErr.RequestFailedError{},
args: args{
model: fixtureInputModel(),
client: &mockAPIClient{
getInstancesMock: &mockExecutable{
executeFails: true,
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := run(testCtx, tt.args.model, tt.args.client)
if !testUtils.AssertError(t, err, tt.wantErr) {
return
}
testUtils.AssertValue(t, got, tt.want)
})
}
}
func TestOutputResult(t *testing.T) {
type args struct {
model *inputModel
instances []edge.Instance
projectLabel string
}
tests := []struct {
name string
wantErr error
args args
}{
{
name: "no instance",
args: args{
model: fixtureInputModel(),
},
},
{
name: "output json",
args: args{
model: fixtureInputModel(func(model *inputModel) {
model.OutputFormat = print.JSONOutputFormat
}),
instances: []edge.Instance{
{Id: utils.Ptr("instance-1"), DisplayName: utils.Ptr("namea")},
},
projectLabel: "test-project",
},
},
{
name: "output yaml",
args: args{
model: fixtureInputModel(func(model *inputModel) {
model.OutputFormat = print.YAMLOutputFormat
}),
instances: []edge.Instance{
{Id: utils.Ptr("instance-1"), DisplayName: utils.Ptr("namea")},
},
projectLabel: "test-project",
},
},
{
name: "output default with instances",
args: args{
model: fixtureInputModel(),
instances: []edge.Instance{
{
Id: utils.Ptr("instance-1"),
DisplayName: utils.Ptr("namea"),
FrontendUrl: utils.Ptr("https://example.com"),
},
{
Id: utils.Ptr("instance-2"),
DisplayName: utils.Ptr("nameb"),
FrontendUrl: utils.Ptr("https://example2.com"),
},
},
projectLabel: "test-project",
},
},
{
name: "output default with no instances",
args: args{
model: fixtureInputModel(),
instances: []edge.Instance{},
projectLabel: "test-project",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := print.NewPrinter()
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
err := outputResult(p, tt.args.model.OutputFormat, tt.args.projectLabel, tt.args.instances)
testUtils.AssertError(t, err, tt.wantErr)
})
}
}
func TestBuildRequest(t *testing.T) {
type args struct {
model *inputModel
client client.APIClient
}
tests := []struct {
name string
wantErr error
want *listRequestSpec
args args
}{
{
name: "success",
want: &listRequestSpec{
ProjectID: testProjectId,
Region: testRegion,
},
args: args{
model: fixtureInputModel(),
client: &mockAPIClient{
getInstancesMock: &mockExecutable{},
},
},
},
{
name: "success with limit",
want: &listRequestSpec{
ProjectID: testProjectId,
Region: testRegion,
Limit: utils.Ptr(int64(10)),
},
args: args{
model: fixtureInputModel(func(model *inputModel) {
model.Limit = utils.Ptr(int64(10))
}),
client: &mockAPIClient{
getInstancesMock: &mockExecutable{},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := buildRequest(testCtx, tt.args.model, tt.args.client)
if !testUtils.AssertError(t, err, tt.wantErr) {
return
}
testUtils.AssertValue(t, got, tt.want, testUtils.WithIgnoreFields(listRequestSpec{}, "Execute"))
})
}
}