-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtmap_test.go
126 lines (118 loc) · 3.19 KB
/
virtmap_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
package main
import (
"reflect"
"testing"
)
var ansibleOutput = []byte(`kvm21.example.com | FAILED => FAILED: [Errno -2] Name or service not known
kvm09.example.com | success | rc=0 >>
Id Name State
----------------------------------------------------
4 tam running
- olh shut off
kvm43.example.com | success | rc=0 >>
Id Name State
----------------------------------------------------
99 compute-64 paused
kvm30.example.com | FAILED => FAILED: timed out
kvm59.example.com | success | rc=0 >>
Id Name State
----------------------------------------------------
`)
func TestParseAnsibleOutput(t *testing.T) {
vmap := ParseAnsibleOutput(ansibleOutput)
if vmap.Length() == 0 {
t.Fatal("ParseAnsibleOutput() returned nothing")
}
expected := &Vmap{
Hosts: map[string]VHost{
"kvm09": VHost{"up", []string{"olh", "tam"}},
"kvm43": VHost{"up", []string{"compute-64"}},
"kvm30": VHost{"down", []string(nil)},
"kvm59": VHost{"up", []string(nil)},
},
Guests: map[string]VGuest{
"tam": VGuest{"running", "kvm09"},
"olh": VGuest{"shut", "kvm09"},
"compute-64": VGuest{"paused", "kvm43"},
},
}
if !reflect.DeepEqual(vmap, expected) {
t.Fatalf("ParseAnsibleOutput() failed.\nGot:\n%#v\nExpected:\n%#v", vmap.Hosts, expected.Hosts)
}
}
func TestGet(t *testing.T) {
vmap := ParseAnsibleOutput(ansibleOutput)
tests := []struct {
target string
result *Vmap
error string
}{
{
"kvm43",
&Vmap{
Hosts: map[string]VHost{"kvm43": VHost{"up", []string{"compute-64"}}},
Guests: map[string]VGuest(nil),
},
"",
},
{
"olh",
&Vmap{
Hosts: map[string]VHost(nil),
Guests: map[string]VGuest{"olh": VGuest{"shut", "kvm09"}},
},
"",
},
{
"nonsuch",
nil,
"Node not found",
},
{
"kvm59",
&Vmap{
Hosts: map[string]VHost{"kvm59": VHost{"up", []string(nil)}},
Guests: map[string]VGuest(nil),
},
"",
},
}
for _, test := range tests {
t.Run(test.target, func(t *testing.T) {
node, err := vmap.Get(test.target)
if test.error != "" {
if err.Error() != test.error {
t.Fatalf("Get() returned the wrong error\nGot:\n%v\nExpected:\n%v", err, test.error)
}
} else if err != nil {
t.Fatalf("Get() returned an error unexpectedly: %v", err)
}
if !reflect.DeepEqual(node, test.result) {
t.Fatalf("Get() returned bad node data\nGot:\n%#v\nExpected:\n%#v", node, test.result)
}
})
}
}
func TestInfo(t *testing.T) {
vmap := ParseAnsibleOutput(ansibleOutput)
tests := []struct {
node string
info string
}{
{"kvm09", "kvm09 is a virtual host for guests: olh, tam"},
{"tam", "tam is a virtual guest on host: kvm09"},
{"gone", "Node gone not found"},
{"kvm59", "kvm59 is a virtual host for guests: "},
}
for _, test := range tests {
t.Run(test.node, func(t *testing.T) {
returned := vmap.Info(test.node)
if returned == "" {
t.Fatalf("Info returned nothing for node %s\n", test.node)
}
if returned != test.info {
t.Fatalf("Info() problem\nGot:\n%#v\nExpected:\n%#v", returned, test.info)
}
})
}
}