From 9b9d08237285a16de805b0cc499f79399d27724b Mon Sep 17 00:00:00 2001 From: j4ckstraw Date: Mon, 18 Nov 2024 19:59:10 +0800 Subject: [PATCH] fix: fix host application collector test Signed-off-by: j4ckstraw --- .../host_app_collector_test.go | 90 ++++++++++++++++++- 1 file changed, 88 insertions(+), 2 deletions(-) diff --git a/pkg/koordlet/metricsadvisor/collectors/hostapplication/host_app_collector_test.go b/pkg/koordlet/metricsadvisor/collectors/hostapplication/host_app_collector_test.go index 58cd24f0e..f355079c3 100644 --- a/pkg/koordlet/metricsadvisor/collectors/hostapplication/host_app_collector_test.go +++ b/pkg/koordlet/metricsadvisor/collectors/hostapplication/host_app_collector_test.go @@ -45,8 +45,9 @@ func Test_hostAppCollector_collectHostAppResUsed(t *testing.T) { initLastStat func(lastState *gocache.Cache) } type wants struct { - hostAppCPU map[string]float64 - hostAppMemory map[string]float64 + hostAppCPU map[string]float64 + hostAppMemory map[string]float64 + hostAppMemoryWithPageCache map[string]float64 } tests := []struct { name string @@ -74,6 +75,77 @@ total_inactive_file 104857600 total_active_file 0 total_unevictable 0 `) + helper.WriteCgroupFileContents(testParentDir, system.MemoryUsage, `209715200`) + }, + getNodeSLO: &slov1alpha1.NodeSLO{ + Spec: slov1alpha1.NodeSLOSpec{ + HostApplications: []slov1alpha1.HostApplicationSpec{ + { + Name: "test-host-app", + CgroupPath: &slov1alpha1.CgroupPath{ + Base: slov1alpha1.CgroupBaseTypeKubeBesteffort, + RelativePath: "test-host-app/", + }, + }, + }, + }, + }, + initLastStat: func(lastState *gocache.Cache) { + lastState.Set("test-host-app", framework.CPUStat{ + CPUUsage: 0, + Timestamp: testNow.Add(-time.Second), + }, gocache.DefaultExpiration) + }, + }, + wants: wants{}, + }, + { + name: "host app metric with bad memory stat format", + fields: fields{ + SetSysUtil: func(helper *system.FileTestUtil) { + helper.WriteCgroupFileContents(testParentDir, system.CPUAcctUsage, `1000000000`) + helper.WriteCgroupFileContents(testParentDir, system.MemoryStat, ` +bad format +`) + helper.WriteCgroupFileContents(testParentDir, system.MemoryUsage, `209715200`) + }, + getNodeSLO: &slov1alpha1.NodeSLO{ + Spec: slov1alpha1.NodeSLOSpec{ + HostApplications: []slov1alpha1.HostApplicationSpec{ + { + Name: "test-host-app", + CgroupPath: &slov1alpha1.CgroupPath{ + Base: slov1alpha1.CgroupBaseTypeKubeBesteffort, + RelativePath: "test-host-app/", + }, + }, + }, + }, + }, + initLastStat: func(lastState *gocache.Cache) { + lastState.Set("test-host-app", framework.CPUStat{ + CPUUsage: 0, + Timestamp: testNow.Add(-time.Second), + }, gocache.DefaultExpiration) + }, + }, + wants: wants{}, + }, + { + name: "host app metric with bad memory usage format", + fields: fields{ + SetSysUtil: func(helper *system.FileTestUtil) { + helper.WriteCgroupFileContents(testParentDir, system.CPUAcctUsage, `1000000000`) + helper.WriteCgroupFileContents(testParentDir, system.MemoryStat, ` +total_cache 104857600 +total_rss 104857600 +total_inactive_anon 104857600 +total_active_anon 0 +total_inactive_file 104857600 +total_active_file 0 +total_unevictable 0 +`) + helper.WriteCgroupFileContents(testParentDir, system.MemoryUsage, `bad format`) }, getNodeSLO: &slov1alpha1.NodeSLO{ Spec: slov1alpha1.NodeSLOSpec{ @@ -111,6 +183,7 @@ total_inactive_file 104857600 total_active_file 0 total_unevictable 0 `) + helper.WriteCgroupFileContents(testParentDir, system.MemoryUsage, `209715200`) }, getNodeSLO: &slov1alpha1.NodeSLO{ Spec: slov1alpha1.NodeSLOSpec{ @@ -142,6 +215,7 @@ total_inactive_file 104857600 total_active_file 0 total_unevictable 0 `) + helper.WriteCgroupFileContents(testParentDir, system.MemoryUsage, `209715200`) }, getNodeSLO: &slov1alpha1.NodeSLO{ Spec: slov1alpha1.NodeSLOSpec{ @@ -170,6 +244,9 @@ total_unevictable 0 hostAppMemory: map[string]float64{ "test-host-app": 104857600, }, + hostAppMemoryWithPageCache: map[string]float64{ + "test-host-app": 209715200, + }, }, }, } @@ -238,6 +315,15 @@ total_unevictable 0 assert.NoError(t, err) assert.Equal(t, wantMemory, gotMemory) } + for appName, wantMemory := range tt.wants.hostAppMemoryWithPageCache { + queryMeta, err := metriccache.HostAppMemoryUsageWithPageCacheMetric.BuildQueryMeta(metriccache.MetricPropertiesFunc.HostApplication(appName)) + assert.NoError(t, err) + aggregateResult := metriccache.DefaultAggregateResultFactory.New(queryMeta) + assert.NoError(t, querier.Query(queryMeta, nil, aggregateResult)) + gotMemoryWithPageCache, err := aggregateResult.Value(metriccache.AggregationTypeLast) + assert.NoError(t, err) + assert.Equal(t, wantMemory, gotMemoryWithPageCache) + } }) } }