122 lines
3.0 KiB
Go
122 lines
3.0 KiB
Go
package qmp
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/testutil/socketdir"
|
|
)
|
|
|
|
type fakeExecutor struct {
|
|
calls []map[string]any
|
|
failDown bool
|
|
}
|
|
|
|
func (f *fakeExecutor) Execute(cmd string, args any) error {
|
|
m := map[string]any{"cmd": cmd}
|
|
if argMap, ok := args.(map[string]any); ok {
|
|
for k, v := range argMap {
|
|
m[k] = v
|
|
}
|
|
}
|
|
f.calls = append(f.calls, m)
|
|
if f.failDown && m["up"] == false {
|
|
return errors.New("down failed")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func TestCycleLink(t *testing.T) {
|
|
t.Run("normal cycle", func(t *testing.T) {
|
|
ex := &fakeExecutor{}
|
|
if err := CycleLink(context.Background(), ex, "reimsbridge0", 10*time.Millisecond); err != nil {
|
|
t.Fatalf("CycleLink: %v", err)
|
|
}
|
|
if len(ex.calls) != 2 || ex.calls[0]["up"] != false || ex.calls[1]["up"] != true {
|
|
t.Fatalf("calls = %+v", ex.calls)
|
|
}
|
|
})
|
|
|
|
t.Run("context canceled during hold still restores up", func(t *testing.T) {
|
|
ex := &fakeExecutor{}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
go func() {
|
|
time.Sleep(15 * time.Millisecond)
|
|
cancel()
|
|
}()
|
|
err := CycleLink(ctx, ex, "reimsbridge0", 2*time.Second)
|
|
if !errors.Is(err, context.Canceled) {
|
|
t.Fatalf("err = %v, want context.Canceled", err)
|
|
}
|
|
if len(ex.calls) != 2 || ex.calls[0]["up"] != false || ex.calls[1]["up"] != true {
|
|
t.Fatalf("calls = %+v, want down then up recovery", ex.calls)
|
|
}
|
|
})
|
|
|
|
t.Run("down failure still attempts up", func(t *testing.T) {
|
|
ex := &fakeExecutor{failDown: true}
|
|
if err := CycleLink(context.Background(), ex, "reimsbridge0", 10*time.Millisecond); err == nil {
|
|
t.Fatal("expected error on down failure")
|
|
}
|
|
if len(ex.calls) != 2 || ex.calls[1]["up"] != true {
|
|
t.Fatalf("calls = %+v, want up recovery after down failure", ex.calls)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestRestoreLinkOverUnixQMP(t *testing.T) {
|
|
vmDir := socketdir.New(t)
|
|
sock := SocketPath(vmDir)
|
|
if len(sock) > 100 {
|
|
t.Fatalf("socket path too long: %d", len(sock))
|
|
}
|
|
ln, err := net.Listen("unix", sock)
|
|
if err != nil {
|
|
t.Fatalf("listen qmp: %v", err)
|
|
}
|
|
defer ln.Close()
|
|
|
|
gotUp := make(chan bool, 1)
|
|
go func() {
|
|
conn, err := ln.Accept()
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer conn.Close()
|
|
_, _ = conn.Write([]byte(`{"QMP":{"version":{"qemu":{"micro":0,"minor":1,"major":11}}}}` + "\n"))
|
|
sc := bufio.NewScanner(conn)
|
|
for sc.Scan() {
|
|
var req struct {
|
|
Execute string `json:"execute"`
|
|
Arguments map[string]any `json:"arguments"`
|
|
}
|
|
_ = json.Unmarshal(sc.Bytes(), &req)
|
|
_, _ = conn.Write([]byte(`{"return":{}}` + "\n"))
|
|
if req.Execute == "set_link" {
|
|
up, _ := req.Arguments["up"].(bool)
|
|
gotUp <- up
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
defer cancel()
|
|
if err := RestoreLink(ctx, vmDir, "reimsbridge0"); err != nil {
|
|
t.Fatalf("RestoreLink: %v", err)
|
|
}
|
|
select {
|
|
case up := <-gotUp:
|
|
if !up {
|
|
t.Fatal("RestoreLink sent up=false")
|
|
}
|
|
case <-time.After(time.Second):
|
|
t.Fatal("timeout waiting for set_link")
|
|
}
|
|
}
|