@@ -133,11 +133,11 @@ func TestServer_setLoggerLevel(t *testing.T) {
133133 }
134134}
135135
136- func TestServer_configDumpWorkload ( t * testing. T ) {
137- w1 := & workloadapi.Workload {
136+ func buildWorkload ( name string ) * workloadapi. Workload {
137+ return & workloadapi.Workload {
138138 Uid : "cluster0//Pod/ns/name" ,
139139 Namespace : "ns" ,
140- Name : " name" ,
140+ Name : name ,
141141 Addresses : [][]byte {netip .AddrFrom4 ([4 ]byte {1 , 2 , 3 , 4 }).AsSlice ()},
142142 Network : "testnetwork" ,
143143 CanonicalName : "foo" ,
@@ -173,10 +173,13 @@ func TestServer_configDumpWorkload(t *testing.T) {
173173 },
174174 },
175175 }
176- svc := & workloadapi.Service {
177- Name : "svc" ,
176+ }
177+
178+ func buildService (name , hostname string ) * workloadapi.Service {
179+ return & workloadapi.Service {
180+ Name : name ,
178181 Namespace : "ns" ,
179- Hostname : " hostname" ,
182+ Hostname : hostname ,
180183 Ports : []* workloadapi.Port {
181184 {
182185 ServicePort : 80 ,
@@ -199,6 +202,12 @@ func TestServer_configDumpWorkload(t *testing.T) {
199202 },
200203 },
201204 }}
205+ }
206+
207+ func TestServer_configDumpWorkload (t * testing.T ) {
208+ w := buildWorkload ("name" )
209+ svc := buildService ("svc" , "hostname" )
210+
202211 policy := & security.Authorization {
203212 Name : "policy" ,
204213 Namespace : "ns" ,
@@ -207,7 +216,7 @@ func TestServer_configDumpWorkload(t *testing.T) {
207216 }
208217 fakeWorkloadCache := cache .NewWorkloadCache ()
209218 fakeServiceCache := cache .NewServiceCache ()
210- fakeWorkloadCache .AddOrUpdateWorkload (w1 )
219+ fakeWorkloadCache .AddOrUpdateWorkload (w )
211220 fakeServiceCache .AddOrUpdateService (svc )
212221 fakeAuth := auth .NewRbac (fakeWorkloadCache )
213222 fakeAuth .UpdatePolicy (policy )
@@ -225,20 +234,123 @@ func TestServer_configDumpWorkload(t *testing.T) {
225234 }
226235
227236 // Create a new HTTP request and response
228- req := httptest .NewRequest (http .MethodGet , "/configDumpWorkload" , nil )
229- w := httptest .NewRecorder ()
237+ req1 := httptest .NewRequest (http .MethodGet , "/configDumpWorkload" , nil )
238+ w1 := httptest .NewRecorder ()
230239
231240 // Call the configDumpWorkload function
232- server .configDumpWorkload (w , req )
241+ server .configDumpWorkload (w1 , req1 )
233242
234243 // Check the response status code
235- if w .Code != http .StatusOK {
236- t .Errorf ("Expected status code %d, but got %d" , http .StatusOK , w .Code )
244+ if w1 .Code != http .StatusOK {
245+ t .Errorf ("Expected status code %d, but got %d" , http .StatusOK , w1 .Code )
246+ }
247+
248+ util .RefreshGoldenFile (t , w1 .Body .Bytes (), "./testdata/workload_configdump.json" )
249+
250+ util .CompareContent (t , w1 .Body .Bytes (), "./testdata/workload_configdump.json" )
251+
252+ fakeWorkloadCache = cache .NewWorkloadCache ()
253+ fakeServiceCache = cache .NewServiceCache ()
254+
255+ workloads := []* workloadapi.Workload {}
256+ services := []* workloadapi.Service {}
257+
258+ for i := 0 ; i < 10 ; i ++ {
259+ w := buildWorkload (fmt .Sprintf ("workload-%d" , i ))
260+ w .Uid = fmt .Sprintf ("cluster0//Pod/ns/workload-%d" , i )
261+ workloads = append (workloads , w )
262+ svc := buildService (fmt .Sprintf ("service-%d" , i ), fmt .Sprintf ("hostname-%d" , i ))
263+ services = append (services , svc )
264+
265+ fakeWorkloadCache .AddOrUpdateWorkload (w )
266+ fakeServiceCache .AddOrUpdateService (svc )
237267 }
238268
239- util .RefreshGoldenFile (t , w .Body .Bytes (), "./testdata/workload_configdump.json" )
269+ // Create a new HTTP response
270+ w2 := httptest .NewRecorder ()
271+
272+ server = & Server {
273+ xdsClient : & controller.XdsClient {
274+ WorkloadController : & workload.Controller {
275+ Processor : & workload.Processor {
276+ WorkloadCache : fakeWorkloadCache ,
277+ ServiceCache : fakeServiceCache ,
278+ },
279+ Rbac : fakeAuth ,
280+ },
281+ },
282+ }
283+
284+ // Call the configDumpWorkload function
285+ server .configDumpWorkload (w2 , req1 )
286+
287+ // Check the response status code
288+ if w2 .Code != http .StatusOK {
289+ t .Errorf ("Expected status code %d, but got %d" , http .StatusOK , w2 .Code )
290+ }
291+
292+ util .RefreshGoldenFile (t , w2 .Body .Bytes (), "./testdata/workload_configdump_original_sorted.json" )
293+ util .CompareContent (t , w2 .Body .Bytes (), "./testdata/workload_configdump_original_sorted.json" )
294+
295+ fakeWorkloadCache = cache .NewWorkloadCache ()
296+ fakeServiceCache = cache .NewServiceCache ()
297+ // Modify workloads and services properties
298+ for i := 0 ; i < 5 ; i ++ { // Modify first 5 items
299+ // Modify workload properties
300+ w := buildWorkload (fmt .Sprintf ("workload-%d-modified" , i ))
301+ w .ClusterId = "cluster1" // Changed cluster
302+ w .Uid = fmt .Sprintf ("cluster1//Pod/ns/workload-%d-modified" , i )
303+ w .Status = workloadapi .WorkloadStatus_UNHEALTHY
304+
305+ workloads [i ] = w
306+ // Modify service properties
307+ svc := buildService (fmt .Sprintf ("service-%d-modified" , i ), fmt .Sprintf ("hostname-%d-modified" , i ))
308+ // Modify service ports
309+ svc .Ports = []* workloadapi.Port {
310+ {
311+ ServicePort : 90 ,
312+ TargetPort : 9090 ,
313+ },
314+ {
315+ ServicePort : 91 ,
316+ TargetPort : 0 ,
317+ },
318+ {
319+ ServicePort : 92 ,
320+ TargetPort : 0 ,
321+ },
322+ }
323+ services [i ] = svc
324+ }
325+ for _ , w := range workloads {
326+ fakeWorkloadCache .AddOrUpdateWorkload (w )
327+ }
328+ for _ , svc := range services {
329+ fakeServiceCache .AddOrUpdateService (svc )
330+ }
331+
332+ w3 := httptest .NewRecorder ()
333+
334+ server = & Server {
335+ xdsClient : & controller.XdsClient {
336+ WorkloadController : & workload.Controller {
337+ Processor : & workload.Processor {
338+ WorkloadCache : fakeWorkloadCache ,
339+ ServiceCache : fakeServiceCache ,
340+ },
341+ Rbac : fakeAuth ,
342+ },
343+ },
344+ }
345+
346+ server .configDumpWorkload (w3 , req1 )
347+
348+ if w3 .Code != http .StatusOK {
349+ t .Errorf ("Expected status code %d, but got %d" , http .StatusOK , w3 .Code )
350+ }
240351
241- util .CompareContent (t , w .Body .Bytes (), "./testdata/workload_configdump.json" )
352+ util .RefreshGoldenFile (t , w3 .Body .Bytes (), "./testdata/workload_configdump_modified_sorted.json" )
353+ util .CompareContent (t , w3 .Body .Bytes (), "./testdata/workload_configdump_modified_sorted.json" )
242354}
243355
244356func TestServer_dumpWorkloadBpfMap (t * testing.T ) {
0 commit comments