Skip to content

Commit 1520725

Browse files
committed
added testcases
1 parent 75f7943 commit 1520725

File tree

10 files changed

+1094
-15
lines changed

10 files changed

+1094
-15
lines changed

internal/cmd/ske/options/availability_zones/availability_zones.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,15 @@ func buildRequest(ctx context.Context, apiClient *ske.APIClient, model *inputMod
7575
}
7676

7777
func outputResult(p *print.Printer, model *inputModel, options *ske.ProviderOptions) error {
78-
if model == nil {
79-
return fmt.Errorf("model is nil")
80-
} else if options == nil {
78+
if options == nil {
8179
return fmt.Errorf("options is nil")
8280
}
8381

82+
options.KubernetesVersions = nil
83+
options.MachineImages = nil
84+
options.MachineTypes = nil
85+
options.VolumeTypes = nil
86+
8487
return p.OutputResult(model.OutputFormat, options, func() error {
8588
zones := utils.PtrValue(options.AvailabilityZones)
8689

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
package availability_zones
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
8+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
9+
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
12+
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
13+
14+
"github.com/google/go-cmp/cmp"
15+
"github.com/google/go-cmp/cmp/cmpopts"
16+
"github.com/stackitcloud/stackit-sdk-go/services/ske"
17+
)
18+
19+
type testCtxKey struct{}
20+
21+
var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
22+
var testClient = &ske.APIClient{}
23+
24+
const testRegion = "eu01"
25+
26+
func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
27+
flagValues := map[string]string{
28+
globalflags.RegionFlag: testRegion,
29+
}
30+
for _, mod := range mods {
31+
mod(flagValues)
32+
}
33+
return flagValues
34+
}
35+
36+
func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
37+
model := &inputModel{
38+
GlobalFlagModel: globalflags.GlobalFlagModel{
39+
Region: testRegion,
40+
Verbosity: globalflags.VerbosityDefault,
41+
},
42+
}
43+
for _, mod := range mods {
44+
mod(model)
45+
}
46+
return model
47+
}
48+
49+
func TestParseInput(t *testing.T) {
50+
tests := []struct {
51+
description string
52+
argValues []string
53+
flagValues map[string]string
54+
isValid bool
55+
expectedModel *inputModel
56+
}{
57+
{
58+
description: "base",
59+
flagValues: fixtureFlagValues(),
60+
isValid: true,
61+
expectedModel: fixtureInputModel(),
62+
},
63+
{
64+
description: "no values",
65+
flagValues: map[string]string{},
66+
isValid: true,
67+
expectedModel: fixtureInputModel(func(model *inputModel) {
68+
model.Region = ""
69+
}),
70+
},
71+
}
72+
73+
for _, tt := range tests {
74+
t.Run(tt.description, func(t *testing.T) {
75+
testutils.TestParseInput(t, NewCmd, parseInput, tt.expectedModel, tt.argValues, tt.flagValues, tt.isValid)
76+
})
77+
}
78+
}
79+
80+
func TestBuildRequest(t *testing.T) {
81+
tests := []struct {
82+
description string
83+
inputModel *inputModel
84+
expectedRequest ske.ApiListProviderOptionsRequest
85+
}{
86+
{
87+
description: "base",
88+
inputModel: fixtureInputModel(),
89+
expectedRequest: testClient.ListProviderOptions(testCtx, testRegion),
90+
},
91+
}
92+
93+
for _, tt := range tests {
94+
t.Run(tt.description, func(t *testing.T) {
95+
request := buildRequest(testCtx, testClient, tt.inputModel)
96+
97+
diff := cmp.Diff(request, tt.expectedRequest,
98+
cmp.AllowUnexported(tt.expectedRequest),
99+
cmpopts.EquateComparable(testCtx),
100+
)
101+
if diff != "" {
102+
t.Fatalf("Data does not match: %s", diff)
103+
}
104+
})
105+
}
106+
}
107+
108+
func TestOutputResult(t *testing.T) {
109+
type args struct {
110+
model *inputModel
111+
options *ske.ProviderOptions
112+
}
113+
tests := []struct {
114+
name string
115+
args args
116+
wantErr bool
117+
}{
118+
{
119+
name: "empty",
120+
args: args{},
121+
wantErr: true,
122+
},
123+
{
124+
name: "missing options",
125+
args: args{
126+
model: &inputModel{
127+
GlobalFlagModel: globalflags.GlobalFlagModel{},
128+
},
129+
},
130+
wantErr: true,
131+
},
132+
{
133+
name: "empty input model",
134+
args: args{
135+
model: &inputModel{},
136+
options: &ske.ProviderOptions{},
137+
},
138+
wantErr: false,
139+
},
140+
{
141+
name: "set model and options",
142+
args: args{
143+
model: &inputModel{
144+
GlobalFlagModel: globalflags.GlobalFlagModel{},
145+
},
146+
options: &ske.ProviderOptions{},
147+
},
148+
wantErr: false,
149+
},
150+
{
151+
name: "empty values",
152+
args: args{
153+
model: &inputModel{
154+
GlobalFlagModel: globalflags.GlobalFlagModel{},
155+
},
156+
options: &ske.ProviderOptions{
157+
AvailabilityZones: &[]ske.AvailabilityZone{},
158+
},
159+
},
160+
wantErr: false,
161+
},
162+
{
163+
name: "empty value in values",
164+
args: args{
165+
model: &inputModel{
166+
GlobalFlagModel: globalflags.GlobalFlagModel{},
167+
},
168+
options: &ske.ProviderOptions{
169+
AvailabilityZones: &[]ske.AvailabilityZone{{}},
170+
},
171+
},
172+
wantErr: false,
173+
},
174+
{
175+
name: "valid values",
176+
args: args{
177+
model: &inputModel{
178+
GlobalFlagModel: globalflags.GlobalFlagModel{},
179+
},
180+
options: &ske.ProviderOptions{
181+
AvailabilityZones: &[]ske.AvailabilityZone{
182+
ske.AvailabilityZone{
183+
Name: utils.Ptr("zone1"),
184+
},
185+
ske.AvailabilityZone{
186+
Name: utils.Ptr("zone2"),
187+
},
188+
},
189+
},
190+
},
191+
wantErr: false,
192+
},
193+
}
194+
p := print.NewPrinter()
195+
p.Cmd = NewCmd(&types.CmdParams{Printer: p})
196+
for _, tt := range tests {
197+
t.Run(tt.name, func(t *testing.T) {
198+
if err := outputResult(p, tt.args.model, tt.args.options); (err != nil) != tt.wantErr {
199+
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
200+
}
201+
})
202+
}
203+
}

internal/cmd/ske/options/kubernetes_versions/kubernetes_versions.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,15 @@ func buildRequest(ctx context.Context, apiClient *ske.APIClient, model *inputMod
9595
}
9696

9797
func outputResult(p *print.Printer, model *inputModel, options *ske.ProviderOptions) error {
98-
if model == nil {
99-
return fmt.Errorf("model is nil")
100-
} else if options == nil {
98+
if options == nil {
10199
return fmt.Errorf("options is nil")
102100
}
103101

102+
options.AvailabilityZones = nil
103+
options.MachineImages = nil
104+
options.MachineTypes = nil
105+
options.VolumeTypes = nil
106+
104107
return p.OutputResult(model.OutputFormat, options, func() error {
105108
versions := utils.PtrValue(options.KubernetesVersions)
106109

0 commit comments

Comments
 (0)