-
Notifications
You must be signed in to change notification settings - Fork 6
/
poe_set_port_test.go
227 lines (184 loc) · 8.89 KB
/
poe_set_port_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
package main
import (
"github.com/corbym/gocrest/is"
"github.com/corbym/gocrest/then"
"strings"
"testing"
)
var poeExt = &PoeExt{
Hash: "4f11f5d64ef3fd75a92a9f2ad1de3060",
PortMaxPower: "30.0",
}
func TestFindHashInHtml(t *testing.T) {
tests := []struct {
model string
fileName string
expectedVal string
}{
{
model: "GS305EP",
fileName: "PoEPortConfig.cgi.html",
expectedVal: "4f11f5d64ef3fd75a92a9f2ad1de3060",
},
{
model: "GS308EPP",
fileName: "PoEPortConfig.cgi.html",
expectedVal: "5c183d939eee1c74c1bb9055ec82d2d6",
},
}
for _, test := range tests {
t.Run(test.model, func(t *testing.T) {
hash, err := findHashInHtml("", strings.NewReader(loadTestFile(test.model, test.fileName)))
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, hash, is.EqualTo(test.expectedVal))
})
}
}
func TestFindMaxPoePowerLimit(t *testing.T) {
tests := []struct {
model string
fileName string
expectedVal string
}{
{
model: "GS305EP",
fileName: "PoEPortConfig.cgi.html",
expectedVal: "30.0",
},
{
model: "GS308EPP",
fileName: "PoEPortConfig.cgi.html",
expectedVal: "30.0",
},
}
for _, test := range tests {
t.Run(test.model, func(t *testing.T) {
pwrLimit, err := findMaxPwrLimitInHtml(strings.NewReader(loadTestFile(test.model, test.fileName)))
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, pwrLimit, is.EqualTo(test.expectedVal))
})
}
}
func TestComparePoeSettingsUnknown(t *testing.T) {
for _, setting := range []Setting{PortPrio, PwrMode, LimitType, DetecType, LongerDetect} {
setting, _ := comparePoeSettings(setting, "defaultValue", "newValue", poeExt)
then.AssertThat(t, setting, is.EqualTo("unknown").Reason("when providing a value that does not exist, return unknown to the caller"))
}
}
func TestComparePoeSettingsPwrLimit(t *testing.T) {
pwrLimit, err := comparePoeSettings(PwrLimit, "3.0", "30.0", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, pwrLimit, is.EqualTo("30.0").Reason("allow values up to the maximum power in PortMaxPower"))
pwrLimitDefault, err := comparePoeSettings(PwrLimit, "15.0", "15.0", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, pwrLimitDefault, is.EqualTo("15.0").Reason("pass the default back if user did not change value"))
pwrLimitOutOfRange, _ := comparePoeSettings(PwrLimit, "30.0", "99999999.0", poeExt)
then.AssertThat(t, pwrLimitOutOfRange, is.EqualTo("30.0").Reason("use the default value if power limit is out of range"))
pwrLimitMidRange, _ := comparePoeSettings(PwrLimit, "30.0", "15", poeExt)
then.AssertThat(t, pwrLimitMidRange, is.EqualTo("15").Reason("integer values should work"))
}
func TestComparePoePortPrio(t *testing.T) {
setting, err := comparePoeSettings(PortPrio, "critical", "low", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, setting, is.EqualTo("0").Reason("allow user to change port priority to low"))
setting, err = comparePoeSettings(PortPrio, "low", "critical", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, setting, is.EqualTo("3").Reason("allow user to change port priority to critical"))
setting, err = comparePoeSettings(PortPrio, "low", "high", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, setting, is.EqualTo("2").Reason("allow user to change port priority to high"))
setting, err = comparePoeSettings(PortPrio, "low", "low", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, setting, is.EqualTo("0").Reason("maintain the same port priority"))
setting, err = comparePoeSettings(PortPrio, "0", "", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, setting, is.EqualTo("0").Reason("maintain the prior value when new nothing is specified"))
}
func TestComparePoePwrMode(t *testing.T) {
setting, err := comparePoeSettings(PwrMode, "802.3af", "legacy", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, setting, is.EqualTo("1").Reason("allow user to change the power mode to legacy"))
setting, err = comparePoeSettings(PwrMode, "legacy", "pre-802.3at", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, setting, is.EqualTo("2").Reason("allow user to change the power mode to pre-802.3at"))
setting, err = comparePoeSettings(PwrMode, "pre-802.3at", "802.3at", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, setting, is.EqualTo("3").Reason("allow user to change the power mode to 802.3at"))
setting, err = comparePoeSettings(PwrMode, "802.3af", "802.3af", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, setting, is.EqualTo("0").Reason("maintain the same power mode"))
setting, err = comparePoeSettings(PwrMode, "0", "", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, setting, is.EqualTo("0").Reason("maintain the prior value when nothing new is specified"))
}
func TestComparePoeLimitType(t *testing.T) {
setting, err := comparePoeSettings(LimitType, "user", "none", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, setting, is.EqualTo("0").Reason("allow user to change the limit type to none"))
setting, err = comparePoeSettings(LimitType, "none", "class", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, setting, is.EqualTo("1").Reason("allow user to change the limit type to class"))
setting, err = comparePoeSettings(LimitType, "class", "user", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, setting, is.EqualTo("2").Reason("allow user to change the limit type to user"))
setting, err = comparePoeSettings(LimitType, "user", "user", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, setting, is.EqualTo("2").Reason("maintain the same limit type"))
setting, err = comparePoeSettings(LimitType, "2", "", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, setting, is.EqualTo("2").Reason("maintain the prior value when nothing new is specified"))
}
func TestComparePoeDetecType(t *testing.T) {
setting, err := comparePoeSettings(DetecType, "IEEE 802", "Legacy", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, setting, is.EqualTo("1").Reason("allow user to change the detect type to Legacy"))
setting, err = comparePoeSettings(DetecType, "Legacy", "4pt 802.3af + Legacy", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, setting, is.EqualTo("3").Reason("allow user to change the detect type to 4pt 802.3af + Legacy"))
setting, err = comparePoeSettings(DetecType, "4pt 802.3af + Legacy", "IEEE 802", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, setting, is.EqualTo("2").Reason("allow user to change the detect type to IEEE 802"))
setting, err = comparePoeSettings(DetecType, "IEEE 802", "IEEE 802", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, setting, is.EqualTo("2").Reason("maintain the same detect type"))
setting, err = comparePoeSettings(DetecType, "1", "", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, setting, is.EqualTo("1").Reason("maintain the prior value when nothing new is specified"))
}
func TestComparePoeLongerDetect(t *testing.T) {
setting, err := comparePoeSettings(LongerDetect, "Get Value Fault", "disable", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, setting, is.EqualTo("2").Reason("allow user to change the longer detection time to Disable from Get Value Fault"))
setting, err = comparePoeSettings(LongerDetect, "Get Value Fault", "enable", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, setting, is.EqualTo("3").Reason("allow user to change the longer detection time to Enable from Get Value Fault"))
setting, err = comparePoeSettings(LongerDetect, "enable", "disable", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, setting, is.EqualTo("2").Reason("allow user to change the longer detection time to Disable"))
setting, err = comparePoeSettings(LongerDetect, "disable", "enable", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, setting, is.EqualTo("3").Reason("allow user to change the longer detection time to Enable"))
setting, err = comparePoeSettings(LongerDetect, "enable", "", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, setting, is.EqualTo("enable").Reason("maintain the same longer detect type when nothing new is specified"))
setting, err = comparePoeSettings(LongerDetect, "2", "", poeExt)
then.AssertThat(t, err, is.Nil())
then.AssertThat(t, setting, is.EqualTo("2").Reason("maintain the same longer detect type when nothing new is specified"))
}
func TestCollectChangedPoePortConfiguration(t *testing.T) {
var ports = []int{1, 2}
var settings = []PoePortSetting{
PoePortSetting{
PortIndex: 1,
PortName: "port 1",
},
PoePortSetting{
PortIndex: 2,
PortName: "port 2",
},
}
changed := collectChangedPoePortConfiguration(ports, settings)
then.AssertThat(t, len(changed), is.EqualTo(2))
then.AssertThat(t, int(changed[1].PortIndex), is.EqualTo(2))
then.AssertThat(t, changed[0].PortName, is.EqualTo("port 1"))
}