summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/pihole/pihole_test.go
blob: 6af8267f1664c03badb23f54a08a6347106b9d5c (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
// SPDX-License-Identifier: GPL-3.0-or-later

package pihole

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"os"
	"testing"

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

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

const (
	pathSetupVarsOK    = "testdata/setupVars.conf"
	pathSetupVarsWrong = "testdata/wrong.conf"
)

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

	dataEmptyResp                     = []byte("[]")
	dataSummaryRawResp, _             = os.ReadFile("testdata/summaryRaw.json")
	dataGetQueryTypesResp, _          = os.ReadFile("testdata/getQueryTypes.json")
	dataGetForwardDestinationsResp, _ = os.ReadFile("testdata/getForwardDestinations.json")
)

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

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

func TestPihole_Init(t *testing.T) {
	tests := map[string]struct {
		wantFail bool
		config   Config
	}{
		"success with default": {
			wantFail: false,
			config:   New().Config,
		},
		"fail when URL not set": {
			wantFail: true,
			config: Config{
				HTTP: web.HTTP{
					Request: web.Request{URL: ""},
				},
			},
		},
	}

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

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

func TestPihole_Check(t *testing.T) {
	tests := map[string]struct {
		wantFail bool
		prepare  func(t *testing.T) (p *Pihole, cleanup func())
	}{
		"success with web password": {
			wantFail: false,
			prepare:  caseSuccessWithWebPassword,
		},
		"fail without web password": {
			wantFail: true,
			prepare:  caseFailNoWebPassword,
		},
		"fail on unsupported version": {
			wantFail: true,
			prepare:  caseFailUnsupportedVersion,
		},
	}

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

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

func TestPihole_Charts(t *testing.T) {
	assert.NotNil(t, New().Charts())
}

func TestPihole_Collect(t *testing.T) {
	tests := map[string]struct {
		prepare       func(t *testing.T) (p *Pihole, cleanup func())
		wantMetrics   map[string]int64
		wantNumCharts int
	}{
		"success with web password": {
			prepare:       caseSuccessWithWebPassword,
			wantNumCharts: len(baseCharts) + 2,
			wantMetrics: map[string]int64{
				"A":                        1229,
				"AAAA":                     1229,
				"ANY":                      100,
				"PTR":                      7143,
				"SOA":                      100,
				"SRV":                      100,
				"TXT":                      100,
				"ads_blocked_today":        1,
				"ads_blocked_today_perc":   33333,
				"ads_percentage_today":     100,
				"blocking_status_disabled": 0,
				"blocking_status_enabled":  1,
				"blocklist_last_update":    106273651,
				"destination_blocked":      220,
				"destination_cached":       8840,
				"destination_other":        940,
				"dns_queries_today":        1,
				"domains_being_blocked":    1,
				"queries_cached":           1,
				"queries_cached_perc":      33333,
				"queries_forwarded":        1,
				"queries_forwarded_perc":   33333,
				"unique_clients":           1,
			},
		},
		"fail without web password": {
			prepare:     caseFailNoWebPassword,
			wantMetrics: nil,
		},
		"fail on unsupported version": {
			prepare:     caseFailUnsupportedVersion,
			wantMetrics: nil,
		},
	}

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

			mx := p.Collect()

			copyBlockListLastUpdate(mx, test.wantMetrics)
			require.Equal(t, test.wantMetrics, mx)
			if len(test.wantMetrics) > 0 {
				assert.Len(t, *p.Charts(), test.wantNumCharts)
			}
		})
	}
}

func caseSuccessWithWebPassword(t *testing.T) (*Pihole, func()) {
	p, srv := New(), mockPiholeServer{}.newPiholeHTTPServer()

	p.SetupVarsPath = pathSetupVarsOK
	p.URL = srv.URL

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

	return p, srv.Close
}

func caseFailNoWebPassword(t *testing.T) (*Pihole, func()) {
	p, srv := New(), mockPiholeServer{}.newPiholeHTTPServer()

	p.SetupVarsPath = pathSetupVarsWrong
	p.URL = srv.URL

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

	return p, srv.Close
}

func caseFailUnsupportedVersion(t *testing.T) (*Pihole, func()) {
	p, srv := New(), mockPiholeServer{unsupportedVersion: true}.newPiholeHTTPServer()

	p.SetupVarsPath = pathSetupVarsOK
	p.URL = srv.URL

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

	return p, srv.Close
}

type mockPiholeServer struct {
	unsupportedVersion bool
	errOnAPIVersion    bool
	errOnSummary       bool
	errOnQueryTypes    bool
	errOnGetForwardDst bool
}

func (m mockPiholeServer) newPiholeHTTPServer() *httptest.Server {
	return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path != urlPathAPI || len(r.URL.Query()) == 0 {
			w.WriteHeader(http.StatusBadRequest)
		}

		if r.URL.Query().Get(urlQueryKeyAuth) == "" {
			_, _ = w.Write(dataEmptyResp)
			return
		}

		if r.URL.Query().Has(urlQueryKeyAPIVersion) {
			if m.errOnAPIVersion {
				w.WriteHeader(http.StatusNotFound)
			} else if m.unsupportedVersion {
				_, _ = w.Write([]byte(fmt.Sprintf(`{"version": %d}`, wantAPIVersion+1)))
			} else {
				_, _ = w.Write([]byte(fmt.Sprintf(`{"version": %d}`, wantAPIVersion)))
			}
			return
		}

		if r.URL.Query().Has(urlQueryKeySummaryRaw) {
			if m.errOnSummary {
				w.WriteHeader(http.StatusNotFound)
			} else {
				_, _ = w.Write(dataSummaryRawResp)
			}
			return
		}

		data := dataEmptyResp
		isErr := false
		switch {
		case r.URL.Query().Has(urlQueryKeyGetQueryTypes):
			data, isErr = dataGetQueryTypesResp, m.errOnQueryTypes
		case r.URL.Query().Has(urlQueryKeyGetForwardDestinations):
			data, isErr = dataGetForwardDestinationsResp, m.errOnGetForwardDst
		}

		if isErr {
			w.WriteHeader(http.StatusNotFound)
		} else {
			_, _ = w.Write(data)
		}
	}))
}

func copyBlockListLastUpdate(dst, src map[string]int64) {
	k := "blocklist_last_update"
	if v, ok := src[k]; ok {
		if _, ok := dst[k]; ok {
			dst[k] = v
		}
	}
}