summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/unbound/config/parse_test.go
blob: 72542a861f3da329ebd29def2e37eb722d8525af (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
// SPDX-License-Identifier: GPL-3.0-or-later

package config

import (
	"fmt"
	"testing"

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

func TestParse(t *testing.T) {
	tests := map[string]struct {
		path    string
		wantCfg UnboundConfig
		wantErr bool
	}{
		"valid include": {
			path: "testdata/valid_include.conf",
			wantCfg: UnboundConfig{
				cumulative: "yes",
				enable:     "yes",
				iface:      "10.0.0.1",
				port:       "8955",
				useCert:    "yes",
				keyFile:    "/etc/unbound/unbound_control_2.key",
				certFile:   "/etc/unbound/unbound_control_2.pem",
			},
		},
		"valid include-toplevel": {
			path: "testdata/valid_include_toplevel.conf",
			wantCfg: UnboundConfig{
				cumulative: "yes",
				enable:     "yes",
				iface:      "10.0.0.1",
				port:       "8955",
				useCert:    "yes",
				keyFile:    "/etc/unbound/unbound_control_2.key",
				certFile:   "/etc/unbound/unbound_control_2.pem",
			},
		},
		"valid glob include": {
			path: "testdata/valid_glob.conf",
			wantCfg: UnboundConfig{
				cumulative: "yes",
				enable:     "yes",
				iface:      "10.0.0.1",
				port:       "8955",
				useCert:    "yes",
				keyFile:    "/etc/unbound/unbound_control_2.key",
				certFile:   "/etc/unbound/unbound_control_2.pem",
			},
		},
		"non existent glob include": {
			path: "testdata/non_existent_glob_include.conf",
			wantCfg: UnboundConfig{
				cumulative: "yes",
				enable:     "yes",
				iface:      "10.0.0.1",
				port:       "8953",
				useCert:    "yes",
				keyFile:    "/etc/unbound/unbound_control.key",
				certFile:   "/etc/unbound/unbound_control.pem",
			},
		},
		"infinite recursion include": {
			path:    "testdata/infinite_rec.conf",
			wantErr: true,
		},
		"non existent include": {
			path:    "testdata/non_existent_include.conf",
			wantErr: true,
		},
		"non existent path": {
			path:    "testdata/non_existent_path.conf",
			wantErr: true,
		},
	}

	for name, test := range tests {
		name = fmt.Sprintf("%s (%s)", name, test.path)
		t.Run(name, func(t *testing.T) {
			cfg, err := Parse(test.path)

			if test.wantErr {
				assert.Error(t, err)
			} else {
				assert.NoError(t, err)
				assert.Equal(t, test.wantCfg, *cfg)
			}
		})
	}
}