Skip to content

Commit e17e5ea

Browse files
committed
[scanner] fix: add tests for federation provider action execution
Adds comprehensive table-driven tests for Clusternet provider action execution functions to improve coverage: - TestUnstructuredNestedBool: covers helper function with 9 test cases - TestClusternetApproveCluster_ValidationErrors: validates empty cluster name - TestClusternetUnregisterCluster_ValidationErrors: validates empty cluster name Signed-off-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 81fcf9e commit e17e5ea

1 file changed

Lines changed: 173 additions & 0 deletions

File tree

pkg/agent/federation/providers/clusternet_actions_test.go

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,176 @@ func TestClusternetExecuteUnknownAction(t *testing.T) {
7575
t.Error("expected error for unknown action")
7676
}
7777
}
78+
79+
func TestUnstructuredNestedBool(t *testing.T) {
80+
tests := []struct {
81+
name string
82+
obj map[string]interface{}
83+
fields []string
84+
wantValue bool
85+
wantFound bool
86+
wantErr bool
87+
}{
88+
{
89+
name: "simple bool field exists and is true",
90+
obj: map[string]interface{}{
91+
"approved": true,
92+
},
93+
fields: []string{"approved"},
94+
wantValue: true,
95+
wantFound: true,
96+
wantErr: false,
97+
},
98+
{
99+
name: "simple bool field exists and is false",
100+
obj: map[string]interface{}{
101+
"approved": false,
102+
},
103+
fields: []string{"approved"},
104+
wantValue: false,
105+
wantFound: true,
106+
wantErr: false,
107+
},
108+
{
109+
name: "nested bool field exists",
110+
obj: map[string]interface{}{
111+
"spec": map[string]interface{}{
112+
"approved": true,
113+
},
114+
},
115+
fields: []string{"spec", "approved"},
116+
wantValue: true,
117+
wantFound: true,
118+
wantErr: false,
119+
},
120+
{
121+
name: "deeply nested bool field exists",
122+
obj: map[string]interface{}{
123+
"spec": map[string]interface{}{
124+
"template": map[string]interface{}{
125+
"enabled": false,
126+
},
127+
},
128+
},
129+
fields: []string{"spec", "template", "enabled"},
130+
wantValue: false,
131+
wantFound: true,
132+
wantErr: false,
133+
},
134+
{
135+
name: "field does not exist",
136+
obj: map[string]interface{}{},
137+
fields: []string{"missing"},
138+
wantValue: false,
139+
wantFound: false,
140+
wantErr: false,
141+
},
142+
{
143+
name: "nested field does not exist",
144+
obj: map[string]interface{}{
145+
"spec": map[string]interface{}{},
146+
},
147+
fields: []string{"spec", "missing"},
148+
wantValue: false,
149+
wantFound: false,
150+
wantErr: false,
151+
},
152+
{
153+
name: "field is not bool",
154+
obj: map[string]interface{}{
155+
"value": "string",
156+
},
157+
fields: []string{"value"},
158+
wantValue: false,
159+
wantFound: false,
160+
wantErr: false,
161+
},
162+
{
163+
name: "intermediate field is not map",
164+
obj: map[string]interface{}{
165+
"spec": "not-a-map",
166+
},
167+
fields: []string{"spec", "approved"},
168+
wantValue: false,
169+
wantFound: false,
170+
wantErr: false,
171+
},
172+
{
173+
name: "nested field value is not bool",
174+
obj: map[string]interface{}{
175+
"spec": map[string]interface{}{
176+
"approved": 123,
177+
},
178+
},
179+
fields: []string{"spec", "approved"},
180+
wantValue: false,
181+
wantFound: false,
182+
wantErr: false,
183+
},
184+
}
185+
186+
for _, tt := range tests {
187+
t.Run(tt.name, func(t *testing.T) {
188+
gotValue, gotFound, gotErr := unstructuredNestedBool(tt.obj, tt.fields...)
189+
190+
if (gotErr != nil) != tt.wantErr {
191+
t.Errorf("unstructuredNestedBool() error = %v, wantErr %v", gotErr, tt.wantErr)
192+
return
193+
}
194+
195+
if gotValue != tt.wantValue {
196+
t.Errorf("unstructuredNestedBool() gotValue = %v, want %v", gotValue, tt.wantValue)
197+
}
198+
199+
if gotFound != tt.wantFound {
200+
t.Errorf("unstructuredNestedBool() gotFound = %v, want %v", gotFound, tt.wantFound)
201+
}
202+
})
203+
}
204+
}
205+
206+
func TestClusternetApproveCluster_ValidationErrors(t *testing.T) {
207+
tests := []struct {
208+
name string
209+
clusterName string
210+
wantErrSubstring string
211+
}{
212+
{
213+
name: "empty cluster name",
214+
clusterName: "",
215+
wantErrSubstring: "cluster not found",
216+
},
217+
}
218+
219+
for _, tt := range tests {
220+
t.Run(tt.name, func(t *testing.T) {
221+
_, err := clusternetApproveCluster(context.Background(), nil, tt.clusterName)
222+
if err == nil {
223+
t.Fatalf("expected error containing %q, got nil", tt.wantErrSubstring)
224+
}
225+
})
226+
}
227+
}
228+
229+
func TestClusternetUnregisterCluster_ValidationErrors(t *testing.T) {
230+
tests := []struct {
231+
name string
232+
clusterName string
233+
wantErrSubstring string
234+
}{
235+
{
236+
name: "empty cluster name",
237+
clusterName: "",
238+
wantErrSubstring: "cluster already removed",
239+
},
240+
}
241+
242+
for _, tt := range tests {
243+
t.Run(tt.name, func(t *testing.T) {
244+
_, err := clusternetUnregisterCluster(context.Background(), nil, tt.clusterName)
245+
if err == nil {
246+
t.Fatalf("expected error, got nil")
247+
}
248+
})
249+
}
250+
}

0 commit comments

Comments
 (0)