go-libvirt-plain/libvirt_integration_test.go
Ben LeMasurier cfa567708b Adds StoragePool(), StoragePoolRefresh() (#28)
This adds two new methods:
- `StoragePool()`, used to lookup a storage pool by name.
- `StoragePoolRefresh()`, used to refresh a storage pool by name.
2017-01-09 14:54:30 -07:00

209 lines
4.0 KiB
Go

// Copyright 2016 The go-libvirt 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.
// +build integration
package libvirt
import (
"encoding/xml"
"net"
"testing"
"time"
)
const testAddr = "127.0.0.1:16509"
func TestConnectIntegration(t *testing.T) {
l := New(testConn(t))
defer l.Disconnect()
if err := l.Connect(); err != nil {
t.Error(err)
}
}
func TestDisconnectIntegration(t *testing.T) {
l := New(testConn(t))
if err := l.Disconnect(); err != nil {
t.Error(err)
}
}
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 TestStoragePoolIntegration(t *testing.T) {
l := New(testConn(t))
defer l.Disconnect()
if err := l.Connect(); err != nil {
t.Fatal(err)
}
wantName := "test"
pool, err := l.StoragePool(wantName)
if err != nil {
t.Fatal(err)
}
gotName := pool.Name
if gotName != wantName {
t.Errorf("expected name %q, got %q", wantName, gotName)
}
}
func TestStoragePoolInvalidIntegration(t *testing.T) {
l := New(testConn(t))
defer l.Disconnect()
if err := l.Connect(); err != nil {
t.Fatal(err)
}
_, err := l.StoragePool("test-does-not-exist")
if err == nil {
t.Errorf("expected non-existent storage pool return error")
}
}
func TestStoragePoolsIntegration(t *testing.T) {
l := New(testConn(t))
defer l.Disconnect()
if err := l.Connect(); err != nil {
t.Fatal(err)
}
pools, err := l.StoragePools(StoragePoolsFlagActive)
if err != nil {
t.Error(err)
}
wantLen := 1
gotLen := len(pools)
if gotLen != wantLen {
t.Fatalf("expected %d storage pool, got %d", wantLen, gotLen)
}
wantName := "test"
gotName := pools[0].Name
if gotName != wantName {
t.Errorf("expected name %q, got %q", wantName, gotName)
}
}
func TestStoragePoolsAutostartIntegration(t *testing.T) {
l := New(testConn(t))
defer l.Disconnect()
if err := l.Connect(); err != nil {
t.Fatal(err)
}
pools, err := l.StoragePools(StoragePoolsFlagAutostart)
if err != nil {
t.Error(err)
}
wantLen := 0
gotLen := len(pools)
if gotLen != wantLen {
t.Errorf("expected %d storage pool, got %d", wantLen, gotLen)
}
}
func TestStoragePoolRefreshIntegration(t *testing.T) {
l := New(testConn(t))
defer l.Disconnect()
if err := l.Connect(); err != nil {
t.Fatal(err)
}
err := l.StoragePoolRefresh("test")
if err != nil {
t.Error(err)
}
}
func TestStoragePoolRefreshInvalidIntegration(t *testing.T) {
l := New(testConn(t))
defer l.Disconnect()
if err := l.Connect(); err != nil {
t.Fatal(err)
}
err := l.StoragePoolRefresh("test-does-not-exist")
if err == nil {
t.Error("expected non-existent storage pool to fail refresh")
}
}
func TestXMLIntegration(t *testing.T) {
l := New(testConn(t))
if err := l.Connect(); err != nil {
t.Error(err)
}
defer l.Disconnect()
var flags DomainXMLFlags
data, err := l.XML("test", flags)
if err != nil {
t.Fatal(err)
}
var v interface{}
if err := xml.Unmarshal(data, &v); err != nil {
t.Error(err)
}
}
func testConn(t *testing.T) net.Conn {
conn, err := net.DialTimeout("tcp", testAddr, time.Second*2)
if err != nil {
t.Fatal(err)
}
return conn
}