From 5a1fe4c14a5588b5fad9673a61aed9963faa1894 Mon Sep 17 00:00:00 2001 From: Ben LeMasurier Date: Fri, 30 Sep 2016 09:54:47 -0600 Subject: [PATCH] Adds Capabilities() This adds a new method, Capabilities(), which returns an XML definition of the hypervisor's system capabilities, much like `virsh capabilities`. I've only included integration tests for these two methods due to the large amount of data returned by the capabilities call. --- internal/constants/constants.go | 1 + libvirt.go | 18 ++++++++++++++++++ libvirt_integration_test.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/internal/constants/constants.go b/internal/constants/constants.go index 5c9532d..4621ee6 100644 --- a/internal/constants/constants.go +++ b/internal/constants/constants.go @@ -38,6 +38,7 @@ const ( const ( ProcConnectOpen = 1 ProcConnectClose = 2 + ProcConnectGetCapabilties = 7 ProcDomainGetXMLDesc = 14 ProcDomainLookupByName = 23 ProcAuthList = 66 diff --git a/libvirt.go b/libvirt.go index 784c1bc..fb3be48 100644 --- a/libvirt.go +++ b/libvirt.go @@ -170,6 +170,24 @@ const ( DestroyFlagGraceful ) +// Capabilities returns an XML document describing the host's capabilties. +func (l *Libvirt) Capabilities() ([]byte, error) { + resp, err := l.request(constants.ProcConnectGetCapabilties, constants.ProgramRemote, nil) + if err != nil { + return nil, err + } + + r := <-resp + if r.Status != StatusOK { + return nil, decodeError(r.Payload) + } + + dec := xdr.NewDecoder(bytes.NewReader(r.Payload)) + caps, _, err := dec.DecodeString() + + return []byte(caps), err +} + // Connect establishes communication with the libvirt server. // The underlying libvirt socket connection must be previously established. func (l *Libvirt) Connect() error { diff --git a/libvirt_integration_test.go b/libvirt_integration_test.go index c1ce894..8079a69 100644 --- a/libvirt_integration_test.go +++ b/libvirt_integration_test.go @@ -41,6 +41,35 @@ func TestDisconnectIntegration(t *testing.T) { } } +func TestCapabilities(t *testing.T) { + l := New(testConn(t)) + defer l.Disconnect() + + if err := l.Connect(); err != nil { + t.Fatal(err) + } + + resp, err := l.Capabilities() + if err != nil { + t.Fatal(err) + } + + // verify UUID exists within returned XML + var caps struct { + Host struct { + UUID string `xml:"uuid"` + } `xml:"host"` + } + + if err := xml.Unmarshal(resp, &caps); err != nil { + t.Fatal(err) + } + + if caps.Host.UUID == "" { + t.Error("expected capabilities to contain a UUID") + } +} + func TestXMLIntegration(t *testing.T) { l := New(testConn(t))