summaryrefslogtreecommitdiffstats
path: root/pkg/config/config_test.go
blob: 94e37735518cda38dff42cf42e68a7689559fffc (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
package config

import (
	"github.com/creasty/defaults"
	"github.com/icinga/icingadb/pkg/logging"
	"github.com/stretchr/testify/require"
	"os"
	"testing"
)

func TestFromYAMLFile(t *testing.T) {
	const miniConf = `
database:
  host: 192.0.2.1
  database: icingadb
  user: icingadb
  password: icingadb

redis:
  host: 2001:db8::1
`

	subtests := []struct {
		name   string
		input  string
		output *Config
	}{
		{
			name:  "mini",
			input: miniConf,
			output: func() *Config {
				c := &Config{}
				_ = defaults.Set(c)

				c.Database.Host = "192.0.2.1"
				c.Database.Database = "icingadb"
				c.Database.User = "icingadb"
				c.Database.Password = "icingadb"

				c.Redis.Host = "2001:db8::1"
				c.Logging.Output = logging.CONSOLE

				return c
			}(),
		},
		{
			name:   "mini-with-unknown",
			input:  miniConf + "\nunknown: 42",
			output: nil,
		},
	}

	for _, st := range subtests {
		t.Run(st.name, func(t *testing.T) {
			tempFile, err := os.CreateTemp("", "")
			require.NoError(t, err)
			defer func() { _ = os.Remove(tempFile.Name()) }()

			require.NoError(t, os.WriteFile(tempFile.Name(), []byte(st.input), 0o600))

			if actual, err := FromYAMLFile(tempFile.Name()); st.output == nil {
				require.Error(t, err)
			} else {
				require.NoError(t, err)
				require.Equal(t, st.output, actual)
			}
		})
	}
}