@@ -20,6 +20,7 @@ package integration
2020
2121import (
2222 "fmt"
23+ goruntime "runtime"
2324 "testing"
2425 "time"
2526
@@ -74,6 +75,83 @@ func TestContainerStats(t *testing.T) {
7475 testStats (t , s , containerConfig )
7576}
7677
78+ // Test to verify if the consumed stats are correct.
79+ func TestContainerConsumedStats (t * testing.T ) {
80+ t .Logf ("Create a pod config and run sandbox container" )
81+ sbConfig := PodSandboxConfig ("sandbox1" , "stats" )
82+ sb , err := runtimeService .RunPodSandbox (sbConfig , * runtimeHandler )
83+ require .NoError (t , err )
84+ defer func () {
85+ assert .NoError (t , runtimeService .StopPodSandbox (sb ))
86+ assert .NoError (t , runtimeService .RemovePodSandbox (sb ))
87+ }()
88+
89+ testImage := GetImage (ResourceConsumer )
90+ img , err := imageService .PullImage (& runtime.ImageSpec {Image : testImage }, nil , sbConfig )
91+ require .NoError (t , err )
92+ defer func () {
93+ assert .NoError (t , imageService .RemoveImage (& runtime.ImageSpec {Image : img }))
94+ }()
95+
96+ t .Logf ("Create a container config and run container in a pod" )
97+ containerConfig := ContainerConfig (
98+ "container1" ,
99+ testImage ,
100+ WithTestLabels (),
101+ WithTestAnnotations (),
102+ )
103+ cn , err := runtimeService .CreateContainer (sb , containerConfig , sbConfig )
104+ require .NoError (t , err )
105+ defer func () {
106+ assert .NoError (t , runtimeService .RemoveContainer (cn ))
107+ }()
108+ require .NoError (t , runtimeService .StartContainer (cn ))
109+ defer func () {
110+ assert .NoError (t , runtimeService .StopContainer (cn , 10 ))
111+ }()
112+
113+ t .Logf ("Fetch initial stats for container" )
114+ var s * runtime.ContainerStats
115+ require .NoError (t , Eventually (func () (bool , error ) {
116+ s , err = runtimeService .ContainerStats (cn )
117+ if err != nil {
118+ return false , err
119+ }
120+ if s .GetMemory ().GetWorkingSetBytes ().GetValue () > 0 {
121+ return true , nil
122+ }
123+ return false , nil
124+ }, time .Second , 30 * time .Second ))
125+
126+ initialMemory := s .GetMemory ().GetWorkingSetBytes ().GetValue ()
127+ t .Logf ("Initial container memory consumption is %f MB. Consume 100 MB and expect the reported stats to increase accordingly" , float64 (initialMemory )/ (1024 * 1024 ))
128+
129+ // consume 100 MB memory for 30 seconds.
130+ var command []string
131+ if goruntime .GOOS == "windows" {
132+ // -d: Leak and touch memory in specified MBs
133+ // -c: Count of number of objects to allocate
134+ command = []string {"testlimit.exe" , "-accepteula" , "-d" , "25" , "-c" , "4" }
135+ } else {
136+ command = []string {"stress" , "-m" , "1" , "--vm-bytes" , "100M" , "--vm-hang" , "0" , "-t" , "30" }
137+ }
138+
139+ go func () {
140+ _ , _ , err = runtimeService .ExecSync (cn , command , 30 * time .Second )
141+ }()
142+
143+ require .NoError (t , Eventually (func () (bool , error ) {
144+ s , err = runtimeService .ContainerStats (cn )
145+ if err != nil {
146+ return false , err
147+ }
148+ if s .GetMemory ().GetWorkingSetBytes ().GetValue () > initialMemory + 100 * 1024 * 1024 {
149+ return true , nil
150+ }
151+ return false , nil
152+ }, time .Second , 30 * time .Second ))
153+ }
154+
77155// Test to verify filtering without any filter
78156func TestContainerListStats (t * testing.T ) {
79157 t .Logf ("Create a pod config and run sandbox container" )
0 commit comments