From 7c4d57fbe6c038f13adfa12070e9634eb75b4f5e Mon Sep 17 00:00:00 2001 From: Etienne Perot Date: Fri, 19 Apr 2024 12:42:59 -0700 Subject: [PATCH] `dockerutil`: Add `IsGVisorRuntime` helper function. This is useful for CUDA tests, some of which work in gVisor and some not. By checking whether the runtime in use is gVisor, the test can adjust its expectations of success/failure. PiperOrigin-RevId: 626443132 --- pkg/test/dockerutil/container.go | 6 ++++-- pkg/test/dockerutil/dockerutil.go | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/pkg/test/dockerutil/container.go b/pkg/test/dockerutil/container.go index 9230a0140..39f0cd2ea 100644 --- a/pkg/test/dockerutil/container.go +++ b/pkg/test/dockerutil/container.go @@ -207,11 +207,13 @@ func (c *Container) Run(ctx context.Context, r RunOpts, args ...string) (string, } if err := c.Start(ctx); err != nil { - return "", err + logs, _ := c.Logs(ctx) + return logs, err } if err := c.Wait(ctx); err != nil { - return "", err + logs, _ := c.Logs(ctx) + return logs, err } return c.Logs(ctx) diff --git a/pkg/test/dockerutil/dockerutil.go b/pkg/test/dockerutil/dockerutil.go index 4e0509af3..c8710e786 100644 --- a/pkg/test/dockerutil/dockerutil.go +++ b/pkg/test/dockerutil/dockerutil.go @@ -16,6 +16,7 @@ package dockerutil import ( + "context" "encoding/json" "flag" "fmt" @@ -27,6 +28,7 @@ import ( "regexp" "strconv" "strings" + "testing" "time" "gvisor.dev/gvisor/pkg/test/testutil" @@ -122,6 +124,19 @@ func RuntimePath() (string, error) { return p, nil } +// IsGVisorRuntime returns whether the default container runtime used by +// `dockerutil` is gVisor-based or not. +func IsGVisorRuntime(ctx context.Context, t *testing.T) (bool, error) { + output, err := MakeContainer(ctx, t).Run(ctx, RunOpts{Image: "basic/alpine"}, "dmesg") + if err != nil { + if strings.Contains(output, "dmesg: klogctl: Operation not permitted") { + return false, nil + } + return false, fmt.Errorf("failed to run dmesg: %v (output: %q)", err, output) + } + return strings.Contains(output, "gVisor"), nil +} + // UsingSystemdCgroup returns true if the docker configuration has the // native.cgroupdriver=systemd option set in "exec-opts", or if the // system is using cgroupv2, in which case systemd is the default driver.