Skip to content

Commit

Permalink
Fix sniffer_test by embedding the run_sniffer binary in it.
Browse files Browse the repository at this point in the history
This works around file path resolution issues.

PiperOrigin-RevId: 663985469
  • Loading branch information
EtiennePerot authored and gvisor-bot committed Aug 17, 2024
1 parent 6199fc8 commit 043ce9c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ gpu-all-tests: gpu-images gpu-smoke-tests $(RUNTIME_BIN)
@$(call sudo,test/gpu:imagegen_test,--runtime=$(RUNTIME) -test.v $(ARGS))
@$(call sudo,test/gpu:sr_test,--runtime=$(RUNTIME) -test.v $(ARGS))
@$(call sudo,test/gpu:nccl_test,--runtime=$(RUNTIME) -test.v $(ARGS))
@$(call test,--test_env=RUNTIME=$(RUNTIME) test/gpu:sniffer_test)
@$(call sudo,test/gpu:sniffer_test,--runtime=$(RUNTIME) -test.v $(ARGS))
.PHONY: gpu-all-tests

cos-gpu-all-tests: gpu-images cos-gpu-smoke-tests $(RUNTIME_BIN)
Expand All @@ -318,7 +318,7 @@ cos-gpu-all-tests: gpu-images cos-gpu-smoke-tests $(RUNTIME_BIN)
@$(call sudo,test/gpu:imagegen_test,--runtime=$(RUNTIME) -test.v --cos-gpu $(ARGS))
@$(call sudo,test/gpu:sr_test,--runtime=$(RUNTIME) -test.v --cos-gpu $(ARGS))
@$(call sudo,test/gpu:nccl_test,--runtime=$(RUNTIME) -test.v --cos-gpu $(ARGS))
@$(call test,--test_env=RUNTIME=$(RUNTIME) --test_arg="--cos-gpu" test/gpu:sniffer_test)
@$(call sudo,test/gpu:sniffer_test,--runtime=$(RUNTIME) -test.v --cos-gpu $(ARGS))
.PHONY: cos-gpu-all-tests

portforward-tests: load-basic_redis load-basic_nginx $(RUNTIME_BIN)
Expand Down
16 changes: 13 additions & 3 deletions test/gpu/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,22 @@ go_test(
deps = ["//test/gpu/stablediffusion"],
)

# We copy the `run_sniffer` binary here because `go:embed` can only embed
# from the current directory or subdirectories, not parents of it.
genrule(
name = "run_sniffer_copy",
srcs = [
"//tools/ioctl_sniffer:run_sniffer",
],
outs = ["run_sniffer_copy"],
cmd = "cat < $(SRCS) > $@",
)

go_test(
name = "sniffer_test",
srcs = ["sniffer_test.go"],
data = [
"//tools/ioctl_sniffer:run_sniffer",
embedsrcs = [
":run_sniffer_copy", # keep
],
tags = [
"manual",
Expand All @@ -117,7 +128,6 @@ go_test(
visibility = ["//:sandbox"],
deps = [
"//pkg/test/dockerutil",
"//pkg/test/testutil",
"@com_github_docker_docker//api/types/mount:go_default_library",
],
)
Expand Down
33 changes: 28 additions & 5 deletions test/gpu/sniffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,48 @@ package sniffer_test
import (
"context"
"errors"
"os"
"strings"
"testing"
"time"

"github.com/docker/docker/api/types/mount"
"gvisor.dev/gvisor/pkg/test/dockerutil"
"gvisor.dev/gvisor/pkg/test/testutil"

// Needed for go:embed
_ "embed"
)

const maxDuration = 1 * time.Minute

//go:embed run_sniffer_copy
var runSnifferBinary []byte

// RunCommand runs the given command via the sniffer, with the -enforce_compatibility flag.
//
// It's run in a docker container, with the cuda-tests image.
func runCUDATestsCommand(t *testing.T, cmd ...string) (string, error) {
// Find the sniffer binary
cliPath, err := testutil.FindFile("tools/ioctl_sniffer/run_sniffer")
// Extract the sniffer binary to a temporary location.
runSniffer, err := os.CreateTemp("/tmp", "run_sniffer.*")
if err != nil {
t.Fatalf("Failed to find run_sniffer: %v", err)
t.Fatalf("Failed to create temporary file: %v", err)
}
defer func() {
if err := runSniffer.Close(); err != nil {
t.Fatalf("Failed to close temporary file: %v", err)
}
if err := os.Remove(runSniffer.Name()); err != nil {
t.Fatalf("Failed to unlink temporary file: %v", err)
}
}()
if _, err := runSniffer.Write(runSnifferBinary); err != nil {
t.Fatalf("Failed to write to temporary file: %v", err)
}
if err := runSniffer.Sync(); err != nil {
t.Fatalf("Failed to sync temporary file: %v", err)
}
if err := runSniffer.Chmod(0o555); err != nil {
t.Fatalf("Failed to chmod temporary file: %v", err)
}

// Set up our docker container
Expand All @@ -51,7 +74,7 @@ func runCUDATestsCommand(t *testing.T, cmd ...string) (string, error) {
opts.Image = "gpu/cuda-tests"
opts.Mounts = append(opts.Mounts, mount.Mount{
Type: mount.TypeBind,
Source: cliPath,
Source: runSniffer.Name(),
Target: "/run_sniffer",
ReadOnly: false,
})
Expand Down

0 comments on commit 043ce9c

Please sign in to comment.