Skip to content

Commit e20c903

Browse files
authored
Merge pull request #172 from Code-Hex/add/ci-build-check-intel-2
2 parents b32d4ed + ca686f8 commit e20c903

File tree

5 files changed

+165
-68
lines changed

5 files changed

+165
-68
lines changed

issues_test.go

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99
"strings"
1010
"testing"
11+
"time"
1112

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

5051
t.Run("check for segmentation faults", func(t *testing.T) {
51-
cases := map[string]func() error{
52-
"start handler": func() error { return m.Start() },
53-
"pause handler": m.Pause,
54-
"resume handler": m.Resume,
55-
"stop handler": m.Stop,
52+
cases := []struct {
53+
name string
54+
run func() error
55+
}{
56+
{
57+
name: "start handler",
58+
run: func() error { return m.Start() },
59+
},
60+
{
61+
name: "pause handler",
62+
run: m.Pause,
63+
},
64+
{
65+
name: "resume handler",
66+
run: m.Resume,
67+
},
68+
{
69+
name: "stop handler",
70+
run: m.Stop,
71+
},
5672
}
57-
for name, run := range cases {
58-
t.Run(name, func(t *testing.T) {
59-
_ = run()
73+
for _, tc := range cases {
74+
t.Run(tc.name, func(t *testing.T) {
75+
_ = tc.run()
6076
})
6177
}
6278
})
@@ -299,15 +315,41 @@ func TestIssue119(t *testing.T) {
299315
objc.Retain(vm.pointer)
300316
vm.finalize()
301317

302-
// sshSession.Run("poweroff")
303-
vm.Pause()
318+
sendStop := false
319+
if vm.CanStop() {
320+
if err := vm.Stop(); err != nil {
321+
t.Error(err)
322+
}
323+
sendStop = true
324+
}
325+
if vm.CanRequestStop() {
326+
if _, err := vm.RequestStop(); err != nil {
327+
t.Error(err)
328+
}
329+
sendStop = true
330+
}
331+
if !sendStop {
332+
t.Fatal("unexpected failed to send stop signal")
333+
}
334+
335+
timer := time.After(3 * time.Second)
336+
for {
337+
select {
338+
case state := <-vm.StateChangedNotify():
339+
if VirtualMachineStateStopped == state {
340+
return
341+
}
342+
case <-timer:
343+
t.Fatal("failed to shutdown vm")
344+
}
345+
}
304346
}
305347

306348
func setupIssue119Config(bootLoader *LinuxBootLoader) (*VirtualMachineConfiguration, error) {
307349
config, err := NewVirtualMachineConfiguration(
308350
bootLoader,
309351
1,
310-
512*1024*1024,
352+
256*1024*1024,
311353
)
312354
if err != nil {
313355
return nil, fmt.Errorf("failed to create a new virtual machine config: %w", err)

shared_directory_test.go

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package vz_test
33
import (
44
"bytes"
55
"fmt"
6+
"log"
67
"os"
78
"path/filepath"
89
"strings"
910
"testing"
10-
"time"
1111

1212
"github.com/Code-Hex/vz/v3"
1313
)
@@ -78,9 +78,11 @@ func TestSingleDirectoryShare(t *testing.T) {
7878
return nil
7979
},
8080
)
81-
defer container.Close()
82-
83-
vm := container.VirtualMachine
81+
t.Cleanup(func() {
82+
if err := container.Shutdown(); err != nil {
83+
log.Println(err)
84+
}
85+
})
8486

8587
file := "hello.txt"
8688
for _, v := range []struct {
@@ -133,14 +135,6 @@ func TestSingleDirectoryShare(t *testing.T) {
133135
t.Fatalf("failed to run command %q: %v\nstderr: %q", check, err, buf)
134136
}
135137
session.Close()
136-
137-
if err := vm.Stop(); err != nil {
138-
t.Fatal(err)
139-
}
140-
141-
timeout := 3 * time.Second
142-
waitState(t, timeout, vm, vz.VirtualMachineStateStopping)
143-
waitState(t, timeout, vm, vz.VirtualMachineStateStopped)
144138
})
145139
}
146140
}
@@ -187,9 +181,11 @@ func TestMultipleDirectoryShare(t *testing.T) {
187181
return nil
188182
},
189183
)
190-
defer container.Close()
191-
192-
vm := container.VirtualMachine
184+
t.Cleanup(func() {
185+
if err := container.Shutdown(); err != nil {
186+
log.Println(err)
187+
}
188+
})
193189

194190
// Create a file in mount directories.
195191
tmpFile := "tmp.txt"
@@ -244,12 +240,4 @@ func TestMultipleDirectoryShare(t *testing.T) {
244240
if err != nil {
245241
t.Fatalf("expected the file to exist in read/write directory: %v", err)
246242
}
247-
248-
if err := vm.Stop(); err != nil {
249-
t.Fatal(err)
250-
}
251-
252-
timeout := 3 * time.Second
253-
waitState(t, timeout, vm, vz.VirtualMachineStateStopping)
254-
waitState(t, timeout, vm, vz.VirtualMachineStateStopped)
255243
}

socket_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"fmt"
66
"io"
7+
"log"
78
"testing"
89
"time"
910

@@ -12,7 +13,11 @@ import (
1213

1314
func TestVirtioSocketListener(t *testing.T) {
1415
container := newVirtualizationMachine(t)
15-
defer container.Close()
16+
t.Cleanup(func() {
17+
if err := container.Shutdown(); err != nil {
18+
log.Println(err)
19+
}
20+
})
1621

1722
vm := container.VirtualMachine
1823

storage_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package vz_test
22

33
import (
4+
"log"
45
"path/filepath"
56
"strings"
67
"testing"
@@ -76,7 +77,7 @@ func TestBlockDeviceWithCacheAndSyncMode(t *testing.T) {
7677
t.Fatal(err)
7778
}
7879

79-
attachment, err := vz.NewDiskImageStorageDeviceAttachmentWithCacheAndSync(path, false, vz.DiskImageCachingModeAutomatic, vz.DiskImageSynchronizationModeFsync)
80+
attachment, err := vz.NewDiskImageStorageDeviceAttachmentWithCacheAndSync(path, false, vz.DiskImageCachingModeCached, vz.DiskImageSynchronizationModeFsync)
8081
if err != nil {
8182
t.Fatal(err)
8283
}
@@ -90,7 +91,11 @@ func TestBlockDeviceWithCacheAndSyncMode(t *testing.T) {
9091
return nil
9192
},
9293
)
93-
defer container.Close()
94+
t.Cleanup(func() {
95+
if err := container.Shutdown(); err != nil {
96+
log.Println(err)
97+
}
98+
})
9499

95100
vm := container.VirtualMachine
96101

0 commit comments

Comments
 (0)