Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/metrics/cluster/validation_test.go
4094 views
1
package cluster
2
3
import (
4
"fmt"
5
"strings"
6
"testing"
7
8
"github.com/grafana/agent/pkg/metrics/instance"
9
"github.com/grafana/agent/pkg/util"
10
"github.com/stretchr/testify/require"
11
)
12
13
func Test_validateNoFiles(t *testing.T) {
14
tt := []struct {
15
name string
16
input string
17
expect error
18
}{
19
{
20
name: "valid config",
21
input: util.Untab(`
22
scrape_configs:
23
- job_name: innocent_scrape
24
static_configs:
25
- targets: ['127.0.0.1:12345']
26
remote_write:
27
- url: http://localhost:9009/api/prom/push
28
`),
29
expect: nil,
30
},
31
{
32
name: "all SDs",
33
input: util.Untab(`
34
scrape_configs:
35
- job_name: basic_sds
36
static_configs:
37
- targets: ['localhost']
38
azure_sd_configs:
39
- subscription_id: fake
40
tenant_id: fake
41
client_id: fake
42
client_secret: fake
43
consul_sd_configs:
44
- {}
45
dns_sd_configs:
46
- names: ['fake']
47
ec2_sd_configs:
48
- region: fake
49
eureka_sd_configs:
50
- server: http://localhost:80/eureka
51
file_sd_configs:
52
- files: ['fake.json']
53
digitalocean_sd_configs:
54
- {}
55
dockerswarm_sd_configs:
56
- host: localhost
57
role: nodes
58
gce_sd_configs:
59
- project: fake
60
zone: fake
61
hetzner_sd_configs:
62
- role: hcloud
63
kubernetes_sd_configs:
64
- role: pod
65
marathon_sd_configs:
66
- servers: ['localhost']
67
nerve_sd_configs:
68
- servers: ['localhost']
69
paths: ['/']
70
openstack_sd_configs:
71
- role: instance
72
region: fake
73
scaleway_sd_configs:
74
- role: instance
75
project_id: ffffffff-ffff-ffff-ffff-ffffffffffff
76
secret_key: ffffffff-ffff-ffff-ffff-ffffffffffff
77
access_key: SCWXXXXXXXXXXXXXXXXX
78
serverset_sd_configs:
79
- servers: ['localhost']
80
paths: ['/']
81
triton_sd_configs:
82
- account: fake
83
dns_suffix: fake
84
endpoint: fake
85
`),
86
expect: nil,
87
},
88
{
89
name: "invalid http client config",
90
input: util.Untab(`
91
scrape_configs:
92
- job_name: malicious_scrape
93
static_configs:
94
- targets: ['badsite.com']
95
basic_auth:
96
username: file_leak
97
password_file: /etc/password
98
remote_write:
99
- url: http://localhost:9009/api/prom/push
100
`),
101
expect: fmt.Errorf("failed to validate scrape_config at index 0: password_file must be empty unless dangerous_allow_reading_files is set"),
102
},
103
}
104
105
for _, tc := range tt {
106
t.Run(tc.name, func(t *testing.T) {
107
cfg, err := instance.UnmarshalConfig(strings.NewReader(tc.input))
108
require.NoError(t, err)
109
110
actual := validateNofiles(cfg)
111
if tc.expect == nil {
112
require.NoError(t, actual)
113
} else {
114
require.EqualError(t, actual, tc.expect.Error())
115
}
116
})
117
}
118
}
119
120