Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/logs/config_test.go
4094 views
1
package logs
2
3
import (
4
"fmt"
5
"path/filepath"
6
"strings"
7
"testing"
8
"time"
9
10
pc "github.com/prometheus/common/config"
11
"github.com/stretchr/testify/require"
12
"gopkg.in/yaml.v2"
13
)
14
15
func TestConfig_ApplyDefaults_Validations(t *testing.T) {
16
tt := []struct {
17
name string
18
cfg string
19
err error
20
}{
21
{
22
name: "two configs with different names",
23
err: nil,
24
cfg: untab(`
25
positions_directory: /tmp
26
configs:
27
- name: config-a
28
- name: config-b
29
`),
30
},
31
{
32
name: "two configs with same name",
33
err: fmt.Errorf("found two Loki configs with name config-a"),
34
cfg: untab(`
35
positions_directory: /tmp
36
configs:
37
- name: config-a
38
- name: config-b
39
- name: config-a
40
`),
41
},
42
{
43
name: "two configs, different positions path",
44
err: nil,
45
cfg: untab(`
46
configs:
47
- name: config-a
48
positions:
49
filename: /tmp/file-a.yml
50
- name: config-b
51
positions:
52
filename: /tmp/file-b.yml
53
`),
54
},
55
{
56
name: "re-used positions path",
57
err: fmt.Errorf("Loki configs config-a and config-c must have different positions file paths"),
58
cfg: untab(`
59
configs:
60
- name: config-a
61
positions:
62
filename: /tmp/file-a.yml
63
- name: config-b
64
positions:
65
filename: /tmp/file-b.yml
66
- name: config-c
67
positions:
68
filename: /tmp/file-a.yml
69
`),
70
},
71
{
72
name: "empty name",
73
err: fmt.Errorf("Loki config index 1 must have a name"),
74
cfg: untab(`
75
positions_directory: /tmp
76
configs:
77
- name: config-a
78
- name:
79
- name: config-a
80
`),
81
},
82
{
83
name: "generated positions file path without positions_directory",
84
err: fmt.Errorf("cannot generate Loki positions file path for config-b because positions_directory is not configured"),
85
cfg: untab(`
86
configs:
87
- name: config-a
88
positions:
89
filename: /tmp/config-a.yaml
90
- name: config-b
91
`),
92
},
93
{
94
name: "global filewatcher",
95
err: nil,
96
cfg: untab(`
97
global:
98
file_watch_config:
99
min_poll_frequency: 1s
100
max_poll_frequency: 20s
101
positions_directory: /tmp
102
configs:
103
- name: config-a
104
- name: config-b
105
`),
106
},
107
}
108
109
for _, tc := range tt {
110
t.Run(tc.name, func(t *testing.T) {
111
var cfg Config
112
err := yaml.UnmarshalStrict([]byte(tc.cfg), &cfg)
113
require.NoError(t, err)
114
err = cfg.ApplyDefaults()
115
if tc.err == nil {
116
require.NoError(t, err)
117
} else {
118
require.EqualError(t, err, tc.err.Error())
119
}
120
})
121
}
122
}
123
124
func TestConfig_ApplyDefaults_Defaults(t *testing.T) {
125
cfgText := untab(`
126
positions_directory: /tmp
127
global:
128
clients:
129
- basic_auth:
130
password: password_default
131
username: username_default
132
url: https://default.com
133
configs:
134
- name: config-a
135
positions:
136
filename: /config-a.yml
137
- name: config-b
138
- name: config-c
139
clients:
140
- basic_auth:
141
password: password
142
username: username
143
url: https://example.com
144
`)
145
var cfg Config
146
err := yaml.UnmarshalStrict([]byte(cfgText), &cfg)
147
require.NoError(t, err)
148
err = cfg.ApplyDefaults()
149
require.NoError(t, err)
150
151
var (
152
pathA = cfg.Configs[0].PositionsConfig.PositionsFile
153
pathB = cfg.Configs[1].PositionsConfig.PositionsFile
154
155
clientB = cfg.Configs[1].ClientConfigs[0]
156
clientC = cfg.Configs[2].ClientConfigs[0]
157
)
158
159
require.Equal(t, "/config-a.yml", pathA)
160
require.Equal(t, filepath.Join("/tmp", "config-b.yml"), pathB)
161
require.Equal(t, "https://default.com", clientB.URL.String())
162
require.Equal(t, &pc.BasicAuth{
163
Password: "password_default",
164
Username: "username_default",
165
}, clientB.Client.BasicAuth)
166
167
require.Equal(t, "https://example.com", clientC.URL.String())
168
require.Equal(t, &pc.BasicAuth{
169
Password: "password",
170
Username: "username",
171
}, clientC.Client.BasicAuth)
172
}
173
174
// untab is a utility function to make it easier to write YAML tests, where some editors
175
// will insert tabs into strings by default.
176
func untab(s string) string {
177
return strings.ReplaceAll(s, "\t", " ")
178
}
179
180
func TestInstanceConfig_Initialize(t *testing.T) {
181
cfgText := `
182
name: config-c
183
`
184
var cfg InstanceConfig
185
err := yaml.UnmarshalStrict([]byte(cfgText), &cfg)
186
require.NoError(t, err)
187
188
// Make sure the default values from flags are applied
189
require.Equal(t, 10*time.Second, cfg.PositionsConfig.SyncPeriod)
190
require.Equal(t, "", cfg.PositionsConfig.PositionsFile)
191
require.Equal(t, false, cfg.PositionsConfig.IgnoreInvalidYaml)
192
require.Equal(t, false, cfg.TargetConfig.Stdin)
193
}
194
195