2016-10-20 21:51:44 +03:00
|
|
|
// 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 (
|
2016-12-01 04:37:30 +03:00
|
|
|
"encoding/xml"
|
2016-10-20 21:51:44 +03:00
|
|
|
"net"
|
2017-12-07 20:47:52 +03:00
|
|
|
"sync"
|
2016-10-20 21:51:44 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
2017-01-20 05:40:31 +03:00
|
|
|
|
|
|
|
"github.com/digitalocean/go-libvirt/internal/constants"
|
2016-10-20 21:51:44 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-30 18:54:47 +03:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-20 05:40:31 +03:00
|
|
|
func TestSecretsIntegration(t *testing.T) {
|
|
|
|
l := New(testConn(t))
|
|
|
|
defer l.Disconnect()
|
|
|
|
|
|
|
|
if err := l.Connect(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
secrets, err := l.Secrets()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
wantLen := 1
|
|
|
|
gotLen := len(secrets)
|
|
|
|
if gotLen != wantLen {
|
2017-08-01 23:52:12 +03:00
|
|
|
t.Fatalf("expected %d secrets, got %d", wantLen, gotLen)
|
2017-01-20 05:40:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
s := secrets[0]
|
|
|
|
|
|
|
|
wantType := SecretUsageTypeVolume
|
2017-11-17 01:14:05 +03:00
|
|
|
if s.UsageType != int32(wantType) {
|
2017-08-01 23:52:12 +03:00
|
|
|
t.Errorf("expected usage type: %d, got %d", wantType, s.UsageType)
|
2017-01-20 05:40:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
wantID := "/tmp"
|
|
|
|
if s.UsageID != wantID {
|
2017-08-01 23:52:12 +03:00
|
|
|
t.Errorf("expected usage id: %q, got %q", wantID, s.UsageID)
|
2017-01-20 05:40:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 19fdc2f2-fa64-46f3-bacf-42a8aafca6dd
|
|
|
|
wantUUID := [constants.UUIDSize]byte{
|
|
|
|
0x19, 0xfd, 0xc2, 0xf2, 0xfa, 0x64, 0x46, 0xf3,
|
|
|
|
0xba, 0xcf, 0x42, 0xa8, 0xaa, 0xfc, 0xa6, 0xdd,
|
|
|
|
}
|
|
|
|
if s.UUID != wantUUID {
|
|
|
|
t.Errorf("expected UUID %q, got %q", wantUUID, s.UUID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-10 00:54:30 +03:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-07 00:58:27 +03:00
|
|
|
func TestStoragePoolsIntegration(t *testing.T) {
|
|
|
|
l := New(testConn(t))
|
|
|
|
defer l.Disconnect()
|
|
|
|
|
|
|
|
if err := l.Connect(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-01-03 23:19:28 +03:00
|
|
|
pools, err := l.StoragePools(ConnectListStoragePoolsActive)
|
2017-01-07 00:58:27 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-01-03 23:19:28 +03:00
|
|
|
pools, err := l.StoragePools(ConnectListStoragePoolsAutostart)
|
2017-01-07 00:58:27 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
wantLen := 0
|
|
|
|
gotLen := len(pools)
|
|
|
|
if gotLen != wantLen {
|
|
|
|
t.Errorf("expected %d storage pool, got %d", wantLen, gotLen)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-10 00:54:30 +03:00
|
|
|
func TestStoragePoolRefreshIntegration(t *testing.T) {
|
|
|
|
l := New(testConn(t))
|
|
|
|
defer l.Disconnect()
|
|
|
|
|
|
|
|
if err := l.Connect(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-11-17 01:28:40 +03:00
|
|
|
pool, err := l.StoragePool("test")
|
2017-11-17 01:14:05 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = l.StoragePoolRefresh(pool, 0)
|
2017-01-10 00:54:30 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-11-17 01:14:05 +03:00
|
|
|
pool, err := l.StoragePool("test-does-not-exist")
|
|
|
|
if err == nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2017-11-17 01:16:10 +03:00
|
|
|
err = l.StoragePoolRefresh(pool, 0)
|
2017-01-10 00:54:30 +03:00
|
|
|
if err == nil {
|
|
|
|
t.Error("expected non-existent storage pool to fail refresh")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 04:37:30 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-07 20:47:52 +03:00
|
|
|
// verify we're able to concurrently communicate with libvirtd.
|
|
|
|
// see: https://github.com/digitalocean/go-libvirt/pull/52
|
|
|
|
func Test_concurrentWrite(t *testing.T) {
|
|
|
|
l := New(testConn(t))
|
|
|
|
|
|
|
|
if err := l.Connect(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
defer l.Disconnect()
|
|
|
|
|
|
|
|
count := 10
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(count)
|
|
|
|
|
|
|
|
start := make(chan struct{})
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
<-start
|
|
|
|
|
|
|
|
_, err := l.Domains()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
close(start)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Fatal("timed out waiting for execution to complete")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-20 21:51:44 +03:00
|
|
|
func testConn(t *testing.T) net.Conn {
|
|
|
|
conn, err := net.DialTimeout("tcp", testAddr, time.Second*2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn
|
|
|
|
}
|