You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
255 lines
7.3 KiB
Go
255 lines
7.3 KiB
Go
//go:build libvirt
|
|
|
|
package hypervisor
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"slices"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.luoli.live/root/virt/agent/operation"
|
|
"libvirt.org/go/libvirt"
|
|
)
|
|
|
|
type fakeStreamResult struct {
|
|
count int
|
|
err error
|
|
}
|
|
|
|
type fakeStorageStream struct {
|
|
mu sync.Mutex
|
|
sendResults []fakeStreamResult
|
|
recvResults []fakeStreamResult
|
|
callback libvirt.StreamEventCallback
|
|
notifyOnUpdate bool
|
|
updated chan struct{}
|
|
removeErrors []error
|
|
finishErr error
|
|
abortErr error
|
|
freeErr error
|
|
operations []string
|
|
registeredEvents libvirt.StreamEventType
|
|
}
|
|
|
|
func (f *fakeStorageStream) Send(_ []byte) (int, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.operations = append(f.operations, "send")
|
|
if len(f.sendResults) == 0 {
|
|
return 0, io.ErrNoProgress
|
|
}
|
|
result := f.sendResults[0]
|
|
f.sendResults = f.sendResults[1:]
|
|
return result.count, result.err
|
|
}
|
|
|
|
func (f *fakeStorageStream) Recv(buffer []byte) (int, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.operations = append(f.operations, "recv")
|
|
if len(f.recvResults) == 0 {
|
|
return 0, io.EOF
|
|
}
|
|
result := f.recvResults[0]
|
|
f.recvResults = f.recvResults[1:]
|
|
for index := 0; index < result.count && index < len(buffer); index++ {
|
|
buffer[index] = byte(index + 1)
|
|
}
|
|
return result.count, result.err
|
|
}
|
|
|
|
func (f *fakeStorageStream) EventAddCallback(events libvirt.StreamEventType, callback libvirt.StreamEventCallback) error {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.operations = append(f.operations, "add")
|
|
f.registeredEvents = events
|
|
f.callback = callback
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeStorageStream) EventUpdateCallback(events libvirt.StreamEventType) error {
|
|
f.mu.Lock()
|
|
f.operations = append(f.operations, "update")
|
|
callback := f.callback
|
|
notify := events != 0 && f.notifyOnUpdate
|
|
updated := f.updated
|
|
f.mu.Unlock()
|
|
if updated != nil && events != 0 {
|
|
select {
|
|
case updated <- struct{}{}:
|
|
default:
|
|
}
|
|
}
|
|
if notify && callback != nil {
|
|
storageStreamEventReady(f)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeStorageStream) EventRemoveCallback() error {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.operations = append(f.operations, "remove")
|
|
if len(f.removeErrors) == 0 {
|
|
return nil
|
|
}
|
|
err := f.removeErrors[0]
|
|
f.removeErrors = f.removeErrors[1:]
|
|
return err
|
|
}
|
|
|
|
func (f *fakeStorageStream) Finish() error {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.operations = append(f.operations, "finish")
|
|
return f.finishErr
|
|
}
|
|
|
|
func (f *fakeStorageStream) Abort() error {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.operations = append(f.operations, "abort")
|
|
return f.abortErr
|
|
}
|
|
|
|
func (f *fakeStorageStream) Free() error {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.operations = append(f.operations, "free")
|
|
return f.freeErr
|
|
}
|
|
|
|
func (f *fakeStorageStream) operationLog() []string {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
return append([]string(nil), f.operations...)
|
|
}
|
|
|
|
func TestStorageStreamSendWaitsAndFinishes(t *testing.T) {
|
|
stream := &fakeStorageStream{
|
|
sendResults: []fakeStreamResult{{count: -2}, {count: -2}, {count: 2}, {count: 2}},
|
|
notifyOnUpdate: true,
|
|
}
|
|
transfer := &storageStreamTransfer{stream: stream}
|
|
written, err := transfer.send(context.Background(), []byte("test"))
|
|
if err != nil {
|
|
t.Fatalf("send: %v", err)
|
|
}
|
|
if written != 4 {
|
|
t.Fatalf("written = %d, want 4", written)
|
|
}
|
|
if err := transfer.finish(context.Background()); err != nil {
|
|
t.Fatalf("finish: %v", err)
|
|
}
|
|
want := []string{"send", "add", "send", "update", "update", "send", "send", "remove", "finish", "free"}
|
|
if got := stream.operationLog(); !slices.Equal(got, want) {
|
|
t.Fatalf("operations = %v, want %v", got, want)
|
|
}
|
|
if stream.registeredEvents&libvirt.STREAM_EVENT_WRITABLE == 0 || stream.registeredEvents&libvirt.STREAM_EVENT_ERROR == 0 || stream.registeredEvents&libvirt.STREAM_EVENT_HANGUP == 0 {
|
|
t.Fatalf("registered events = %d, missing required flags", stream.registeredEvents)
|
|
}
|
|
}
|
|
|
|
func TestStorageStreamRecvWaitsAndFinishes(t *testing.T) {
|
|
stream := &fakeStorageStream{
|
|
recvResults: []fakeStreamResult{{count: -2}, {count: -2}, {count: 3}, {err: io.EOF}},
|
|
notifyOnUpdate: true,
|
|
}
|
|
transfer := &storageStreamTransfer{stream: stream}
|
|
buffer := make([]byte, 8)
|
|
count, err := transfer.recv(context.Background(), buffer)
|
|
if err != nil || count != 3 {
|
|
t.Fatalf("recv = (%d, %v), want (3, nil)", count, err)
|
|
}
|
|
count, err = transfer.recv(context.Background(), buffer)
|
|
if !errors.Is(err, io.EOF) || count != 0 {
|
|
t.Fatalf("recv EOF = (%d, %v)", count, err)
|
|
}
|
|
if err := transfer.finish(context.Background()); err != nil {
|
|
t.Fatalf("finish: %v", err)
|
|
}
|
|
if stream.registeredEvents&libvirt.STREAM_EVENT_READABLE == 0 {
|
|
t.Fatalf("registered events = %d, missing readable", stream.registeredEvents)
|
|
}
|
|
}
|
|
|
|
func TestStorageStreamCancellationRemovesCallbackBeforeAbort(t *testing.T) {
|
|
updated := make(chan struct{}, 1)
|
|
stream := &fakeStorageStream{
|
|
sendResults: []fakeStreamResult{{count: -2}, {count: -2}},
|
|
updated: updated,
|
|
}
|
|
transfer := &storageStreamTransfer{stream: stream}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
result := make(chan error, 1)
|
|
go func() {
|
|
_, err := transfer.send(ctx, []byte("x"))
|
|
result <- err
|
|
}()
|
|
select {
|
|
case <-updated:
|
|
cancel()
|
|
case <-time.After(time.Second):
|
|
t.Fatal("stream callback was not armed")
|
|
}
|
|
err := <-result
|
|
if !errors.Is(err, context.Canceled) {
|
|
t.Fatalf("send error = %v, want context canceled", err)
|
|
}
|
|
_ = transfer.abort(err)
|
|
wantSuffix := []string{"remove", "abort", "free"}
|
|
operations := stream.operationLog()
|
|
if len(operations) < len(wantSuffix) || !slices.Equal(operations[len(operations)-len(wantSuffix):], wantSuffix) {
|
|
t.Fatalf("operations = %v, want suffix %v", operations, wantSuffix)
|
|
}
|
|
}
|
|
|
|
func TestStorageStreamFinishFailureAborts(t *testing.T) {
|
|
finishErr := errors.New("finish failed")
|
|
stream := &fakeStorageStream{finishErr: finishErr}
|
|
transfer := &storageStreamTransfer{stream: stream, callbackRegistered: true}
|
|
err := transfer.finish(context.Background())
|
|
if !errors.Is(err, finishErr) {
|
|
t.Fatalf("finish error = %v, want %v", err, finishErr)
|
|
}
|
|
want := []string{"remove", "finish", "abort", "free"}
|
|
if got := stream.operationLog(); !slices.Equal(got, want) {
|
|
t.Fatalf("operations = %v, want %v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestStorageStreamRetainedWhenCallbackCannotBeRemoved(t *testing.T) {
|
|
removeErr := errors.New("remove failed")
|
|
stream := &fakeStorageStream{removeErrors: []error{removeErr, removeErr}}
|
|
transfer := &storageStreamTransfer{stream: stream, callbackRegistered: true}
|
|
err := transfer.abort(errors.New("transfer failed"))
|
|
if !errors.Is(err, removeErr) {
|
|
t.Fatalf("abort error = %v, want callback removal error", err)
|
|
}
|
|
if !operation.IsUncertain(err) {
|
|
t.Fatalf("abort error = %v, want uncertain cleanup", err)
|
|
}
|
|
want := []string{"remove", "remove"}
|
|
if got := stream.operationLog(); !slices.Equal(got, want) {
|
|
t.Fatalf("operations = %v, want %v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestStorageStreamRetainedWhenAbortFails(t *testing.T) {
|
|
abortErr := errors.New("abort failed")
|
|
stream := &fakeStorageStream{abortErr: abortErr}
|
|
transfer := &storageStreamTransfer{stream: stream}
|
|
err := transfer.abort(errors.New("transfer failed"))
|
|
if !errors.Is(err, abortErr) || !operation.IsUncertain(err) {
|
|
t.Fatalf("abort error = %v, want uncertain abort failure", err)
|
|
}
|
|
want := []string{"abort"}
|
|
if got := stream.operationLog(); !slices.Equal(got, want) {
|
|
t.Fatalf("operations = %v, want %v", got, want)
|
|
}
|
|
}
|