-
Notifications
You must be signed in to change notification settings - Fork 132
Sort dump output by name #1432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sort dump output by name #1432
Changes from all commits
cac60ce
6b54579
52bc681
0690f18
04935ed
81896f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -133,11 +133,11 @@ func TestServer_setLoggerLevel(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| func TestServer_configDumpWorkload(t *testing.T) { | ||
| w1 := &workloadapi.Workload{ | ||
| func buildWorkload(name string) *workloadapi.Workload { | ||
| return &workloadapi.Workload{ | ||
| Uid: "cluster0//Pod/ns/name", | ||
| Namespace: "ns", | ||
| Name: "name", | ||
| Name: name, | ||
| Addresses: [][]byte{netip.AddrFrom4([4]byte{1, 2, 3, 4}).AsSlice()}, | ||
| Network: "testnetwork", | ||
| CanonicalName: "foo", | ||
|
|
@@ -173,10 +173,13 @@ func TestServer_configDumpWorkload(t *testing.T) { | |
| }, | ||
| }, | ||
| } | ||
| svc := &workloadapi.Service{ | ||
| Name: "svc", | ||
| } | ||
|
|
||
| func buildService(name, hostname string) *workloadapi.Service { | ||
| return &workloadapi.Service{ | ||
| Name: name, | ||
| Namespace: "ns", | ||
| Hostname: "hostname", | ||
| Hostname: hostname, | ||
| Ports: []*workloadapi.Port{ | ||
| { | ||
| ServicePort: 80, | ||
|
|
@@ -199,6 +202,12 @@ func TestServer_configDumpWorkload(t *testing.T) { | |
| }, | ||
| }, | ||
| }} | ||
| } | ||
|
|
||
| func TestServer_configDumpWorkload(t *testing.T) { | ||
| w := buildWorkload("name") | ||
| svc := buildService("svc", "hostname") | ||
|
|
||
| policy := &security.Authorization{ | ||
| Name: "policy", | ||
| Namespace: "ns", | ||
|
|
@@ -207,7 +216,7 @@ func TestServer_configDumpWorkload(t *testing.T) { | |
| } | ||
| fakeWorkloadCache := cache.NewWorkloadCache() | ||
| fakeServiceCache := cache.NewServiceCache() | ||
| fakeWorkloadCache.AddOrUpdateWorkload(w1) | ||
| fakeWorkloadCache.AddOrUpdateWorkload(w) | ||
| fakeServiceCache.AddOrUpdateService(svc) | ||
| fakeAuth := auth.NewRbac(fakeWorkloadCache) | ||
| fakeAuth.UpdatePolicy(policy) | ||
|
|
@@ -225,20 +234,123 @@ func TestServer_configDumpWorkload(t *testing.T) { | |
| } | ||
|
|
||
| // Create a new HTTP request and response | ||
| req := httptest.NewRequest(http.MethodGet, "/configDumpWorkload", nil) | ||
| w := httptest.NewRecorder() | ||
| req1 := httptest.NewRequest(http.MethodGet, "/configDumpWorkload", nil) | ||
| w1 := httptest.NewRecorder() | ||
|
|
||
| // Call the configDumpWorkload function | ||
| server.configDumpWorkload(w, req) | ||
| server.configDumpWorkload(w1, req1) | ||
|
|
||
| // Check the response status code | ||
| if w.Code != http.StatusOK { | ||
| t.Errorf("Expected status code %d, but got %d", http.StatusOK, w.Code) | ||
| if w1.Code != http.StatusOK { | ||
| t.Errorf("Expected status code %d, but got %d", http.StatusOK, w1.Code) | ||
| } | ||
|
|
||
| util.RefreshGoldenFile(t, w1.Body.Bytes(), "./testdata/workload_configdump.json") | ||
|
|
||
| util.CompareContent(t, w1.Body.Bytes(), "./testdata/workload_configdump.json") | ||
hzxuzhonghu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| fakeWorkloadCache = cache.NewWorkloadCache() | ||
| fakeServiceCache = cache.NewServiceCache() | ||
|
|
||
| workloads := []*workloadapi.Workload{} | ||
| services := []*workloadapi.Service{} | ||
|
|
||
| for i := 0; i < 10; i++ { | ||
| w := buildWorkload(fmt.Sprintf("workload-%d", i)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this make the workloads alreadt sorted?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Later, when w is added to fakeWorkloadCache(line 326) and then a workDump is created(line 346 and status_servers.go, line 493), the wokload in workloadDump will be unordered due to the use of map. |
||
| w.Uid = fmt.Sprintf("cluster0//Pod/ns/workload-%d", i) | ||
| workloads = append(workloads, w) | ||
| svc := buildService(fmt.Sprintf("service-%d", i), fmt.Sprintf("hostname-%d", i)) | ||
| services = append(services, svc) | ||
|
|
||
| fakeWorkloadCache.AddOrUpdateWorkload(w) | ||
| fakeServiceCache.AddOrUpdateService(svc) | ||
| } | ||
|
|
||
| util.RefreshGoldenFile(t, w.Body.Bytes(), "./testdata/workload_configdump.json") | ||
| // Create a new HTTP response | ||
| w2 := httptest.NewRecorder() | ||
|
|
||
| server = &Server{ | ||
| xdsClient: &controller.XdsClient{ | ||
| WorkloadController: &workload.Controller{ | ||
| Processor: &workload.Processor{ | ||
| WorkloadCache: fakeWorkloadCache, | ||
| ServiceCache: fakeServiceCache, | ||
| }, | ||
| Rbac: fakeAuth, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| // Call the configDumpWorkload function | ||
| server.configDumpWorkload(w2, req1) | ||
|
|
||
| // Check the response status code | ||
| if w2.Code != http.StatusOK { | ||
| t.Errorf("Expected status code %d, but got %d", http.StatusOK, w2.Code) | ||
| } | ||
|
|
||
| util.RefreshGoldenFile(t, w2.Body.Bytes(), "./testdata/workload_configdump_original_sorted.json") | ||
| util.CompareContent(t, w2.Body.Bytes(), "./testdata/workload_configdump_original_sorted.json") | ||
|
|
||
| fakeWorkloadCache = cache.NewWorkloadCache() | ||
| fakeServiceCache = cache.NewServiceCache() | ||
| // Modify workloads and services properties | ||
| for i := 0; i < 5; i++ { // Modify first 5 items | ||
| // Modify workload properties | ||
| w := buildWorkload(fmt.Sprintf("workload-%d-modified", i)) | ||
| w.ClusterId = "cluster1" // Changed cluster | ||
| w.Uid = fmt.Sprintf("cluster1//Pod/ns/workload-%d-modified", i) | ||
| w.Status = workloadapi.WorkloadStatus_UNHEALTHY | ||
|
|
||
| workloads[i] = w | ||
| // Modify service properties | ||
| svc := buildService(fmt.Sprintf("service-%d-modified", i), fmt.Sprintf("hostname-%d-modified", i)) | ||
| // Modify service ports | ||
| svc.Ports = []*workloadapi.Port{ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why modify ports, does it influence the order?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to test that the sorting is not affected by other modifications, like port modifications. |
||
| { | ||
| ServicePort: 90, | ||
| TargetPort: 9090, | ||
| }, | ||
| { | ||
| ServicePort: 91, | ||
| TargetPort: 0, | ||
| }, | ||
| { | ||
| ServicePort: 92, | ||
| TargetPort: 0, | ||
| }, | ||
| } | ||
| services[i] = svc | ||
| } | ||
| for _, w := range workloads { | ||
| fakeWorkloadCache.AddOrUpdateWorkload(w) | ||
| } | ||
| for _, svc := range services { | ||
| fakeServiceCache.AddOrUpdateService(svc) | ||
| } | ||
|
|
||
| w3 := httptest.NewRecorder() | ||
|
|
||
| server = &Server{ | ||
| xdsClient: &controller.XdsClient{ | ||
| WorkloadController: &workload.Controller{ | ||
| Processor: &workload.Processor{ | ||
| WorkloadCache: fakeWorkloadCache, | ||
| ServiceCache: fakeServiceCache, | ||
| }, | ||
| Rbac: fakeAuth, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| server.configDumpWorkload(w3, req1) | ||
|
|
||
| if w3.Code != http.StatusOK { | ||
| t.Errorf("Expected status code %d, but got %d", http.StatusOK, w3.Code) | ||
| } | ||
|
|
||
| util.CompareContent(t, w.Body.Bytes(), "./testdata/workload_configdump.json") | ||
| util.RefreshGoldenFile(t, w3.Body.Bytes(), "./testdata/workload_configdump_modified_sorted.json") | ||
| util.CompareContent(t, w3.Body.Bytes(), "./testdata/workload_configdump_modified_sorted.json") | ||
| } | ||
|
|
||
| func TestServer_dumpWorkloadBpfMap(t *testing.T) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.