Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/river/vm/vm_errors_test.go
4095 views
1
package vm_test
2
3
import (
4
"testing"
5
6
"github.com/grafana/agent/pkg/river/parser"
7
"github.com/grafana/agent/pkg/river/vm"
8
"github.com/stretchr/testify/require"
9
)
10
11
func TestVM_ExprErrors(t *testing.T) {
12
type Target struct {
13
Key struct {
14
Object struct {
15
Field1 []int `river:"field1,attr"`
16
} `river:"object,attr"`
17
} `river:"key,attr"`
18
}
19
20
tt := []struct {
21
name string
22
input string
23
into interface{}
24
scope *vm.Scope
25
expect string
26
}{
27
{
28
name: "basic wrong type",
29
input: `key = true`,
30
into: &Target{},
31
expect: "test:1:7: true should be object, got bool",
32
},
33
{
34
name: "deeply nested literal",
35
input: `
36
key = {
37
object = {
38
field1 = [15, 30, "Hello, world!"],
39
},
40
}
41
`,
42
into: &Target{},
43
expect: `test:4:25: "Hello, world!" should be number, got string`,
44
},
45
{
46
name: "deeply nested indirect",
47
input: `key = key_value`,
48
into: &Target{},
49
scope: &vm.Scope{
50
Variables: map[string]interface{}{
51
"key_value": map[string]interface{}{
52
"object": map[string]interface{}{
53
"field1": []interface{}{15, 30, "Hello, world!"},
54
},
55
},
56
},
57
},
58
expect: `test:1:7: key_value.object.field1[2] should be number, got string`,
59
},
60
{
61
name: "complex expr",
62
input: `key = [0, 1, 2]`,
63
into: &struct {
64
Key string `river:"key,attr"`
65
}{},
66
expect: `test:1:7: [0, 1, 2] should be string, got array`,
67
},
68
}
69
70
for _, tc := range tt {
71
t.Run(tc.name, func(t *testing.T) {
72
res, err := parser.ParseFile("test", []byte(tc.input))
73
require.NoError(t, err)
74
75
eval := vm.New(res)
76
err = eval.Evaluate(tc.scope, tc.into)
77
require.EqualError(t, err, tc.expect)
78
})
79
}
80
}
81
82