File: example_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 (113 lines) | stat: -rw-r--r-- 2,711 bytes parent folder | download | duplicates (3)
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
// 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 hypervisor_test

import (
	"fmt"
	"log"

	"github.com/digitalocean/go-qemu/hypervisor"
	"github.com/digitalocean/go-qemu/qmp"
	"github.com/digitalocean/go-qemu/qmp/qmptest"
)

func ExampleNew() {
	// Create a hypervisor.Hypervisor using an in-memory hypervisor.Driver,
	// that returns a fixed list of three domains.
	hv := hypervisor.New(newDriver([]string{
		"foo",
		"bar",
		"baz",
	}))

	// Connect to the monitor socket of each domain using the in-memory driver.
	domains, err := hv.Domains()
	if err != nil {
		log.Fatalf("failed to connect to domains: %v", err)
	}

	fmt.Println("hypervisor:")
	for _, d := range domains {
		// Return the domain's QEMU version.
		version, err := d.Version()
		if err != nil {
			log.Fatalf("failed to check domain QEMU version: %v", err)
		}

		// Print some information about the domain.
		fmt.Printf("  - %s - QEMU %s\n", d.Name, version)

		// Close the domain to clean up its resources and underlying monitor.
		if err := d.Close(); err != nil {
			log.Fatalf("failed to close domain: %v", err)
		}
	}

	// Output:
	// hypervisor:
	//   - foo - QEMU 2.0.0
	//   - bar - QEMU 2.0.1
	//   - baz - QEMU 2.0.2
}

var _ hypervisor.Driver = &driver{}

func newDriver(domains []string) *driver {
	return &driver{
		domains: domains,
	}
}

type driver struct {
	domains []string
}

func (d *driver) NewMonitor(domain string) (qmp.Monitor, error) {
	m := qmptest.NewMonitor(func(cmd qmp.Command) (interface{}, error) {
		return run(domain, cmd)
	})

	return m, nil
}

func (d *driver) DomainNames() ([]string, error) {
	return d.domains, nil
}

func run(domain string, cmd qmp.Command) (interface{}, error) {
	switch cmd.Execute {
	case "query-version":
		return runQueryVersion(domain), nil
	}

	return nil, fmt.Errorf("unknown command: %q", cmd.Execute)
}

func runQueryVersion(domain string) interface{} {
	var response struct {
		ID     string      `json:"id"`
		Return qmp.Version `json:"return"`
	}
	response.Return.QEMU.Major = 2

	switch domain {
	case "bar":
		response.Return.QEMU.Micro = 1
	case "baz":
		response.Return.QEMU.Micro = 2
	}

	return response
}