summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/openvpn_status_log/openvpn_test.go
blob: 1e6071e01a923da92750e2d744bc201e498c46f3 (plain)
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
// SPDX-License-Identifier: GPL-3.0-or-later

package openvpn_status_log

import (
	"os"
	"strings"
	"testing"

	"github.com/netdata/netdata/go/go.d.plugin/agent/module"
	"github.com/netdata/netdata/go/go.d.plugin/pkg/matcher"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

const (
	pathNonExistentFile         = "testdata/v2.5.1/non-existent.txt"
	pathEmptyFile               = "testdata/v2.5.1/empty.txt"
	pathStaticKey               = "testdata/v2.5.1/static-key.txt"
	pathStatusVersion1          = "testdata/v2.5.1/version1.txt"
	pathStatusVersion1NoClients = "testdata/v2.5.1/version1-no-clients.txt"
	pathStatusVersion2          = "testdata/v2.5.1/version2.txt"
	pathStatusVersion2NoClients = "testdata/v2.5.1/version2-no-clients.txt"
	pathStatusVersion3          = "testdata/v2.5.1/version3.txt"
	pathStatusVersion3NoClients = "testdata/v2.5.1/version3-no-clients.txt"
)

var (
	dataConfigJSON, _ = os.ReadFile("testdata/config.json")
	dataConfigYAML, _ = os.ReadFile("testdata/config.yaml")
)

func Test_testDataIsValid(t *testing.T) {
	for name, data := range map[string][]byte{
		"dataConfigJSON": dataConfigJSON,
		"dataConfigYAML": dataConfigYAML,
	} {
		require.NotNil(t, data, name)
	}
}

func TestOpenVPNStatusLog_ConfigurationSerialize(t *testing.T) {
	module.TestConfigurationSerialize(t, &OpenVPNStatusLog{}, dataConfigJSON, dataConfigYAML)
}

func TestOpenVPNStatusLog_Init(t *testing.T) {
	tests := map[string]struct {
		config   Config
		wantFail bool
	}{
		"default config": {
			config: New().Config,
		},
		"unset 'log_path'": {
			wantFail: true,
			config: Config{
				LogPath: "",
			},
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			ovpn := New()
			ovpn.Config = test.config

			if test.wantFail {
				assert.Error(t, ovpn.Init())
			} else {
				assert.NoError(t, ovpn.Init())
			}
		})
	}
}

func TestOpenVPNStatusLog_Check(t *testing.T) {
	tests := map[string]struct {
		prepare  func() *OpenVPNStatusLog
		wantFail bool
	}{
		"status version 1":                 {prepare: prepareCaseStatusVersion1},
		"status version 1 with no clients": {prepare: prepareCaseStatusVersion1NoClients},
		"status version 2":                 {prepare: prepareCaseStatusVersion2},
		"status version 2 with no clients": {prepare: prepareCaseStatusVersion2NoClients},
		"status version 3":                 {prepare: prepareCaseStatusVersion3},
		"status version 3 with no clients": {prepare: prepareCaseStatusVersion3NoClients},
		"empty file":                       {prepare: prepareCaseEmptyFile, wantFail: true},
		"non-existent file":                {prepare: prepareCaseNonExistentFile, wantFail: true},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			ovpn := test.prepare()

			require.NoError(t, ovpn.Init())

			if test.wantFail {
				assert.Error(t, ovpn.Check())
			} else {
				assert.NoError(t, ovpn.Check())
			}
		})
	}
}

func TestOpenVPNStatusLog_Charts(t *testing.T) {
	tests := map[string]struct {
		prepare       func() *OpenVPNStatusLog
		wantNumCharts int
	}{
		"status version 1 with user stats": {
			prepare:       prepareCaseStatusVersion1WithUserStats,
			wantNumCharts: len(charts) + len(userCharts)*2,
		},
		"status version 2 with user stats": {
			prepare:       prepareCaseStatusVersion2WithUserStats,
			wantNumCharts: len(charts) + len(userCharts)*2,
		},
		"status version 3 with user stats": {
			prepare:       prepareCaseStatusVersion2WithUserStats,
			wantNumCharts: len(charts) + len(userCharts)*2,
		},
		"status version with static key": {
			prepare:       prepareCaseStatusStaticKey,
			wantNumCharts: len(charts),
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			ovpn := test.prepare()

			require.NoError(t, ovpn.Init())
			_ = ovpn.Check()
			_ = ovpn.Collect()

			assert.Equal(t, test.wantNumCharts, len(*ovpn.Charts()))
		})
	}
}

func TestOpenVPNStatusLog_Collect(t *testing.T) {
	tests := map[string]struct {
		prepare  func() *OpenVPNStatusLog
		expected map[string]int64
	}{
		"status version 1": {
			prepare: prepareCaseStatusVersion1,
			expected: map[string]int64{
				"bytes_in":  6168,
				"bytes_out": 6369,
				"clients":   2,
			},
		},
		"status version 1 with user stats": {
			prepare: prepareCaseStatusVersion1WithUserStats,
			expected: map[string]int64{
				"bytes_in":                   6168,
				"bytes_out":                  6369,
				"clients":                    2,
				"vpnclient2_bytes_in":        3084,
				"vpnclient2_bytes_out":       3184,
				"vpnclient2_connection_time": 63793143069,
				"vpnclient_bytes_in":         3084,
				"vpnclient_bytes_out":        3185,
				"vpnclient_connection_time":  63793143069,
			},
		},
		"status version 1 with no clients": {
			prepare: prepareCaseStatusVersion1NoClients,
			expected: map[string]int64{
				"bytes_in":  0,
				"bytes_out": 0,
				"clients":   0,
			},
		},
		"status version 2": {
			prepare: prepareCaseStatusVersion2,
			expected: map[string]int64{
				"bytes_in":  6241,
				"bytes_out": 6369,
				"clients":   2,
			},
		},
		"status version 2 with user stats": {
			prepare: prepareCaseStatusVersion2WithUserStats,
			expected: map[string]int64{
				"bytes_in":                   6241,
				"bytes_out":                  6369,
				"clients":                    2,
				"vpnclient2_bytes_in":        3157,
				"vpnclient2_bytes_out":       3184,
				"vpnclient2_connection_time": 264610,
				"vpnclient_bytes_in":         3084,
				"vpnclient_bytes_out":        3185,
				"vpnclient_connection_time":  264609,
			},
		},
		"status version 2 with no clients": {
			prepare: prepareCaseStatusVersion2NoClients,
			expected: map[string]int64{
				"bytes_in":  0,
				"bytes_out": 0,
				"clients":   0,
			},
		},
		"status version 3": {
			prepare: prepareCaseStatusVersion3,
			expected: map[string]int64{
				"bytes_in":  7308,
				"bytes_out": 7235,
				"clients":   2,
			},
		},
		"status version 3 with user stats": {
			prepare: prepareCaseStatusVersion3WithUserStats,
			expected: map[string]int64{
				"bytes_in":                   7308,
				"bytes_out":                  7235,
				"clients":                    2,
				"vpnclient2_bytes_in":        3654,
				"vpnclient2_bytes_out":       3617,
				"vpnclient2_connection_time": 265498,
				"vpnclient_bytes_in":         3654,
				"vpnclient_bytes_out":        3618,
				"vpnclient_connection_time":  265496,
			},
		},
		"status version 3 with no clients": {
			prepare: prepareCaseStatusVersion3NoClients,
			expected: map[string]int64{
				"bytes_in":  0,
				"bytes_out": 0,
				"clients":   0,
			},
		},
		"status with static key": {
			prepare: prepareCaseStatusStaticKey,
			expected: map[string]int64{
				"bytes_in":  19265,
				"bytes_out": 261631,
				"clients":   0,
			},
		},
		"empty file": {
			prepare:  prepareCaseEmptyFile,
			expected: nil,
		},
		"non-existent file": {
			prepare:  prepareCaseNonExistentFile,
			expected: nil,
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			ovpn := test.prepare()

			require.NoError(t, ovpn.Init())
			_ = ovpn.Check()

			collected := ovpn.Collect()

			copyConnTime(collected, test.expected)
			assert.Equal(t, test.expected, collected)
		})
	}
}

func prepareCaseStatusVersion1() *OpenVPNStatusLog {
	ovpn := New()
	ovpn.LogPath = pathStatusVersion1
	return ovpn
}

func prepareCaseStatusVersion1WithUserStats() *OpenVPNStatusLog {
	ovpn := New()
	ovpn.LogPath = pathStatusVersion1
	ovpn.PerUserStats = matcher.SimpleExpr{
		Includes: []string{"* *"},
	}
	return ovpn
}

func prepareCaseStatusVersion1NoClients() *OpenVPNStatusLog {
	ovpn := New()
	ovpn.LogPath = pathStatusVersion1NoClients
	return ovpn
}

func prepareCaseStatusVersion2() *OpenVPNStatusLog {
	ovpn := New()
	ovpn.LogPath = pathStatusVersion2
	return ovpn
}

func prepareCaseStatusVersion2WithUserStats() *OpenVPNStatusLog {
	ovpn := New()
	ovpn.LogPath = pathStatusVersion2
	ovpn.PerUserStats = matcher.SimpleExpr{
		Includes: []string{"* *"},
	}
	return ovpn
}

func prepareCaseStatusVersion2NoClients() *OpenVPNStatusLog {
	ovpn := New()
	ovpn.LogPath = pathStatusVersion2NoClients
	return ovpn
}

func prepareCaseStatusVersion3() *OpenVPNStatusLog {
	ovpn := New()
	ovpn.LogPath = pathStatusVersion3
	return ovpn
}

func prepareCaseStatusVersion3WithUserStats() *OpenVPNStatusLog {
	ovpn := New()
	ovpn.LogPath = pathStatusVersion3
	ovpn.PerUserStats = matcher.SimpleExpr{
		Includes: []string{"* *"},
	}
	return ovpn
}

func prepareCaseStatusVersion3NoClients() *OpenVPNStatusLog {
	ovpn := New()
	ovpn.LogPath = pathStatusVersion3NoClients
	return ovpn
}

func prepareCaseStatusStaticKey() *OpenVPNStatusLog {
	ovpn := New()
	ovpn.LogPath = pathStaticKey
	return ovpn
}

func prepareCaseEmptyFile() *OpenVPNStatusLog {
	ovpn := New()
	ovpn.LogPath = pathEmptyFile
	return ovpn
}

func prepareCaseNonExistentFile() *OpenVPNStatusLog {
	ovpn := New()
	ovpn.LogPath = pathNonExistentFile
	return ovpn
}

func copyConnTime(dst, src map[string]int64) {
	for k, v := range src {
		if !strings.HasSuffix(k, "connection_time") {
			continue
		}
		if _, ok := dst[k]; !ok {
			continue
		}
		dst[k] = v
	}
}