-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector_test.go
252 lines (212 loc) · 7.34 KB
/
collector_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
package main
import (
"context"
"errors"
"io"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/tetafro/connectbox"
"go.uber.org/mock/gomock"
)
func TestNewCollector(t *testing.T) {
c := NewCollector(
3*time.Second,
map[string]ConnectBox{"test": &connectbox.Client{}},
)
require.Len(t, c.targets, 1)
}
func TestCollector_ServeHTTP(t *testing.T) {
log.SetOutput(io.Discard)
t.Run("success", func(t *testing.T) {
ctrl := gomock.NewController(t)
metrics := NewMockConnectBox(ctrl)
metrics.EXPECT().Login(gomock.Any()).Return(nil)
var cmSystemInfoData CMSystemInfo
metrics.EXPECT().Get(
gomock.Any(), FnCMSystemInfo, &cmSystemInfoData,
).Do(func(ctx context.Context, fn string, out any) error {
data := out.(*CMSystemInfo)
data.DocsisMode = "DocsisMode"
data.HardwareVersion = "HardwareVersion"
data.MacAddr = "MacAddr"
data.SerialNumber = "SerialNumber"
data.SystemUptime = 100
data.NetworkAccess = NetworkAccessAllowed
return nil
})
var lanUserTableData LANUserTable
metrics.EXPECT().Get(
gomock.Any(), FnLANUserTable, &lanUserTableData,
).Do(func(ctx context.Context, fn string, out any) error {
data := out.(*LANUserTable)
data.Ethernet = []LANUserTableClientInfo{{
Interface: "EthernetInterface",
IPv4Addr: "EthernetIPv4Addr",
Index: "EthernetIndex",
InterfaceID: "EthernetInterfaceID",
Hostname: "EthernetHostname",
MACAddr: "EthernetMACAddr",
Method: "EthernetMethod",
LeaseTime: "EthernetLeaseTime",
Speed: "EthernetSpeed",
}}
data.WIFI = []LANUserTableClientInfo{{
Interface: "WIFIInterface",
IPv4Addr: "WIFIIPv4Addr",
Index: "WIFIIndex",
InterfaceID: "WIFIInterfaceID",
Hostname: "WIFIHostname",
MACAddr: "WIFIMACAddr",
Method: "WIFIMethod",
LeaseTime: "WIFILeaseTime",
Speed: "WIFISpeed",
}}
return nil
})
var cmStateData CMState
metrics.EXPECT().Get(
gomock.Any(), FnCMState, &cmStateData,
).Do(func(ctx context.Context, fn string, out any) error {
data := out.(*CMState)
data.TunnerTemperature = 10
data.Temperature = 20
data.OperState = OperStateOK
data.WANIPv4Addr = "WANIPv4Addr"
data.WANIPv6Addrs = []string{"WANIPv6Addr"}
return nil
})
metrics.EXPECT().Logout(gomock.Any()).Return(nil)
col := &Collector{
targets: map[string]ConnectBox{
"127.0.0.1": metrics,
},
}
req, err := http.NewRequest(http.MethodGet, "/probe?target=127.0.0.1", nil)
require.NoError(t, err)
rec := httptest.NewRecorder()
col.ServeHTTP(rec, req)
require.Equal(t, http.StatusOK, rec.Code)
want := strings.Join([]string{
`# HELP connect_box_cm_docsis_mode DocSis mode.`,
`# TYPE connect_box_cm_docsis_mode gauge`,
`connect_box_cm_docsis_mode{mode="DocsisMode"} 1`,
`# HELP connect_box_cm_hardware_version Hardware version.`,
`# TYPE connect_box_cm_hardware_version gauge`,
`connect_box_cm_hardware_version{version="HardwareVersion"} 1`,
`# HELP connect_box_cm_mac_addr MAC address.`,
`# TYPE connect_box_cm_mac_addr gauge`,
`connect_box_cm_mac_addr{addr="MacAddr"} 1`,
`# HELP connect_box_cm_network_access Network access.`,
`# TYPE connect_box_cm_network_access gauge`,
`connect_box_cm_network_access 1`,
`# HELP connect_box_cm_serial_number Serial number.`,
`# TYPE connect_box_cm_serial_number gauge`,
`connect_box_cm_serial_number{sn="SerialNumber"} 1`,
`# HELP connect_box_cm_system_uptime System uptime.`,
`# TYPE connect_box_cm_system_uptime gauge`,
`connect_box_cm_system_uptime 100`,
`# HELP connect_box_lan_client LAN client.`,
`# TYPE connect_box_lan_client gauge`,
`connect_box_lan_client{` +
`connection="ethernet",hostname="EthernetHostname",` +
`interface="EthernetInterface",ipv4="EthernetIPv4Addr",` +
`mac="EthernetMACAddr"} 1`,
`connect_box_lan_client{` +
`connection="wifi",hostname="WIFIHostname",` +
`interface="WIFIInterface",ipv4="WIFIIPv4Addr",` +
`mac="WIFIMACAddr"} 1`,
`# HELP connect_box_oper_state Operational state.`,
`# TYPE connect_box_oper_state gauge`,
`connect_box_oper_state 1`,
`# HELP connect_box_temperature Temperature.`,
`# TYPE connect_box_temperature gauge`,
`connect_box_temperature 20`,
`# HELP connect_box_tunner_temperature Tunner temperature.`,
`# TYPE connect_box_tunner_temperature gauge`,
`connect_box_tunner_temperature 10`,
`# HELP connect_box_wan_ipv4_addr WAN IPv4 address.`,
`# TYPE connect_box_wan_ipv4_addr gauge`,
`connect_box_wan_ipv4_addr{ip="WANIPv4Addr"} 1`,
`# HELP connect_box_wan_ipv6_addr WAN IPv6 address.`,
`# TYPE connect_box_wan_ipv6_addr gauge`,
`connect_box_wan_ipv6_addr{ip="WANIPv6Addr"} 1`,
}, "\n") + "\n"
require.Equal(t, want, rec.Body.String())
})
t.Run("no target", func(t *testing.T) {
col := &Collector{
targets: map[string]ConnectBox{},
}
req, err := http.NewRequest(http.MethodGet, "/probe?target=127.0.0.1", nil)
require.NoError(t, err)
rec := httptest.NewRecorder()
col.ServeHTTP(rec, req)
require.Equal(t, http.StatusBadRequest, rec.Code)
})
t.Run("failed to login", func(t *testing.T) {
ctrl := gomock.NewController(t)
metrics := NewMockConnectBox(ctrl)
metrics.EXPECT().Login(gomock.Any()).Return(errors.New("fail"))
col := &Collector{
targets: map[string]ConnectBox{
"127.0.0.1": metrics,
},
}
req, err := http.NewRequest(http.MethodGet, "/probe?target=127.0.0.1", nil)
require.NoError(t, err)
rec := httptest.NewRecorder()
col.ServeHTTP(rec, req)
require.Equal(t, http.StatusInternalServerError, rec.Code)
})
t.Run("failed to get metrics", func(t *testing.T) {
ctrl := gomock.NewController(t)
metrics := NewMockConnectBox(ctrl)
metrics.EXPECT().Login(gomock.Any()).Return(nil)
metrics.EXPECT().Get(gomock.Any(), FnCMSystemInfo, gomock.Any()).
Return(errors.New("fail"))
metrics.EXPECT().Get(gomock.Any(), FnLANUserTable, gomock.Any()).
Return(errors.New("fail"))
metrics.EXPECT().Get(gomock.Any(), FnCMState, gomock.Any()).
Return(errors.New("fail"))
metrics.EXPECT().Logout(gomock.Any()).Return(nil)
col := &Collector{
targets: map[string]ConnectBox{
"127.0.0.1": metrics,
},
}
req, err := http.NewRequest(http.MethodGet, "/probe?target=127.0.0.1", nil)
require.NoError(t, err)
rec := httptest.NewRecorder()
col.ServeHTTP(rec, req)
require.Equal(t, http.StatusOK, rec.Code)
require.Equal(t, "", rec.Body.String())
})
t.Run("failed to get metrics and to logout", func(t *testing.T) {
ctrl := gomock.NewController(t)
metrics := NewMockConnectBox(ctrl)
metrics.EXPECT().Login(gomock.Any()).Return(nil)
metrics.EXPECT().Get(gomock.Any(), FnCMSystemInfo, gomock.Any()).
Return(errors.New("fail"))
metrics.EXPECT().Get(gomock.Any(), FnLANUserTable, gomock.Any()).
Return(errors.New("fail"))
metrics.EXPECT().Get(gomock.Any(), FnCMState, gomock.Any()).
Return(errors.New("fail"))
metrics.EXPECT().Logout(gomock.Any()).Return(errors.New("fail"))
col := &Collector{
targets: map[string]ConnectBox{
"127.0.0.1": metrics,
},
}
req, err := http.NewRequest(http.MethodGet, "/probe?target=127.0.0.1", nil)
require.NoError(t, err)
rec := httptest.NewRecorder()
col.ServeHTTP(rec, req)
require.Equal(t, http.StatusOK, rec.Code)
require.Equal(t, "", rec.Body.String())
})
}