Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/config/remote_config_test.go
4094 views
1
package config
2
3
import (
4
"fmt"
5
"net/http"
6
"net/http/httptest"
7
"os"
8
"testing"
9
10
"github.com/prometheus/common/config"
11
"github.com/stretchr/testify/assert"
12
"github.com/stretchr/testify/require"
13
)
14
15
func TestRemoteConfigHTTP(t *testing.T) {
16
testCfg := `
17
metrics:
18
global:
19
scrape_timeout: 33s
20
`
21
22
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
23
if r.URL.Path == "/agent.yml" {
24
_, _ = w.Write([]byte(testCfg))
25
}
26
}))
27
28
svrWithBasicAuth := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
29
user, pass, _ := r.BasicAuth()
30
if user != "foo" && pass != "bar" {
31
w.WriteHeader(http.StatusUnauthorized)
32
return
33
}
34
if r.URL.Path == "/agent.yml" {
35
_, _ = w.Write([]byte(testCfg))
36
}
37
}))
38
39
tempDir := t.TempDir()
40
err := os.WriteFile(fmt.Sprintf("%s/password-file.txt", tempDir), []byte("bar"), 0644)
41
require.NoError(t, err)
42
43
passwdFileCfg := &config.HTTPClientConfig{
44
BasicAuth: &config.BasicAuth{
45
Username: "foo",
46
PasswordFile: fmt.Sprintf("%s/password-file.txt", tempDir),
47
},
48
}
49
dir, err := os.Getwd()
50
require.NoError(t, err)
51
passwdFileCfg.SetDirectory(dir)
52
53
type args struct {
54
rawURL string
55
opts *remoteOpts
56
}
57
tests := []struct {
58
name string
59
args args
60
want []byte
61
wantErr bool
62
}{
63
{
64
name: "httpScheme config",
65
args: args{
66
rawURL: fmt.Sprintf("%s/agent.yml", svr.URL),
67
},
68
want: []byte(testCfg),
69
wantErr: false,
70
},
71
{
72
name: "httpScheme config with basic auth",
73
args: args{
74
rawURL: fmt.Sprintf("%s/agent.yml", svrWithBasicAuth.URL),
75
opts: &remoteOpts{
76
HTTPClientConfig: &config.HTTPClientConfig{
77
BasicAuth: &config.BasicAuth{
78
Username: "foo",
79
Password: "bar",
80
},
81
},
82
},
83
},
84
want: []byte(testCfg),
85
wantErr: false,
86
},
87
{
88
name: "httpScheme config with basic auth password file",
89
args: args{
90
rawURL: fmt.Sprintf("%s/agent.yml", svrWithBasicAuth.URL),
91
opts: &remoteOpts{
92
HTTPClientConfig: passwdFileCfg,
93
},
94
},
95
want: []byte(testCfg),
96
wantErr: false,
97
},
98
{
99
name: "unsupported scheme throws error",
100
args: args{
101
rawURL: "ssh://unsupported/scheme",
102
},
103
want: nil,
104
wantErr: true,
105
},
106
{
107
name: "invalid url throws error",
108
args: args{
109
rawURL: "://invalid/url",
110
},
111
want: nil,
112
wantErr: true,
113
},
114
}
115
116
for _, tt := range tests {
117
t.Run(tt.name, func(t *testing.T) {
118
rc, err := newRemoteProvider(tt.args.rawURL, tt.args.opts)
119
if tt.wantErr {
120
assert.Error(t, err)
121
return
122
}
123
assert.NoError(t, err)
124
bb, err := rc.retrieve()
125
assert.NoError(t, err)
126
assert.Equal(t, string(tt.want), string(bb))
127
})
128
}
129
}
130
131