Skip to content

Commit

Permalink
fix: fix host application collector test
Browse files Browse the repository at this point in the history
Signed-off-by: j4ckstraw <[email protected]>
  • Loading branch information
j4ckstraw committed Nov 22, 2024
1 parent db3e629 commit 71a87b9
Showing 1 changed file with 90 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -170,6 +244,9 @@ total_unevictable 0
hostAppMemory: map[string]float64{
"test-host-app": 104857600,
},
hostAppMemoryWithPageCache: map[string]float64{
"test-host-app": 209715200,
},
},
},
}
Expand All @@ -196,6 +273,7 @@ total_unevictable 0
statesInformer := mock_statesinformer.NewMockStatesInformer(ctrl)
statesInformer.EXPECT().HasSynced().Return(true).AnyTimes()
statesInformer.EXPECT().GetNodeSLO().Return(tt.fields.getNodeSLO).Times(1)
statesInformer.EXPECT().GetNodeMetric().Return(&slov1alpha1.NodeMetric{}).AnyTimes()

collector := New(&framework.Options{
Config: &framework.Config{
Expand Down Expand Up @@ -238,6 +316,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)
}
})
}
}
Expand Down Expand Up @@ -305,6 +392,7 @@ func Test_hostAppCollector_Run(t *testing.T) {
mockStatesInformer := mock_statesinformer.NewMockStatesInformer(ctrl)
mockStatesInformer.EXPECT().HasSynced().Return(true).AnyTimes()
mockStatesInformer.EXPECT().GetNodeSLO().Return(&slov1alpha1.NodeSLO{}).AnyTimes()
mockStatesInformer.EXPECT().GetNodeMetric().Return(&slov1alpha1.NodeMetric{}).AnyTimes()
c := New(&framework.Options{
Config: framework.NewDefaultConfig(),
StatesInformer: mockStatesInformer,
Expand Down

0 comments on commit 71a87b9

Please sign in to comment.