diff --git a/pkg/cache/v2/cluster_test.go b/pkg/cache/v2/cluster_test.go index d7e4d8193..523941331 100644 --- a/pkg/cache/v2/cluster_test.go +++ b/pkg/cache/v2/cluster_test.go @@ -28,6 +28,7 @@ import ( cluster_v2 "kmesh.net/kmesh/api/v2/cluster" core_v2 "kmesh.net/kmesh/api/v2/core" "kmesh.net/kmesh/api/v2/endpoint" + "kmesh.net/kmesh/daemon/options" maps_v2 "kmesh.net/kmesh/pkg/cache/v2/maps" "kmesh.net/kmesh/pkg/nets" "kmesh.net/kmesh/pkg/utils/hash" @@ -178,7 +179,12 @@ func TestClusterFlush(t *testing.T) { func BenchmarkClusterFlush(b *testing.B) { t := &testing.T{} - cleanup := test.InitBpfMap(t) + config := options.BpfConfig{ + Mode: "ads", + BpfFsPath: "/sys/fs/bpf", + Cgroup2Path: "/mnt/kmesh_cgroup2", + } + cleanup, _ := test.InitBpfMap(t, config) b.Cleanup(cleanup) cluster := cluster_v2.Cluster{ diff --git a/pkg/cache/v2/listener_test.go b/pkg/cache/v2/listener_test.go index 606fc4d33..7474f7f56 100644 --- a/pkg/cache/v2/listener_test.go +++ b/pkg/cache/v2/listener_test.go @@ -28,6 +28,7 @@ import ( core_v2 "kmesh.net/kmesh/api/v2/core" "kmesh.net/kmesh/api/v2/filter" listener_v2 "kmesh.net/kmesh/api/v2/listener" + "kmesh.net/kmesh/daemon/options" maps_v2 "kmesh.net/kmesh/pkg/cache/v2/maps" "kmesh.net/kmesh/pkg/nets" "kmesh.net/kmesh/pkg/utils/hash" @@ -187,7 +188,12 @@ func TestListenerFlush(t *testing.T) { func BenchmarkFlush(b *testing.B) { t := &testing.T{} - cleanup := test.InitBpfMap(t) + config := options.BpfConfig{ + Mode: "ads", + BpfFsPath: "/sys/fs/bpf", + Cgroup2Path: "/mnt/kmesh_cgroup2", + } + cleanup, _ := test.InitBpfMap(t, config) b.Cleanup(cleanup) listener := &listener_v2.Listener{ diff --git a/pkg/controller/ads/ads_processor_test.go b/pkg/controller/ads/ads_processor_test.go index 05a3b428f..62a0384cf 100644 --- a/pkg/controller/ads/ads_processor_test.go +++ b/pkg/controller/ads/ads_processor_test.go @@ -32,13 +32,19 @@ import ( cluster_v2 "kmesh.net/kmesh/api/v2/cluster" core_v2 "kmesh.net/kmesh/api/v2/core" + "kmesh.net/kmesh/daemon/options" cache_v2 "kmesh.net/kmesh/pkg/cache/v2" "kmesh.net/kmesh/pkg/utils/hash" "kmesh.net/kmesh/pkg/utils/test" ) func TestHandleCdsResponse(t *testing.T) { - cleanup := test.InitBpfMap(t) + config := options.BpfConfig{ + Mode: "ads", + BpfFsPath: "/sys/fs/bpf", + Cgroup2Path: "/mnt/kmesh_cgroup2", + } + cleanup, _ := test.InitBpfMap(t, config) t.Cleanup(cleanup) t.Run("new cluster, cluster type is eds", func(t *testing.T) { p := newProcessor() @@ -246,7 +252,12 @@ func TestHandleCdsResponse(t *testing.T) { } func TestHandleEdsResponse(t *testing.T) { - cleanup := test.InitBpfMap(t) + config := options.BpfConfig{ + Mode: "ads", + BpfFsPath: "/sys/fs/bpf", + Cgroup2Path: "/mnt/kmesh_cgroup2", + } + cleanup, _ := test.InitBpfMap(t, config) t.Cleanup(cleanup) t.Run("cluster's apiStatus is UPDATE", func(t *testing.T) { p := newProcessor() @@ -402,7 +413,12 @@ func TestHandleEdsResponse(t *testing.T) { } func TestHandleLdsResponse(t *testing.T) { - cleanup := test.InitBpfMap(t) + config := options.BpfConfig{ + Mode: "ads", + BpfFsPath: "/sys/fs/bpf", + Cgroup2Path: "/mnt/kmesh_cgroup2", + } + cleanup, _ := test.InitBpfMap(t, config) t.Cleanup(cleanup) t.Run("normal function test", func(t *testing.T) { adsLoader := NewAdsCache() @@ -574,7 +590,12 @@ func TestHandleLdsResponse(t *testing.T) { } func TestHandleRdsResponse(t *testing.T) { - cleanup := test.InitBpfMap(t) + config := options.BpfConfig{ + Mode: "ads", + BpfFsPath: "/sys/fs/bpf", + Cgroup2Path: "/mnt/kmesh_cgroup2", + } + cleanup, _ := test.InitBpfMap(t, config) t.Cleanup(cleanup) t.Run("normal function test", func(t *testing.T) { p := newProcessor() diff --git a/pkg/controller/client_test.go b/pkg/controller/client_test.go index 4e952b6d5..ca091cce9 100644 --- a/pkg/controller/client_test.go +++ b/pkg/controller/client_test.go @@ -24,9 +24,9 @@ import ( "testing" "github.com/agiledragon/gomonkey/v2" + "github.com/stretchr/testify/assert" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" - "gotest.tools/assert" "kmesh.net/kmesh/pkg/bpf" "kmesh.net/kmesh/pkg/constants" @@ -80,7 +80,7 @@ func TestClientResponseProcess(t *testing.T) { utClient := NewXdsClient(constants.AdsMode, &bpf.BpfKmeshWorkload{}) err := utClient.createGrpcStreamClient() - assert.NilError(t, err) + assert.NoError(t, err) reConnectPatches := gomonkey.NewPatches() defer reConnectPatches.Reset() @@ -127,7 +127,7 @@ func TestClientResponseProcess(t *testing.T) { utClient := NewXdsClient(constants.WorkloadMode, &bpf.BpfKmeshWorkload{}) err := utClient.createGrpcStreamClient() - assert.NilError(t, err) + assert.NoError(t, err) reConnectPatches := gomonkey.NewPatches() defer reConnectPatches.Reset() diff --git a/pkg/controller/workload/workload_processor_test.go b/pkg/controller/workload/workload_processor_test.go new file mode 100644 index 000000000..dcc107276 --- /dev/null +++ b/pkg/controller/workload/workload_processor_test.go @@ -0,0 +1,86 @@ +/* + * Copyright 2024 The Kmesh Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package workload + +import ( + "net/netip" + "testing" + + "github.com/stretchr/testify/assert" + "k8s.io/apimachinery/pkg/util/rand" + + "kmesh.net/kmesh/api/v2/workloadapi" + "kmesh.net/kmesh/daemon/options" + "kmesh.net/kmesh/pkg/constants" + "kmesh.net/kmesh/pkg/utils/test" +) + +func BenchmarkHandleDataWithService(b *testing.B) { + t := &testing.T{} + config := options.BpfConfig{ + Mode: constants.WorkloadMode, + BpfFsPath: "/sys/fs/bpf", + Cgroup2Path: "/mnt/kmesh_cgroup2", + EnableMda: false, + } + cleanup, bpfLoader := test.InitBpfMap(t, config) + b.Cleanup(cleanup) + + workloadController := NewController(bpfLoader.GetBpfKmeshWorkload().SockConn.KmeshCgroupSockWorkloadMaps) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + workload := createTestWorkload() + err := workloadController.Processor.handleDataWithService(workload) + assert.NoError(t, err) + } +} + +func createTestWorkload() *workloadapi.Workload { + workload := workloadapi.Workload{ + Namespace: "ns", + Name: "name", + Addresses: [][]byte{netip.AddrFrom4([4]byte{1, 2, 3, 4}).AsSlice()}, + Network: "testnetwork", + CanonicalName: "foo", + CanonicalRevision: "latest", + WorkloadType: workloadapi.WorkloadType_POD, + WorkloadName: "name", + Status: workloadapi.WorkloadStatus_HEALTHY, + ClusterId: "cluster0", + Services: map[string]*workloadapi.PortList{ + "ns/hostname": { + Ports: []*workloadapi.Port{ + { + ServicePort: 80, + TargetPort: 8080, + }, + { + ServicePort: 81, + TargetPort: 8180, + }, + { + ServicePort: 82, + TargetPort: 82, + }, + }, + }, + }, + } + workload.Uid = "cluster0/" + rand.String(6) + return &workload +} diff --git a/pkg/utils/test/bpf_map.go b/pkg/utils/test/bpf_map.go index a273717d6..9c38f08ec 100644 --- a/pkg/utils/test/bpf_map.go +++ b/pkg/utils/test/bpf_map.go @@ -24,11 +24,12 @@ import ( "kmesh.net/kmesh/daemon/options" "kmesh.net/kmesh/pkg/bpf" + "kmesh.net/kmesh/pkg/constants" ) type CleanupFn func() -func InitBpfMap(t *testing.T) CleanupFn { +func InitBpfMap(t *testing.T, config options.BpfConfig) (CleanupFn, *bpf.BpfLoader) { err := os.MkdirAll("/mnt/kmesh_cgroup2", 0755) if err != nil { t.Fatalf("Failed to create dir /mnt/kmesh_cgroup2: %v", err) @@ -43,13 +44,16 @@ func InitBpfMap(t *testing.T) CleanupFn { CleanupBpfMap() t.Fatalf("Failed to mount /sys/fs/bpf: %v", err) } - config := options.BpfConfig{ - Mode: "ads", - BpfFsPath: "/sys/fs/bpf", - Cgroup2Path: "/mnt/kmesh_cgroup2", - } + loader := bpf.NewBpfLoader(&config) - err = loader.StartAdsMode() + + if config.Mode == constants.AdsMode { + err = loader.StartAdsMode() + } + if config.Mode == constants.WorkloadMode { + err = loader.StartWorkloadMode() + } + if err != nil { CleanupBpfMap() t.Fatalf("bpf init failed: %v", err) @@ -57,7 +61,7 @@ func InitBpfMap(t *testing.T) CleanupFn { return func() { loader.Stop() CleanupBpfMap() - } + }, loader } func CleanupBpfMap() {