File: rpc_test.go

package info (click to toggle)
golang-github-digitalocean-go-qemu 0.0~git20250212.ee9b066-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 860 kB
  • sloc: sh: 34; makefile: 3
file content (230 lines) | stat: -rw-r--r-- 5,437 bytes parent folder | download | duplicates (2)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright 2016 The go-qemu Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package qmp

import (
	"context"
	"encoding/json"
	"testing"

	"github.com/digitalocean/go-libvirt"
	"github.com/digitalocean/go-libvirt/libvirttest"
)

var testEvent = []byte{
	0x00, 0x00, 0x00, 0xb0, // length
	0x20, 0x00, 0x80, 0x87, // program
	0x00, 0x00, 0x00, 0x01, // version
	0x00, 0x00, 0x00, 0x06, // procedure
	0x00, 0x00, 0x00, 0x01, // type
	0x00, 0x00, 0x00, 0x00, // serial
	0x00, 0x00, 0x00, 0x00, // status
	0x00, 0x00, 0x00, 0x01, // callback id

	// domain name ("test")
	0x00, 0x00, 0x00, 0x04, 0x74, 0x65, 0x73, 0x74,

	// uuid (dc229f87d4de47198cfd2e21c6105b01)
	0xdc, 0x22, 0x9f, 0x87, 0xd4, 0xde, 0x47, 0x19,
	0x8c, 0xfd, 0x2e, 0x21, 0xc6, 0x10, 0x5b, 0x01,

	// domain id (14)
	0x00, 0x00, 0x00, 0x0e,

	// event name (BLOCK_JOB_COMPLETED)
	0x00, 0x00, 0x00, 0x13, 0x42, 0x4c, 0x4f, 0x43,
	0x4b, 0x5f, 0x4a, 0x4f, 0x42, 0x5f, 0x43, 0x4f,
	0x4d, 0x50, 0x4c, 0x45, 0x54, 0x45, 0x44, 0x00,

	// seconds (1462211891)
	0x00, 0x00, 0x00, 0x00, 0x57, 0x27, 0x95, 0x33,

	// microseconds (931791)
	0x00, 0x0e, 0x37, 0xcf,

	// event json data
	// ({"device":"drive-ide0-0-0","len":0,"offset":0,"speed":0,"type":"commit"})
	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x48,
	0x7b, 0x22, 0x64, 0x65, 0x76, 0x69, 0x63, 0x65,
	0x22, 0x3a, 0x22, 0x64, 0x72, 0x69, 0x76, 0x65,
	0x2d, 0x69, 0x64, 0x65, 0x30, 0x2d, 0x30, 0x2d,
	0x30, 0x22, 0x2c, 0x22, 0x6c, 0x65, 0x6e, 0x22,
	0x3a, 0x30, 0x2c, 0x22, 0x6f, 0x66, 0x66, 0x73,
	0x65, 0x74, 0x22, 0x3a, 0x30, 0x2c, 0x22, 0x73,
	0x70, 0x65, 0x65, 0x64, 0x22, 0x3a, 0x30, 0x2c,
	0x22, 0x74, 0x79, 0x70, 0x65, 0x22, 0x3a, 0x22,
	0x63, 0x6f, 0x6d, 0x6d, 0x69, 0x74, 0x22, 0x7d,
}

func TestNewLibvirtRPCMonitor(t *testing.T) {
	lv := libvirttest.New()
	conn, err := lv.Dial()
	if err != nil {
		t.Error(err)
	}

	domain := "test-1"
	rpc := NewLibvirtRPCMonitor(domain, conn)

	if rpc.Domain != domain {
		t.Errorf("expected domain %q, got %q", domain, rpc.Domain)
	}
}

func TestQMPEvent(t *testing.T) {
	e := &libvirt.DomainEvent{
		CallbackID:   1,
		Domain:       libvirt.Domain{Name: "test"},
		Event:        "test",
		Seconds:      1,
		Microseconds: 1,
		Details:      []byte(`{"device":"drive-virtio-disk0","len":0,"offset":0,"speed":0,"type":"commit"}`),
	}

	qe, err := qmpEvent(e)
	if err != nil {
		t.Error(err)
	}

	if qe.Event != e.Event {
		t.Errorf("expected event %q, got %q", e.Event, qe.Event)
	}

	if qe.Timestamp.Seconds != int64(e.Seconds) {
		t.Errorf("expected seconds to be %q, got %q", e.Seconds, qe.Timestamp.Seconds)
	}

	if qe.Timestamp.Microseconds != int64(e.Microseconds) {
		t.Errorf("expected microseconds to be %q, got %q", e.Microseconds, qe.Timestamp.Microseconds)
	}

	expected := "drive-virtio-disk0"
	actual, ok := qe.Data["device"]
	if !ok {
		t.Error("expected event data 'device'")
	}

	if actual != expected {
		t.Errorf("expected device %q, got %q", expected, actual)
	}
}

func TestLibvirtRPCMonitorConnect(t *testing.T) {
	lv := libvirttest.New()
	conn, err := lv.Dial()
	if err != nil {
		t.Error(err)
	}
	mon := NewLibvirtRPCMonitor("test", conn)

	err = mon.Connect()
	if err != nil {
		t.Error(err)
	}
}

func TestLibvirtRPCMonitorDisconnect(t *testing.T) {
	lv := libvirttest.New()
	conn, err := lv.Dial()
	if err != nil {
		t.Error(err)
	}
	mon := NewLibvirtRPCMonitor("test", conn)

	err = mon.Disconnect()
	if err != nil {
		t.Error(err)
	}
}

func TestLibvirtRPCMonitorRun(t *testing.T) {
	lv := libvirttest.New()
	conn, err := lv.Dial()
	if err != nil {
		t.Error(err)
	}
	mon := NewLibvirtRPCMonitor("test", conn)

	err = mon.Connect()
	if err != nil {
		t.Error(err)
	}
	defer mon.Disconnect()

	res, err := mon.Run([]byte(`{"query-version"}`))
	if err != nil {
		t.Error(err)
	}

	type version struct {
		Return struct {
			Package string `json:"package"`
			QEMU    struct {
				Major int `json:"major"`
				Micro int `json:"micro"`
				Minor int `json:"minor"`
			} `json:"qemu"`
		} `json:"return"`
	}

	var v version
	err = json.Unmarshal(res, &v)
	if err != nil {
		t.Error(err)
	}

	expected := 2
	if v.Return.QEMU.Major != expected {
		t.Errorf("expected qemu major version %d, got %d", expected, v.Return.QEMU.Major)
	}
}

func TestLibvirtRPCMonitorEvents(t *testing.T) {
	ctx := context.Background()
	lv := libvirttest.New()
	conn, err := lv.Dial()
	if err != nil {
		t.Error(err)
	}
	mon := NewLibvirtRPCMonitor("test", conn)
	err = mon.Connect()
	if err != nil {
		t.Error(err)
	}
	defer mon.Disconnect()

	done := make(chan struct{})

	stream, err := mon.Events(ctx)
	if err != nil {
		t.Error(err)
	}

	go func() {
		defer close(done)
		got := <-stream

		expected := "drive-ide0-0-0"
		if got.Data["device"] != expected {
			t.Errorf("expected device %q, got %q", expected, got.Data["device"])
		}
	}()

	// send an event to the listener goroutine
	_, _ = lv.Test.Write(testEvent)

	// wait for completion
	<-done
}