Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/metrics/instance/marshal_test.go
4094 views
1
package instance
2
3
import (
4
"bytes"
5
"strings"
6
"testing"
7
8
"github.com/stretchr/testify/require"
9
"gopkg.in/yaml.v2"
10
)
11
12
func TestUnmarshalConfig_Valid(t *testing.T) {
13
validConfig := DefaultConfig
14
validConfigContent, err := yaml.Marshal(validConfig)
15
require.NoError(t, err)
16
17
_, err = UnmarshalConfig(bytes.NewReader(validConfigContent))
18
require.NoError(t, err)
19
}
20
21
func TestUnmarshalConfig_Invalid(t *testing.T) {
22
invalidConfigContent := `whyWouldAnyoneThinkThisisAValidConfig: 12345`
23
24
_, err := UnmarshalConfig(strings.NewReader(invalidConfigContent))
25
require.Error(t, err)
26
}
27
28
// TestMarshal_UnmarshalConfig_RetainSecrets ensures that secrets can be
29
// retained.
30
func TestMarshal_UnmarshalConfig_RetainSecrets(t *testing.T) {
31
cfg := `name: test
32
scrape_configs:
33
- job_name: local_scrape
34
follow_redirects: true
35
enable_http2: true
36
honor_timestamps: true
37
metrics_path: /metrics
38
scheme: http
39
static_configs:
40
- targets:
41
- 127.0.0.1:12345
42
labels:
43
cluster: localhost
44
basic_auth:
45
username: admin
46
password: foobar
47
remote_write:
48
- url: http://admin:verysecret@localhost:9009/api/prom/push
49
remote_timeout: 30s
50
name: test-d0f32c
51
send_exemplars: true
52
basic_auth:
53
username: admin
54
password: verysecret
55
queue_config:
56
capacity: 500
57
max_shards: 1000
58
min_shards: 1
59
max_samples_per_send: 100
60
batch_send_deadline: 5s
61
min_backoff: 30ms
62
max_backoff: 100ms
63
follow_redirects: true
64
enable_http2: true
65
metadata_config:
66
max_samples_per_send: 500
67
send: true
68
send_interval: 1m
69
wal_truncate_frequency: 1m0s
70
min_wal_time: 5m0s
71
max_wal_time: 4h0m0s
72
remote_flush_deadline: 1m0s
73
`
74
75
c, err := UnmarshalConfig(strings.NewReader(cfg))
76
require.NoError(t, err)
77
78
out, err := MarshalConfig(c, false)
79
require.NoError(t, err)
80
require.YAMLEq(t, cfg, string(out))
81
}
82
83
// TestMarshal_UnmarshalConfig_ScrubSecrets ensures that secrets can be
84
// scrubbed.
85
func TestMarshal_UnmarshalConfig_ScrubSecrets(t *testing.T) {
86
cfg := `name: test
87
scrape_configs:
88
- job_name: local_scrape
89
follow_redirects: true
90
enable_http2: true
91
honor_timestamps: true
92
metrics_path: /metrics
93
scheme: http
94
static_configs:
95
- targets:
96
- 127.0.0.1:12345
97
labels:
98
cluster: localhost
99
basic_auth:
100
username: admin
101
password: SCRUBME
102
remote_write:
103
- url: http://username:SCRUBURL@localhost:9009/api/prom/push
104
remote_timeout: 30s
105
name: test-d0f32c
106
send_exemplars: true
107
basic_auth:
108
username: admin
109
password: SCRUBME
110
queue_config:
111
capacity: 500
112
max_shards: 1000
113
min_shards: 1
114
max_samples_per_send: 100
115
batch_send_deadline: 5s
116
min_backoff: 30ms
117
max_backoff: 100ms
118
follow_redirects: true
119
enable_http2: true
120
metadata_config:
121
max_samples_per_send: 500
122
send: true
123
send_interval: 1m
124
wal_truncate_frequency: 1m0s
125
min_wal_time: 5m0s
126
max_wal_time: 4h0m0s
127
remote_flush_deadline: 1m0s
128
`
129
130
scrub := func(in string) string {
131
in = strings.ReplaceAll(in, "SCRUBME", "<secret>")
132
in = strings.ReplaceAll(in, "SCRUBURL", "xxxxx")
133
return in
134
}
135
136
t.Run("direct marshal", func(t *testing.T) {
137
var c Config
138
err := yaml.Unmarshal([]byte(cfg), &c)
139
require.NoError(t, err)
140
141
out, err := yaml.Marshal(c)
142
require.NoError(t, err)
143
require.YAMLEq(t, scrub(cfg), string(out))
144
})
145
146
t.Run("direct marshal pointer", func(t *testing.T) {
147
var c Config
148
err := yaml.Unmarshal([]byte(cfg), &c)
149
require.NoError(t, err)
150
151
out, err := yaml.Marshal(&c)
152
require.NoError(t, err)
153
require.YAMLEq(t, scrub(cfg), string(out))
154
})
155
156
t.Run("custom marshal methods", func(t *testing.T) {
157
c, err := UnmarshalConfig(strings.NewReader(cfg))
158
require.NoError(t, err)
159
160
out, err := MarshalConfig(c, true)
161
require.NoError(t, err)
162
require.YAMLEq(t, scrub(cfg), string(out))
163
})
164
}
165
166