forked from stmcginnis/gofish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattery_test.go
142 lines (117 loc) · 3.46 KB
/
battery_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
//
// SPDX-License-Identifier: BSD-3-Clause
//
package redfish
import (
"encoding/json"
"strings"
"testing"
"github.com/stmcginnis/gofish/common"
)
var batteryBody = `{
"@odata.type": "#Battery.v1_2_2.Battery",
"Id": "Module1",
"Name": "Battery 1",
"Status": {
"State": "Enabled",
"Health": "OK"
},
"Actions": {
"#Battery.SelfTest": {
"target": "/redfish/v1/Chassis/1U/PowerSubsystem/Batteries/Module1/Actions/Battery.SelfTest"
},
"#Battery.Calibrate": {
"target": "/redfish/v1/Chassis/1U/PowerSubsystem/Batteries/Module1/Actions/Battery.Calibrate"
}
},
"Location": {
"PartLocation": {
"ServiceLabel": "Battery 1",
"LocationType": "Bay",
"LocationOrdinalValue": 0
}
},
"Model": "RKS-440DC",
"Manufacturer": "Contoso Power",
"FirmwareVersion": "1.00",
"Version": "A05",
"ProductionDate": "2019-10-01T06:00:00Z",
"SerialNumber": "3488247",
"PartNumber": "23456-133",
"SparePartNumber": "93284-133",
"LocationIndicatorActive": false,
"HotPluggable": true,
"CapacityRatedWattHours": 20,
"CapacityActualWattHours": 19.41,
"MaxDischargeRateAmps": 10,
"StateOfHealthPercent": {
"DataSourceUri": "/redfish/v1/Chassis/1U/Sensors/Battery1StateOfHealth",
"Reading": 91
},
"ChargeState": "Idle",
"Metrics": {
"@odata.id": "/redfish/v1/Chassis/1U/PowerSubsystem/Batteries/Module1/Metrics"
},
"@odata.id": "/redfish/v1/Chassis/1U/PowerSubsystem/Batteries/Module1"
}`
// TestBattery tests the parsing of Battery objects.
func TestBattery(t *testing.T) {
var result Battery
err := json.NewDecoder(strings.NewReader(batteryBody)).Decode(&result)
if err != nil {
t.Errorf("Error decoding JSON: %s", err)
}
assertEquals(t, "Module1", result.ID)
assertEquals(t, "Battery 1", result.Name)
assertEquals(t, "RKS-440DC", result.Model)
assertEquals(t, "/redfish/v1/Chassis/1U/PowerSubsystem/Batteries/Module1/Metrics", result.metrics)
assertEquals(t, "Idle", string(result.ChargeState))
if !result.HotPluggable {
t.Error("Expected Hot Pluggable to be true")
}
if result.StateOfHealthPercent.Reading != 91 {
t.Errorf("Expected state of health percent reading to be 91, got %.0f", result.StateOfHealthPercent.Reading)
}
}
// TestBatterySelfTest tests the Battery SelfTest call.
func TestBatterySelfTest(t *testing.T) {
var result Battery
err := json.NewDecoder(strings.NewReader(batteryBody)).Decode(&result)
if err != nil {
t.Errorf("Error decoding JSON: %s", err)
}
testClient := &common.TestClient{}
result.SetClient(testClient)
err = result.SelfTest()
if err != nil {
t.Errorf("Error making Battery SelfTest 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, "") {
t.Errorf("Unexpected self test payload: %s", calls[0].Payload)
}
}
// TestBatteryCalibrate tests the Battery Calibrate call.
func TestBatteryCalibrate(t *testing.T) {
var result Battery
err := json.NewDecoder(strings.NewReader(batteryBody)).Decode(&result)
if err != nil {
t.Errorf("Error decoding JSON: %s", err)
}
testClient := &common.TestClient{}
result.SetClient(testClient)
err = result.Calibrate()
if err != nil {
t.Errorf("Error making Calibbrate 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, "") {
t.Errorf("Unexpected calibrate payload: %s", calls[0].Payload)
}
}