Test to verify if the consumed stats are correct.
(t *testing.T)
| 73 | |
| 74 | // Test to verify if the consumed stats are correct. |
| 75 | func TestContainerConsumedStats(t *testing.T) { |
| 76 | t.Logf("Create a pod config and run sandbox container") |
| 77 | sb, sbConfig := PodSandboxConfigWithCleanup(t, "sandbox1", "stats") |
| 78 | |
| 79 | testImage := images.Get(images.ResourceConsumer) |
| 80 | EnsureImageExists(t, testImage) |
| 81 | |
| 82 | t.Logf("Create a container config and run container in a pod") |
| 83 | containerConfig := ContainerConfig( |
| 84 | "container1", |
| 85 | testImage, |
| 86 | WithTestLabels(), |
| 87 | WithTestAnnotations(), |
| 88 | ) |
| 89 | cn, err := runtimeService.CreateContainer(sb, containerConfig, sbConfig) |
| 90 | require.NoError(t, err) |
| 91 | defer func() { |
| 92 | assert.NoError(t, runtimeService.RemoveContainer(cn)) |
| 93 | }() |
| 94 | require.NoError(t, runtimeService.StartContainer(cn)) |
| 95 | defer func() { |
| 96 | assert.NoError(t, runtimeService.StopContainer(cn, 10)) |
| 97 | }() |
| 98 | |
| 99 | t.Logf("Fetch initial stats for container") |
| 100 | var s *runtime.ContainerStats |
| 101 | require.NoError(t, Eventually(func() (bool, error) { |
| 102 | s, err = runtimeService.ContainerStats(cn) |
| 103 | if err != nil { |
| 104 | return false, err |
| 105 | } |
| 106 | if goruntime.GOOS == "windows" { |
| 107 | if s.GetMemory().GetWorkingSetBytes().GetValue() > 0 { |
| 108 | return true, nil |
| 109 | } |
| 110 | } else { |
| 111 | if s.GetWritableLayer().GetTimestamp() > 0 { |
| 112 | return true, nil |
| 113 | } |
| 114 | } |
| 115 | return false, nil |
| 116 | }, time.Second, 30*time.Second)) |
| 117 | |
| 118 | initialMemory := s.GetMemory().GetWorkingSetBytes().GetValue() |
| 119 | t.Logf("Initial container memory consumption is %f MB. Consume 100 MB and expect the reported stats to increase accordingly", float64(initialMemory)/(1024*1024)) |
| 120 | |
| 121 | // consume 100 MB memory for 30 seconds. |
| 122 | var command []string |
| 123 | if goruntime.GOOS == "windows" { |
| 124 | // -d: Leak and touch memory in specified MBs |
| 125 | // -c: Count of number of objects to allocate |
| 126 | command = []string{"testlimit.exe", "-accepteula", "-d", "25", "-c", "4"} |
| 127 | } else { |
| 128 | command = []string{"stress", "-m", "1", "--vm-bytes", "100M", "--vm-hang", "0", "-t", "30"} |
| 129 | } |
| 130 | |
| 131 | go func() { |
| 132 | _, _, err = runtimeService.ExecSync(cn, command, 30*time.Second) |
nothing calls this directly
no test coverage detected
searching dependent graphs…