-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli_test.go
127 lines (120 loc) · 2.18 KB
/
cli_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
package main
import (
"bytes"
"io/ioutil"
"net/http"
"reflect"
"testing"
)
var tests = map[string]struct {
Resp http.Response
Vmap Vmap
Error error
}{
"http://TESTHOST/api/v1/vmap/kvm09": {
Resp: http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewBufferString(`
{
"hosts": {
"kvm09": {
"state": "up",
"guests": [
"olh",
"tam"
]
}
},
"guests": null
}`)),
},
Vmap: Vmap{
Hosts: map[string]VHost{
"kvm09": VHost{"up", []string{"olh", "tam"}},
},
Guests: map[string]VGuest(nil),
},
Error: nil,
},
"http://TESTHOST/api/v1/vmap/tam": {
Resp: http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewBufferString(`
{
"hosts": null,
"guests": {
"tam": {
"state": "running",
"host": "kvm09"
}
}
}`)),
},
Vmap: Vmap{
Hosts: map[string]VHost(nil),
Guests: map[string]VGuest{
"tam": VGuest{"running", "kvm09"},
},
},
Error: nil,
},
"http://TESTHOST/api/v1/vmap/": {
Resp: http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewBufferString(`
{
"hosts": {
"kvm09": {
"state": "up",
"guests": [
"olh",
"tam"
]
}
},
"guests": {
"olh": {
"state": "running",
"host": "kvm09"
},
"tam": {
"state": "paused",
"host": "kvm09"
}
}
}`)),
},
Vmap: Vmap{
Hosts: map[string]VHost{
"kvm09": VHost{"up", []string{"olh", "tam"}},
},
Guests: map[string]VGuest{
"olh": VGuest{"running", "kvm09"},
"tam": VGuest{"paused", "kvm09"},
},
},
Error: nil,
},
}
func TestQuery(t *testing.T) {
apiPath := "http://TESTHOST/api/v1/vmap/"
HTTPGetter = func(url string) (*http.Response, error) {
test, found := tests[url]
if !found {
t.Fatalf("Bad Query() test: URL %q not found", url)
}
return &test.Resp, test.Error
}
for req, tt := range tests {
req = req[len(apiPath):]
t.Run(req, func(t *testing.T) {
resp, err := Query("TESTHOST", req)
if err != tt.Error {
t.Fatalf("Query() returned the wrong error\nGot:\n%v\nExpected:\n%v", err, tt.Error)
}
if !reflect.DeepEqual(resp, &tt.Vmap) {
t.Fatalf("Query() returned bad Vmap\nGot:\n%#v\nExpected:\n%#v", resp, tt.Vmap)
}
})
}
}