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 <cbelu@cloudbasesolutions.com>
This commit is contained in:
parent
feee16e0e3
commit
3187b6dc81
@ -32,6 +32,7 @@ type ImageList struct {
|
|||||||
Alpine string
|
Alpine string
|
||||||
BusyBox string
|
BusyBox string
|
||||||
Pause string
|
Pause string
|
||||||
|
ResourceConsumer string
|
||||||
VolumeCopyUp string
|
VolumeCopyUp string
|
||||||
VolumeOwnership string
|
VolumeOwnership string
|
||||||
}
|
}
|
||||||
@ -48,6 +49,7 @@ func initImages(imageListFile string) {
|
|||||||
Alpine: "docker.io/library/alpine:latest",
|
Alpine: "docker.io/library/alpine:latest",
|
||||||
BusyBox: "docker.io/library/busybox:latest",
|
BusyBox: "docker.io/library/busybox:latest",
|
||||||
Pause: "k8s.gcr.io/pause:3.5",
|
Pause: "k8s.gcr.io/pause:3.5",
|
||||||
|
ResourceConsumer: "k8s.gcr.io/e2e-test-images/resource-consumer:1.9",
|
||||||
VolumeCopyUp: "gcr.io/k8s-cri-containerd/volume-copy-up:2.0",
|
VolumeCopyUp: "gcr.io/k8s-cri-containerd/volume-copy-up:2.0",
|
||||||
VolumeOwnership: "gcr.io/k8s-cri-containerd/volume-ownership:2.0",
|
VolumeOwnership: "gcr.io/k8s-cri-containerd/volume-ownership:2.0",
|
||||||
}
|
}
|
||||||
@ -79,6 +81,8 @@ const (
|
|||||||
BusyBox
|
BusyBox
|
||||||
// Pause image
|
// Pause image
|
||||||
Pause
|
Pause
|
||||||
|
// ResourceConsumer image
|
||||||
|
ResourceConsumer
|
||||||
// VolumeCopyUp image
|
// VolumeCopyUp image
|
||||||
VolumeCopyUp
|
VolumeCopyUp
|
||||||
// VolumeOwnership image
|
// VolumeOwnership image
|
||||||
@ -90,6 +94,7 @@ func initImageMap(imageList ImageList) map[int]string {
|
|||||||
images[Alpine] = imageList.Alpine
|
images[Alpine] = imageList.Alpine
|
||||||
images[BusyBox] = imageList.BusyBox
|
images[BusyBox] = imageList.BusyBox
|
||||||
images[Pause] = imageList.Pause
|
images[Pause] = imageList.Pause
|
||||||
|
images[ResourceConsumer] = imageList.ResourceConsumer
|
||||||
images[VolumeCopyUp] = imageList.VolumeCopyUp
|
images[VolumeCopyUp] = imageList.VolumeCopyUp
|
||||||
images[VolumeOwnership] = imageList.VolumeOwnership
|
images[VolumeOwnership] = imageList.VolumeOwnership
|
||||||
return images
|
return images
|
||||||
|
@ -20,6 +20,7 @@ package integration
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
goruntime "runtime"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -74,6 +75,83 @@ func TestContainerStats(t *testing.T) {
|
|||||||
testStats(t, s, containerConfig)
|
testStats(t, s, containerConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test to verify if the consumed stats are correct.
|
||||||
|
func TestContainerConsumedStats(t *testing.T) {
|
||||||
|
t.Logf("Create a pod config and run sandbox container")
|
||||||
|
sbConfig := PodSandboxConfig("sandbox1", "stats")
|
||||||
|
sb, err := runtimeService.RunPodSandbox(sbConfig, *runtimeHandler)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer func() {
|
||||||
|
assert.NoError(t, runtimeService.StopPodSandbox(sb))
|
||||||
|
assert.NoError(t, runtimeService.RemovePodSandbox(sb))
|
||||||
|
}()
|
||||||
|
|
||||||
|
testImage := GetImage(ResourceConsumer)
|
||||||
|
img, err := imageService.PullImage(&runtime.ImageSpec{Image: testImage}, nil, sbConfig)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer func() {
|
||||||
|
assert.NoError(t, imageService.RemoveImage(&runtime.ImageSpec{Image: img}))
|
||||||
|
}()
|
||||||
|
|
||||||
|
t.Logf("Create a container config and run container in a pod")
|
||||||
|
containerConfig := ContainerConfig(
|
||||||
|
"container1",
|
||||||
|
testImage,
|
||||||
|
WithTestLabels(),
|
||||||
|
WithTestAnnotations(),
|
||||||
|
)
|
||||||
|
cn, err := runtimeService.CreateContainer(sb, containerConfig, sbConfig)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer func() {
|
||||||
|
assert.NoError(t, runtimeService.RemoveContainer(cn))
|
||||||
|
}()
|
||||||
|
require.NoError(t, runtimeService.StartContainer(cn))
|
||||||
|
defer func() {
|
||||||
|
assert.NoError(t, runtimeService.StopContainer(cn, 10))
|
||||||
|
}()
|
||||||
|
|
||||||
|
t.Logf("Fetch initial stats for container")
|
||||||
|
var s *runtime.ContainerStats
|
||||||
|
require.NoError(t, Eventually(func() (bool, error) {
|
||||||
|
s, err = runtimeService.ContainerStats(cn)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
if s.GetMemory().GetWorkingSetBytes().GetValue() > 0 {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}, time.Second, 30*time.Second))
|
||||||
|
|
||||||
|
initialMemory := s.GetMemory().GetWorkingSetBytes().GetValue()
|
||||||
|
t.Logf("Initial container memory consumption is %f MB. Consume 100 MB and expect the reported stats to increase accordingly", float64(initialMemory)/(1024*1024))
|
||||||
|
|
||||||
|
// consume 100 MB memory for 30 seconds.
|
||||||
|
var command []string
|
||||||
|
if goruntime.GOOS == "windows" {
|
||||||
|
// -d: Leak and touch memory in specified MBs
|
||||||
|
// -c: Count of number of objects to allocate
|
||||||
|
command = []string{"testlimit.exe", "-accepteula", "-d", "25", "-c", "4"}
|
||||||
|
} else {
|
||||||
|
command = []string{"stress", "-m", "1", "--vm-bytes", "100M", "--vm-hang", "0", "-t", "30"}
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
_, _, err = runtimeService.ExecSync(cn, command, 30*time.Second)
|
||||||
|
}()
|
||||||
|
|
||||||
|
require.NoError(t, Eventually(func() (bool, error) {
|
||||||
|
s, err = runtimeService.ContainerStats(cn)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
if s.GetMemory().GetWorkingSetBytes().GetValue() > initialMemory+100*1024*1024 {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}, time.Second, 30*time.Second))
|
||||||
|
}
|
||||||
|
|
||||||
// Test to verify filtering without any filter
|
// Test to verify filtering without any filter
|
||||||
func TestContainerListStats(t *testing.T) {
|
func TestContainerListStats(t *testing.T) {
|
||||||
t.Logf("Create a pod config and run sandbox container")
|
t.Logf("Create a pod config and run sandbox container")
|
||||||
|
Loading…
Reference in New Issue
Block a user