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.