Skip to content

Commit 73e3ca3

Browse files
committed
refactor(ufw):
- added tests for describe implementation
1 parent 5f4a80d commit 73e3ca3

1 file changed

Lines changed: 217 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)