Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/prometheus/exporter/redis/redis_test.go
4095 views
1
package redis
2
3
import (
4
"testing"
5
"time"
6
7
"github.com/grafana/agent/pkg/integrations/redis_exporter"
8
"github.com/grafana/agent/pkg/river"
9
"github.com/stretchr/testify/require"
10
)
11
12
func TestRiverUnmarshal(t *testing.T) {
13
riverConfig := `
14
redis_addr = "localhost:6379"
15
redis_user = "redis_user"
16
redis_password_file = "/tmp/pass"
17
namespace = "namespace"
18
config_command = "TEST_CONFIG"
19
check_keys = ["key1*", "cache_*"]
20
check_key_groups = ["other_key%d+"]
21
check_key_groups_batch_size = 5000
22
max_distinct_key_groups = 50
23
check_single_keys = ["particular_key"]
24
check_streams = ["stream1*"]
25
check_single_streams = ["particular_stream"]
26
count_keys = ["count_key1", "count_key2"]
27
script_path = "/tmp/metrics-script.lua,/tmp/cooler-metrics-script.lua"
28
connection_timeout = "7s"
29
tls_client_key_file = "/tmp/client-key.pem"
30
tls_client_cert_file = "/tmp/client-cert.pem"
31
tls_ca_cert_file = "/tmp/ca-cert.pem"
32
set_client_name = false
33
is_tile38 = true
34
export_client_list = false
35
export_client_port = true
36
redis_metrics_only = false
37
ping_on_connect = true
38
incl_system_metrics = true
39
skip_tls_verification = false
40
is_cluster = true
41
`
42
var args Arguments
43
err := river.Unmarshal([]byte(riverConfig), &args)
44
45
require.NoError(t, err)
46
expected := Arguments{
47
RedisAddr: "localhost:6379",
48
RedisUser: "redis_user",
49
RedisPasswordFile: "/tmp/pass",
50
Namespace: "namespace",
51
ConfigCommand: "TEST_CONFIG",
52
53
CheckKeys: []string{"key1*", "cache_*"},
54
CheckKeyGroups: []string{"other_key%d+"},
55
CheckSingleKeys: []string{"particular_key"},
56
CheckKeyGroupsBatchSize: int64(5000),
57
MaxDistinctKeyGroups: int64(50),
58
59
CheckStreams: []string{"stream1*"},
60
CheckSingleStreams: []string{"particular_stream"},
61
CountKeys: []string{"count_key1", "count_key2"},
62
63
ScriptPath: "/tmp/metrics-script.lua,/tmp/cooler-metrics-script.lua",
64
ConnectionTimeout: 7 * time.Second,
65
66
TLSClientKeyFile: "/tmp/client-key.pem",
67
TLSClientCertFile: "/tmp/client-cert.pem",
68
TLSCaCertFile: "/tmp/ca-cert.pem",
69
70
SetClientName: false,
71
IsTile38: true,
72
ExportClientList: false,
73
ExportClientPort: true,
74
RedisMetricsOnly: false,
75
PingOnConnect: true,
76
InclSystemMetrics: true,
77
SkipTLSVerification: false,
78
IsCluster: true,
79
}
80
require.Equal(t, expected, args)
81
}
82
83
func TestUnmarshalInvalid(t *testing.T) {
84
validRiverConfig := `
85
redis_addr = "localhost:1234"
86
script_path = "/tmp/metrics.lua"`
87
88
var args Arguments
89
err := river.Unmarshal([]byte(validRiverConfig), &args)
90
require.NoError(t, err)
91
92
invalidRiverConfig := `
93
redis_addr = "localhost:1234
94
script_path = "/tmp/metrics.lua"
95
script_paths = ["/tmp/more-metrics.lua", "/tmp/even-more-metrics.lua"]`
96
97
var invalidArgs Arguments
98
err = river.Unmarshal([]byte(invalidRiverConfig), &invalidArgs)
99
require.Error(t, err)
100
}
101
102
func TestRiverConvert(t *testing.T) {
103
orig := Arguments{
104
RedisAddr: "localhost:6379",
105
RedisUser: "redis_user",
106
RedisPasswordFile: "/tmp/pass",
107
Namespace: "namespace",
108
ConfigCommand: "TEST_CONFIG",
109
110
CheckKeys: []string{"key1*", "cache_*"},
111
CheckKeyGroups: []string{"other_key%d+"},
112
CheckSingleKeys: []string{"particular_key"},
113
CountKeys: []string{"count_key1", "count_key2"},
114
CheckKeyGroupsBatchSize: 5000,
115
MaxDistinctKeyGroups: 50,
116
117
CheckStreams: []string{"stream1*", "stream2*"},
118
CheckSingleStreams: []string{"particular_stream"},
119
120
ScriptPath: "/tmp/metrics-script.lua,/tmp/cooler-metrics-script.lua",
121
ConnectionTimeout: 7 * time.Second,
122
123
TLSClientKeyFile: "/tmp/client-key.pem",
124
TLSClientCertFile: "/tmp/client-cert.pem",
125
TLSCaCertFile: "/tmp/ca-cert.pem",
126
127
SetClientName: false,
128
IsTile38: true,
129
ExportClientList: false,
130
ExportClientPort: true,
131
RedisMetricsOnly: false,
132
PingOnConnect: true,
133
InclSystemMetrics: true,
134
SkipTLSVerification: false,
135
}
136
converted := orig.Convert()
137
expected := redis_exporter.Config{
138
RedisAddr: "localhost:6379",
139
RedisUser: "redis_user",
140
RedisPasswordFile: "/tmp/pass",
141
Namespace: "namespace",
142
ConfigCommand: "TEST_CONFIG",
143
144
CheckKeys: "key1*,cache_*",
145
CheckKeyGroups: "other_key%d+",
146
CheckSingleKeys: "particular_key",
147
CountKeys: "count_key1,count_key2",
148
CheckKeyGroupsBatchSize: 5000,
149
MaxDistinctKeyGroups: 50,
150
151
CheckStreams: "stream1*,stream2*",
152
CheckSingleStreams: "particular_stream",
153
154
ScriptPath: "/tmp/metrics-script.lua,/tmp/cooler-metrics-script.lua",
155
ConnectionTimeout: 7 * time.Second,
156
157
TLSClientKeyFile: "/tmp/client-key.pem",
158
TLSClientCertFile: "/tmp/client-cert.pem",
159
TLSCaCertFile: "/tmp/ca-cert.pem",
160
161
SetClientName: false,
162
IsTile38: true,
163
ExportClientList: false,
164
ExportClientPort: true,
165
RedisMetricsOnly: false,
166
PingOnConnect: true,
167
InclSystemMetrics: true,
168
SkipTLSVerification: false,
169
}
170
171
require.Equal(t, expected, *converted)
172
}
173
174