Skip to content

Commit e35895c

Browse files
authored
Merge pull request #1432 from zrggw/sort_dump_output
Sort dump output by name
2 parents 59a040f + 81896f5 commit e35895c

File tree

5 files changed

+1152
-19
lines changed

5 files changed

+1152
-19
lines changed

pkg/controller/security/manager_test.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/agiledragon/gomonkey/v2"
2525
"github.com/stretchr/testify/assert"
2626
"istio.io/istio/pkg/security"
27+
"istio.io/istio/pkg/test/util/retry"
2728

2829
camock "kmesh.net/kmesh/pkg/controller/security/mock"
2930
)
@@ -124,7 +125,7 @@ func runTestCertRotate(t *testing.T) {
124125
for {
125126
secretManager.certsCache.mu.RLock()
126127
cert2 := secretManager.certsCache.certs[identity1]
127-
if cert2 != nil && cert2.cert.CreatedTime != oldCert.CreatedTime {
128+
if cert2 != nil && cert2.cert != nil && cert2.cert.CreatedTime != oldCert.CreatedTime {
128129
newCert = *cert2.cert
129130
secretManager.certsCache.mu.RUnlock()
130131
break
@@ -160,13 +161,32 @@ func runTestretryFetchCert(t *testing.T) {
160161

161162
go secretManager.Run(stopCh)
162163
identity := "identity"
163-
identity1 := "identity1"
164164
secretManager.SendCertRequest(identity, ADD)
165165
time.Sleep(100 * time.Millisecond)
166166
patches2.Reset()
167-
secretManager.SendCertRequest(identity1, RETRY)
168-
time.Sleep(2000 * time.Millisecond)
169-
assert.NotNil(t, secretManager.GetCert(identity).cert)
167+
168+
secretManager.SendCertRequest(identity, RETRY)
169+
170+
err = retry.UntilSuccess(
171+
func() error {
172+
cert := secretManager.GetCert(identity)
173+
if cert != nil {
174+
secretManager.certsCache.mu.RLock()
175+
hasCert := cert.cert != nil
176+
secretManager.certsCache.mu.RUnlock()
177+
if hasCert {
178+
return nil
179+
}
180+
}
181+
return fmt.Errorf("cert not found for identity %s", identity)
182+
},
183+
retry.Delay(100*time.Millisecond),
184+
retry.Timeout(6*time.Second),
185+
)
186+
187+
if err != nil {
188+
t.Errorf("Failed to fetch cert after retry: %v", err)
189+
}
170190

171191
close(stopCh)
172192
}

pkg/status/status_server.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"io"
2424
"net/http"
2525
"net/http/pprof"
26+
"sort"
2627
"strconv"
2728
"time"
2829

@@ -568,6 +569,16 @@ func (s *Server) StopServer() error {
568569
}
569570

570571
func printWorkloadDump(w http.ResponseWriter, wd WorkloadDump) {
572+
sort.Slice(wd.Workloads, func(i, j int) bool {
573+
return wd.Workloads[i].Name < wd.Workloads[j].Name
574+
})
575+
sort.Slice(wd.Services, func(i, j int) bool {
576+
return wd.Services[i].Name < wd.Services[j].Name
577+
})
578+
sort.Slice(wd.Policies, func(i, j int) bool {
579+
return wd.Policies[i].Name < wd.Policies[j].Name
580+
})
581+
571582
data, err := json.MarshalIndent(wd, "", " ")
572583
if err != nil {
573584
log.Errorf("Failed to marshal WorkloadDump: %v", err)

pkg/status/status_server_test.go

Lines changed: 126 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

244356
func TestServer_dumpWorkloadBpfMap(t *testing.T) {

0 commit comments

Comments
 (0)