summaryrefslogtreecommitdiffhomepage
path: root/test/e2e
diff options
context:
space:
mode:
authorFabricio Voznika <fvoznika@google.com>2021-10-14 18:35:08 -0700
committergVisor bot <gvisor-bot@google.com>2021-10-14 18:42:07 -0700
commit33b41d8fe98e7820118e8d42b0cfbec4ca159d62 (patch)
tree84b6bbdb35f4044e7f0c8210f4f771cf15547d1a /test/e2e
parent1711fd9efe86948198d836faf060e7dea60bae8d (diff)
Report total memory based on limit or host
gVisor was previously reporting the lower of cgroup limit or 2GB as total memory. This may cause applications to make bad decisions based on amount of memory available to them when more than 2GB is required. This change makes the lower of cgroup limit or the host total memory to be reported inside the sandbox. This also is more inline with docker which always reports host total memory. Note that reporting cgroup limit is strictly better than host total memory when there is a limit set. Fixes #5608 PiperOrigin-RevId: 403241608
Diffstat (limited to 'test/e2e')
-rw-r--r--test/e2e/integration_test.go44
1 files changed, 40 insertions, 4 deletions
diff --git a/test/e2e/integration_test.go b/test/e2e/integration_test.go
index d41139944..c5aa35623 100644
--- a/test/e2e/integration_test.go
+++ b/test/e2e/integration_test.go
@@ -29,6 +29,7 @@ import (
"net"
"net/http"
"os"
+ "os/exec"
"path/filepath"
"regexp"
"strconv"
@@ -41,8 +42,12 @@ import (
"gvisor.dev/gvisor/pkg/test/testutil"
)
-// defaultWait is the default wait time used for tests.
-const defaultWait = time.Minute
+const (
+ // defaultWait is the default wait time used for tests.
+ defaultWait = time.Minute
+
+ memInfoCmd = "cat /proc/meminfo | grep MemTotal: | awk '{print $2}'"
+)
func TestMain(m *testing.M) {
dockerutil.EnsureSupportedDockerVersion()
@@ -255,16 +260,47 @@ func TestConnectToSelf(t *testing.T) {
}
}
+func TestMemory(t *testing.T) {
+ // Find total amount of memory in the host.
+ host, err := exec.Command("sh", "-c", memInfoCmd).CombinedOutput()
+ if err != nil {
+ t.Fatal(err)
+ }
+ want, err := strconv.ParseUint(strings.TrimSpace(string(host)), 10, 64)
+ if err != nil {
+ t.Fatalf("failed to parse %q: %v", host, err)
+ }
+
+ ctx := context.Background()
+ d := dockerutil.MakeContainer(ctx, t)
+ defer d.CleanUp(ctx)
+
+ out, err := d.Run(ctx, dockerutil.RunOpts{Image: "basic/alpine"}, "sh", "-c", memInfoCmd)
+ if err != nil {
+ t.Fatalf("docker run failed: %v", err)
+ }
+
+ // Get memory from inside the container and ensure it matches the host.
+ got, err := strconv.ParseUint(strings.TrimSpace(out), 10, 64)
+ if err != nil {
+ t.Fatalf("failed to parse %q: %v", out, err)
+ }
+ if got != want {
+ t.Errorf("MemTotal got: %d, want: %d", got, want)
+ }
+}
+
func TestMemLimit(t *testing.T) {
ctx := context.Background()
d := dockerutil.MakeContainer(ctx, t)
defer d.CleanUp(ctx)
allocMemoryKb := 50 * 1024
- out, err := d.Run(ctx, dockerutil.RunOpts{
+ opts := dockerutil.RunOpts{
Image: "basic/alpine",
Memory: allocMemoryKb * 1024, // In bytes.
- }, "sh", "-c", "cat /proc/meminfo | grep MemTotal: | awk '{print $2}'")
+ }
+ out, err := d.Run(ctx, opts, "sh", "-c", memInfoCmd)
if err != nil {
t.Fatalf("docker run failed: %v", err)
}