diff --git a/pkg/agent/containerd/config_linux.go b/pkg/agent/containerd/config_linux.go index e83b273f..063634bc 100644 --- a/pkg/agent/containerd/config_linux.go +++ b/pkg/agent/containerd/config_linux.go @@ -11,13 +11,13 @@ import ( fuseoverlayfs "github.com/containerd/fuse-overlayfs-snapshotter" stargz "github.com/containerd/stargz-snapshotter/service" "github.com/docker/docker/pkg/parsers/kernel" + "github.com/opencontainers/runc/libcontainer/userns" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" "github.com/xiaods/k8e/pkg/agent/templates" "github.com/xiaods/k8e/pkg/cgroups" "github.com/xiaods/k8e/pkg/daemons/config" "github.com/xiaods/k8e/pkg/version" - "github.com/opencontainers/runc/libcontainer/userns" - "github.com/pkg/errors" - "github.com/sirupsen/logrus" "golang.org/x/sys/unix" "k8s.io/cri-client/pkg/util" ) @@ -116,4 +116,4 @@ func FuseoverlayfsSupported(root string) error { func StargzSupported(root string) error { return stargz.Supported(root) -} \ No newline at end of file +} diff --git a/pkg/agent/containerd/runtimes.go b/pkg/agent/containerd/runtimes.go index df056c82..2c03e99e 100644 --- a/pkg/agent/containerd/runtimes.go +++ b/pkg/agent/containerd/runtimes.go @@ -6,7 +6,7 @@ package containerd import ( "errors" "io/fs" - "path/filepath" + "os/exec" "github.com/sirupsen/logrus" "github.com/xiaods/k8e/pkg/agent/templates" @@ -17,61 +17,40 @@ import ( type runtimeConfigs map[string]templates.ContainerdRuntimeConfig // searchForRuntimes searches for runtimes and add into foundRuntimes -// It checks install locations provided via potentitalRuntimes variable. -// The binaries are searched at the locations specivied by locationsToCheck. -// The given fs.FS should represent the filesystem root directory to search in. -func searchForRuntimes(root fs.FS, potentialRuntimes runtimeConfigs, locationsToCheck []string, foundRuntimes runtimeConfigs) { - // Check these locations in order. The GPU operator's installation should - // take precedence over the package manager's installation. - +// It checks the PATH for the executables +func searchForRuntimes(potentialRuntimes runtimeConfigs, foundRuntimes runtimeConfigs) { // Fill in the binary location with just the name of the binary, // and check against each of the possible locations. If a match is found, // set the location to the full path. for runtimeName, runtimeConfig := range potentialRuntimes { - for _, location := range locationsToCheck { - binaryPath := filepath.Join(location, runtimeConfig.BinaryName) - logrus.Debugf("Searching for %s container runtime at /%s", runtimeName, binaryPath) - if info, err := fs.Stat(root, binaryPath); err == nil { - if info.IsDir() { - logrus.Debugf("Found %s container runtime at /%s, but it is a directory. Skipping.", runtimeName, binaryPath) - continue - } - runtimeConfig.BinaryName = filepath.Join("/", binaryPath) - logrus.Infof("Found %s container runtime at %s", runtimeName, runtimeConfig.BinaryName) - foundRuntimes[runtimeName] = runtimeConfig - break + logrus.Debugf("Searching for %s container runtime", runtimeName) + path, err := exec.LookPath(runtimeConfig.BinaryName) + if err != nil { + if errors.Is(err, fs.ErrNotExist) { + logrus.Debugf("%s container runtime not found in $PATH: %v", runtimeName, err) } else { - if errors.Is(err, fs.ErrNotExist) { - logrus.Debugf("%s container runtime not found at /%s", runtimeName, binaryPath) - } else { - logrus.Errorf("Error searching for %s container runtime at /%s: %v", runtimeName, binaryPath, err) - } + logrus.Debugf("Error searching for %s in $PATH: %v", runtimeName, err) } + continue } + + logrus.Infof("Found %s container runtime at %s", runtimeName, path) + runtimeConfig.BinaryName = path + foundRuntimes[runtimeName] = runtimeConfig } } // findContainerRuntimes is a function that searches for all the runtimes and // return a list with all the runtimes that have been found -func findContainerRuntimes(root fs.FS) runtimeConfigs { +func findContainerRuntimes() runtimeConfigs { foundRuntimes := runtimeConfigs{} - findCRunContainerRuntime(root, foundRuntimes) - findNvidiaContainerRuntimes(root, foundRuntimes) - findWasiRuntimes(root, foundRuntimes) + findCRunContainerRuntime(foundRuntimes) + findNvidiaContainerRuntimes(foundRuntimes) + findWasiRuntimes(foundRuntimes) return foundRuntimes } -// findCRunContainerRuntime finds if crun is available in the system and adds to foundRuntimes -func findCRunContainerRuntime(root fs.FS, foundRuntimes runtimeConfigs) { - // Check these locations in order. - locationsToCheck := []string{ - "usr/sbin", // Path when installing via package manager - "usr/bin", // Path when installing via package manager - } - - // Fill in the binary location with just the name of the binary, - // and check against each of the possible locations. If a match is found, - // set the location to the full path. +func findCRunContainerRuntime(foundRuntimes runtimeConfigs) { potentialRuntimes := runtimeConfigs{ "crun": { RuntimeType: "io.containerd.runc.v2", @@ -79,25 +58,10 @@ func findCRunContainerRuntime(root fs.FS, foundRuntimes runtimeConfigs) { }, } - searchForRuntimes(root, potentialRuntimes, locationsToCheck, foundRuntimes) + searchForRuntimes(potentialRuntimes, foundRuntimes) } -// findNvidiaContainerRuntimes finds the nvidia runtimes that are are available on the system -// and adds to foundRuntimes. It checks install locations used by the nvidia -// gpu operator and by system package managers. The gpu operator installation -// takes precedence over the system package manager installation. -// The given fs.FS should represent the filesystem root directory to search in. -func findNvidiaContainerRuntimes(root fs.FS, foundRuntimes runtimeConfigs) { - // Check these locations in order. The GPU operator's installation should - // take precedence over the package manager's installation. - locationsToCheck := []string{ - "usr/local/nvidia/toolkit", // Path when installing via GPU Operator - "usr/bin", // Path when installing via package manager - } - - // Fill in the binary location with just the name of the binary, - // and check against each of the possible locations. If a match is found, - // set the location to the full path. +func findNvidiaContainerRuntimes(foundRuntimes runtimeConfigs) { potentialRuntimes := runtimeConfigs{ "nvidia": { RuntimeType: "io.containerd.runc.v2", @@ -107,55 +71,46 @@ func findNvidiaContainerRuntimes(root fs.FS, foundRuntimes runtimeConfigs) { RuntimeType: "io.containerd.runc.v2", BinaryName: "nvidia-container-runtime-experimental", }, + "nvidia-cdi": { + RuntimeType: "io.containerd.runc.v2", + BinaryName: "nvidia-container-runtime.cdi", + }, } - searchForRuntimes(root, potentialRuntimes, locationsToCheck, foundRuntimes) -} -// findWasiRuntimes finds the WebAssembly (WASI) container runtimes that -// are available on the system and adds to foundRuntimes. It checks install locations used by the kwasm -// operator and by system package managers. The kwasm operator installation -// takes precedence over the system package manager installation. -// The given fs.FS should represent the filesystem root directory to search in. -func findWasiRuntimes(root fs.FS, foundRuntimes runtimeConfigs) { - // Check these locations in order. - locationsToCheck := []string{ - "opt/kwasm/bin", // Path when installing via kwasm Operator - "usr/bin", // Path when installing via package manager - "usr/sbin", // Path when installing via package manager - } + searchForRuntimes(potentialRuntimes, foundRuntimes) +} - // Fill in the binary location with just the name of the binary, - // and check against each of the possible locations. If a match is found, - // set the location to the full path. +func findWasiRuntimes(foundRuntimes runtimeConfigs) { potentialRuntimes := runtimeConfigs{ "lunatic": { - RuntimeType: "io.containerd.lunatic.v2", + RuntimeType: "io.containerd.lunatic.v1", BinaryName: "containerd-shim-lunatic-v1", }, "slight": { - RuntimeType: "io.containerd.slight.v2", + RuntimeType: "io.containerd.slight.v1", BinaryName: "containerd-shim-slight-v1", }, "spin": { RuntimeType: "io.containerd.spin.v2", - BinaryName: "containerd-shim-spin-v1", + BinaryName: "containerd-shim-spin-v2", }, "wws": { - RuntimeType: "io.containerd.wws.v2", + RuntimeType: "io.containerd.wws.v1", BinaryName: "containerd-shim-wws-v1", }, "wasmedge": { - RuntimeType: "io.containerd.wasmedge.v2", + RuntimeType: "io.containerd.wasmedge.v1", BinaryName: "containerd-shim-wasmedge-v1", }, "wasmer": { - RuntimeType: "io.containerd.wasmer.v2", + RuntimeType: "io.containerd.wasmer.v1", BinaryName: "containerd-shim-wasmer-v1", }, "wasmtime": { - RuntimeType: "io.containerd.wasmtime.v2", + RuntimeType: "io.containerd.wasmtime.v1", BinaryName: "containerd-shim-wasmtime-v1", }, } - searchForRuntimes(root, potentialRuntimes, locationsToCheck, foundRuntimes) + + searchForRuntimes(potentialRuntimes, foundRuntimes) } diff --git a/pkg/agent/containerd/runtimes_test.go b/pkg/agent/containerd/runtimes_test.go index b209b495..13da7cba 100644 --- a/pkg/agent/containerd/runtimes_test.go +++ b/pkg/agent/containerd/runtimes_test.go @@ -4,17 +4,15 @@ package containerd import ( - "io/fs" + "os" + "path/filepath" "reflect" "testing" - "testing/fstest" ) func Test_UnitFindContainerRuntimes(t *testing.T) { - executable := &fstest.MapFile{Mode: 0755} - type args struct { - root fs.FS + exec []string } tests := []struct { @@ -24,773 +22,117 @@ func Test_UnitFindContainerRuntimes(t *testing.T) { }{ { name: "No runtimes", - args: args{ - root: fstest.MapFS{}, - }, + args: args{}, want: runtimeConfigs{}, }, { name: "Found crun, nvidia and wasm", args: args{ - root: fstest.MapFS{ - "usr/bin/nvidia-container-runtime": executable, - "usr/bin/crun": executable, - "opt/kwasm/bin/containerd-shim-lunatic-v1": executable, + exec: []string{ + "nvidia-container-runtime", + "crun", + "containerd-shim-lunatic-v1", }, }, want: runtimeConfigs{ "nvidia": { RuntimeType: "io.containerd.runc.v2", - BinaryName: "/usr/bin/nvidia-container-runtime", + BinaryName: "/tmp/testExecutables/nvidia-container-runtime", }, "crun": { RuntimeType: "io.containerd.runc.v2", - BinaryName: "/usr/bin/crun", + BinaryName: "/tmp/testExecutables/crun", }, "lunatic": { - RuntimeType: "io.containerd.lunatic.v2", - BinaryName: "/opt/kwasm/bin/containerd-shim-lunatic-v1", - }, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - foundRuntimes := findContainerRuntimes(tt.args.root) - if !reflect.DeepEqual(foundRuntimes, tt.want) { - t.Errorf("findContainerRuntimes = %+v\nWant = %+v", foundRuntimes, tt.want) - } - }) - } -} - -func Test_UnitSearchContainerRuntimes(t *testing.T) { - executable := &fstest.MapFile{Mode: 0755} - locationsToCheck := []string{ - "usr/local/nvidia/toolkit", // Path for nvidia shim when installing via GPU Operator - "opt/kwasm/bin", // Path for wasm shim when installing via the kwasm operator - "usr/bin", // Path when installing via package manager - "usr/sbin", // Path when installing via package manager - } - - potentialRuntimes := runtimeConfigs{ - "nvidia": { - RuntimeType: "io.containerd.runc.v2", - BinaryName: "nvidia-container-runtime", - }, - "spin": { - RuntimeType: "io.containerd.spin.v2", - BinaryName: "containerd-shim-spin-v1", - }, - } - - type args struct { - root fs.FS - potentialRuntimes runtimeConfigs - locationsToCheck []string - } - tests := []struct { - name string - args args - want runtimeConfigs - }{ - { - name: "No runtimes", - args: args{ - root: fstest.MapFS{}, - locationsToCheck: locationsToCheck, - potentialRuntimes: potentialRuntimes, - }, - want: runtimeConfigs{}, - }, - { - name: "Nvidia runtime in /usr/bin", - args: args{ - root: fstest.MapFS{ - "usr/bin/nvidia-container-runtime": executable, - }, - locationsToCheck: locationsToCheck, - potentialRuntimes: potentialRuntimes, - }, - want: runtimeConfigs{ - "nvidia": { - RuntimeType: "io.containerd.runc.v2", - BinaryName: "/usr/bin/nvidia-container-runtime", + RuntimeType: "io.containerd.lunatic.v1", + BinaryName: "/tmp/testExecutables/containerd-shim-lunatic-v1", }, }, }, { - name: "Two runtimes in separate directories", + name: "Found only wasm", args: args{ - root: fstest.MapFS{ - "usr/bin/nvidia-container-runtime": executable, - "opt/kwasm/bin/containerd-shim-spin-v1": executable, + exec: []string{ + "containerd-shim-lunatic-v1", + "containerd-shim-wasmtime-v1", + "containerd-shim-lunatic-v1", + "containerd-shim-slight-v1", + "containerd-shim-spin-v2", + "containerd-shim-wws-v1", + "containerd-shim-wasmedge-v1", + "containerd-shim-wasmer-v1", }, - locationsToCheck: locationsToCheck, - potentialRuntimes: potentialRuntimes, - }, - want: runtimeConfigs{ - "nvidia": { - RuntimeType: "io.containerd.runc.v2", - BinaryName: "/usr/bin/nvidia-container-runtime", - }, - "spin": { - RuntimeType: "io.containerd.spin.v2", - BinaryName: "/opt/kwasm/bin/containerd-shim-spin-v1", - }, - }, - }, - { - name: "Same runtime in two directories", - args: args{ - root: fstest.MapFS{ - "usr/bin/containerd-shim-spin-v1": executable, - "opt/kwasm/bin/containerd-shim-spin-v1": executable, - }, - locationsToCheck: locationsToCheck, - potentialRuntimes: potentialRuntimes, - }, - want: runtimeConfigs{ - "spin": { - RuntimeType: "io.containerd.spin.v2", - BinaryName: "/opt/kwasm/bin/containerd-shim-spin-v1", - }, - }, - }, - { - name: "Both runtimes in /usr/bin", - args: args{ - root: fstest.MapFS{ - "usr/bin/containerd-shim-spin-v1": executable, - "usr/bin/nvidia-container-runtime": executable, - }, - locationsToCheck: locationsToCheck, - potentialRuntimes: potentialRuntimes, - }, - want: runtimeConfigs{ - "nvidia": { - RuntimeType: "io.containerd.runc.v2", - BinaryName: "/usr/bin/nvidia-container-runtime", - }, - "spin": { - RuntimeType: "io.containerd.spin.v2", - BinaryName: "/usr/bin/containerd-shim-spin-v1", - }, - }, - }, - { - name: "Both runtimes in both directories", - args: args{ - root: fstest.MapFS{ - "usr/local/nvidia/toolkit/nvidia-container-runtime": executable, - "usr/bin/nvidia-container-runtime": executable, - "usr/bin/containerd-shim-spin-v1": executable, - "opt/kwasm/bin/containerd-shim-spin-v1": executable, - }, - locationsToCheck: locationsToCheck, - potentialRuntimes: potentialRuntimes, - }, - want: runtimeConfigs{ - "nvidia": { - RuntimeType: "io.containerd.runc.v2", - BinaryName: "/usr/local/nvidia/toolkit/nvidia-container-runtime", - }, - "spin": { - RuntimeType: "io.containerd.spin.v2", - BinaryName: "/opt/kwasm/bin/containerd-shim-spin-v1", - }, - }, - }, - { - name: "Both runtimes in /usr/bin and one duplicate in /usr/local/nvidia/toolkit", - args: args{ - root: fstest.MapFS{ - "usr/bin/nvidia-container-runtime": executable, - "usr/bin/containerd-shim-spin-v1": executable, - "usr/local/nvidia/toolkit/nvidia-container-runtime": executable, - }, - locationsToCheck: locationsToCheck, - potentialRuntimes: potentialRuntimes, - }, - want: runtimeConfigs{ - "spin": { - RuntimeType: "io.containerd.spin.v2", - BinaryName: "/usr/bin/containerd-shim-spin-v1", - }, - "nvidia": { - RuntimeType: "io.containerd.runc.v2", - BinaryName: "/usr/local/nvidia/toolkit/nvidia-container-runtime", - }, - }, - }, - { - name: "Runtime is a directory", - args: args{ - root: fstest.MapFS{ - "usr/bin/nvidia-container-runtime": &fstest.MapFile{ - Mode: fs.ModeDir, - }, - }, - locationsToCheck: locationsToCheck, - potentialRuntimes: potentialRuntimes, - }, - want: runtimeConfigs{}, - }, - { - name: "Runtime in both directories, but one is a directory", - args: args{ - root: fstest.MapFS{ - "usr/bin/nvidia-container-runtime": executable, - "usr/local/nvidia/toolkit/nvidia-container-runtime": &fstest.MapFile{ - Mode: fs.ModeDir, - }, - }, - locationsToCheck: locationsToCheck, - potentialRuntimes: potentialRuntimes, - }, - want: runtimeConfigs{ - "nvidia": { - RuntimeType: "io.containerd.runc.v2", - BinaryName: "/usr/bin/nvidia-container-runtime", - }, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - foundRuntimes := runtimeConfigs{} - searchForRuntimes(tt.args.root, tt.args.potentialRuntimes, tt.args.locationsToCheck, foundRuntimes) - if !reflect.DeepEqual(foundRuntimes, tt.want) { - t.Errorf("findContainerRuntimes() = %+v\nWant = %+v", foundRuntimes, tt.want) - } - }) - } -} - -func Test_UnitSearchWasiRuntimes(t *testing.T) { - executable := &fstest.MapFile{Mode: 0755} - - locationsToCheck := []string{ - "usr/local/nvidia/toolkit", // Path for nvidia shim when installing via GPU Operator - "opt/kwasm/bin", // Path for wasm shim when installing via the kwasm operator - "usr/bin", // Path when installing via package manager - "usr/sbin", // Path when installing via package manager - } - - potentialRuntimes := runtimeConfigs{ - "wasmtime": { - RuntimeType: "io.containerd.wasmtime.v2", - BinaryName: "containerd-shim-wasmtime-v1", - }, - "lunatic": { - RuntimeType: "io.containerd.lunatic.v2", - BinaryName: "containerd-shim-lunatic-v1", - }, - "slight": { - RuntimeType: "io.containerd.slight.v2", - BinaryName: "containerd-shim-slight-v1", - }, - "spin": { - RuntimeType: "io.containerd.spin.v2", - BinaryName: "containerd-shim-spin-v1", - }, - "wws": { - RuntimeType: "io.containerd.wws.v2", - BinaryName: "containerd-shim-wws-v1", - }, - "wasmedge": { - RuntimeType: "io.containerd.wasmedge.v2", - BinaryName: "containerd-shim-wasmedge-v1", - }, - "wasmer": { - RuntimeType: "io.containerd.wasmer.v2", - BinaryName: "containerd-shim-wasmer-v1", - }, - "nvidia": { - RuntimeType: "io.containerd.runc.v2", - BinaryName: "nvidia-container-runtime", - }, - } - - type args struct { - root fs.FS - potentialRuntimes runtimeConfigs - locationsToCheck []string - } - - tests := []struct { - name string - args args - want runtimeConfigs - }{ - { - name: "No runtimes", - args: args{ - root: fstest.MapFS{}, - locationsToCheck: locationsToCheck, - potentialRuntimes: potentialRuntimes, - }, - want: runtimeConfigs{}, - }, - { - name: "wasmtime runtime in /usr/sbin", - args: args{ - root: fstest.MapFS{ - "usr/sbin/containerd-shim-wasmtime-v1": executable, - }, - locationsToCheck: locationsToCheck, - potentialRuntimes: potentialRuntimes, }, want: runtimeConfigs{ "wasmtime": { - RuntimeType: "io.containerd.wasmtime.v2", - BinaryName: "/usr/sbin/containerd-shim-wasmtime-v1", - }, - }, - }, - { - name: "lunatic runtime in /opt/kwasm/bin/", - args: args{ - root: fstest.MapFS{ - "opt/kwasm/bin/containerd-shim-lunatic-v1": executable, - }, - locationsToCheck: locationsToCheck, - potentialRuntimes: potentialRuntimes, - }, - want: runtimeConfigs{ - "lunatic": { - RuntimeType: "io.containerd.lunatic.v2", - BinaryName: "/opt/kwasm/bin/containerd-shim-lunatic-v1", - }, - }, - }, - { - name: "Two runtimes in separate directories", - args: args{ - root: fstest.MapFS{ - "usr/bin/containerd-shim-wasmer-v1": executable, - "opt/kwasm/bin/containerd-shim-slight-v1": executable, - }, - locationsToCheck: locationsToCheck, - potentialRuntimes: potentialRuntimes, - }, - want: runtimeConfigs{ - "slight": { - RuntimeType: "io.containerd.slight.v2", - BinaryName: "/opt/kwasm/bin/containerd-shim-slight-v1", - }, - "wasmer": { - RuntimeType: "io.containerd.wasmer.v2", - BinaryName: "/usr/bin/containerd-shim-wasmer-v1", - }, - }, - }, - { - name: "Same runtime in two directories", - args: args{ - root: fstest.MapFS{ - "usr/bin/containerd-shim-wasmedge-v1": executable, - "opt/kwasm/bin/containerd-shim-wasmedge-v1": executable, - }, - locationsToCheck: locationsToCheck, - potentialRuntimes: potentialRuntimes, - }, - want: runtimeConfigs{ - "wasmedge": { - RuntimeType: "io.containerd.wasmedge.v2", - BinaryName: "/opt/kwasm/bin/containerd-shim-wasmedge-v1", - }, - }, - }, - { - name: "All runtimes in /usr/bin", - args: args{ - root: fstest.MapFS{ - "usr/bin/containerd-shim-lunatic-v1": executable, - "usr/bin/containerd-shim-slight-v1": executable, - "usr/bin/containerd-shim-spin-v1": executable, - "usr/bin/containerd-shim-wws-v1": executable, - "usr/bin/containerd-shim-wasmedge-v1": executable, - "usr/bin/containerd-shim-wasmer-v1": executable, - "usr/bin/containerd-shim-wasmtime-v1": executable, + RuntimeType: "io.containerd.wasmtime.v1", + BinaryName: "/tmp/testExecutables/containerd-shim-wasmtime-v1", }, - locationsToCheck: locationsToCheck, - potentialRuntimes: potentialRuntimes, - }, - want: runtimeConfigs{ "lunatic": { - RuntimeType: "io.containerd.lunatic.v2", - BinaryName: "/usr/bin/containerd-shim-lunatic-v1", + RuntimeType: "io.containerd.lunatic.v1", + BinaryName: "/tmp/testExecutables/containerd-shim-lunatic-v1", }, "slight": { - RuntimeType: "io.containerd.slight.v2", - BinaryName: "/usr/bin/containerd-shim-slight-v1", + RuntimeType: "io.containerd.slight.v1", + BinaryName: "/tmp/testExecutables/containerd-shim-slight-v1", }, "spin": { RuntimeType: "io.containerd.spin.v2", - BinaryName: "/usr/bin/containerd-shim-spin-v1", + BinaryName: "/tmp/testExecutables/containerd-shim-spin-v2", }, "wws": { - RuntimeType: "io.containerd.wws.v2", - BinaryName: "/usr/bin/containerd-shim-wws-v1", + RuntimeType: "io.containerd.wws.v1", + BinaryName: "/tmp/testExecutables/containerd-shim-wws-v1", }, "wasmedge": { - RuntimeType: "io.containerd.wasmedge.v2", - BinaryName: "/usr/bin/containerd-shim-wasmedge-v1", + RuntimeType: "io.containerd.wasmedge.v1", + BinaryName: "/tmp/testExecutables/containerd-shim-wasmedge-v1", }, "wasmer": { - RuntimeType: "io.containerd.wasmer.v2", - BinaryName: "/usr/bin/containerd-shim-wasmer-v1", - }, - "wasmtime": { - RuntimeType: "io.containerd.wasmtime.v2", - BinaryName: "/usr/bin/containerd-shim-wasmtime-v1", - }, - }, - }, - { - name: "Both runtimes in both directories", - args: args{ - root: fstest.MapFS{ - "opt/kwasm/bin/containerd-shim-slight-v1": executable, - "opt/kwasm/bin/containerd-shim-wasmtime-v1": executable, - "usr/bin/containerd-shim-slight-v1": executable, - "usr/bin/containerd-shim-wasmtime-v1": executable, - }, - locationsToCheck: locationsToCheck, - potentialRuntimes: potentialRuntimes, - }, - want: runtimeConfigs{ - "slight": { - RuntimeType: "io.containerd.slight.v2", - BinaryName: "/opt/kwasm/bin/containerd-shim-slight-v1", - }, - "wasmtime": { - RuntimeType: "io.containerd.wasmtime.v2", - BinaryName: "/opt/kwasm/bin/containerd-shim-wasmtime-v1", - }, - }, - }, - { - name: "Preserve already found runtimes", - args: args{ - root: fstest.MapFS{ - "opt/kwasm/bin/containerd-shim-wasmtime-v1": executable, - "usr/bin/nvidia-container-runtime": executable, - }, - locationsToCheck: locationsToCheck, - potentialRuntimes: potentialRuntimes, - }, - want: runtimeConfigs{ - "nvidia": { - RuntimeType: "io.containerd.runc.v2", - BinaryName: "/usr/bin/nvidia-container-runtime", - }, - "wasmtime": { - RuntimeType: "io.containerd.wasmtime.v2", - BinaryName: "/opt/kwasm/bin/containerd-shim-wasmtime-v1", + RuntimeType: "io.containerd.wasmer.v1", + BinaryName: "/tmp/testExecutables/containerd-shim-wasmer-v1", }, }, }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - foundRuntimes := runtimeConfigs{} - searchForRuntimes(tt.args.root, tt.args.potentialRuntimes, tt.args.locationsToCheck, foundRuntimes) - if !reflect.DeepEqual(foundRuntimes, tt.want) { - t.Errorf("searchForRuntimes = %+v\nWant = %+v", foundRuntimes, tt.want) + tempDirPath := filepath.Join(os.TempDir(), "testExecutables") + err := os.Mkdir(tempDirPath, 0755) + if err != nil { + t.Errorf("Error creating directory: %v", err) } - }) - } -} -func Test_UnitSearchNvidiaContainerRuntimes(t *testing.T) { - executable := &fstest.MapFile{Mode: 0755} + defer os.RemoveAll(tempDirPath) - locationsToCheck := []string{ - "usr/local/nvidia/toolkit", // Path for nvidia shim when installing via GPU Operator - "opt/kwasm/bin", // Path for wasm shim when installing via the kwasm operator - "usr/bin", // Path when installing via package manager - "usr/sbin", // Path when installing via package manager - } - - potentialRuntimes := runtimeConfigs{ - "nvidia": { - RuntimeType: "io.containerd.runc.v2", - BinaryName: "nvidia-container-runtime", - }, - "nvidia-experimental": { - RuntimeType: "io.containerd.runc.v2", - BinaryName: "nvidia-container-runtime-experimental", - }, - "slight": { - RuntimeType: "io.containerd.slight.v2", - BinaryName: "containerd-shim-slight-v1", - }, - "wasmtime": { - RuntimeType: "io.containerd.wasmtime.v2", - BinaryName: "containerd-shim-wasmtime-v1", - }, - } + for _, execName := range tt.args.exec { + execPath := filepath.Join(tempDirPath, execName) + if err := createExec(execPath); err != nil { + t.Errorf("Failed to create executable %s: %v", execPath, err) + } + } - type args struct { - root fs.FS - potentialRuntimes runtimeConfigs - locationsToCheck []string - } + originalPath := os.Getenv("PATH") + os.Setenv("PATH", tempDirPath) + defer os.Setenv("PATH", originalPath) - tests := []struct { - name string - args args - want runtimeConfigs - }{ - { - name: "No runtimes", - args: args{ - root: fstest.MapFS{}, - potentialRuntimes: potentialRuntimes, - locationsToCheck: locationsToCheck, - }, - want: runtimeConfigs{}, - }, - { - name: "Nvidia runtime in /usr/bin", - args: args{ - root: fstest.MapFS{ - "usr/bin/nvidia-container-runtime": executable, - }, - potentialRuntimes: potentialRuntimes, - locationsToCheck: locationsToCheck, - }, - want: runtimeConfigs{ - "nvidia": { - RuntimeType: "io.containerd.runc.v2", - BinaryName: "/usr/bin/nvidia-container-runtime", - }, - }, - }, - { - name: "Experimental runtime in /usr/local/nvidia/toolkit", - args: args{ - root: fstest.MapFS{ - "usr/local/nvidia/toolkit/nvidia-container-runtime": executable, - }, - potentialRuntimes: potentialRuntimes, - locationsToCheck: locationsToCheck, - }, - want: runtimeConfigs{ - "nvidia": { - RuntimeType: "io.containerd.runc.v2", - BinaryName: "/usr/local/nvidia/toolkit/nvidia-container-runtime", - }, - }, - }, - { - name: "Two runtimes in separate directories", - args: args{ - root: fstest.MapFS{ - "usr/bin/nvidia-container-runtime": executable, - "usr/local/nvidia/toolkit/nvidia-container-runtime": executable, - }, - potentialRuntimes: potentialRuntimes, - locationsToCheck: locationsToCheck, - }, - want: runtimeConfigs{ - "nvidia": { - RuntimeType: "io.containerd.runc.v2", - BinaryName: "/usr/local/nvidia/toolkit/nvidia-container-runtime", - }, - }, - }, - { - name: "Experimental runtime in /usr/bin", - args: args{ - root: fstest.MapFS{ - "usr/bin/nvidia-container-runtime-experimental": executable, - }, - potentialRuntimes: potentialRuntimes, - locationsToCheck: locationsToCheck, - }, - want: runtimeConfigs{ - "nvidia-experimental": { - RuntimeType: "io.containerd.runc.v2", - BinaryName: "/usr/bin/nvidia-container-runtime-experimental", - }, - }, - }, - { - name: "Same runtime in two directories", - args: args{ - root: fstest.MapFS{ - "usr/bin/nvidia-container-runtime-experimental": executable, - "usr/local/nvidia/toolkit/nvidia-container-runtime-experimental": executable, - }, - potentialRuntimes: potentialRuntimes, - locationsToCheck: locationsToCheck, - }, - want: runtimeConfigs{ - "nvidia-experimental": { - RuntimeType: "io.containerd.runc.v2", - BinaryName: "/usr/local/nvidia/toolkit/nvidia-container-runtime-experimental", - }, - }, - }, - { - name: "Both runtimes in /usr/bin", - args: args{ - root: fstest.MapFS{ - "usr/bin/nvidia-container-runtime-experimental": executable, - "usr/bin/nvidia-container-runtime": executable, - }, - potentialRuntimes: potentialRuntimes, - locationsToCheck: locationsToCheck, - }, - want: runtimeConfigs{ - "nvidia": { - RuntimeType: "io.containerd.runc.v2", - BinaryName: "/usr/bin/nvidia-container-runtime", - }, - "nvidia-experimental": { - RuntimeType: "io.containerd.runc.v2", - BinaryName: "/usr/bin/nvidia-container-runtime-experimental", - }, - }, - }, - { - name: "Both runtimes in both directories", - args: args{ - root: fstest.MapFS{ - "usr/local/nvidia/toolkit/nvidia-container-runtime": executable, - "usr/local/nvidia/toolkit/nvidia-container-runtime-experimental": executable, - "usr/bin/nvidia-container-runtime": executable, - "usr/bin/nvidia-container-runtime-experimental": executable, - }, - potentialRuntimes: potentialRuntimes, - locationsToCheck: locationsToCheck, - }, - want: runtimeConfigs{ - "nvidia": { - RuntimeType: "io.containerd.runc.v2", - BinaryName: "/usr/local/nvidia/toolkit/nvidia-container-runtime", - }, - "nvidia-experimental": { - RuntimeType: "io.containerd.runc.v2", - BinaryName: "/usr/local/nvidia/toolkit/nvidia-container-runtime-experimental", - }, - }, - }, - { - name: "Both runtimes in /usr/local/nvidia/toolkit", - args: args{ - root: fstest.MapFS{ - "usr/local/nvidia/toolkit/nvidia-container-runtime": executable, - "usr/local/nvidia/toolkit/nvidia-container-runtime-experimental": executable, - }, - potentialRuntimes: potentialRuntimes, - locationsToCheck: locationsToCheck, - }, - want: runtimeConfigs{ - "nvidia": { - RuntimeType: "io.containerd.runc.v2", - BinaryName: "/usr/local/nvidia/toolkit/nvidia-container-runtime", - }, - "nvidia-experimental": { - RuntimeType: "io.containerd.runc.v2", - BinaryName: "/usr/local/nvidia/toolkit/nvidia-container-runtime-experimental", - }, - }, - }, - { - name: "Both runtimes in /usr/bin and one duplicate in /usr/local/nvidia/toolkit", - args: args{ - root: fstest.MapFS{ - "usr/bin/nvidia-container-runtime": executable, - "usr/bin/nvidia-container-runtime-experimental": executable, - "usr/local/nvidia/toolkit/nvidia-container-runtime-experimental": executable, - }, - potentialRuntimes: potentialRuntimes, - locationsToCheck: locationsToCheck, - }, - want: runtimeConfigs{ - "nvidia": { - RuntimeType: "io.containerd.runc.v2", - BinaryName: "/usr/bin/nvidia-container-runtime", - }, - "nvidia-experimental": { - RuntimeType: "io.containerd.runc.v2", - BinaryName: "/usr/local/nvidia/toolkit/nvidia-container-runtime-experimental", - }, - }, - }, - { - name: "Runtime is a directory", - args: args{ - root: fstest.MapFS{ - "usr/bin/nvidia-container-runtime": &fstest.MapFile{ - Mode: fs.ModeDir, - }, - }, - potentialRuntimes: potentialRuntimes, - locationsToCheck: locationsToCheck, - }, - want: runtimeConfigs{}, - }, - { - name: "Runtime in both directories, but one is a directory", - args: args{ - root: fstest.MapFS{ - "usr/bin/nvidia-container-runtime": executable, - "usr/local/nvidia/toolkit/nvidia-container-runtime": &fstest.MapFile{ - Mode: fs.ModeDir, - }, - }, - potentialRuntimes: potentialRuntimes, - locationsToCheck: locationsToCheck, - }, - want: runtimeConfigs{ - "nvidia": { - RuntimeType: "io.containerd.runc.v2", - BinaryName: "/usr/bin/nvidia-container-runtime", - }, - }, - }, - { - name: "Preserve already found runtimes", - args: args{ - root: fstest.MapFS{ - "usr/bin/nvidia-container-runtime": executable, - "opt/kwasm/bin/containerd-shim-wasmtime-v1": executable, - "opt/kwasm/bin/containerd-shim-slight-v1": executable, - "usr/local/nvidia/toolkit/nvidia-container-runtime": &fstest.MapFile{ - Mode: fs.ModeDir, - }, - }, - potentialRuntimes: potentialRuntimes, - locationsToCheck: locationsToCheck, - }, - want: runtimeConfigs{ - "slight": { - RuntimeType: "io.containerd.slight.v2", - BinaryName: "/opt/kwasm/bin/containerd-shim-slight-v1", - }, - "wasmtime": { - RuntimeType: "io.containerd.wasmtime.v2", - BinaryName: "/opt/kwasm/bin/containerd-shim-wasmtime-v1", - }, - "nvidia": { - RuntimeType: "io.containerd.runc.v2", - BinaryName: "/usr/bin/nvidia-container-runtime", - }, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - foundRuntimes := runtimeConfigs{} - searchForRuntimes(tt.args.root, tt.args.potentialRuntimes, tt.args.locationsToCheck, foundRuntimes) + foundRuntimes := findContainerRuntimes() if !reflect.DeepEqual(foundRuntimes, tt.want) { - t.Errorf("searchForRuntimes() = %+v\nWant = %+v", foundRuntimes, tt.want) + t.Errorf("findContainerRuntimes = %+v\nWant = %+v", foundRuntimes, tt.want) } }) } } + +func createExec(path string) error { + if err := os.WriteFile(path, []byte{}, 0755); err != nil { + return err + } + + if err := os.Chmod(path, 0755); err != nil { + return err + } + + return nil +} \ No newline at end of file