@@ -78,6 +78,7 @@ func Test_collector_collectPodResUsed(t *testing.T) {
7878 }
7979 type fields struct {
8080 podFilterOption framework.PodFilter
81+ deviceCollectors map [string ]framework.DeviceCollector
8182 getPodMetas []* statesinformer.PodMeta
8283 initPodLastStat func (lastState * gocache.Cache )
8384 initContainerLastStat func (lastState * gocache.Cache )
@@ -96,6 +97,9 @@ func Test_collector_collectPodResUsed(t *testing.T) {
9697 name : "cgroups v1" ,
9798 fields : fields {
9899 podFilterOption : framework .DefaultPodFilter ,
100+ deviceCollectors : map [string ]framework.DeviceCollector {
101+ "TestDeviceCollector" : & fakeDeviceCollector {isEnabled : true },
102+ },
99103 getPodMetas : []* statesinformer.PodMeta {
100104 {
101105 CgroupDir : testPodMetaDir ,
@@ -150,6 +154,9 @@ total_unevictable 0
150154 name : "cgroups v2" ,
151155 fields : fields {
152156 podFilterOption : framework .DefaultPodFilter ,
157+ deviceCollectors : map [string ]framework.DeviceCollector {
158+ "TestDeviceCollector" : & fakeDeviceCollector {isEnabled : true },
159+ },
153160 getPodMetas : []* statesinformer.PodMeta {
154161 {
155162 CgroupDir : testPodMetaDir ,
@@ -212,9 +219,20 @@ unevictable 0
212219 },
213220 },
214221 {
215- name : "cgroups v1, filter non-running pods" ,
222+ name : "cgroups v1, filter non-running pods, skip disabled device collector " ,
216223 fields : fields {
217224 podFilterOption : & framework.TerminatedPodFilter {},
225+ deviceCollectors : map [string ]framework.DeviceCollector {
226+ "TestDeviceCollector" : & fakeDeviceCollector {
227+ isEnabled : false ,
228+ getPodMetric : func (uid , podParentDir string , cs []corev1.ContainerStatus ) ([]metriccache.MetricSample , error ) {
229+ panic ("should not be called" )
230+ },
231+ getContainerMetric : func (uid , podParentDir string , c * corev1.ContainerStatus ) ([]metriccache.MetricSample , error ) {
232+ panic ("should not be called" )
233+ },
234+ },
235+ },
218236 getPodMetas : []* statesinformer.PodMeta {
219237 {
220238 CgroupDir : testPodMetaDir ,
@@ -304,7 +322,8 @@ total_unevictable 0
304322 },
305323 })
306324 collector .Setup (& framework.Context {
307- State : framework .NewSharedState (),
325+ State : framework .NewSharedState (),
326+ DeviceCollectors : tt .fields .deviceCollectors ,
308327 })
309328 c := collector .(* podResourceCollector )
310329 tt .fields .initPodLastStat (c .lastPodCPUStat )
@@ -350,3 +369,28 @@ func Test_podResourceCollector_Run(t *testing.T) {
350369 close (stopCh )
351370 })
352371}
372+
373+ type fakeDeviceCollector struct {
374+ framework.DeviceCollector
375+ isEnabled bool
376+ getPodMetric func (uid , podParentDir string , cs []corev1.ContainerStatus ) ([]metriccache.MetricSample , error )
377+ getContainerMetric func (uid , podParentDir string , c * corev1.ContainerStatus ) ([]metriccache.MetricSample , error )
378+ }
379+
380+ func (f * fakeDeviceCollector ) Enabled () bool {
381+ return f .isEnabled
382+ }
383+
384+ func (f * fakeDeviceCollector ) GetPodMetric (uid , podParentDir string , cs []corev1.ContainerStatus ) ([]metriccache.MetricSample , error ) {
385+ if f .getPodMetric != nil {
386+ return f .getPodMetric (uid , podParentDir , cs )
387+ }
388+ return nil , nil
389+ }
390+
391+ func (f * fakeDeviceCollector ) GetContainerMetric (containerID , podParentDir string , c * corev1.ContainerStatus ) ([]metriccache.MetricSample , error ) {
392+ if f .getContainerMetric != nil {
393+ return f .getContainerMetric (containerID , podParentDir , c )
394+ }
395+ return nil , nil
396+ }
0 commit comments