Skip to content

Commit 5f4a80d

Browse files
committed
refactor(ufw):
- added tests for list implementation
1 parent 8ab1d41 commit 5f4a80d

2 files changed

Lines changed: 199 additions & 1 deletion

File tree

internal/cmd/ufw/rules/list/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
9393
}
9494

9595
if resp.Rules == nil {
96-
params.Printer.Info("(...)", projectLabel)
96+
params.Printer.Info("(...) %s", projectLabel)
9797
return nil
9898
}
9999

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

0 commit comments

Comments
 (0)