@@ -89,15 +89,15 @@ func Test_criService_podSandboxStats(t *testing.T) {
8989 metrics map [string ]* wstats.Statistics
9090 sandbox sandboxstore.Sandbox
9191 containers []containerstore.Container
92- expectedPodStats expectedStats
92+ expectedPodStats * expectedStats
9393 expectedContainerStats []expectedStats
9494 expectError bool
9595 }{
9696 "no metrics found should return error" : {
9797 metrics : map [string ]* wstats.Statistics {},
9898 sandbox : sandboxstore.Sandbox {},
9999 containers : []containerstore.Container {},
100- expectedPodStats : expectedStats {},
100+ expectedPodStats : & expectedStats {},
101101 expectedContainerStats : []expectedStats {},
102102 expectError : true ,
103103 },
@@ -114,7 +114,7 @@ func Test_criService_podSandboxStats(t *testing.T) {
114114 containers : []containerstore.Container {
115115 newContainer ("c1" , running , nil ),
116116 },
117- expectedPodStats : expectedStats {
117+ expectedPodStats : & expectedStats {
118118 UsageCoreNanoSeconds : 400 ,
119119 UsageNanoCores : 0 ,
120120 WorkingSetBytes : 40 ,
@@ -145,7 +145,7 @@ func Test_criService_podSandboxStats(t *testing.T) {
145145 newContainer ("c1" , running , nil ),
146146 newContainer ("i1" , running , nil ),
147147 },
148- expectedPodStats : expectedStats {
148+ expectedPodStats : & expectedStats {
149149 UsageCoreNanoSeconds : 600 ,
150150 UsageNanoCores : 0 ,
151151 WorkingSetBytes : 60 ,
@@ -178,7 +178,7 @@ func Test_criService_podSandboxStats(t *testing.T) {
178178 newContainer ("c1" , running , nil ),
179179 newContainer ("i1" , exitedValid , nil ),
180180 },
181- expectedPodStats : expectedStats {
181+ expectedPodStats : & expectedStats {
182182 UsageCoreNanoSeconds : 400 ,
183183 UsageNanoCores : 0 ,
184184 WorkingSetBytes : 40 ,
@@ -206,7 +206,7 @@ func Test_criService_podSandboxStats(t *testing.T) {
206206 newContainer ("c1" , running , nil ),
207207 newContainer ("i1" , exitedInvalid , nil ),
208208 },
209- expectedPodStats : expectedStats {
209+ expectedPodStats : & expectedStats {
210210 UsageCoreNanoSeconds : 400 ,
211211 UsageNanoCores : 0 ,
212212 WorkingSetBytes : 40 ,
@@ -236,7 +236,7 @@ func Test_criService_podSandboxStats(t *testing.T) {
236236 UsageCoreNanoSeconds : 200 ,
237237 }),
238238 },
239- expectedPodStats : expectedStats {
239+ expectedPodStats : & expectedStats {
240240 UsageCoreNanoSeconds : 800 ,
241241 UsageNanoCores : 400 ,
242242 WorkingSetBytes : 40 ,
@@ -264,7 +264,7 @@ func Test_criService_podSandboxStats(t *testing.T) {
264264 UsageCoreNanoSeconds : 200 ,
265265 }),
266266 },
267- expectedPodStats : expectedStats {
267+ expectedPodStats : & expectedStats {
268268 UsageCoreNanoSeconds : 400 ,
269269 UsageNanoCores : 200 ,
270270 WorkingSetBytes : 20 ,
@@ -292,7 +292,7 @@ func Test_criService_podSandboxStats(t *testing.T) {
292292 UsageCoreNanoSeconds : 200 ,
293293 }),
294294 },
295- expectedPodStats : expectedStats {
295+ expectedPodStats : & expectedStats {
296296 UsageCoreNanoSeconds : 400 ,
297297 UsageNanoCores : 200 ,
298298 WorkingSetBytes : 20 ,
@@ -306,6 +306,32 @@ func Test_criService_podSandboxStats(t *testing.T) {
306306 },
307307 expectError : false ,
308308 },
309+ {
310+ desc : "pod sandbox with a container that has no cpu shouldn't error" ,
311+ metrics : map [string ]* wstats.Statistics {
312+ "c1" : {},
313+ "s1" : {},
314+ },
315+ sandbox : sandboxPod ("s1" , initialStatsTimestamp , 200 ),
316+ containers : []containerstore.Container {
317+ newContainer ("c1" , running , & stats.ContainerStats {
318+ Timestamp : initialStatsTimestamp ,
319+ UsageCoreNanoSeconds : 200 ,
320+ }),
321+ },
322+ expectedPodStats : nil ,
323+ expectedContainerStats : []expectedStats {},
324+ expectError : false ,
325+ },
326+ {
327+ desc : "pod sandbox with no stats in metric mapp will fail" ,
328+ metrics : map [string ]* wstats.Statistics {},
329+ sandbox : sandboxPod ("s1" , initialStatsTimestamp , 200 ),
330+ containers : []containerstore.Container {},
331+ expectedPodStats : nil ,
332+ expectedContainerStats : []expectedStats {},
333+ expectError : true ,
334+ },
309335 } {
310336 t .Run (desc , func (t * testing.T ) {
311337 actualPodStats , actualContainerStats , err := c .toPodSandboxStats (test .sandbox , test .metrics , test .containers , currentStatsTimestamp )
@@ -314,6 +340,13 @@ func Test_criService_podSandboxStats(t *testing.T) {
314340 return
315341 }
316342 assert .Nil (t , err )
343+
344+ if test .expectedPodStats == nil {
345+ assert .Nil (t , actualPodStats .Cpu )
346+ assert .Nil (t , actualPodStats .Memory )
347+ return
348+ }
349+
317350 assert .Equal (t , test .expectedPodStats .UsageCoreNanoSeconds , actualPodStats .Cpu .UsageCoreNanoSeconds .Value )
318351 assert .Equal (t , test .expectedPodStats .UsageNanoCores , actualPodStats .Cpu .UsageNanoCores .Value )
319352
0 commit comments