Skip to content

Commit 2d5bcd4

Browse files
committed
refactor(ufw):
- added tests for delete implementation
1 parent 73e3ca3 commit 2d5bcd4

1 file changed

Lines changed: 182 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)