Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/flow/config_test.go
4093 views
1
package flow_test
2
3
import (
4
"strings"
5
"testing"
6
7
"github.com/grafana/agent/pkg/flow"
8
"github.com/grafana/agent/pkg/river/ast"
9
"github.com/stretchr/testify/require"
10
11
_ "github.com/grafana/agent/pkg/flow/internal/testcomponents" // Include test components
12
)
13
14
func TestReadFile(t *testing.T) {
15
content := `
16
testcomponents.tick "ticker_a" {
17
frequency = "1s"
18
}
19
20
testcomponents.passthrough "static" {
21
input = "hello, world!"
22
}
23
`
24
25
f, err := flow.ReadFile(t.Name(), []byte(content))
26
require.NoError(t, err)
27
require.NotNil(t, f)
28
29
require.Len(t, f.Components, 2)
30
require.Equal(t, "testcomponents.tick.ticker_a", getBlockID(f.Components[0]))
31
require.Equal(t, "testcomponents.passthrough.static", getBlockID(f.Components[1]))
32
}
33
34
func TestReadFileWithConfigBlock(t *testing.T) {
35
content := `
36
logging {
37
log_format = "json"
38
}
39
40
testcomponents.tick "ticker_a" {
41
frequency = "1s"
42
}
43
`
44
45
f, err := flow.ReadFile(t.Name(), []byte(content))
46
require.NoError(t, err)
47
require.NotNil(t, f)
48
49
require.Len(t, f.Components, 1)
50
require.Equal(t, "testcomponents.tick.ticker_a", getBlockID(f.Components[0]))
51
require.Len(t, f.ConfigBlocks, 1)
52
require.Equal(t, "logging", getBlockID(f.ConfigBlocks[0]))
53
}
54
55
func TestReadFile_Defaults(t *testing.T) {
56
f, err := flow.ReadFile(t.Name(), []byte(``))
57
require.NotNil(t, f)
58
require.NoError(t, err)
59
60
require.Len(t, f.Components, 0)
61
}
62
63
func getBlockID(b *ast.BlockStmt) string {
64
var parts []string
65
parts = append(parts, b.Name...)
66
if b.Label != "" {
67
parts = append(parts, b.Label)
68
}
69
return strings.Join(parts, ".")
70
}
71
72