Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make flaky tests more stable #172

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 53 additions & 11 deletions issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"strings"
"testing"
"time"

"github.com/Code-Hex/vz/v3/internal/objc"
)
Expand Down Expand Up @@ -48,15 +49,30 @@ func TestIssue50(t *testing.T) {
}

t.Run("check for segmentation faults", func(t *testing.T) {
cases := map[string]func() error{
"start handler": func() error { return m.Start() },
"pause handler": m.Pause,
"resume handler": m.Resume,
"stop handler": m.Stop,
cases := []struct {
name string
run func() error
}{
{
name: "start handler",
run: func() error { return m.Start() },
},
{
name: "pause handler",
run: m.Pause,
},
{
name: "resume handler",
run: m.Resume,
},
{
name: "stop handler",
run: m.Stop,
},
}
for name, run := range cases {
t.Run(name, func(t *testing.T) {
_ = run()
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_ = tc.run()
})
}
})
Expand Down Expand Up @@ -299,15 +315,41 @@ func TestIssue119(t *testing.T) {
objc.Retain(vm.pointer)
vm.finalize()

// sshSession.Run("poweroff")
vm.Pause()
sendStop := false
if vm.CanStop() {
if err := vm.Stop(); err != nil {
t.Error(err)
}
sendStop = true
}
if vm.CanRequestStop() {
if _, err := vm.RequestStop(); err != nil {
t.Error(err)
}
sendStop = true
}
if !sendStop {
t.Fatal("unexpected failed to send stop signal")
}

timer := time.After(3 * time.Second)
for {
select {
case state := <-vm.StateChangedNotify():
if VirtualMachineStateStopped == state {
return
}
case <-timer:
t.Fatal("failed to shutdown vm")
}
}
}

func setupIssue119Config(bootLoader *LinuxBootLoader) (*VirtualMachineConfiguration, error) {
config, err := NewVirtualMachineConfiguration(
bootLoader,
1,
512*1024*1024,
256*1024*1024,
)
if err != nil {
return nil, fmt.Errorf("failed to create a new virtual machine config: %w", err)
Expand Down
34 changes: 11 additions & 23 deletions shared_directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package vz_test
import (
"bytes"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"testing"
"time"

"github.com/Code-Hex/vz/v3"
)
Expand Down Expand Up @@ -78,9 +78,11 @@ func TestSingleDirectoryShare(t *testing.T) {
return nil
},
)
defer container.Close()

vm := container.VirtualMachine
t.Cleanup(func() {
if err := container.Shutdown(); err != nil {
log.Println(err)
}
})

file := "hello.txt"
for _, v := range []struct {
Expand Down Expand Up @@ -133,14 +135,6 @@ func TestSingleDirectoryShare(t *testing.T) {
t.Fatalf("failed to run command %q: %v\nstderr: %q", check, err, buf)
}
session.Close()

if err := vm.Stop(); err != nil {
t.Fatal(err)
}

timeout := 3 * time.Second
waitState(t, timeout, vm, vz.VirtualMachineStateStopping)
waitState(t, timeout, vm, vz.VirtualMachineStateStopped)
})
}
}
Expand Down Expand Up @@ -187,9 +181,11 @@ func TestMultipleDirectoryShare(t *testing.T) {
return nil
},
)
defer container.Close()

vm := container.VirtualMachine
t.Cleanup(func() {
if err := container.Shutdown(); err != nil {
log.Println(err)
}
})

// Create a file in mount directories.
tmpFile := "tmp.txt"
Expand Down Expand Up @@ -244,12 +240,4 @@ func TestMultipleDirectoryShare(t *testing.T) {
if err != nil {
t.Fatalf("expected the file to exist in read/write directory: %v", err)
}

if err := vm.Stop(); err != nil {
t.Fatal(err)
}

timeout := 3 * time.Second
waitState(t, timeout, vm, vz.VirtualMachineStateStopping)
waitState(t, timeout, vm, vz.VirtualMachineStateStopped)
}
7 changes: 6 additions & 1 deletion socket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"io"
"log"
"testing"
"time"

Expand All @@ -12,7 +13,11 @@ import (

func TestVirtioSocketListener(t *testing.T) {
container := newVirtualizationMachine(t)
defer container.Close()
t.Cleanup(func() {
if err := container.Shutdown(); err != nil {
log.Println(err)
}
})

vm := container.VirtualMachine

Expand Down
9 changes: 7 additions & 2 deletions storage_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package vz_test

import (
"log"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -76,7 +77,7 @@ func TestBlockDeviceWithCacheAndSyncMode(t *testing.T) {
t.Fatal(err)
}

attachment, err := vz.NewDiskImageStorageDeviceAttachmentWithCacheAndSync(path, false, vz.DiskImageCachingModeAutomatic, vz.DiskImageSynchronizationModeFsync)
attachment, err := vz.NewDiskImageStorageDeviceAttachmentWithCacheAndSync(path, false, vz.DiskImageCachingModeCached, vz.DiskImageSynchronizationModeFsync)
if err != nil {
t.Fatal(err)
}
Expand All @@ -90,7 +91,11 @@ func TestBlockDeviceWithCacheAndSyncMode(t *testing.T) {
return nil
},
)
defer container.Close()
t.Cleanup(func() {
if err := container.Shutdown(); err != nil {
log.Println(err)
}
})

vm := container.VirtualMachine

Expand Down
Loading