Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions pkg/controller/security/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/agiledragon/gomonkey/v2"
"github.com/stretchr/testify/assert"
"istio.io/istio/pkg/security"
"istio.io/istio/pkg/test/util/retry"

camock "kmesh.net/kmesh/pkg/controller/security/mock"
)
Expand Down Expand Up @@ -124,7 +125,7 @@ func runTestCertRotate(t *testing.T) {
for {
secretManager.certsCache.mu.RLock()
cert2 := secretManager.certsCache.certs[identity1]
if cert2 != nil && cert2.cert.CreatedTime != oldCert.CreatedTime {
if cert2 != nil && cert2.cert != nil && cert2.cert.CreatedTime != oldCert.CreatedTime {
newCert = *cert2.cert
secretManager.certsCache.mu.RUnlock()
break
Expand Down Expand Up @@ -160,13 +161,32 @@ func runTestretryFetchCert(t *testing.T) {

go secretManager.Run(stopCh)
identity := "identity"
identity1 := "identity1"
secretManager.SendCertRequest(identity, ADD)
time.Sleep(100 * time.Millisecond)
patches2.Reset()
secretManager.SendCertRequest(identity1, RETRY)
time.Sleep(2000 * time.Millisecond)
assert.NotNil(t, secretManager.GetCert(identity).cert)

secretManager.SendCertRequest(identity, RETRY)

err = retry.UntilSuccess(
func() error {
cert := secretManager.GetCert(identity)
if cert != nil {
secretManager.certsCache.mu.RLock()
hasCert := cert.cert != nil
secretManager.certsCache.mu.RUnlock()
if hasCert {
return nil
}
}
return fmt.Errorf("cert not found for identity %s", identity)
},
retry.Delay(100*time.Millisecond),
retry.Timeout(6*time.Second),
)

if err != nil {
t.Errorf("Failed to fetch cert after retry: %v", err)
}

close(stopCh)
}
11 changes: 11 additions & 0 deletions pkg/status/status_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"io"
"net/http"
"net/http/pprof"
"sort"
"strconv"
"time"

Expand Down Expand Up @@ -568,6 +569,16 @@
}

func printWorkloadDump(w http.ResponseWriter, wd WorkloadDump) {
sort.Slice(wd.Workloads, func(i, j int) bool {
return wd.Workloads[i].Name < wd.Workloads[j].Name
})
sort.Slice(wd.Services, func(i, j int) bool {
return wd.Services[i].Name < wd.Services[j].Name
})
sort.Slice(wd.Policies, func(i, j int) bool {
return wd.Policies[i].Name < wd.Policies[j].Name
})

Check warning on line 580 in pkg/status/status_server.go

View check run for this annotation

Codecov / codecov/patch

pkg/status/status_server.go#L579-L580

Added lines #L579 - L580 were not covered by tests

data, err := json.MarshalIndent(wd, "", " ")
if err != nil {
log.Errorf("Failed to marshal WorkloadDump: %v", err)
Expand Down
140 changes: 126 additions & 14 deletions pkg/status/status_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand All @@ -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",
Expand All @@ -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)
Expand All @@ -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")

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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this make the workloads alreadt sorted?

Copy link
Contributor Author

@zrggw zrggw Jul 4, 2025

Choose a reason for hiding this comment

The 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{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why modify ports, does it influence the order?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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) {
Expand Down
Loading
Loading