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
|
// SPDX-License-Identifier: GPL-3.0-or-later
package dnsmasq_dhcp
import (
"os"
"testing"
"github.com/netdata/netdata/go/plugins/plugin/go.d/agent/module"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
dataConfigJSON, _ = os.ReadFile("testdata/config.json")
dataConfigYAML, _ = os.ReadFile("testdata/config.yaml")
)
const (
testLeasesPath = "testdata/dnsmasq.leases"
testConfPath = "testdata/dnsmasq.conf"
testConfDir = "testdata/dnsmasq.d"
)
func Test_testDataIsValid(t *testing.T) {
for name, data := range map[string][]byte{
"dataConfigJSON": dataConfigJSON,
"dataConfigYAML": dataConfigYAML,
} {
require.NotNil(t, data, name)
}
}
func TestDnsmasqDHCP_ConfigurationSerialize(t *testing.T) {
module.TestConfigurationSerialize(t, &DnsmasqDHCP{}, dataConfigJSON, dataConfigYAML)
}
func TestDnsmasqDHCP_Init(t *testing.T) {
job := New()
job.LeasesPath = testLeasesPath
job.ConfPath = testConfPath
job.ConfDir = testConfDir
assert.NoError(t, job.Init())
}
func TestDnsmasqDHCP_InitEmptyLeasesPath(t *testing.T) {
job := New()
job.LeasesPath = ""
assert.Error(t, job.Init())
}
func TestDnsmasqDHCP_InitInvalidLeasesPath(t *testing.T) {
job := New()
job.LeasesPath = testLeasesPath
job.LeasesPath += "!"
assert.Error(t, job.Init())
}
func TestDnsmasqDHCP_InitZeroDHCPRanges(t *testing.T) {
job := New()
job.LeasesPath = testLeasesPath
job.ConfPath = "testdata/dnsmasq3.conf"
job.ConfDir = ""
assert.NoError(t, job.Init())
}
func TestDnsmasqDHCP_Check(t *testing.T) {
job := New()
job.LeasesPath = testLeasesPath
job.ConfPath = testConfPath
job.ConfDir = testConfDir
require.NoError(t, job.Init())
assert.NoError(t, job.Check())
}
func TestDnsmasqDHCP_Charts(t *testing.T) {
job := New()
job.LeasesPath = testLeasesPath
job.ConfPath = testConfPath
job.ConfDir = testConfDir
require.NoError(t, job.Init())
assert.NotNil(t, job.Charts())
}
func TestDnsmasqDHCP_Cleanup(t *testing.T) {
assert.NotPanics(t, New().Cleanup)
}
func TestDnsmasqDHCP_Collect(t *testing.T) {
job := New()
job.LeasesPath = testLeasesPath
job.ConfPath = testConfPath
job.ConfDir = testConfDir
require.NoError(t, job.Init())
require.NoError(t, job.Check())
expected := map[string]int64{
"dhcp_range_1230::1-1230::64_allocated_leases": 7,
"dhcp_range_1230::1-1230::64_utilization": 7,
"dhcp_range_1231::1-1231::64_allocated_leases": 1,
"dhcp_range_1231::1-1231::64_utilization": 1,
"dhcp_range_1232::1-1232::64_allocated_leases": 1,
"dhcp_range_1232::1-1232::64_utilization": 1,
"dhcp_range_1233::1-1233::64_allocated_leases": 1,
"dhcp_range_1233::1-1233::64_utilization": 1,
"dhcp_range_1234::1-1234::64_allocated_leases": 1,
"dhcp_range_1234::1-1234::64_utilization": 1,
"dhcp_range_192.168.0.1-192.168.0.100_allocated_leases": 6,
"dhcp_range_192.168.0.1-192.168.0.100_utilization": 6,
"dhcp_range_192.168.1.1-192.168.1.100_allocated_leases": 5,
"dhcp_range_192.168.1.1-192.168.1.100_utilization": 5,
"dhcp_range_192.168.2.1-192.168.2.100_allocated_leases": 4,
"dhcp_range_192.168.2.1-192.168.2.100_utilization": 4,
"dhcp_range_192.168.200.1-192.168.200.100_allocated_leases": 1,
"dhcp_range_192.168.200.1-192.168.200.100_utilization": 1,
"dhcp_range_192.168.3.1-192.168.3.100_allocated_leases": 1,
"dhcp_range_192.168.3.1-192.168.3.100_utilization": 1,
"dhcp_range_192.168.4.1-192.168.4.100_allocated_leases": 1,
"dhcp_range_192.168.4.1-192.168.4.100_utilization": 1,
"ipv4_dhcp_hosts": 6,
"ipv4_dhcp_ranges": 6,
"ipv6_dhcp_hosts": 5,
"ipv6_dhcp_ranges": 5,
}
assert.Equal(t, expected, job.Collect())
}
func TestDnsmasqDHCP_CollectFailedToOpenLeasesPath(t *testing.T) {
job := New()
job.LeasesPath = testLeasesPath
job.ConfPath = testConfPath
job.ConfDir = testConfDir
require.NoError(t, job.Init())
require.NoError(t, job.Check())
job.LeasesPath = ""
assert.Nil(t, job.Collect())
}
func TestDnsmasqDHCP_parseDHCPRangeValue(t *testing.T) {
tests := map[string]struct {
input string
wantFail bool
}{
"ipv4": {
input: "192.168.0.50,192.168.0.150,12h",
},
"ipv4 with netmask": {
input: "192.168.0.50,192.168.0.150,255.255.255.0,12h",
},
"ipv4 with netmask and tag": {
input: "set:red,1.1.1.50,1.1.2.150, 255.255.252.0",
},
"ipv4 with iface": {
input: "enp3s0, 172.16.1.2, 172.16.1.254, 1h",
},
"ipv4 with iface 2": {
input: "enp2s0.100, 192.168.100.2, 192.168.100.254, 1h",
},
"ipv4 static": {
wantFail: true,
input: "192.168.0.0,static",
},
"ipv6": {
input: "1234::2,1234::500",
},
"ipv6 slacc": {
input: "1234::2,1234::500, slaac",
},
"ipv6 with with prefix length and lease time": {
input: "1234::2,1234::500, 64, 12h",
},
"ipv6 ra-only": {
wantFail: true,
input: "1234::,ra-only",
},
"ipv6 ra-names": {
wantFail: true,
input: "1234::,ra-names",
},
"ipv6 ra-stateless": {
wantFail: true,
input: "1234::,ra-stateless",
},
"invalid": {
wantFail: true,
input: "192.168.0.0",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
v := parseDHCPRangeValue(test.input)
if test.wantFail {
assert.Emptyf(t, v, "parsing '%s' must fail", test.input)
} else {
assert.NotEmptyf(t, v, "parsing '%s' must not fail", test.input)
}
})
}
}
|