-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvrops_test.go
232 lines (202 loc) · 9.66 KB
/
vrops_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetAdapterResources(t *testing.T) {
// setup fake http server for test
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := fmt.Fprint(w, "{\"pageInfo\":{\"totalCount\":99,\"page\":1,\"pageSize\":10},\"links\":[{\"href\":\"foo\",\"rel\":\"SELF\",\"name\":\"current\"}],\"resourceList\":[{\"creationTime\":1524505047160,\"resourceKey\":{\"name\":\"vcenter.test.pdxfixit.com\",\"adapterKindKey\":\"TEST\",\"resourceKindKey\":\"TestAdapterInstance\",\"resourceIdentifiers\":[{\"identifierType\":{\"name\":\"TestName\",\"dataType\":\"STRING\",\"isPartOfUniqueness\":false},\"value\":\"vcenter.test.pdxfixit.com\"}]},\"resourceStatusStates\":[{\"adapterInstanceId\":\"3f6da67e-b49b-4714-963c-18b3b56e82a2\",\"resourceStatus\":\"DATA_RECEIVING\",\"resourceState\":\"STARTED\",\"statusMessage\":\"TESTING\"}],\"resourceHealth\":\"GREEN\",\"resourceHealthValue\":100.0,\"dtEnabled\":true,\"badges\":[{\"type\":\"TEST\",\"color\":\"GREEN\",\"score\":0.0}],\"relatedResources\":[],\"links\":[{\"href\":\"foo\",\"rel\":\"SELF\",\"name\":\"linkToSelf\"}],\"identifier\":\"2fb6adf9-7665-4bec-9d53-e49c5a71d63a\"}]}")
if err != nil {
t.Error(err.Error())
}
}))
defer ts.Close()
config.Vrops.Host = ts.URL
adapterResources, err := getAdapterResources("fake-id-because-its-a-test", 0)
if err != nil {
t.Errorf("%v", err)
}
assert.Equal(t, adapterResources.PageInfo.TotalCount, 99, "PageInfo TotalCount")
assert.Equal(t, adapterResources.PageInfo.Page, 1, "PageInfo Page")
assert.Equal(t, adapterResources.PageInfo.PageSize, 10, "PageInfo PageSize")
assert.Len(t, adapterResources.Links, 1, "Links count")
assert.Equal(t, adapterResources.Links[0].Href, "foo", "Link Href")
assert.Equal(t, adapterResources.Links[0].Rel, "SELF", "Link Rel")
assert.Equal(t, adapterResources.Links[0].Name, "current", "Link Name")
assert.Len(t, adapterResources.ResourceList, 1, "ResourceList count")
assert.Equal(t, adapterResources.ResourceList[0].CreationTime, 1524505047160, "ResourceList CreationTime")
assert.Equal(t, adapterResources.ResourceList[0].ResourceKey.Name, "vcenter.test.pdxfixit.com", "ResourceList ResourceKey Name")
assert.Equal(t, adapterResources.ResourceList[0].ResourceKey.AdapterKindKey, "TEST", "ResourceList ResourceKey AdapterKindKey")
assert.Equal(t, adapterResources.ResourceList[0].ResourceKey.ResourceKindKey, "TestAdapterInstance", "ResourceList ResourceKey ResourceKindKey")
assert.Len(t, adapterResources.ResourceList[0].ResourceKey.ResourceIdentifiers, 1, "ResourceList ResourceKey ResourceIdentifiers count")
assert.Equal(t, adapterResources.ResourceList[0].ResourceKey.ResourceIdentifiers[0].IdentifierType.Name, "TestName", "ResourceList ResourceKey ResourceIdentifier IdentifierType Name")
assert.Equal(t, adapterResources.ResourceList[0].ResourceKey.ResourceIdentifiers[0].IdentifierType.DataType, "STRING", "ResourceList ResourceKey ResourceIdentifier IdentifierType DataType")
assert.Equal(t, adapterResources.ResourceList[0].ResourceKey.ResourceIdentifiers[0].Value, "vcenter.test.pdxfixit.com", "ResourceList ResourceKey ResourceIdentifier Value")
assert.Len(t, adapterResources.ResourceList[0].ResourceStatusStates, 1, "ResourceList ResourceStatusStates count")
assert.Equal(t, adapterResources.ResourceList[0].ResourceStatusStates[0].AdapterInstanceID, "3f6da67e-b49b-4714-963c-18b3b56e82a2", "ResourceList ResourceStatusStates AdapterInstanceID")
assert.Equal(t, adapterResources.ResourceList[0].ResourceStatusStates[0].ResourceStatus, "DATA_RECEIVING", "ResourceList ResourceStatusStates ResourceStatus")
assert.Equal(t, adapterResources.ResourceList[0].ResourceStatusStates[0].ResourceState, "STARTED", "ResourceList ResourceStatusStates ResourceState")
assert.Equal(t, adapterResources.ResourceList[0].ResourceStatusStates[0].StatusMessage, "TESTING", "ResourceList ResourceStatusStates StatusMessage")
assert.Equal(t, adapterResources.ResourceList[0].ResourceHealth, "GREEN", "ResourceList ResourceHealth")
assert.Equal(t, adapterResources.ResourceList[0].ResourceHealthValue, float32(100), "ResourceList ResourceHealthValue")
assert.Equal(t, adapterResources.ResourceList[0].DtEnabled, true, "ResourceList DtEnabled")
assert.Len(t, adapterResources.ResourceList[0].Badges, 1, "ResourceList Badges count")
assert.Equal(t, adapterResources.ResourceList[0].Badges[0].Type, "TEST", "ResourceList Badge Type")
assert.Equal(t, adapterResources.ResourceList[0].Badges[0].Color, "GREEN", "ResourceList Badge Color")
assert.Equal(t, adapterResources.ResourceList[0].Badges[0].Score, float32(0), "ResourceList Badge Score")
assert.Len(t, adapterResources.ResourceList[0].RelatedResources, 0, "ResourceList RelatedResources")
assert.Len(t, adapterResources.ResourceList[0].Links, 1, "ResourceList Links count")
assert.Equal(t, adapterResources.ResourceList[0].Links[0].Href, "foo", "ResourceList Link Href")
assert.Equal(t, adapterResources.ResourceList[0].Links[0].Rel, "SELF", "ResourceList Link Rel")
assert.Equal(t, adapterResources.ResourceList[0].Links[0].Name, "linkToSelf", "ResourceList Link Name")
assert.Equal(t, adapterResources.ResourceList[0].Identifier, "2fb6adf9-7665-4bec-9d53-e49c5a71d63a", "ResourceList Identifier")
}
func TestGetResourceProperties(t *testing.T) {
data := `{"resourceId":"2fb6adf9-7665-4bec-9d53-e49c5a71d63a","property":[{"name":"test","value":"yes"}]}`
// setup fake http server for test
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := fmt.Fprint(w, data)
if err != nil {
t.Error(err.Error())
}
}))
defer ts.Close()
config.Vrops.Host = ts.URL
config.Vrops.ResourceKindKeys = []string{"Test Adapter Instance"}
testResources := []vropsResource{
{
1524505047160,
vropsResourceKey{
"vcenter.test.pdxfixit.com",
"TEST",
"Test Adapter Instance",
[]vropsResourceIdentifier{
{
vropsResourceIdentifierType{
"TestName",
"STRING",
false,
},
"vcenter.test.pdxfixit.com",
},
},
},
[]vropsResourceStatusState{
{
"3f6da67e-b49b-4714-963c-18b3b56e82a2",
"DATA_RECEIVING",
"STARTED",
"TESTING",
},
},
"GREEN",
100.0,
true,
[]vropsBadge{
{
"TEST",
"GREEN",
0,
},
},
[]interface{}{},
[]vropsLink{},
"2fb6adf9-7665-4bec-9d53-e49c5a71d63a",
},
}
collection := getResourceProperties(testResources)
assert.Len(t, collection, 1, "count of records")
assert.Empty(t, collection[0].ID, "id should be empty")
assert.Equal(t, collection[0].Type, "vrops-test-test_adapter_instance", "record type")
assert.Empty(t, collection[0].Hostname, "hostname should be empty")
assert.Empty(t, collection[0].IP, "ip should be empty")
assert.NotEmpty(t, collection[0].Timestamp, "timestamp should not be empty")
assert.NotEmpty(t, collection[0].Committer, "committer should not be empty")
assert.Empty(t, collection[0].Context, "context should be empty")
assert.Equal(t, collection[0].Data, json.RawMessage(data), "payload")
assert.Empty(t, collection[0].Hash, "hash should be empty")
}
// todo: duplicate below test for -hostsystem records
// test the special function for type -virtualmachine records
func TestVirtualMachineMetadata(t *testing.T) {
data := `{"resourceId":"01afa5ae-216f-4b27-91a7-43abfe5d5905","property":[{"name":"summary|guest|hostName","value":"localhost"},{"name":"summary|guest|ipAddress","value":"127.0.0.1"}]}`
// setup fake http server for test
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := fmt.Fprint(w, data)
if err != nil {
t.Error(err.Error())
}
}))
defer ts.Close()
config.Vrops.Host = ts.URL
config.Vrops.ResourceKindKeys = []string{"virtualmachine"}
testResources := []vropsResource{
{
1524505047160,
vropsResourceKey{
"vcenter.test.pdxfixit.com",
"vmware",
"virtualmachine",
[]vropsResourceIdentifier{
{
vropsResourceIdentifierType{
"TestName",
"STRING",
false,
},
"vcenter.test.pdxfixit.com",
},
},
},
[]vropsResourceStatusState{
{
"3f6da67e-b49b-4714-963c-18b3b56e82a2",
"DATA_RECEIVING",
"STARTED",
"TESTING",
},
},
"GREEN",
100.0,
true,
[]vropsBadge{
{
"TEST",
"GREEN",
0,
},
},
[]interface{}{},
[]vropsLink{},
"01afa5ae-216f-4b27-91a7-43abfe5d5905",
},
}
collection := getResourceProperties(testResources)
assert.Len(t, collection, 1, "count of records")
assert.Empty(t, collection[0].ID, "id should be empty")
assert.Equal(t, collection[0].Type, "vrops-vmware-virtualmachine", "record type")
assert.Empty(t, collection[0].Hostname, "hostname should be empty")
assert.Empty(t, collection[0].IP, "ip should be empty")
assert.NotEmpty(t, collection[0].Timestamp, "timestamp should not be empty")
assert.NotEmpty(t, collection[0].Committer, "committer should not be empty")
assert.Empty(t, collection[0].Context, "context should be empty")
assert.Equal(t, collection[0].Data, json.RawMessage(data), "payload")
assert.Empty(t, collection[0].Hash, "hash should be empty")
}
func TestGetSessionToken(t *testing.T) {
// setup fake http server for test
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := fmt.Fprint(w, "{\"token\":\"c0e7aa16-43e1-45a9-abc6-b90c8155a3a7::8066a69b-f6f9-4d65-84b4-691a13f1346a\",\"validity\":1546127294284,\"expiresAt\":\"Tuesday, January 1, 2019 0:00:00 AM UTC\",\"roles\":[]}")
if err != nil {
t.Error(err.Error())
}
}))
defer ts.Close()
config.Vrops.Host = ts.URL
assert.NotEmpty(t, getSessionToken())
}