Skip to content

Commit 0d0dbd3

Browse files
committed
added Shutdown method in tesitng
1 parent b32d4ed commit 0d0dbd3

5 files changed

+116
-39
lines changed

issues_test.go

+30-3
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
)
@@ -299,15 +300,41 @@ func TestIssue119(t *testing.T) {
299300
objc.Retain(vm.pointer)
300301
vm.finalize()
301302

302-
// sshSession.Run("poweroff")
303-
vm.Pause()
303+
sendStop := false
304+
if vm.CanStop() {
305+
if err := vm.Stop(); err != nil {
306+
t.Error(err)
307+
}
308+
sendStop = true
309+
}
310+
if vm.CanRequestStop() {
311+
if _, err := vm.RequestStop(); err != nil {
312+
t.Error(err)
313+
}
314+
sendStop = true
315+
}
316+
if !sendStop {
317+
t.Fatal("unexpected failed to send stop signal")
318+
}
319+
320+
timer := time.After(3 * time.Second)
321+
for {
322+
select {
323+
case state := <-vm.StateChangedNotify():
324+
if VirtualMachineStateStopped == state {
325+
return
326+
}
327+
case <-timer:
328+
t.Fatal("failed to shutdown vm")
329+
}
330+
}
304331
}
305332

306333
func setupIssue119Config(bootLoader *LinuxBootLoader) (*VirtualMachineConfiguration, error) {
307334
config, err := NewVirtualMachineConfiguration(
308335
bootLoader,
309336
1,
310-
512*1024*1024,
337+
256*1024*1024,
311338
)
312339
if err != nil {
313340
return nil, fmt.Errorf("failed to create a new virtual machine config: %w", err)

shared_directory_test.go

+11-23
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

+6-1
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

+7-2
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

virtualization_test.go

+62-10
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func setupConfiguration(bootLoader vz.BootLoader) (*vz.VirtualMachineConfigurati
5656
config, err := vz.NewVirtualMachineConfiguration(
5757
bootLoader,
5858
1,
59-
512*1024*1024,
59+
256*1024*1024,
6060
)
6161
if err != nil {
6262
return nil, fmt.Errorf("failed to create a new virtual machine config: %w", err)
@@ -101,10 +101,6 @@ type Container struct {
101101
*ssh.Client
102102
}
103103

104-
func (c *Container) Close() error {
105-
return c.Client.Close()
106-
}
107-
108104
func (c *Container) NewSession(t *testing.T) *ssh.Session {
109105
sshSession, err := c.Client.NewSession()
110106
if err != nil {
@@ -114,6 +110,50 @@ func (c *Container) NewSession(t *testing.T) *ssh.Session {
114110
return sshSession
115111
}
116112

113+
func (c *Container) Shutdown() error {
114+
defer func() {
115+
log.Println("shutdown done")
116+
c.Client.Close()
117+
}()
118+
119+
vm := c.VirtualMachine
120+
121+
if got := vm.State(); vz.VirtualMachineStateStopped == got {
122+
return nil
123+
}
124+
125+
switch {
126+
case vm.CanStop():
127+
if err := vm.Stop(); err != nil {
128+
return fmt.Errorf("failed to call stop: %w", err)
129+
}
130+
case vm.CanRequestStop():
131+
if _, err := vm.RequestStop(); err != nil {
132+
return fmt.Errorf("failed to send request stop: %w", err)
133+
}
134+
default:
135+
sshSession, err := c.Client.NewSession()
136+
if err != nil {
137+
return fmt.Errorf("failed to create a new session: %w", err)
138+
}
139+
if err := sshSession.Run("poweroff"); err != nil {
140+
return fmt.Errorf("failed to run poweroff command: %w", err)
141+
}
142+
}
143+
144+
wait := time.After(3 * time.Second)
145+
for {
146+
select {
147+
case got := <-vm.StateChangedNotify():
148+
if vz.VirtualMachineStateStopped == got {
149+
return nil
150+
}
151+
case <-wait:
152+
return fmt.Errorf("failed to wait stopped state")
153+
}
154+
}
155+
}
156+
117157
func getFreePort() (int, error) {
118158
l, err := net.Listen("tcp", "127.0.0.1:0")
119159
if err != nil {
@@ -221,7 +261,7 @@ RETRY:
221261
t.Fatalf("failed to connect vsock: %v", err)
222262
}
223263

224-
t.Log("setup ssh client in container")
264+
log.Println("setup ssh client in container")
225265

226266
initialized := make(chan struct{})
227267
retry := make(chan struct{})
@@ -248,7 +288,7 @@ RETRY:
248288

249289
close(initialized)
250290

251-
t.Logf("container setup done")
291+
log.Println("container setup done")
252292

253293
return &Container{
254294
VirtualMachine: vm,
@@ -281,7 +321,11 @@ func TestRun(t *testing.T) {
281321
return setupConsoleConfig(vmc)
282322
},
283323
)
284-
defer container.Close()
324+
t.Cleanup(func() {
325+
if err := container.Shutdown(); err != nil {
326+
log.Println(err)
327+
}
328+
})
285329

286330
sshSession := container.NewSession(t)
287331
defer sshSession.Close()
@@ -344,7 +388,11 @@ func TestStop(t *testing.T) {
344388
}
345389

346390
container := newVirtualizationMachine(t)
347-
defer container.Close()
391+
t.Cleanup(func() {
392+
if err := container.Shutdown(); err != nil {
393+
log.Println(err)
394+
}
395+
})
348396

349397
vm := container.VirtualMachine
350398

@@ -415,7 +463,11 @@ func TestRunIssue124(t *testing.T) {
415463
return setupConsoleConfig(vmc)
416464
},
417465
)
418-
defer container.Close()
466+
t.Cleanup(func() {
467+
if err := container.Shutdown(); err != nil {
468+
log.Println(err)
469+
}
470+
})
419471

420472
sshSession := container.NewSession(t)
421473
defer sshSession.Close()

0 commit comments

Comments
 (0)