Skip to content

Commit 3187b6d

Browse files
committed
tests: Adds consumed memory stats test
We can check that the reported containers stats are not lower than it should by using the resource-consumer image. This image contains stress / testlimit.exe which can consume a specified amount of memory. Signed-off-by: Claudiu Belu <[email protected]>
1 parent feee16e commit 3187b6d

2 files changed

Lines changed: 93 additions & 10 deletions

File tree

integration/common.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ import (
2929

3030
// ImageList holds public image references
3131
type ImageList struct {
32-
Alpine string
33-
BusyBox string
34-
Pause string
35-
VolumeCopyUp string
36-
VolumeOwnership string
32+
Alpine string
33+
BusyBox string
34+
Pause string
35+
ResourceConsumer string
36+
VolumeCopyUp string
37+
VolumeOwnership string
3738
}
3839

3940
var (
@@ -45,11 +46,12 @@ var (
4546

4647
func initImages(imageListFile string) {
4748
imageList = ImageList{
48-
Alpine: "docker.io/library/alpine:latest",
49-
BusyBox: "docker.io/library/busybox:latest",
50-
Pause: "k8s.gcr.io/pause:3.5",
51-
VolumeCopyUp: "gcr.io/k8s-cri-containerd/volume-copy-up:2.0",
52-
VolumeOwnership: "gcr.io/k8s-cri-containerd/volume-ownership:2.0",
49+
Alpine: "docker.io/library/alpine:latest",
50+
BusyBox: "docker.io/library/busybox:latest",
51+
Pause: "k8s.gcr.io/pause:3.5",
52+
ResourceConsumer: "k8s.gcr.io/e2e-test-images/resource-consumer:1.9",
53+
VolumeCopyUp: "gcr.io/k8s-cri-containerd/volume-copy-up:2.0",
54+
VolumeOwnership: "gcr.io/k8s-cri-containerd/volume-ownership:2.0",
5355
}
5456

5557
if imageListFile != "" {
@@ -79,6 +81,8 @@ const (
7981
BusyBox
8082
// Pause image
8183
Pause
84+
// ResourceConsumer image
85+
ResourceConsumer
8286
// VolumeCopyUp image
8387
VolumeCopyUp
8488
// VolumeOwnership image
@@ -90,6 +94,7 @@ func initImageMap(imageList ImageList) map[int]string {
9094
images[Alpine] = imageList.Alpine
9195
images[BusyBox] = imageList.BusyBox
9296
images[Pause] = imageList.Pause
97+
images[ResourceConsumer] = imageList.ResourceConsumer
9398
images[VolumeCopyUp] = imageList.VolumeCopyUp
9499
images[VolumeOwnership] = imageList.VolumeOwnership
95100
return images

integration/container_stats_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package integration
2020

2121
import (
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
78156
func TestContainerListStats(t *testing.T) {
79157
t.Logf("Create a pod config and run sandbox container")

0 commit comments

Comments
 (0)