@@ -93,7 +93,7 @@ func Test_criService_podSandboxStats(t *testing.T) {
9393 metrics map [string ]* wstats.Statistics
9494 sandbox sandboxstore.Sandbox
9595 containers []containerstore.Container
96- expectedPodStats expectedStats
96+ expectedPodStats * expectedStats
9797 expectedContainerStats []expectedStats
9898 expectError bool
9999 }{
@@ -102,7 +102,7 @@ func Test_criService_podSandboxStats(t *testing.T) {
102102 metrics : map [string ]* wstats.Statistics {},
103103 sandbox : sandboxstore.Sandbox {},
104104 containers : []containerstore.Container {},
105- expectedPodStats : expectedStats {},
105+ expectedPodStats : & expectedStats {},
106106 expectedContainerStats : []expectedStats {},
107107 expectError : true ,
108108 },
@@ -120,7 +120,7 @@ func Test_criService_podSandboxStats(t *testing.T) {
120120 containers : []containerstore.Container {
121121 newContainer ("c1" , running , nil ),
122122 },
123- expectedPodStats : expectedStats {
123+ expectedPodStats : & expectedStats {
124124 UsageCoreNanoSeconds : 400 ,
125125 UsageNanoCores : 0 ,
126126 WorkingSetBytes : 40 ,
@@ -152,7 +152,7 @@ func Test_criService_podSandboxStats(t *testing.T) {
152152 newContainer ("c1" , running , nil ),
153153 newContainer ("i1" , running , nil ),
154154 },
155- expectedPodStats : expectedStats {
155+ expectedPodStats : & expectedStats {
156156 UsageCoreNanoSeconds : 600 ,
157157 UsageNanoCores : 0 ,
158158 WorkingSetBytes : 60 ,
@@ -186,7 +186,7 @@ func Test_criService_podSandboxStats(t *testing.T) {
186186 newContainer ("c1" , running , nil ),
187187 newContainer ("i1" , exitedValid , nil ),
188188 },
189- expectedPodStats : expectedStats {
189+ expectedPodStats : & expectedStats {
190190 UsageCoreNanoSeconds : 400 ,
191191 UsageNanoCores : 0 ,
192192 WorkingSetBytes : 40 ,
@@ -215,7 +215,7 @@ func Test_criService_podSandboxStats(t *testing.T) {
215215 newContainer ("c1" , running , nil ),
216216 newContainer ("i1" , exitedInvalid , nil ),
217217 },
218- expectedPodStats : expectedStats {
218+ expectedPodStats : & expectedStats {
219219 UsageCoreNanoSeconds : 400 ,
220220 UsageNanoCores : 0 ,
221221 WorkingSetBytes : 40 ,
@@ -246,7 +246,7 @@ func Test_criService_podSandboxStats(t *testing.T) {
246246 UsageCoreNanoSeconds : 200 ,
247247 }),
248248 },
249- expectedPodStats : expectedStats {
249+ expectedPodStats : & expectedStats {
250250 UsageCoreNanoSeconds : 800 ,
251251 UsageNanoCores : 400 ,
252252 WorkingSetBytes : 40 ,
@@ -275,7 +275,7 @@ func Test_criService_podSandboxStats(t *testing.T) {
275275 UsageCoreNanoSeconds : 200 ,
276276 }),
277277 },
278- expectedPodStats : expectedStats {
278+ expectedPodStats : & expectedStats {
279279 UsageCoreNanoSeconds : 400 ,
280280 UsageNanoCores : 200 ,
281281 WorkingSetBytes : 20 ,
@@ -304,7 +304,7 @@ func Test_criService_podSandboxStats(t *testing.T) {
304304 UsageCoreNanoSeconds : 200 ,
305305 }),
306306 },
307- expectedPodStats : expectedStats {
307+ expectedPodStats : & expectedStats {
308308 UsageCoreNanoSeconds : 400 ,
309309 UsageNanoCores : 200 ,
310310 WorkingSetBytes : 20 ,
@@ -318,6 +318,32 @@ func Test_criService_podSandboxStats(t *testing.T) {
318318 },
319319 expectError : false ,
320320 },
321+ {
322+ desc : "pod sandbox with a container that has no cpu shouldn't error" ,
323+ metrics : map [string ]* wstats.Statistics {
324+ "c1" : {},
325+ "s1" : {},
326+ },
327+ sandbox : sandboxPod ("s1" , initialStatsTimestamp , 200 ),
328+ containers : []containerstore.Container {
329+ newContainer ("c1" , running , & stats.ContainerStats {
330+ Timestamp : initialStatsTimestamp ,
331+ UsageCoreNanoSeconds : 200 ,
332+ }),
333+ },
334+ expectedPodStats : nil ,
335+ expectedContainerStats : []expectedStats {},
336+ expectError : false ,
337+ },
338+ {
339+ desc : "pod sandbox with no stats in metric mapp will fail" ,
340+ metrics : map [string ]* wstats.Statistics {},
341+ sandbox : sandboxPod ("s1" , initialStatsTimestamp , 200 ),
342+ containers : []containerstore.Container {},
343+ expectedPodStats : nil ,
344+ expectedContainerStats : []expectedStats {},
345+ expectError : true ,
346+ },
321347 } {
322348 test := test
323349 t .Run (test .desc , func (t * testing.T ) {
@@ -327,6 +353,13 @@ func Test_criService_podSandboxStats(t *testing.T) {
327353 return
328354 }
329355 assert .Nil (t , err )
356+
357+ if test .expectedPodStats == nil {
358+ assert .Nil (t , actualPodStats .Cpu )
359+ assert .Nil (t , actualPodStats .Memory )
360+ return
361+ }
362+
330363 assert .Equal (t , test .expectedPodStats .UsageCoreNanoSeconds , actualPodStats .Cpu .UsageCoreNanoSeconds .Value )
331364 assert .Equal (t , test .expectedPodStats .UsageNanoCores , actualPodStats .Cpu .UsageNanoCores .Value )
332365
0 commit comments