Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/integrations/register_test.go
5302 views
1
package integrations
2
3
import (
4
"fmt"
5
"testing"
6
"time"
7
8
"github.com/go-kit/log"
9
"github.com/stretchr/testify/require"
10
"gopkg.in/yaml.v2"
11
)
12
13
func TestIntegrationRegistration(t *testing.T) {
14
// This test checks for a few things:
15
//
16
// 1. Registered integrations will be parseable
17
// 2. Registered integrations that are not present will not be unmarshaled to
18
// the list of configs
19
// 3. Registered integrations that have defaults may still be parsed
20
// 4. Strict parsing should still work as expected.
21
22
var cfgToParse = `
23
name: John Doe
24
duration: 500ms
25
test:
26
text: Hello, world!
27
`
28
29
var fullCfg testFullConfig
30
err := yaml.UnmarshalStrict([]byte(cfgToParse), &fullCfg)
31
require.NoError(t, err)
32
33
expect := testFullConfig{
34
Name: "John Doe",
35
Duration: 500 * time.Millisecond,
36
Default: 12345,
37
Configs: []UnmarshaledConfig{{
38
Config: &testIntegrationA{Text: "Hello, world!", Truth: true},
39
}},
40
}
41
require.Equal(t, expect, fullCfg)
42
}
43
44
type testIntegrationA struct {
45
Text string `yaml:"text"`
46
Truth bool `yaml:"truth"`
47
}
48
49
func (i *testIntegrationA) Name() string { return "test" }
50
func (i *testIntegrationA) InstanceKey(_ string) (string, error) { return "integrationA", nil }
51
52
func (i *testIntegrationA) NewIntegration(l log.Logger) (Integration, error) {
53
return nil, fmt.Errorf("not implemented")
54
}
55
56
func (i *testIntegrationA) UnmarshalYAML(unmarshal func(interface{}) error) error {
57
i.Truth = true
58
type plain testIntegrationA
59
return unmarshal((*plain)(i))
60
}
61
62
type testIntegrationB struct {
63
Text string `yaml:"text"`
64
}
65
66
func (*testIntegrationB) Name() string { return "shouldnotbefound" }
67
func (*testIntegrationB) InstanceKey(_ string) (string, error) { return "integrationB", nil }
68
69
func (*testIntegrationB) NewIntegration(l log.Logger) (Integration, error) {
70
return nil, fmt.Errorf("not implemented")
71
}
72
73
type testFullConfig struct {
74
// Some random fields that will also be exposed
75
Name string `yaml:"name"`
76
Duration time.Duration `yaml:"duration"`
77
Default int `yaml:"default"`
78
79
Configs Configs `yaml:"-"`
80
}
81
82
func (c *testFullConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
83
// This default value should not change.
84
c.Default = 12345
85
86
// Mock out registered integrations.
87
registered := []Config{
88
&testIntegrationA{},
89
&testIntegrationB{},
90
}
91
return unmarshalIntegrationsWithList(registered, c, unmarshal)
92
}
93
94