Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/discovery/dns/dns_test.go
4096 views
1
package dns
2
3
import (
4
"strings"
5
"testing"
6
"time"
7
8
"github.com/grafana/agent/pkg/river"
9
"github.com/prometheus/common/model"
10
"github.com/stretchr/testify/require"
11
"gotest.tools/assert"
12
)
13
14
func TestRiverUnmarshal(t *testing.T) {
15
var exampleRiverConfig = `
16
refresh_interval = "5m"
17
port = 54
18
names = ["foo.com"]
19
type = "A"
20
`
21
22
var args Arguments
23
err := river.Unmarshal([]byte(exampleRiverConfig), &args)
24
require.NoError(t, err)
25
26
assert.Equal(t, 5*time.Minute, args.RefreshInterval)
27
assert.Equal(t, 54, args.Port)
28
assert.Equal(t, "foo.com", strings.Join(args.Names, ","))
29
}
30
31
func TestBadRiverConfig(t *testing.T) {
32
var tests = []struct {
33
Desc string
34
Config string
35
}{
36
{
37
Desc: "No Name",
38
Config: "",
39
},
40
{
41
Desc: "Bad Type",
42
Config: `names = ["example"]
43
type = "CNAME"`,
44
},
45
{
46
Desc: "A without port",
47
Config: `names = ["example"]
48
type = "A"`,
49
},
50
{
51
Desc: "AAAA without port",
52
Config: `names = ["example"]
53
type = "AAAA"`,
54
},
55
}
56
for _, tst := range tests {
57
cfg := tst.Config
58
t.Run(tst.Desc, func(t *testing.T) {
59
var args Arguments
60
err := river.Unmarshal([]byte(cfg), &args)
61
require.Error(t, err)
62
})
63
}
64
}
65
66
func TestConvert(t *testing.T) {
67
args := Arguments{
68
RefreshInterval: 5 * time.Minute,
69
Port: 8181,
70
Type: "A",
71
Names: []string{"example.com", "example2.com"},
72
}
73
74
converted := args.Convert()
75
assert.Equal(t, model.Duration(5*time.Minute), converted.RefreshInterval)
76
assert.Equal(t, 8181, converted.Port)
77
assert.Equal(t, "A", converted.Type)
78
assert.Equal(t, "example.com,example2.com", strings.Join(converted.Names, ","))
79
}
80
81