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
|
// Package vsutil provides added functionality for package vsock-internal use.
package vsutil
import (
"errors"
"net"
"os"
"golang.org/x/sys/unix"
"testing"
"github.com/mdlayher/vsock"
)
// IsHypervisor detects if this machine is a hypervisor by determining if
// /dev/vsock is available, and then if its context ID matches the one assigned
// to hosts.
func IsHypervisor(t *testing.T) bool {
t.Helper()
cid, err := vsock.ContextID()
if err != nil {
SkipDeviceError(t, err)
t.Fatalf("failed to retrieve context ID: %v", err)
}
return cid == vsock.Host
}
// SkipDeviceError skips this test if err is related to a failure to access the
// /dev/vsock device.
func SkipDeviceError(t *testing.T, err error) {
t.Helper()
// Unwrap net.OpError if needed.
// TODO(mdlayher): errors.Unwrap in Go 1.13.
if nerr, ok := err.(*net.OpError); ok {
err = nerr.Err
}
if os.IsNotExist(err) {
t.Skipf("skipping, vsock device does not exist (try: 'modprobe vhost_vsock'): %v", err)
}
if os.IsPermission(err) {
t.Skipf("skipping, permission denied (try: 'chmod 666 /dev/vsock'): %v", err)
}
if errors.Is(err, unix.EAFNOSUPPORT) {
t.Skipf("skipping, vsock kernel module isn't loaded: %v", err)
}
}
// SkipHostIntegration skips this test if this machine is a host and cannot
// perform a given test.
func SkipHostIntegration(t *testing.T) {
t.Helper()
if IsHypervisor(t) {
t.Skip("skipping, this integration test must be run in a guest")
}
}
|