Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/flow/internal/stdlib/stdlib_test.go
4096 views
1
package stdlib
2
3
import (
4
"reflect"
5
"testing"
6
7
"github.com/grafana/agent/component/discovery"
8
"github.com/grafana/agent/pkg/river/parser"
9
"github.com/grafana/agent/pkg/river/vm"
10
"github.com/prometheus/common/model"
11
"github.com/stretchr/testify/require"
12
)
13
14
func TestVM_Stdlib_Scoped(t *testing.T) {
15
rootScope := &vm.Scope{
16
Variables: Identifiers,
17
}
18
19
tt := []struct {
20
name string
21
input string
22
scope *vm.Scope
23
expect interface{}
24
}{
25
{
26
name: "discovery_target_decode",
27
input: `discovery_target_decode(input)`,
28
scope: &vm.Scope{
29
Parent: rootScope,
30
Variables: map[string]interface{}{
31
"input": `[
32
{
33
"targets": ["host-a:12345", "host-a:12346"],
34
"labels": {
35
"foo": "bar"
36
}
37
},
38
{
39
"targets": ["host-b:12345", "host-b:12346"],
40
"labels": {
41
"hello": "world"
42
}
43
}
44
]`,
45
},
46
},
47
expect: []discovery.Target{
48
{
49
model.AddressLabel: "host-a:12345",
50
"foo": "bar",
51
},
52
{
53
model.AddressLabel: "host-a:12346",
54
"foo": "bar",
55
},
56
{
57
model.AddressLabel: "host-b:12345",
58
"hello": "world",
59
},
60
{
61
model.AddressLabel: "host-b:12346",
62
"hello": "world",
63
},
64
},
65
},
66
}
67
68
for _, tc := range tt {
69
t.Run(tc.name, func(t *testing.T) {
70
expr, err := parser.ParseExpression(tc.input)
71
require.NoError(t, err)
72
73
eval := vm.New(expr)
74
75
rv := reflect.New(reflect.TypeOf(tc.expect))
76
require.NoError(t, eval.Evaluate(tc.scope, rv.Interface()))
77
require.Equal(t, tc.expect, rv.Elem().Interface())
78
})
79
}
80
}
81
82