package plugin import ( "strings" "testing" ) func TestHostFeaturesAdvertiseGuestDriverContract(t *testing.T) { has := map[string]bool{} for _, f := range HostFeatures() { has[f] = true } for _, want := range []string{ "driver.snapshot", "driver.clone", "driver.disk", "driver.logs", "driver.phase", "driver.hosted", "driver.runtimeKind.external", "driver.pasteCombo", "driver.keymap", "host.vm.spawn.ref", "host.vm.reportState", "driver.console", "host.secret.materialize", "host.download", "host.elevate.request", } { if !has[want] { t.Errorf("stable feature %s is not advertised", want) } } } func guestDriverManifest(mutate func(*GuestDriverContribution)) Manifest { gd := GuestDriverContribution{ ID: "vendor.driver.lab", Name: LocalizedText{EnUS: "Lab"}, RuntimeKind: "external", Capabilities: []string{"hosted", "snapshot"}, } if mutate != nil { mutate(&gd) } return Manifest{ Schema: 1, ID: "vendor.guest", Version: "1.0.0", Name: LocalizedText{EnUS: "Guest"}, Entry: Entry{Sidecar: "bin/guest.exe"}, Permissions: []string{"driver:vm"}, Contributes: Contributes{GuestDrivers: []GuestDriverContribution{gd}}, } } func TestValidateManifestGuestDriverFields(t *testing.T) { cases := []struct { name string mutate func(*GuestDriverContribution) wantErr string }{ {"external runtime kind", nil, ""}, {"qmp console", func(g *GuestDriverContribution) { g.ConsoleEndpoint = "qmp" }, ""}, {"driver console", func(g *GuestDriverContribution) { g.ConsoleEndpoint = "driver" }, ""}, {"unknown console", func(g *GuestDriverContribution) { g.ConsoleEndpoint = "vnc" }, "consoleEndpoint"}, {"paste combo", func(g *GuestDriverContribution) { g.PasteCombo = "ctrl+shift+v" }, ""}, {"paste combo meta", func(g *GuestDriverContribution) { g.PasteCombo = "meta_l+v" }, ""}, {"bad paste combo", func(g *GuestDriverContribution) { g.PasteCombo = "Ctrl+V" }, "pasteCombo"}, {"paste combo injection", func(g *GuestDriverContribution) { g.PasteCombo = "ctrl+v; calc" }, "pasteCombo"}, {"keymap", func(g *GuestDriverContribution) { g.Keymap = "us" }, ""}, {"bad keymap", func(g *GuestDriverContribution) { g.Keymap = "../x" }, "keymap"}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { err := ValidateManifest(guestDriverManifest(tc.mutate), "", false) if tc.wantErr == "" && err != nil { t.Fatalf("unexpected error: %v", err) } if tc.wantErr != "" && (err == nil || !strings.Contains(err.Error(), tc.wantErr)) { t.Fatalf("err = %v, want %q", err, tc.wantErr) } }) } } func TestDriverConsoleEndpointIsAStableContract(t *testing.T) { for _, mutate := range []func(*GuestDriverContribution){ nil, func(g *GuestDriverContribution) { g.ConsoleEndpoint = "driver" }, func(g *GuestDriverContribution) { g.ConsoleEndpoint = "qmp" }, } { for _, f := range ContractFindings(guestDriverManifest(mutate), nil) { if f.Check == "contract-experimental" { t.Fatalf("consoleEndpoint got an experimental finding: %+v", f) } } } }