Adapt Intel VM suspend/resume to driver.SuspendDriver and lifecycle.Service, move native VM close confirmation into internal/host, migrate vm-suspend legacy capability detection, drop moved runtime build tooling, and regenerate i18n and Wails bindings.
89 lines
3.0 KiB
Go
89 lines
3.0 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestParseWindowArgs(t *testing.T) {
|
|
cases := []struct {
|
|
args []string
|
|
want WindowArgs
|
|
isPlain bool // window launch, not a command
|
|
}{
|
|
{nil, WindowArgs{}, true},
|
|
{[]string{"--background"}, WindowArgs{Background: true}, true},
|
|
{[]string{"--background", "--start-vm=m0", "--start-driver=test.driver.linux", `--start-result=D:\tmp\cli-start-1.json`},
|
|
WindowArgs{Background: true, StartVM: "m0", StartDriver: "test.driver.linux", StartResult: `D:\tmp\cli-start-1.json`}, true},
|
|
{[]string{"vm", "start", "m0"}, WindowArgs{}, false},
|
|
{[]string{"--background", "vm", "list"}, WindowArgs{}, false},
|
|
{[]string{"--help"}, WindowArgs{}, false},
|
|
{[]string{"--start-vm", "m0"}, WindowArgs{}, false}, // value must be joined with =
|
|
{[]string{"--close-vm=mac os"}, WindowArgs{CloseVM: "mac os"}, true},
|
|
{[]string{"--close-vm="}, WindowArgs{}, false},
|
|
{[]string{"--wait-pid=4242"}, WindowArgs{WaitPID: 4242}, true},
|
|
{[]string{"--background", "--wait-pid=7"}, WindowArgs{Background: true, WaitPID: 7}, true},
|
|
{[]string{"--wait-pid=0"}, WindowArgs{}, false},
|
|
{[]string{"--wait-pid=abc"}, WindowArgs{}, false},
|
|
}
|
|
for _, c := range cases {
|
|
got, ok := ParseWindowArgs(c.args)
|
|
if ok != c.isPlain || !reflect.DeepEqual(got, c.want) {
|
|
t.Errorf("ParseWindowArgs(%q) = %+v, %v; want %+v, %v", c.args, got, ok, c.want, c.isPlain)
|
|
}
|
|
if IsCommand(c.args) == c.isPlain {
|
|
t.Errorf("IsCommand(%q) = %v", c.args, !c.isPlain)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCloseWindowArgsRoundTrip(t *testing.T) {
|
|
want := WindowArgs{CloseVM: "macOS Test"}
|
|
if got, ok := ParseWindowArgs(want.Args()); !ok || got != want {
|
|
t.Fatalf("close request round trip = %+v, %v", got, ok)
|
|
}
|
|
}
|
|
|
|
func TestWindowArgsRoundTrip(t *testing.T) {
|
|
w := WindowArgs{Background: true, StartVM: "mac os", StartDriver: "test.driver.linux", StartResult: `D:\Data\tmp\cli-start-9.json`, WaitPID: 31337}
|
|
got, ok := ParseWindowArgs(w.Args())
|
|
if !ok || got != w {
|
|
t.Fatalf("round trip = %+v, %v", got, ok)
|
|
}
|
|
}
|
|
|
|
func TestStartResultRoundTrip(t *testing.T) {
|
|
dir := t.TempDir()
|
|
okPath := filepath.Join(dir, "cli-start-1.json")
|
|
if err := WriteStartResult(okPath, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
res, err := waitStartResult(context.Background(), okPath, time.Second)
|
|
if err != nil || !res.OK {
|
|
t.Fatalf("ok result = %+v, %v", res, err)
|
|
}
|
|
if _, err := os.Stat(okPath); !os.IsNotExist(err) {
|
|
t.Fatal("result file should be removed once read")
|
|
}
|
|
|
|
failPath := filepath.Join(dir, "cli-start-2.json")
|
|
go func() {
|
|
time.Sleep(300 * time.Millisecond)
|
|
_ = WriteStartResult(failPath, errors.New("not enough memory"))
|
|
}()
|
|
res, err = waitStartResult(context.Background(), failPath, 5*time.Second)
|
|
if err != nil || res.OK || res.Error != "not enough memory" {
|
|
t.Fatalf("failed result = %+v, %v", res, err)
|
|
}
|
|
|
|
_, err = waitStartResult(context.Background(), filepath.Join(dir, "never.json"), 300*time.Millisecond)
|
|
if !errors.Is(err, context.DeadlineExceeded) {
|
|
t.Fatalf("missing result err = %v", err)
|
|
}
|
|
}
|