2023-11-13 22:43:41 +00:00
|
|
|
//go:build linux
|
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package containerd
|
|
|
|
|
|
|
|
import (
|
2024-01-28 20:22:59 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-11-13 22:43:41 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_UnitFindContainerRuntimes(t *testing.T) {
|
2023-11-21 20:38:56 +00:00
|
|
|
type args struct {
|
2024-01-28 20:22:59 +00:00
|
|
|
exec []string
|
2023-11-21 20:38:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want runtimeConfigs
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "No runtimes",
|
2024-01-28 20:22:59 +00:00
|
|
|
args: args{},
|
2023-11-21 20:38:56 +00:00
|
|
|
want: runtimeConfigs{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Found crun, nvidia and wasm",
|
|
|
|
args: args{
|
2024-01-28 20:22:59 +00:00
|
|
|
exec: []string{
|
|
|
|
"nvidia-container-runtime",
|
|
|
|
"crun",
|
|
|
|
"containerd-shim-lunatic-v1",
|
2023-11-21 20:38:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
want: runtimeConfigs{
|
|
|
|
"nvidia": {
|
|
|
|
RuntimeType: "io.containerd.runc.v2",
|
2024-01-28 20:22:59 +00:00
|
|
|
BinaryName: "/tmp/testExecutables/nvidia-container-runtime",
|
2023-11-21 20:38:56 +00:00
|
|
|
},
|
|
|
|
"crun": {
|
|
|
|
RuntimeType: "io.containerd.runc.v2",
|
2024-01-28 20:22:59 +00:00
|
|
|
BinaryName: "/tmp/testExecutables/crun",
|
2023-11-21 20:38:56 +00:00
|
|
|
},
|
|
|
|
"lunatic": {
|
|
|
|
RuntimeType: "io.containerd.lunatic.v2",
|
2024-01-28 20:22:59 +00:00
|
|
|
BinaryName: "/tmp/testExecutables/containerd-shim-lunatic-v1",
|
2023-11-13 22:43:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-01-28 20:22:59 +00:00
|
|
|
name: "Found only wasm",
|
2023-11-13 22:43:41 +00:00
|
|
|
args: args{
|
2024-01-28 20:22:59 +00:00
|
|
|
exec: []string{
|
|
|
|
"containerd-shim-lunatic-v1",
|
|
|
|
"containerd-shim-wasmtime-v1",
|
|
|
|
"containerd-shim-lunatic-v1",
|
|
|
|
"containerd-shim-slight-v1",
|
|
|
|
"containerd-shim-spin-v1",
|
|
|
|
"containerd-shim-wws-v1",
|
|
|
|
"containerd-shim-wasmedge-v1",
|
|
|
|
"containerd-shim-wasmer-v1",
|
2023-11-13 22:43:41 +00:00
|
|
|
},
|
2023-11-21 20:38:56 +00:00
|
|
|
},
|
|
|
|
want: runtimeConfigs{
|
|
|
|
"wasmtime": {
|
|
|
|
RuntimeType: "io.containerd.wasmtime.v2",
|
2024-01-28 20:22:59 +00:00
|
|
|
BinaryName: "/tmp/testExecutables/containerd-shim-wasmtime-v1",
|
2023-11-21 20:38:56 +00:00
|
|
|
},
|
|
|
|
"lunatic": {
|
|
|
|
RuntimeType: "io.containerd.lunatic.v2",
|
2024-01-28 20:22:59 +00:00
|
|
|
BinaryName: "/tmp/testExecutables/containerd-shim-lunatic-v1",
|
2023-11-21 20:38:56 +00:00
|
|
|
},
|
|
|
|
"slight": {
|
|
|
|
RuntimeType: "io.containerd.slight.v2",
|
2024-01-28 20:22:59 +00:00
|
|
|
BinaryName: "/tmp/testExecutables/containerd-shim-slight-v1",
|
2023-11-21 20:38:56 +00:00
|
|
|
},
|
|
|
|
"spin": {
|
|
|
|
RuntimeType: "io.containerd.spin.v2",
|
2024-01-28 20:22:59 +00:00
|
|
|
BinaryName: "/tmp/testExecutables/containerd-shim-spin-v1",
|
2023-11-21 20:38:56 +00:00
|
|
|
},
|
|
|
|
"wws": {
|
|
|
|
RuntimeType: "io.containerd.wws.v2",
|
2024-01-28 20:22:59 +00:00
|
|
|
BinaryName: "/tmp/testExecutables/containerd-shim-wws-v1",
|
2023-11-21 20:38:56 +00:00
|
|
|
},
|
|
|
|
"wasmedge": {
|
|
|
|
RuntimeType: "io.containerd.wasmedge.v2",
|
2024-01-28 20:22:59 +00:00
|
|
|
BinaryName: "/tmp/testExecutables/containerd-shim-wasmedge-v1",
|
2023-11-21 20:38:56 +00:00
|
|
|
},
|
|
|
|
"wasmer": {
|
|
|
|
RuntimeType: "io.containerd.wasmer.v2",
|
2024-01-28 20:22:59 +00:00
|
|
|
BinaryName: "/tmp/testExecutables/containerd-shim-wasmer-v1",
|
2023-11-21 20:38:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2024-01-28 20:22:59 +00:00
|
|
|
|
2023-11-21 20:38:56 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-01-28 20:22:59 +00:00
|
|
|
tempDirPath := filepath.Join(os.TempDir(), "testExecutables")
|
|
|
|
err := os.Mkdir(tempDirPath, 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error creating directory: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer os.RemoveAll(tempDirPath)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
originalPath := os.Getenv("PATH")
|
|
|
|
os.Setenv("PATH", tempDirPath)
|
|
|
|
defer os.Setenv("PATH", originalPath)
|
|
|
|
|
|
|
|
foundRuntimes := findContainerRuntimes()
|
2023-11-21 20:38:56 +00:00
|
|
|
if !reflect.DeepEqual(foundRuntimes, tt.want) {
|
2024-01-28 20:22:59 +00:00
|
|
|
t.Errorf("findContainerRuntimes = %+v\nWant = %+v", foundRuntimes, tt.want)
|
2023-11-21 20:38:56 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-28 20:22:59 +00:00
|
|
|
func createExec(path string) error {
|
|
|
|
if err := os.WriteFile(path, []byte{}, 0755); err != nil {
|
|
|
|
return err
|
2023-11-21 20:38:56 +00:00
|
|
|
}
|
|
|
|
|
2024-01-28 20:22:59 +00:00
|
|
|
if err := os.Chmod(path, 0755); err != nil {
|
|
|
|
return err
|
2023-11-21 20:38:56 +00:00
|
|
|
}
|
|
|
|
|
2024-01-28 20:22:59 +00:00
|
|
|
return nil
|
2023-11-21 20:38:56 +00:00
|
|
|
}
|