Debian Codegraph @

exporter/pwd_file_test.go

raw symbols SCIP
1 package exporter
2
3 import (
4 "fmt"
5 "net/http"
6 "net/http/httptest"
7 "net/url"
8 "os"
9 "strings"
10 "testing"
11 )
12
13 func TestLoadPwdFile(t *testing.T) {
14 for _, tst := range []struct {
15 name string
16 pwdFile string
17 ok bool
18 }{
19 {
20 name: "load-password-file-success",
21 pwdFile: "../contrib/sample-pwd-file.json",
22 ok: true,
23 },
24 {
25 name: "load-password-file-missing",
26 pwdFile: "non-existent.json",
27 ok: false,
28 },
29 {
30 name: "load-password-file-malformed",
31 pwdFile: "../contrib/sample-pwd-file.json-malformed",
32 ok: false,
33 },
34 } {
35 t.Run(tst.name, func(t *testing.T) {
36 _, err := LoadPwdFile(tst.pwdFile)
37 if err == nil && !tst.ok {
38 t.Fatalf("Test Failed, result is not what we want")
39 }
40 if err != nil && tst.ok {
41 t.Fatalf("Test Failed, result is not what we want")
42 }
43 })
44 }
45 }
46
47 func TestPasswordMap(t *testing.T) {
48 pwdFile := "../contrib/sample-pwd-file.json"
49 passwordMap, err := LoadPwdFile(pwdFile)
50 if err != nil {
51 t.Fatalf("Test Failed, error: %v", err)
52 }
53
54 if len(passwordMap) == 0 {
55 t.Fatalf("Password map is empty - failing")
56 }
57
58 for _, tst := range []struct {
59 name string
60 addr string
61 want string
62 }{
63 {name: "password-hit", addr: "redis://localhost:16380", want: "redis-password"},
64 {name: "password-missed", addr: "Non-existent-redis-host", want: ""},
65 } {
66 t.Run(tst.name, func(t *testing.T) {
67 pwd := passwordMap[tst.addr]
68 if !strings.Contains(pwd, tst.want) {
69 t.Errorf("redis host: %s password is not what we want", tst.addr)
70 }
71 })
72 }
73 }
74
75 func TestHTTPScrapeWithPasswordFile(t *testing.T) {
76 if os.Getenv("TEST_PWD_REDIS_URI") == "" {
77 t.Skipf("Skipping TestHTTPScrapeWithPasswordFile, missing env variables")
78 }
79
80 pwdFile := "../contrib/sample-pwd-file.json"
81 passwordMap, err := LoadPwdFile(pwdFile)
82 if err != nil {
83 t.Fatalf("Test Failed, error: %v", err)
84 }
85
86 if len(passwordMap) == 0 {
87 t.Fatalf("Password map is empty!")
88 }
89 for _, tst := range []struct {
90 name string
91 addr string
92 wants []string
93 useWrongPassword bool
94 wantStatusCode int
95 }{
96 {name: "scrape-pwd-file", addr: os.Getenv("TEST_PWD_REDIS_URI"), wants: []string{
97 "uptime_in_seconds",
98 "test_up 1",
99 }},
100 {name: "scrape-pwd-file-wrong-password", addr: "redis://localhost:16380", useWrongPassword: true, wants: []string{
101 "test_up 0",
102 }},
103 } {
104 if tst.useWrongPassword {
105 passwordMap[tst.addr] = "wrong-password"
106 }
107 options := Options{
108 Namespace: "test",
109 PasswordMap: passwordMap,
110 LuaScript: map[string][]byte{
111 "test.lua": []byte(`return {"a", "11", "b", "12", "c", "13"}`),
112 },
113 }
114 t.Run(tst.name, func(t *testing.T) {
115 e, _ := NewRedisExporter(tst.addr, options)
116 ts := httptest.NewServer(e)
117
118 u := ts.URL
119 u += "/scrape"
120 v := url.Values{}
121 v.Add("target", tst.addr)
122
123 up, _ := url.Parse(u)
124 up.RawQuery = v.Encode()
125 u = up.String()
126
127 wantStatusCode := http.StatusOK
128 if tst.wantStatusCode != 0 {
129 wantStatusCode = tst.wantStatusCode
130 }
131
132 gotStatusCode, body := downloadURLWithStatusCode(t, u)
133
134 if gotStatusCode != wantStatusCode {
135 t.Fatalf("got status code: %d wanted: %d", gotStatusCode, wantStatusCode)
136 return
137 }
138
139 // we can stop here if we expected a non-200 response
140 if wantStatusCode != http.StatusOK {
141 return
142 }
143
144 for _, want := range tst.wants {
145 if !strings.Contains(body, want) {
146 t.Errorf("url: %s want metrics to include %q, have:\n%s", u, want, body)
147 break
148 }
149 }
150 ts.Close()
151 })
152 }
153 }
154
155 func TestHTTPScrapeWithUsername(t *testing.T) {
156 if os.Getenv("TEST_USER_PWD_REDIS_URI") == "" {
157 t.Skipf("Skipping TestHTTPScrapeWithPasswordFile, missing env variables")
158 }
159
160 pwdFile := "../contrib/sample-pwd-file.json"
161 passwordMap, err := LoadPwdFile(pwdFile)
162 if err != nil {
163 t.Fatalf("Test Failed, error: %v", err)
164 }
165
166 if len(passwordMap) == 0 {
167 t.Fatalf("Password map is empty!")
168 }
169
170 // use provided uri but remove password before sending it over the wire
171 // after all, we want to test the lookup in the password map
172 u, err := url.Parse(os.Getenv("TEST_USER_PWD_REDIS_URI"))
173 u.User = url.User(u.User.Username())
174 uriWithUser := u.String()
175 uriWithUser = strings.Replace(uriWithUser, fmt.Sprintf(":@%s", u.Host), fmt.Sprintf("@%s", u.Host), 1)
176
177 for _, tst := range []struct {
178 name string
179 addr string
180 wants []string
181 wantStatusCode int
182 }{
183 {
184 name: "scrape-pwd-file",
185 wantStatusCode: http.StatusOK,
186 addr: uriWithUser, wants: []string{
187 "uptime_in_seconds",
188 "test_up 1",
189 }},
190 } {
191 options := Options{
192 Namespace: "test",
193 PasswordMap: passwordMap,
194 }
195 t.Run(tst.name, func(t *testing.T) {
196 e, _ := NewRedisExporter(tst.addr, options)
197 ts := httptest.NewServer(e)
198
199 u := ts.URL
200 u += "/scrape"
201 v := url.Values{}
202 v.Add("target", tst.addr)
203
204 up, _ := url.Parse(u)
205 up.RawQuery = v.Encode()
206 u = up.String()
207
208 gotStatusCode, body := downloadURLWithStatusCode(t, u)
209
210 if gotStatusCode != tst.wantStatusCode {
211 t.Fatalf("got status code: %d wanted: %d", gotStatusCode, tst.wantStatusCode)
212 return
213 }
214
215 // we can stop here if we expected a non-200 response
216 if tst.wantStatusCode != http.StatusOK {
217 return
218 }
219
220 for _, want := range tst.wants {
221 if !strings.Contains(body, want) {
222 t.Errorf("url: %s want metrics to include %q, have:\n%s", u, want, body)
223 break
224 }
225 }
226 ts.Close()
227 })
228 }
229 }