-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonitor_test.go
91 lines (74 loc) · 2.1 KB
/
monitor_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package qmp
import (
"context"
"fmt"
"log"
"time"
)
// This example shows how to use the Monitor to communicate with a QEMU instance via QMP.
func ExampleMonitor() {
mon, err := NewMonitor("/var/run/qemu/alice.qmp", 60*time.Second)
if err != nil {
log.Fatalln(err)
}
defer mon.Close()
done := make(chan struct{})
go func() {
ts := time.Now()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
got, err := mon.GetEvents(ctx, "SHUTDOWN", uint64(ts.Unix()))
if err != nil {
log.Printf("Timeout error (type=%T): %s\n", err, err)
} else {
log.Printf("OK, got a SHUTDOWN event: %#v\n", got)
}
close(done)
}()
log.Println("Sleeping for three seconds ...")
time.Sleep(3 * time.Second)
log.Println("... and sending a 'system_powerdown' command.")
if err := mon.Run(Command{"system_powerdown", nil}, nil); err != nil {
log.Fatalln(err)
}
<-done
}
// An example of executing a command via human monitor.
func ExampleMonitor_Run() {
mon, err := NewMonitor("/var/run/qemu/alice.qmp", 60*time.Second)
if err != nil {
log.Fatalln(err)
}
var out string
if err := mon.Run(Command{"human-monitor-command", &HumanCommand{"info vnc"}}, &out); err != nil {
log.Fatalln(err)
}
fmt.Println(out)
}
// An example of removing a device from a guest.
// Completion of the process is signaled with a DEVICE_DELETED event.
func ExampleMonitor_WaitDeviceDeletedEvent() {
mon, err := NewMonitor("/var/run/qemu/alice.qmp", 60*time.Second)
if err != nil {
log.Fatalln(err)
}
deviceID := struct {
Id string `json:"id"`
}{
"blk_alice",
}
ts := time.Now()
if err := mon.Run(Command{"device_del", &deviceID}, nil); err != nil {
log.Fatalln("device_del error:", err)
}
// ... and wait until the operation is completed
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
switch _, err := mon.WaitDeviceDeletedEvent(ctx, "blk_alice", uint64(ts.Unix())); {
case err == nil:
case err == context.DeadlineExceeded:
log.Fatalln("device_del timeout error: failed to complete within 60 seconds")
default:
log.Fatalln(err)
}
}