Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/integrations/postgres_exporter/postgres_exporter_test.go
5304 views
1
package postgres_exporter //nolint:golint
2
3
import (
4
"testing"
5
6
"github.com/stretchr/testify/require"
7
"gopkg.in/yaml.v2"
8
)
9
10
func Test_ParsePostgresURL(t *testing.T) {
11
dsn := "postgresql://linus:42secret@localhost:5432/postgres?sslmode=disable"
12
expected := map[string]string{
13
"dbname": "postgres",
14
"host": "localhost",
15
"password": "42secret",
16
"port": "5432",
17
"sslmode": "disable",
18
"user": "linus",
19
}
20
21
actual, err := parsePostgresURL(dsn)
22
require.NoError(t, err)
23
require.Equal(t, actual, expected)
24
}
25
26
func Test_getDataSourceNames(t *testing.T) {
27
tt := []struct {
28
name string
29
config string
30
env string
31
expect []string
32
}{
33
{
34
name: "env",
35
config: "{}",
36
env: "foo",
37
expect: []string{"foo"},
38
},
39
{
40
name: "multi-env",
41
config: "{}",
42
env: "foo,bar",
43
expect: []string{"foo", "bar"},
44
},
45
{
46
name: "config",
47
config: `{
48
"data_source_names": [
49
"foo"
50
]
51
}`,
52
env: "",
53
expect: []string{"foo"},
54
},
55
{
56
name: "config and env",
57
config: `{
58
"data_source_names": [
59
"foo"
60
]
61
}`,
62
env: "bar",
63
expect: []string{"foo"},
64
},
65
}
66
67
for _, tc := range tt {
68
t.Run(tc.name, func(t *testing.T) {
69
t.Setenv("POSTGRES_EXPORTER_DATA_SOURCE_NAME", tc.env)
70
71
var cfg Config
72
err := yaml.Unmarshal([]byte(tc.config), &cfg)
73
require.NoError(t, err)
74
75
res, err := cfg.getDataSourceNames()
76
require.NoError(t, err)
77
require.Equal(t, tc.expect, res)
78
})
79
}
80
}
81
82