Skip to content

Commit

Permalink
Merge pull request #856 from LiZhenCheng9527/bpf-cleanup-fix
Browse files Browse the repository at this point in the history
fix clean bpf_map twice in test
  • Loading branch information
kmesh-bot authored Sep 14, 2024
2 parents 089540f + 8105676 commit 89adfd1
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 16 deletions.
3 changes: 3 additions & 0 deletions pkg/bpf/bpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,21 @@ func (l *BpfLoader) Stop() {
if l.config.AdsEnabled() {
C.deserial_uninit()
if err = l.obj.Detach(); err != nil {
CleanupBpfMap()
log.Errorf("failed detach when stop kmesh, err:%s", err)
return
}
} else if l.config.WdsEnabled() {
if err = l.workloadObj.Detach(); err != nil {
CleanupBpfMap()
log.Errorf("failed detach when stop kmesh, err:%s", err)
return
}
}

if l.config.EnableMda {
if err = StopMda(); err != nil {
CleanupBpfMap()
log.Errorf("failed disable mda when stop kmesh, err:%s", err)
return
}
Expand Down
12 changes: 4 additions & 8 deletions pkg/bpf/bpf_restart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,19 @@ func TestRestart(t *testing.T) {
}

func setDir(t *testing.T) options.BpfConfig {
CleanupBpfMap()
err := os.MkdirAll("/mnt/kmesh_cgroup2", 0755)
if err != nil {
if err := os.MkdirAll("/mnt/kmesh_cgroup2", 0755); err != nil {
t.Fatalf("Failed to create dir /mnt/kmesh_cgroup2: %v", err)
}
err = syscall.Mount("none", "/mnt/kmesh_cgroup2/", "cgroup2", 0, "")
if err != nil {
if err := syscall.Mount("none", "/mnt/kmesh_cgroup2/", "cgroup2", 0, ""); err != nil {
CleanupBpfMap()
t.Fatalf("Failed to mount /mnt/kmesh_cgroup2/: %v", err)
}
err = syscall.Mount("/sys/fs/bpf", "/sys/fs/bpf", "bpf", 0, "")
if err != nil {
if err := syscall.Mount("/sys/fs/bpf", "/sys/fs/bpf", "bpf", 0, ""); err != nil {
CleanupBpfMap()
t.Fatalf("Failed to mount /sys/fs/bpf: %v", err)
}

if err = rlimit.RemoveMemlock(); err != nil {
if err := rlimit.RemoveMemlock(); err != nil {
CleanupBpfMap()
t.Fatalf("Failed to remove mem limit: %v", err)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/controller/ads/ads_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,7 @@ func TestHandleLdsResponse(t *testing.T) {
BpfFsPath: "/sys/fs/bpf",
Cgroup2Path: "/mnt/kmesh_cgroup2",
}
cleanup, _ := test.InitBpfMap(t, config)
t.Cleanup(cleanup)
_, loader := test.InitBpfMap(t, config)
t.Run("normal function test", func(t *testing.T) {
adsLoader := NewAdsCache()
adsLoader.routeNames = []string{
Expand Down Expand Up @@ -655,6 +654,7 @@ func TestHandleLdsResponse(t *testing.T) {
assert.Equal(t, wantHash, actualHash)
assert.Equal(t, []string{"ut-rds"}, p.req.ResourceNames)
})
loader.Stop()
}

func TestHandleRdsResponse(t *testing.T) {
Expand All @@ -663,8 +663,7 @@ func TestHandleRdsResponse(t *testing.T) {
BpfFsPath: "/sys/fs/bpf",
Cgroup2Path: "/mnt/kmesh_cgroup2",
}
cleanup, _ := test.InitBpfMap(t, config)
t.Cleanup(cleanup)
_, loader := test.InitBpfMap(t, config)
t.Run("normal function test", func(t *testing.T) {
p := newProcessor()
p.ack = &service_discovery_v3.DiscoveryRequest{
Expand Down Expand Up @@ -816,4 +815,5 @@ func TestHandleRdsResponse(t *testing.T) {
assert.Equal(t, wantHash2, actualHash2)
assert.Equal(t, []string{"ut-routeconfig1", "ut-routeconfig2"}, p.ack.ResourceNames)
})
loader.Stop()
}
4 changes: 2 additions & 2 deletions pkg/status/status_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (s *Server) loggersHandler(w http.ResponseWriter, r *http.Request) {
}
}

func (s *Server) getLoggerNames(w http.ResponseWriter, r *http.Request) {
func (s *Server) getLoggerNames(w http.ResponseWriter) {
loggerNames := append(logger.GetLoggerNames(), bpfLoggerName)
data, err := json.MarshalIndent(&loggerNames, "", " ")
if err != nil {
Expand All @@ -180,7 +180,7 @@ func (s *Server) getLoggerNames(w http.ResponseWriter, r *http.Request) {
func (s *Server) getLoggerLevel(w http.ResponseWriter, r *http.Request) {
loggerName := r.URL.Query().Get("name")
if loggerName == "" {
s.getLoggerNames(w, r)
s.getLoggerNames(w)
return
}
var loggerInfo *LoggerInfo
Expand Down
2 changes: 1 addition & 1 deletion pkg/status/status_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ func TestServer_getAndSetBpfLevel(t *testing.T) {
for _, config := range configs {
t.Run(config.Mode, func(t *testing.T) {
cleanup, bpfLoader := test.InitBpfMap(t, config)
defer cleanup()
server := &Server{
xdsClient: &controller.XdsClient{
WorkloadController: &workload.Controller{
Expand Down Expand Up @@ -155,6 +154,7 @@ func TestServer_getAndSetBpfLevel(t *testing.T) {
assert.NotNil(t, expectedLoggerInfo)
assert.Equal(t, expectedLoggerInfo.Level, actualLoggerInfo.Level)
assert.Equal(t, expectedLoggerInfo.Name, actualLoggerInfo.Name)
cleanup()
})
}
}
Expand Down
1 change: 0 additions & 1 deletion pkg/utils/test/bpf_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ func InitBpfMap(t *testing.T, config options.BpfConfig) (CleanupFn, *bpf.BpfLoad
}
return func() {
loader.Stop()
bpf.CleanupBpfMap()
}, loader
}

Expand Down

0 comments on commit 89adfd1

Please sign in to comment.