forked from tinkerbell/hegel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrpc_server_test.go
320 lines (300 loc) · 10.3 KB
/
grpc_server_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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"reflect"
"testing"
"github.com/tinkerbell/tink/protos/packet"
)
func TestGetByIPCacher(t *testing.T) {
for name, test := range cacherGrpcTests {
t.Log(name)
dataModelVersion := os.Getenv("DATA_MODEL_VERSION")
defer os.Setenv("DATA_MODEL_VERSION", dataModelVersion)
os.Unsetenv("DATA_MODEL_VERSION")
hegelTestServer := &server{
log: logger,
hardwareClient: hardwareGetterMock{test.json},
}
ehw, err := getByIP(context.Background(), hegelTestServer, mockUserIP) // returns hardware data as []byte
if err != nil {
t.Fatal("unexpected error while getting hardware by ip:", err)
}
hw := exportedHardwareCacher{}
err = json.Unmarshal(ehw, &hw)
if test.error != "" {
if err == nil {
t.Fatalf("unmarshal should have returned error: %v", test.error)
} else if err.Error() != test.error {
t.Fatalf("unmarshal returned wrong error, want: %v, got: %v\n", err, test.error)
}
}
if hw.State != test.state {
t.Fatalf("unexpected state, want: %v, got: %v\n", test.state, hw.State)
}
if hw.Facility != test.facility {
t.Fatalf("unexpected facility, want: %v, got: %v\n", test.facility, hw.Facility)
}
if len(hw.NetworkPorts) > 0 && hw.NetworkPorts[0]["data"].(map[string]interface{})["mac"] != test.mac {
t.Fatalf("unexpected mac, want: %v, got: %v\n", test.mac, hw.NetworkPorts[0]["data"].(map[string]interface{})["mac"])
}
if len(hw.Instance.Storage.Disks) > 0 {
if hw.Instance.Storage.Disks[0].Device != test.diskDevice {
t.Fatalf("unexpected storage disk device, want: %v, got: %v\n", test.diskDevice, hw.Instance.Storage.Disks[0].Device)
}
if hw.Instance.Storage.Disks[0].WipeTable != test.wipeTable {
t.Fatalf("unexpected storage disk wipe table, want: %v, got: %v\n", test.wipeTable, hw.Instance.Storage.Disks[0].WipeTable)
}
t.Log("want:", test.partitionSize, " got:", hw.Instance.Storage.Disks[0].Paritions[0].Size)
if fmt.Sprintf("%v", hw.Instance.Storage.Disks[0].Paritions[0].Size) != fmt.Sprintf("%v", test.partitionSize) {
t.Fatalf("unexpected storage disk partition size, want: %v, got: %v\n", test.partitionSize, hw.Instance.Storage.Disks[0].Paritions[0].Size)
}
}
if len(hw.Instance.Storage.Filesystems) > 0 {
if hw.Instance.Storage.Filesystems[0].Mount.Device != test.filesystemDevice {
t.Fatalf("unexpected storage filesystem mount device, want: %v, got: %v\n", test.filesystemDevice, hw.Instance.Storage.Filesystems[0].Mount.Device)
}
if hw.Instance.Storage.Filesystems[0].Mount.Format != test.filesystemFormat {
t.Fatalf("unexpected storage filesystem mount format, want: %v, got: %v\n", test.filesystemFormat, hw.Instance.Storage.Filesystems[0].Mount.Format)
}
}
if test.osSlug != "" && hw.Instance.OS.OsSlug != test.osSlug {
t.Fatalf("unexpected os slug, want: %v, got: %v\n", test.osSlug, hw.Instance.OS.OsSlug)
}
if hw.PlanSlug != test.planSlug {
t.Fatalf("unexpected plan slug, want: %v, got: %v\n", test.planSlug, hw.PlanSlug)
}
}
}
func TestGetByIPTinkerbell(t *testing.T) {
dataModelVersion := os.Getenv("DATA_MODEL_VERSION")
defer os.Setenv("DATA_MODEL_VERSION", dataModelVersion)
os.Setenv("DATA_MODEL_VERSION", "1")
for name, test := range tinkerbellGrpcTests {
t.Log(name)
hegelTestServer := &server{
log: logger,
hardwareClient: hardwareGetterMock{test.json},
}
ehw, err := getByIP(context.Background(), hegelTestServer, mockUserIP) // returns hardware data as []byte
if test.error != "" {
if err == nil {
t.Fatalf("getByIP should have returned error: %v", test.error)
} else if err.Error() != test.error {
t.Fatalf("getByIP returned wrong error: got %v want %v", err, test.error)
}
}
hw := struct {
ID string `json:"id"`
Metadata packet.Metadata `json:"metadata"`
}{}
err = json.Unmarshal(ehw, &hw)
if err != nil {
t.Error("error in unmarshalling hardware metadata", err)
}
if hw.ID != test.id {
t.Errorf("handler returned unexpected id: got %v want %v",
hw.ID, test.id)
}
if reflect.DeepEqual(hw.Metadata, packet.Metadata{}) { // return if metadata is empty
return
}
if hw.Metadata.State != test.state {
t.Fatalf("unexpected state, want: %v, got: %v\n", test.state, hw.Metadata.State)
}
if hw.Metadata.BondingMode != test.bondingMode {
t.Fatalf("unexpected bonding mode, want: %v, got: %v\n", test.bondingMode, hw.Metadata.BondingMode)
}
if len(hw.Metadata.Instance.Storage.Disks) > 0 {
if hw.Metadata.Instance.Storage.Disks[0].Device != test.diskDevice {
t.Fatalf("unexpected disk device, want: %v, got: %v\n", test.diskDevice, hw.Metadata.Instance.Storage.Disks[0].Device)
}
if hw.Metadata.Instance.Storage.Disks[0].WipeTable != test.wipeTable {
t.Fatalf("unexpected wipe table, want: %v, got: %v\n", test.wipeTable, hw.Metadata.Instance.Storage.Disks[0].WipeTable)
}
if fmt.Sprintf("%v", hw.Metadata.Instance.Storage.Disks[0].Partitions[0].Size) != fmt.Sprintf("%v", test.partitionSize) {
t.Fatalf("unexpected partition size, want: %v, got: %v\n", test.partitionSize, hw.Metadata.Instance.Storage.Disks[0].Partitions[0].Size)
}
}
if len(hw.Metadata.Instance.Storage.Filesystems) > 0 {
if hw.Metadata.Instance.Storage.Filesystems[0].Mount.Device != test.filesystemDevice {
t.Fatalf("unexpected filesystem mount device, want: %v, got: %v\n", test.filesystemDevice, hw.Metadata.Instance.Storage.Filesystems[0].Mount.Device)
}
if hw.Metadata.Instance.Storage.Filesystems[0].Mount.Format != test.filesystemFormat {
t.Fatalf("unexpected filesystem mount format, want: %v, got: %v\n", test.filesystemFormat, hw.Metadata.Instance.Storage.Filesystems[0].Mount.Format)
}
}
if hw.Metadata.Instance.OperatingSystemVersion.OsSlug != test.osSlug {
t.Fatalf("unexpected os slug, want: %v, got: %v\n", test.osSlug, hw.Metadata.Instance.OperatingSystemVersion.OsSlug)
}
if hw.Metadata.Facility.PlanSlug != test.planSlug {
t.Fatalf("unexpected os slug, want: %v, got: %v\n", test.planSlug, hw.Metadata.Facility.PlanSlug)
}
}
}
func TestFilterMetadata(t *testing.T) {
for name, test := range tinkerbellFilterMetadataTests {
t.Run(name, func(t *testing.T) {
res, err := filterMetadata([]byte(test.json), test.filter)
if test.error != "" {
if err == nil {
t.Errorf("filterMetadata should have returned error: %v", test.error)
} else if err.Error() != test.error {
t.Errorf("filterMetadata returned wrong error: got %v want %v", err, test.error)
}
}
if string(res) != test.result {
t.Errorf("filterMetadata returned wrong result: got %s want %v", res, test.result)
}
})
}
}
// test cases for TestGetByIPCacher
var cacherGrpcTests = map[string]struct {
id string
state string
facility string
mac string
diskDevice string
wipeTable bool
partitionSize interface{}
filesystemDevice string
filesystemFormat string
osSlug string
planSlug string
json string
error string
}{
"cacher": {
state: "provisioning",
facility: "onprem",
mac: "98:03:9b:48:de:bc",
diskDevice: "/dev/sda",
wipeTable: true,
partitionSize: 4096,
filesystemDevice: "/dev/sda3",
filesystemFormat: "ext4",
osSlug: "ubuntu_16_04",
planSlug: "t1.small.x86",
json: cacherDataModel,
},
"cacher_partition_size_int": { // 4096
partitionSize: 4096,
json: cacherPartitionSizeInt,
},
"cacher_partition_size_string": { // "3333"
partitionSize: 3333,
json: cacherPartitionSizeString,
},
"cacher_partition_size_b_lower": { // "1000000b"
partitionSize: "1000000b",
json: cacherPartitionSizeBLower,
},
}
// test cases for TestGetByIPTinkerbell
var tinkerbellGrpcTests = map[string]struct {
id string
state string
bondingMode int64
diskDevice string
wipeTable bool
partitionSize interface{}
filesystemDevice string
filesystemFormat string
osSlug string
planSlug string
json string
error string
}{
"tinkerbell": {
id: "fde7c87c-d154-447e-9fce-7eb7bdec90c0",
bondingMode: 5,
diskDevice: "/dev/sda",
wipeTable: true,
partitionSize: 4096,
filesystemDevice: "/dev/sda3",
filesystemFormat: "ext4",
osSlug: "ubuntu_18_04",
planSlug: "c2.medium.x86",
json: tinkerbellDataModel,
},
"tinkerbell no metadata": {
id: "363115b0-f03d-4ce5-9a15-5514193d131a",
json: tinkerbellNoMetadata,
},
}
// test cases for TestFilterMetadata
var tinkerbellFilterMetadataTests = map[string]struct {
filter string
result string
error string
json string
}{
"single result (simple)": {
filter: ec2Filters["/user-data"],
result: `#!/bin/bash
echo "Hello world!"`,
json: tinkerbellFilterMetadata,
},
"single result (complex)": {
filter: ec2Filters["/meta-data/public-ipv4"],
result: "139.175.86.114",
json: tinkerbellFilterMetadata,
},
"multiple results (separated list results from hardware)": {
filter: ec2Filters["/meta-data/tags"],
result: `hello
test`,
json: tinkerbellFilterMetadata,
},
"multiple results (separated list results from filter)": {
filter: ec2Filters["/meta-data/operating-system"],
result: `distro
image_tag
license_activation
slug
version`,
json: tinkerbellFilterMetadata,
},
"multiple results (/meta-data filter with spot field present)": {
filter: ec2Filters["/meta-data"],
result: `facility
hostname
instance-id
iqn
local-ipv4
operating-system
plan
public-ipv4
public-ipv6
public-keys
spot
tags`,
json: tinkerbellFilterMetadata,
},
"invalid filter syntax": {
filter: "invalid",
error: "error while filtering with gojq: function not defined: invalid/0",
json: tinkerbellFilterMetadata,
},
"valid filter syntax, nonexistent field": {
filter: "metadata.nonexistent",
json: tinkerbellFilterMetadata,
},
"empty string filter": {
filter: "",
result: tinkerbellFilterMetadata,
json: tinkerbellFilterMetadata,
},
"list filter on nonexistent field, without '?'": {
filter: ".metadata.nonexistent[]",
error: "error while filtering with gojq: cannot iterate over: null",
json: tinkerbellFilterMetadata,
},
"list filter on nonexistent field, with '?'": {
filter: ".metadata.nonexistent[]?",
json: tinkerbellFilterMetadata,
},
}