forked from mistifyio/lochness
/
hypervisor_test.go
169 lines (128 loc) · 3.42 KB
/
hypervisor_test.go
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package lochness_test
import (
"os"
"strings"
"testing"
log "github.com/Sirupsen/logrus"
h "github.com/bakins/test-helpers"
"github.com/mistifyio/lochness"
)
func newHypervisor(t *testing.T) *lochness.Hypervisor {
c := newContext(t)
hv := c.NewHypervisor()
err := hv.Save()
h.Ok(t, err)
return hv
}
func TestNewHypervisor(t *testing.T) {
hv := newHypervisor(t)
defer contextCleanup(t)
h.Equals(t, 36, len(hv.ID))
}
func TestHypervisor(t *testing.T) {
c := newContext(t)
defer contextCleanup(t)
hv := newHypervisor(t)
id := hv.ID
hv, err := c.Hypervisor(id)
h.Ok(t, err)
h.Equals(t, id, hv.ID)
}
func TestHypervisorIsAlive(t *testing.T) {
hv := newHypervisor(t)
defer contextCleanup(t)
h.Equals(t, false, hv.IsAlive())
}
func TestHypervisorsAlias(t *testing.T) {
_ = lochness.Hypervisors([]*lochness.Hypervisor{})
}
func TestHypervisorSetConfig(t *testing.T) {
hv := newHypervisor(t)
defer contextCleanup(t)
h.Ok(t, hv.SetConfig("foo", "bar"))
h.Equals(t, "bar", hv.Config["foo"])
h.Ok(t, hv.Refresh())
h.Equals(t, "bar", hv.Config["foo"])
h.Ok(t, hv.SetConfig("foo", ""))
_, ok := hv.Config["foo"]
h.Equals(t, ok, false)
}
func TestFirstHypervisor(t *testing.T) {
c := newContext(t)
newHypervisor(t)
newHypervisor(t)
hv := newHypervisor(t)
defer contextCleanup(t)
found, err := c.FirstHypervisor(func(h *lochness.Hypervisor) bool {
return h.ID == "foo"
})
h.Ok(t, err)
h.Assert(t, found == nil, "unexpected value")
found, err = c.FirstHypervisor(func(h *lochness.Hypervisor) bool {
return h.ID == hv.ID
})
h.Ok(t, err)
h.Assert(t, found != nil, "unexpected nil")
}
func TestHypervisorDestroy(t *testing.T) {
defer contextCleanup(t)
hv := newHypervisor(t)
err := hv.Destroy()
h.Ok(t, err)
// need a test with a guest
}
func TestHypervisorWithBadID(t *testing.T) {
c := newContext(t)
_, err := c.Hypervisor("")
h.Assert(t, err != nil, "should have got an error")
h.Assert(t, strings.Contains(err.Error(), "invalid UUID"), "unexpected error")
_, err = c.Hypervisor("foo")
h.Assert(t, err != nil, "should have got an error")
h.Assert(t, strings.Contains(err.Error(), "invalid UUID"), "unexpected error")
}
func TestSetHypervisorID(t *testing.T) {
// passing test with uuid
uuid := "d3cac004-4d89-4f26-9776-97df74a41417"
id, err := lochness.SetHypervisorID(uuid)
h.Ok(t, err)
h.Equals(t, uuid, id)
id, err = lochness.SetHypervisorID("foo")
h.Assert(t, err != nil, "should have got an error")
h.Equals(t, "", id)
// set with ENV
uuid = "3e0f2128-0342-49f6-8e5f-ecd401bae99e"
if err := os.Setenv("HYPERVISOR_ID", uuid); err != nil {
log.WithFields(log.Fields{
"error": err,
"uuid": uuid,
}).Error("failed to set HYPERVISOR_ID env variable")
}
id, err = lochness.SetHypervisorID("")
h.Ok(t, err)
h.Equals(t, uuid, id)
}
func TestGetHypervisorID(t *testing.T) {
uuid := "d3cac004-4d89-4f26-9776-97df74a41417"
id, err := lochness.SetHypervisorID(uuid)
h.Ok(t, err)
h.Equals(t, uuid, id)
id = lochness.GetHypervisorID()
h.Equals(t, uuid, id)
}
func TestVerifyOnHV(t *testing.T) {
defer contextCleanup(t)
hv := newHypervisor(t)
// failing test
uuid := "d3cac004-4d89-4f26-9776-97df74a41417"
id, err := lochness.SetHypervisorID(uuid)
h.Ok(t, err)
h.Equals(t, uuid, id)
err = hv.VerifyOnHV()
h.Assert(t, err != nil, "should have got an error")
// passing
id, err = lochness.SetHypervisorID(hv.ID)
h.Ok(t, err)
h.Equals(t, hv.ID, id)
err = hv.VerifyOnHV()
h.Ok(t, err)
}