Path: blob/main/pkg/metrics/cluster/validation_test.go
4094 views
package cluster12import (3"fmt"4"strings"5"testing"67"github.com/grafana/agent/pkg/metrics/instance"8"github.com/grafana/agent/pkg/util"9"github.com/stretchr/testify/require"10)1112func Test_validateNoFiles(t *testing.T) {13tt := []struct {14name string15input string16expect error17}{18{19name: "valid config",20input: util.Untab(`21scrape_configs:22- job_name: innocent_scrape23static_configs:24- targets: ['127.0.0.1:12345']25remote_write:26- url: http://localhost:9009/api/prom/push27`),28expect: nil,29},30{31name: "all SDs",32input: util.Untab(`33scrape_configs:34- job_name: basic_sds35static_configs:36- targets: ['localhost']37azure_sd_configs:38- subscription_id: fake39tenant_id: fake40client_id: fake41client_secret: fake42consul_sd_configs:43- {}44dns_sd_configs:45- names: ['fake']46ec2_sd_configs:47- region: fake48eureka_sd_configs:49- server: http://localhost:80/eureka50file_sd_configs:51- files: ['fake.json']52digitalocean_sd_configs:53- {}54dockerswarm_sd_configs:55- host: localhost56role: nodes57gce_sd_configs:58- project: fake59zone: fake60hetzner_sd_configs:61- role: hcloud62kubernetes_sd_configs:63- role: pod64marathon_sd_configs:65- servers: ['localhost']66nerve_sd_configs:67- servers: ['localhost']68paths: ['/']69openstack_sd_configs:70- role: instance71region: fake72scaleway_sd_configs:73- role: instance74project_id: ffffffff-ffff-ffff-ffff-ffffffffffff75secret_key: ffffffff-ffff-ffff-ffff-ffffffffffff76access_key: SCWXXXXXXXXXXXXXXXXX77serverset_sd_configs:78- servers: ['localhost']79paths: ['/']80triton_sd_configs:81- account: fake82dns_suffix: fake83endpoint: fake84`),85expect: nil,86},87{88name: "invalid http client config",89input: util.Untab(`90scrape_configs:91- job_name: malicious_scrape92static_configs:93- targets: ['badsite.com']94basic_auth:95username: file_leak96password_file: /etc/password97remote_write:98- url: http://localhost:9009/api/prom/push99`),100expect: fmt.Errorf("failed to validate scrape_config at index 0: password_file must be empty unless dangerous_allow_reading_files is set"),101},102}103104for _, tc := range tt {105t.Run(tc.name, func(t *testing.T) {106cfg, err := instance.UnmarshalConfig(strings.NewReader(tc.input))107require.NoError(t, err)108109actual := validateNofiles(cfg)110if tc.expect == nil {111require.NoError(t, actual)112} else {113require.EqualError(t, actual, tc.expect.Error())114}115})116}117}118119120