mirror of
https://github.com/vee1e/runtimeclass-debugger.git
synced 2026-09-01 18:27:58 +00:00
83 lines
1.9 KiB
Go
83 lines
1.9 KiB
Go
package cri
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func loadFixture(t *testing.T, content string) string {
|
|
t.Helper()
|
|
path := filepath.Join(t.TempDir(), "config.toml")
|
|
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return path
|
|
}
|
|
|
|
func TestLoadWithKata(t *testing.T) {
|
|
cc, err := Load(loadFixture(t, configWithKata))
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
if !cc.HasHandler("kata") {
|
|
t.Fatalf("expected handler kata, got %v", cc.HandlerNames())
|
|
}
|
|
}
|
|
|
|
func TestLoadWithMultipleRuntimes(t *testing.T) {
|
|
cc, err := Load(loadFixture(t, configWithMultipleRuntimes))
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
want := []string{"kata", "kata-qemu", "kata-qemu-tdx"}
|
|
if !reflect.DeepEqual(cc.HandlerNames(), want) {
|
|
t.Fatalf("expected %v, got %v", want, cc.HandlerNames())
|
|
}
|
|
}
|
|
|
|
func TestLoadWithoutRuntimes(t *testing.T) {
|
|
cc, err := Load(loadFixture(t, configWithoutRuntimes))
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
if got := cc.HandlerNames(); len(got) != 0 {
|
|
t.Fatalf("expected no handlers, got %v", got)
|
|
}
|
|
}
|
|
|
|
func TestLoadLegacyCRIName(t *testing.T) {
|
|
cc, err := Load(loadFixture(t, configWithLegacyCRIName))
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
if !cc.HasHandler("runsc") {
|
|
t.Fatalf("expected handler runsc, got %v", cc.HandlerNames())
|
|
}
|
|
}
|
|
|
|
func TestLoadEmptyRuntimes(t *testing.T) {
|
|
cc, err := Load(loadFixture(t, configWithEmptyRuntimes))
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
if got := cc.HandlerNames(); len(got) != 0 {
|
|
t.Fatalf("expected no handlers, got %v", got)
|
|
}
|
|
}
|
|
|
|
func TestLoadMissingFile(t *testing.T) {
|
|
_, err := Load(filepath.Join(t.TempDir(), "does-not-exist.toml"))
|
|
if !errors.Is(err, ErrConfigNotFound) {
|
|
t.Fatalf("expected ErrConfigNotFound, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestLoadMalformed(t *testing.T) {
|
|
_, err := Load(loadFixture(t, "this is not = toml [["))
|
|
if err == nil {
|
|
t.Fatal("expected parse error, got nil")
|
|
}
|
|
}
|