summaryrefslogtreecommitdiff
path: root/libvirt/utils_libvirt_test.go
blob: abaaea30d0a6d61c5d22149872abd9a806db8f73 (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package libvirt

import (
	"encoding/xml"
	"testing"
	"time"

	"os"

	libvirt "github.com/libvirt/libvirt-go"
	"github.com/libvirt/libvirt-go-xml"
)

func TestGetHostXMLDesc(t *testing.T) {
	ip := "127.0.0.1"
	mac := "XX:YY:ZZ"
	name := "localhost"

	data := getHostXMLDesc(ip, mac, name)

	dd := libvirtxml.NetworkDHCPHost{}
	err := xml.Unmarshal([]byte(data), &dd)
	if err != nil {
		t.Errorf("error %v", err)
	}

	if dd.IP != ip {
		t.Errorf("expected ip %s, got %s", ip, dd.IP)
	}

	if dd.MAC != mac {
		t.Errorf("expected mac %s, got %s", mac, dd.MAC)
	}

	if dd.Name != name {
		t.Errorf("expected name %s, got %s", name, dd.Name)
	}
}

func connect(t *testing.T) *libvirt.Connect {
	conn, err := libvirt.NewConnect(os.Getenv("LIBVIRT_DEFAULT_URI"))
	if err != nil {
		t.Fatal(err)
	}

	return conn
}

func TestGetHostArchitecture(t *testing.T) {
	if !testAccEnabled() {
		t.Logf("Acceptance tests skipped unless env 'TF_ACC' set")
		return
	}

	conn := connect(t)
	defer conn.Close()

	arch, err := getHostArchitecture(conn)

	if err != nil {
		t.Errorf("error %v", err)
	}

	t.Logf("[DEBUG] arch - %s", arch)

	if arch == "" {
		t.Errorf("arch is blank.")
	}
}

func TestGetCanonicalMachineName(t *testing.T) {
	if !testAccEnabled() {
		t.Logf("Acceptance tests skipped unless env 'TF_ACC' set")
		return
	}

	conn := connect(t)
	defer conn.Close()
	arch := "x86_64"
	virttype := "hvm"
	machine := "pc"

	caps, err := getHostCapabilities(conn)
	if err != nil {
		t.Error(err)
	}

	name, err := getCanonicalMachineName(caps, arch, virttype, machine)

	if err != nil {
		t.Errorf("Could not get canonical name for %s/%s", arch, machine)
		return
	}

	t.Logf("Canonical name for %s/%s = %s", arch, machine, name)
}

func TestGetOriginalMachineName(t *testing.T) {
	if !testAccEnabled() {
		t.Logf("Acceptance tests skipped unless env 'TF_ACC' set")
		return
	}

	conn := connect(t)
	defer conn.Close()
	arch := "x86_64"
	virttype := "hvm"
	machine := "pc"

	caps, err := getHostCapabilities(conn)
	if err != nil {
		t.Error(err)
	}

	canonname, err := getCanonicalMachineName(caps, arch, virttype, machine)
	if err != nil {
		t.Error(err)
	}
	reversename, err := getOriginalMachineName(caps, arch, virttype, canonname)
	if err != nil {
		t.Error(err)
	}
	if reversename != machine {
		t.Errorf("Cannot reverse canonical machine lookup")
	}

	t.Logf("Reverse canonical lookup for %s is %s which matches %s", canonname, reversename, machine)
}

func TestGetHostCapabilties(t *testing.T) {
	if !testAccEnabled() {
		t.Logf("Acceptance tests skipped unless env 'TF_ACC' set")
		return
	}

	start := time.Now()
	conn := connect(t)
	defer conn.Close()
	caps, err := getHostCapabilities(conn)
	if err != nil {
		t.Errorf("Can't get host capabilties")
	}
	if caps.Host.UUID == "" {
		t.Errorf("Host has no UUID!")
	}

	elapsed := time.Since(start)
	t.Logf("[DEBUG] Get host capabilites took %s", elapsed)
}