Skip to content

Commit 73697a4

Browse files
Merge pull request digitalocean#150 from adamwg/awg/package-version
domain: Add a function to get the qemu package version
2 parents 39453ac + 1fbd5e2 commit 73697a4

File tree

4 files changed

+101
-3
lines changed

4 files changed

+101
-3
lines changed

qemu/domain.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,17 @@ func (d *Domain) Version() (string, error) {
346346
return response.Return.String(), nil
347347
}
348348

349+
// PackageVersion returns the domain's QEMU package version, the full build
350+
// information for QEMU.
351+
func (d *Domain) PackageVersion() (string, error) {
352+
vers, err := d.rm.QueryVersion()
353+
if err != nil {
354+
return "", err
355+
}
356+
357+
return vers.Package, nil
358+
}
359+
349360
// Events streams QEMU QMP events.
350361
// Two channels are returned, the first contains events emitted by the domain.
351362
// The second is used to signal completion of event processing.

qemu/version_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"testing"
1919

2020
"github.com/digitalocean/go-qemu/qmp"
21+
"github.com/digitalocean/go-qemu/qmp/raw"
2122
)
2223

2324
func TestVersion(t *testing.T) {
@@ -48,3 +49,33 @@ func TestVersion(t *testing.T) {
4849
t.Errorf("expected version %q, instead got %q", expected, v)
4950
}
5051
}
52+
53+
func TestPackageVersion(t *testing.T) {
54+
result := raw.VersionInfo{}
55+
result.Qemu.Major = 2
56+
result.Qemu.Minor = 8
57+
result.Qemu.Micro = 0
58+
result.Package = "(Debian 1:2.8+dfsg-3ubuntu2.4)"
59+
60+
d, done := testDomain(t, func(cmd qmp.Command) (interface{}, error) {
61+
if want, got := "query-version", cmd.Execute; want != got {
62+
t.Fatalf("unexpected QMP command:\n- want: %q\n- got: %q",
63+
want, got)
64+
}
65+
66+
return success{
67+
Return: result,
68+
}, nil
69+
})
70+
defer done()
71+
72+
v, err := d.PackageVersion()
73+
if err != nil {
74+
t.Error(err)
75+
}
76+
77+
expected := "(Debian 1:2.8+dfsg-3ubuntu2.4)"
78+
if v != expected {
79+
t.Errorf("expected package version %q, instead got %q", expected, v)
80+
}
81+
}

qmp/raw/autogen.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9765,6 +9765,13 @@ type String struct {
97659765
Str string `json:"str"`
97669766
}
97679767

9768+
// TPMEmulatorOptions -> TPMEmulatorOptions (struct)
9769+
9770+
// TPMEmulatorOptions implements the "TPMEmulatorOptions" QMP API type.
9771+
type TPMEmulatorOptions struct {
9772+
Chardev string `json:"chardev"`
9773+
}
9774+
97689775
// TPMInfo -> TPMInfo (struct)
97699776

97709777
// TPMInfo implements the "TPMInfo" QMP API type.
@@ -9863,13 +9870,16 @@ type TPMType int
98639870
// Known values of TPMType.
98649871
const (
98659872
TPMTypePassthrough TPMType = iota
9873+
TPMTypeEmulator
98669874
)
98679875

98689876
// String implements fmt.Stringer.
98699877
func (e TPMType) String() string {
98709878
switch e {
98719879
case TPMTypePassthrough:
98729880
return "passthrough"
9881+
case TPMTypeEmulator:
9882+
return "emulator"
98739883
default:
98749884
return fmt.Sprintf("TPMType(%d)", e)
98759885
}
@@ -9880,6 +9890,8 @@ func (e TPMType) MarshalJSON() ([]byte, error) {
98809890
switch e {
98819891
case TPMTypePassthrough:
98829892
return json.Marshal("passthrough")
9893+
case TPMTypeEmulator:
9894+
return json.Marshal("emulator")
98839895
default:
98849896
return nil, fmt.Errorf("unknown enum value %q for TPMType", e)
98859897
}
@@ -9894,6 +9906,8 @@ func (e *TPMType) UnmarshalJSON(bs []byte) error {
98949906
switch s {
98959907
case "passthrough":
98969908
*e = TPMTypePassthrough
9909+
case "emulator":
9910+
*e = TPMTypeEmulator
98979911
default:
98989912
return fmt.Errorf("unknown enum value %q for TPMType", s)
98999913
}
@@ -9905,11 +9919,26 @@ func (e *TPMType) UnmarshalJSON(bs []byte) error {
99059919
// TPMTypeOptions implements the "TpmTypeOptions" QMP API type.
99069920
//
99079921
// Can be one of:
9922+
// - TPMTypeOptionsEmulator
99089923
// - TPMTypeOptionsPassthrough
99099924
type TPMTypeOptions interface {
99109925
isTPMTypeOptions()
99119926
}
99129927

9928+
// TPMTypeOptionsEmulator is an implementation of TPMTypeOptions.
9929+
type TPMTypeOptionsEmulator TPMEmulatorOptions
9930+
9931+
func (TPMTypeOptionsEmulator) isTPMTypeOptions() {}
9932+
9933+
// MarshalJSON implements json.Marshaler.
9934+
func (s TPMTypeOptionsEmulator) MarshalJSON() ([]byte, error) {
9935+
v := map[string]interface{}{
9936+
"type": "emulator",
9937+
"data": s,
9938+
}
9939+
return json.Marshal(v)
9940+
}
9941+
99139942
// TPMTypeOptionsPassthrough is an implementation of TPMTypeOptions.
99149943
type TPMTypeOptionsPassthrough TPMPassthroughOptions
99159944

@@ -9933,6 +9962,12 @@ func decodeTPMTypeOptions(bs json.RawMessage) (TPMTypeOptions, error) {
99339962
return nil, err
99349963
}
99359964
switch v.T {
9965+
case "emulator":
9966+
var ret TPMTypeOptionsEmulator
9967+
if err := json.Unmarshal([]byte(v.V), &ret); err != nil {
9968+
return nil, err
9969+
}
9970+
return ret, nil
99369971
case "passthrough":
99379972
var ret TPMTypeOptionsPassthrough
99389973
if err := json.Unmarshal([]byte(v.V), &ret); err != nil {

qmp/raw/spec.txt

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9513,13 +9513,16 @@
95139513
# An enumeration of TPM types
95149514
#
95159515
# @passthrough: TPM passthrough type
9516+
# @emulator: Software Emulator TPM type
9517+
# Since: 2.11
95169518
#
95179519
# Since: 1.5
95189520
##
95199521
{
95209522
"enum": "TpmType",
95219523
"data": [
9522-
"passthrough"
9524+
"passthrough",
9525+
"emulator"
95239526
]
95249527
}
95259528

@@ -9535,7 +9538,7 @@
95359538
# Example:
95369539
#
95379540
# -> { "execute": "query-tpm-types" }
9538-
# <- { "return": [ "passthrough" ] }
9541+
# <- { "return": [ "passthrough", "emulator" ] }
95399542
#
95409543
##
95419544
{
@@ -9565,19 +9568,37 @@
95659568
}
95669569
}
95679570

9571+
##
9572+
# @TPMEmulatorOptions:
9573+
#
9574+
# Information about the TPM emulator type
9575+
#
9576+
# @chardev: Name of a unix socket chardev
9577+
#
9578+
# Since: 2.11
9579+
##
9580+
{
9581+
"struct": "TPMEmulatorOptions",
9582+
"data": {
9583+
"chardev": "str"
9584+
}
9585+
}
9586+
95689587
##
95699588
# @TpmTypeOptions:
95709589
#
95719590
# A union referencing different TPM backend types' configuration options
95729591
#
95739592
# @type: 'passthrough' The configuration options for the TPM passthrough type
9593+
# 'emulator' The configuration options for TPM emulator backend type
95749594
#
95759595
# Since: 1.5
95769596
##
95779597
{
95789598
"union": "TpmTypeOptions",
95799599
"data": {
9580-
"passthrough": "TPMPassthroughOptions"
9600+
"passthrough": "TPMPassthroughOptions",
9601+
"emulator": "TPMEmulatorOptions"
95819602
}
95829603
}
95839604

0 commit comments

Comments
 (0)