Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/registry_test.go
4094 views
1
package component
2
3
import (
4
"testing"
5
6
"github.com/stretchr/testify/require"
7
)
8
9
func Test_parseComponentName(t *testing.T) {
10
tt := []struct {
11
check string
12
expectValid bool
13
}{
14
{check: "", expectValid: false},
15
{check: " ", expectValid: false},
16
{check: "test", expectValid: true},
17
{check: "foo.bar", expectValid: true},
18
{check: "foo.bar.", expectValid: false},
19
{check: "foo.bar. ", expectValid: false},
20
{check: "small.LARGE", expectValid: true},
21
{check: "a_b_c_012345", expectValid: true},
22
}
23
24
for _, tc := range tt {
25
t.Run(tc.check, func(t *testing.T) {
26
_, err := parseComponentName(tc.check)
27
if tc.expectValid {
28
require.NoError(t, err, "expected component name to be valid")
29
} else {
30
require.Error(t, err, "expected component name to not be valid")
31
}
32
})
33
}
34
}
35
36
func Test_validatePrefixMatch(t *testing.T) {
37
existing := map[string]parsedName{
38
"remote.http": {"remote", "http"},
39
"test": {"test"},
40
"three.part.name": {"three", "part", "name"},
41
}
42
43
tt := []struct {
44
check string
45
expectValid bool
46
}{
47
{check: "remote.s3", expectValid: true},
48
{check: "remote", expectValid: false},
49
{check: "test2", expectValid: true},
50
{check: "test.new", expectValid: false},
51
{check: "remote.something.else", expectValid: true},
52
{check: "three.part", expectValid: false},
53
{check: "three", expectValid: false},
54
{check: "three.two", expectValid: true},
55
{check: "three.part.other", expectValid: true},
56
}
57
58
for _, tc := range tt {
59
t.Run(tc.check, func(t *testing.T) {
60
parsed, err := parseComponentName(tc.check)
61
require.NoError(t, err)
62
63
err = validatePrefixMatch(parsed, existing)
64
if tc.expectValid {
65
require.NoError(t, err, "expected component to be accepted")
66
} else {
67
require.Error(t, err, "expected component to not be accepted")
68
}
69
})
70
}
71
}
72
73