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
This commit is contained in:
Etienne Perot
2024-04-19 12:45:49 -07:00
committed by gVisor bot
parent cbcb4ec914
commit 7c4d57fbe6
2 changed files with 19 additions and 2 deletions
+4 -2
View File
@@ -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)
+15
View File
@@ -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.