Documentation
¶
Overview ¶
Package libvirtd runs libvirtd (the KVM/QEMU virtualization manager) in a Docker container and returns a connected go-libvirt client. This exposes the full libvirt API — virtual machine (domain) lifecycle, storage pools and volumes, virtual networks, snapshots and more — so tests can drive real virtualization without mocks.
The container is configured to listen on a TCP socket (port 16509) so the test connects over TCP. By default it runs in a least-privilege configuration: every capability is dropped and only the minimal set QEMU/libvirtd need is added back, with Docker's default seccomp profile relaxed (seccomp=unconfined) so QEMU can start. /dev/kvm and /dev/net/tun are passed through when they exist on the host (otherwise QEMU falls back to software/TCG emulation). Call WithPrivileged for full-privilege mode.
Example ¶
This example demonstrates starting a libvirtd container and retrieving the libvirt version via the connected go-libvirt client.
package main
import (
"context"
"fmt"
"time"
"github.com/teran/go-docker-testsuite/applications/libvirtd"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
defer cancel()
app, err := libvirtd.New(ctx)
if err != nil {
fmt.Printf("error: %v (is Docker running, with /dev/kvm?)\n", err)
return
}
defer func() { _ = app.Close(ctx) }()
ver, err := app.Client().ConnectGetLibVersion()
if err != nil {
fmt.Printf("error getting libvirt version: %v\n", err)
return
}
fmt.Printf("libvirt version: %d\n", ver)
}
Output:
Index ¶
- type Libvirt
- func New(ctx context.Context, opts ...Option) (Libvirt, error)
- func NewWithImage(ctx context.Context, image string, opts ...Option) (Libvirt, error)
- func NewWithImageT(t *testing.T, ctx context.Context, image string, opts ...Option) (Libvirt, error)
- func NewWithT(t *testing.T, ctx context.Context, opts ...Option) (Libvirt, error)
- type Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Libvirt ¶
type Libvirt interface {
// Client returns a connected go-libvirt client exposing the full
// libvirt API (domains, storage pools/volumes, networks, snapshots, ...).
Client() *libvirt.Libvirt
// Addr returns the host:port of the libvirtd TCP endpoint.
Addr() string
// HasKVM reports whether /dev/kvm was present and passed into the
// container (i.e. whether KVM acceleration is in use vs software/TCG).
HasKVM() bool
Close(ctx context.Context) error
}
Libvirt is the interface returned by the libvirtd application.
func NewWithImage ¶
NewWithImage starts a libvirtd container using the given image.
libvirtd is configured to listen on TCP (port 16509) via an injected libvirtd.conf, and the port is exposed to the host. /dev/kvm and /dev/net/tun are passed through only if they exist on the host; when /dev/kvm is absent, QEMU falls back to slower software (TCG) emulation rather than failing.
type Option ¶
type Option func(*options)
Option configures a libvirtd container.
func WithPrivileged ¶
func WithPrivileged() Option
WithPrivileged runs the container in full Docker privileged mode. This is an opt-in for workloads that need capabilities beyond the least-privilege default (e.g. SYS_ADMIN for mount/loop-backed storage pools or LVM). It is mutually exclusive with the default capability whitelist.