forked from stmcginnis/gofish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchassis_test.go
372 lines (314 loc) · 9.39 KB
/
chassis_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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
//
// SPDX-License-Identifier: BSD-3-Clause
//
package redfish
import (
"bytes"
"encoding/json"
"io"
"net/http"
"strings"
"testing"
"github.com/stmcginnis/gofish/common"
)
const TestAssetTag = "TestAssetTag"
const TestChassisPath = "/redfish/v1/Chassis/Chassis-1"
var chassisBody = `{
"@odata.context": "/redfish/v1/$metadata#Chassis.Chassis",
"@odata.id": "/redfish/v1/Chassis/Chassis-1",
"@odata.type": "#Chassis.v1_0_0.Chassis",
"Id": "Chassis-1",
"Name": "Computer System Chassis",
"ChassisType": "RackMount",
"Manufacturer": "Redfish Computers",
"Model": "3500RX",
"SKU": "8675309",
"SerialNumber": "437XR1138R2",
"Version": "1.02",
"PartNumber": "224071-J23",
"AssetTag": "Chicago-45Z-2381",
"Status": {
"State": "Enabled",
"Health": "OK"
},
"Assembly": {
"@odata.id": "/redfish/v1/Chassis/Chassis-1/Assembly"
},
"Controls": {
"@odata.id": "/redfish/v1/Chassis/Chassis-1/Controls"
},
"Drives": {
"@odata.id": "/redfish/v1/Chassis/Chassis-1/Drives"
},
"Thermal": {
"@odata.id": "/redfish/v1/Chassis/Chassis-1/Thermal"
},
"Power": {
"@odata.id": "/redfish/v1/Chassis/Chassis-1/Power"
},
"Links": {
"ComputerSystems": [
{
"@odata.id": "/redfish/v1/Systems/System-1"
}
],
"ResourceBlocks": [],
"ManagedBy": [
{
"@odata.id": "/redfish/v1/Managers/BMC-1"
}
]
},
"Actions": {
"#Chassis.Reset": {
"target": "/redfish/v1/Chassis/System.Embedded.1/Actions/Chassis.Reset",
"[email protected]": [
"On",
"ForceOff"
]
}
}
}`
var supermicroRAIDChassisBody = `{
"@odata.type": "#Chassis.v1_9_1.Chassis",
"@odata.id": "/redfish/v1/Chassis/HA-RAID.0.StorageEnclosure.0",
"Id": "HA-RAID.0.StorageEnclosure.0",
"Name": "Internal Enclosure 0",
"ChassisType": "Enclosure",
"Model": "Internal Enclosure",
"SerialNumber": "",
"PartNumber": "",
"Links": {
"ManagedBy": [
{
"@odata.id": "/redfish/v1/Managers/1"
}
],
"Storage": [
{
"@odata.id": "/redfish/v1/Systems/1/Storage/HA-RAID"
}
],
"Drives": [
{
"@odata.id": "/redfish/v1/Chassis/HA-RAID.0.StorageEnclosure.0/Drives/Disk.Bay.0"
},
{
"@odata.id": "/redfish/v1/Chassis/HA-RAID.0.StorageEnclosure.0/Drives/Disk.Bay.1"
},
{
"@odata.id": "/redfish/v1/Chassis/HA-RAID.0.StorageEnclosure.0/Drives/Disk.Bay.2"
},
{
"@odata.id": "/redfish/v1/Chassis/HA-RAID.0.StorageEnclosure.0/Drives/Disk.Bay.3"
}
]
},
"Oem": {}
}`
var driveCollection = `{
"@odata.id": "/redfish/v1/Chassis/IPAttachedDrive/Drives",
"@odata.type": "#DriveCollection.DriveCollection",
"Members": [
{
"@odata.id": "/redfish/v1/Chassis/IPAttachedDrive/Drives/IPAttachedDrive"
}
],
"[email protected]": 1,
"Name": "Drive Collection"
}`
// TestChassis tests the parsing of Chassis objects.
func TestChassis(t *testing.T) {
var result Chassis
err := json.NewDecoder(strings.NewReader(chassisBody)).Decode(&result)
if err != nil {
t.Errorf("Error decoding JSON: %s", err)
}
if result.ID != "Chassis-1" {
t.Errorf("Received invalid ID: %s", result.ID)
}
if result.Name != "Computer System Chassis" {
t.Errorf("Received invalid name: %s", result.Name)
}
if result.AssetTag != "Chicago-45Z-2381" {
t.Errorf("Received invalid asset tag: %s", result.AssetTag)
}
if result.ChassisType != RackMountChassisType {
t.Errorf("Received invalid chassis type: %s", result.ChassisType)
}
if result.Status.Health != common.OKHealth {
t.Errorf("Received invalid health status: %s", result.Status.Health)
}
if result.assembly != "/redfish/v1/Chassis/Chassis-1/Assembly" {
t.Errorf("Received invalid assembly reference: %s", result.assembly)
}
if result.controls != "/redfish/v1/Chassis/Chassis-1/Controls" {
t.Errorf("Received invalid controls reference: %s", result.controls)
}
if result.drives != "/redfish/v1/Chassis/Chassis-1/Drives" {
t.Errorf("Received invalid drive reference: %s", result.drives)
}
if result.thermal != "/redfish/v1/Chassis/Chassis-1/Thermal" {
t.Errorf("Received invalid thermal reference: %s", result.thermal)
}
if result.power != "/redfish/v1/Chassis/Chassis-1/Power" {
t.Errorf("Received invalid power reference: %s", result.power)
}
if len(result.computerSystems) != 1 {
t.Errorf("Expected 1 computer system, got %d", len(result.computerSystems))
}
if result.computerSystems[0] != "/redfish/v1/Systems/System-1" {
t.Errorf("Invalid computer system reference: %s", result.computerSystems[0])
}
if len(result.resourceBlocks) != 0 {
t.Errorf("Resource blocks should have been 0, got %d", len(result.resourceBlocks))
}
if len(result.managedBy) != 1 {
t.Errorf("Expected 1 managed by reference, got %d", len(result.managedBy))
}
if result.managedBy[0] != "/redfish/v1/Managers/BMC-1" {
t.Errorf("Invalid managed by reference: %s", result.managedBy[0])
}
if result.resetTarget != "/redfish/v1/Chassis/System.Embedded.1/Actions/Chassis.Reset" {
t.Errorf("Invalid reset action target: %s", result.resetTarget)
}
if len(result.SupportedResetTypes) != 2 {
t.Errorf("Invalid allowable reset actions, expected 2, got %d",
len(result.SupportedResetTypes))
}
}
// TestMinimumChassis tests a failure we had from how SM returns a RAID
// controller chassis.
//
// The required properties according to the spec are:
// "required": [
//
// "ChassisType",
// "@odata.id",
// "@odata.type",
// "Id",
// "Name"]
func TestMinimumChassis(t *testing.T) {
var result Chassis
err := json.NewDecoder(strings.NewReader(supermicroRAIDChassisBody)).Decode(&result)
if err != nil {
t.Errorf("Error decoding JSON: %s", err)
}
if result.ID != "HA-RAID.0.StorageEnclosure.0" {
t.Errorf("Received invalid ID: %s", result.ID)
}
if result.Name != "Internal Enclosure 0" {
t.Errorf("Received invalid name: %s", result.Name)
}
if result.ChassisType != EnclosureChassisType {
t.Errorf("Received invalid chassis type: %s", result.ChassisType)
}
}
// TestLinkedDriveChassis tests getting drives from versions supporting the older
// Chassis.Links.Drives location.
func TestLinkedDriveChassis(t *testing.T) {
var result Chassis
err := json.NewDecoder(strings.NewReader(supermicroRAIDChassisBody)).Decode(&result)
if err != nil {
t.Errorf("Error decoding JSON: %s", err)
}
if len(result.linkedDrives) != 4 {
t.Errorf("Expected 3 drive links: %v", result.linkedDrives)
}
if result.drives != "" {
t.Errorf("Expected drives link to be empty, got %q", result.drives)
}
}
// TestChassisUpdate tests the Update call.
func TestChassisUpdate(t *testing.T) {
var result Chassis
err := json.NewDecoder(strings.NewReader(chassisBody)).Decode(&result)
if err != nil {
t.Errorf("Error decoding JSON: %s", err)
}
testClient := &common.TestClient{}
result.SetClient(testClient)
result.AssetTag = TestAssetTag
err = result.Update()
if err != nil {
t.Errorf("Error making Update call: %s", err)
}
calls := testClient.CapturedCalls()
if len(calls) != 1 {
t.Errorf("Expected one call to be made, captured: %v", calls)
}
if !strings.Contains(calls[0].Payload, result.AssetTag) {
t.Errorf("Unexpected update payload: %s", calls[0].Payload)
}
}
// getCall returns an http.Response for a GET request.
func getCall(body string) *http.Response {
return &http.Response{
Status: "200 OK",
StatusCode: 200,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Body: io.NopCloser(bytes.NewBufferString(body)),
ContentLength: int64(len(body)),
Header: make(http.Header),
}
}
// TestChassisDrives tests getting the drives for a chassis.
func TestChassisDrives(t *testing.T) {
var result Chassis
err := json.NewDecoder(strings.NewReader(chassisBody)).Decode(&result)
if err != nil {
t.Errorf("Error decoding JSON: %s", err)
}
testClient := &common.TestClient{
CustomReturnForActions: map[string][]interface{}{
http.MethodGet: {
getCall(driveCollection), //nolint
getCall(driveBody), //nolint
},
},
}
result.SetClient(testClient)
drives, err := result.Drives()
if err != nil {
t.Errorf("Error getting drives: %s", err)
}
calls := testClient.CapturedCalls()
if len(calls) != 2 {
t.Errorf("Expected two calls to be made, captured: %v", calls)
}
if len(drives) != 1 {
t.Errorf("Expected 1 drive to be returned, got %d", len(drives))
}
}
// TestChassisLinkedDrives tests getting the drives returned through the links for a chassis.
func TestChassisLinkedDrives(t *testing.T) {
var result Chassis
err := json.NewDecoder(strings.NewReader(supermicroRAIDChassisBody)).Decode(&result)
if err != nil {
t.Errorf("Error decoding JSON: %s", err)
}
testClient := &common.TestClient{
CustomReturnForActions: map[string][]interface{}{
http.MethodGet: {
getCall(driveBody), //nolint
getCall(driveBody), //nolint
getCall(driveBody), //nolint
getCall(driveBody), //nolint
},
},
}
result.SetClient(testClient)
drives, err := result.Drives()
if err != nil {
t.Errorf("Error getting drives: %s", err)
}
calls := testClient.CapturedCalls()
if len(calls) != 4 {
t.Errorf("Expected four calls to be made, captured: %v", calls)
}
if len(drives) != 4 {
t.Errorf("Expected 4 drives to be returned, got %d", len(drives))
}
}