| 1 | package exporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/ |
| 2 | |
| 3 | import ( |
| 4 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 5 | "net/httpscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/" |
| 6 | "net/http/httptestscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/" |
| 7 | "net/urlscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/" |
| 8 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 9 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 10 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 11 | ) |
| 12 | |
| 13 | func TestLoadPwdFilescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestLoadPwdFile().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 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.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(tst.name, func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 36 | _, err := LoadPwdFilescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/LoadPwdFile().(tst.pwdFile) |
| 37 | if err == nil && !tst.ok { |
| 38 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Test Failed, result is not what we want") |
| 39 | } |
| 40 | if err != nil && tst.ok { |
| 41 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Test Failed, result is not what we want") |
| 42 | } |
| 43 | }) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func TestPasswordMapscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestPasswordMap().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 48 | pwdFile := "../contrib/sample-pwd-file.json" |
| 49 | passwordMap, err := LoadPwdFilescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/LoadPwdFile().(pwdFile) |
| 50 | if err != nil { |
| 51 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Test Failed, error: %v", err) |
| 52 | } |
| 53 | |
| 54 | if len(passwordMap) == 0 { |
| 55 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#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.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(tst.name, func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 67 | pwd := passwordMap[tst.addr] |
| 68 | if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(pwd, tst.want) { |
| 69 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("redis host: %s password is not what we want", tst.addr) |
| 70 | } |
| 71 | }) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func TestHTTPScrapeWithPasswordFilescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestHTTPScrapeWithPasswordFile().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 76 | if osscip-go gomod github.com/golang/go/src go1.25.0 os/.Getenvscip-go gomod github.com/golang/go/src go1.25.0 os/Getenv().("TEST_PWD_REDIS_URI") == "" { |
| 77 | t.Skipfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skipf().("Skipping TestHTTPScrapeWithPasswordFile, missing env variables") |
| 78 | } |
| 79 | |
| 80 | pwdFile := "../contrib/sample-pwd-file.json" |
| 81 | passwordMap, err := LoadPwdFilescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/LoadPwdFile().(pwdFile) |
| 82 | if err != nil { |
| 83 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Test Failed, error: %v", err) |
| 84 | } |
| 85 | |
| 86 | if len(passwordMap) == 0 { |
| 87 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#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: osscip-go gomod github.com/golang/go/src go1.25.0 os/.Getenvscip-go gomod github.com/golang/go/src go1.25.0 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 := Optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#{ |
| 108 | Namespacescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#Namespace.: "test", |
| 109 | PasswordMapscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#PasswordMap.: passwordMap, |
| 110 | LuaScriptscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#LuaScript.: map[string][]byte{ |
| 111 | "test.lua": []byte(`return {"a", "11", "b", "12", "c", "13"}`), |
| 112 | }, |
| 113 | } |
| 114 | t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(tst.name, func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 115 | e, _ := NewRedisExporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/NewRedisExporter().(tst.addr, options) |
| 116 | ts := httptestscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/.NewServerscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/NewServer().(e) |
| 117 | |
| 118 | u := ts.URLscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/Server#URL. |
| 119 | u += "/scrape" |
| 120 | v := urlscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/.Valuesscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/Values#{} |
| 121 | v.Addscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/Values#Add().("target", tst.addr) |
| 122 | |
| 123 | up, _ := urlscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/.Parsescip-go gomod github.com/golang/go/src go1.25.0 `net/url`/Parse().(u) |
| 124 | up.RawQueryscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/URL#RawQuery. = v.Encodescip-go gomod github.com/golang/go/src go1.25.0 `net/url`/Values#Encode().() |
| 125 | u = up.Stringscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/URL#String().() |
| 126 | |
| 127 | wantStatusCode := httpscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/.StatusOKscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/StatusOK. |
| 128 | if tst.wantStatusCode != 0 { |
| 129 | wantStatusCode = tst.wantStatusCode |
| 130 | } |
| 131 | |
| 132 | gotStatusCode, body := downloadURLWithStatusCodescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/downloadURLWithStatusCode().(t, u) |
| 133 | |
| 134 | if gotStatusCode != wantStatusCode { |
| 135 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#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 != httpscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/.StatusOKscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/StatusOK. { |
| 141 | return |
| 142 | } |
| 143 | |
| 144 | for _, want := range tst.wants { |
| 145 | if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(body, want) { |
| 146 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("url: %s want metrics to include %q, have:\n%s", u, want, body) |
| 147 | break |
| 148 | } |
| 149 | } |
| 150 | ts.Closescip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/Server#Close().() |
| 151 | }) |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | func TestHTTPScrapeWithUsernamescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestHTTPScrapeWithUsername().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 156 | if osscip-go gomod github.com/golang/go/src go1.25.0 os/.Getenvscip-go gomod github.com/golang/go/src go1.25.0 os/Getenv().("TEST_USER_PWD_REDIS_URI") == "" { |
| 157 | t.Skipfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skipf().("Skipping TestHTTPScrapeWithPasswordFile, missing env variables") |
| 158 | } |
| 159 | |
| 160 | pwdFile := "../contrib/sample-pwd-file.json" |
| 161 | passwordMap, err := LoadPwdFilescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/LoadPwdFile().(pwdFile) |
| 162 | if err != nil { |
| 163 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Test Failed, error: %v", err) |
| 164 | } |
| 165 | |
| 166 | if len(passwordMap) == 0 { |
| 167 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#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 := urlscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/.Parsescip-go gomod github.com/golang/go/src go1.25.0 `net/url`/Parse().(osscip-go gomod github.com/golang/go/src go1.25.0 os/.Getenvscip-go gomod github.com/golang/go/src go1.25.0 os/Getenv().("TEST_USER_PWD_REDIS_URI")) |
| 173 | u.Userscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/URL#User. = urlscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/.Userscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/User().(u.Userscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/URL#User..Usernamescip-go gomod github.com/golang/go/src go1.25.0 `net/url`/Userinfo#Username().()) |
| 174 | uriWithUser := u.Stringscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/URL#String().() |
| 175 | uriWithUser = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Replacescip-go gomod github.com/golang/go/src go1.25.0 strings/Replace().(uriWithUser, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().(":@%s", u.Hostscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/URL#Host.), fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("@%s", u.Hostscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/URL#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: httpscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/.StatusOKscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/StatusOK., |
| 186 | addr: uriWithUser, wants: []string{ |
| 187 | "uptime_in_seconds", |
| 188 | "test_up 1", |
| 189 | }}, |
| 190 | } { |
| 191 | options := Optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#{ |
| 192 | Namespacescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#Namespace.: "test", |
| 193 | PasswordMapscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#PasswordMap.: passwordMap, |
| 194 | } |
| 195 | t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(tst.name, func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 196 | e, _ := NewRedisExporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/NewRedisExporter().(tst.addr, options) |
| 197 | ts := httptestscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/.NewServerscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/NewServer().(e) |
| 198 | |
| 199 | u := ts.URLscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/Server#URL. |
| 200 | u += "/scrape" |
| 201 | v := urlscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/.Valuesscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/Values#{} |
| 202 | v.Addscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/Values#Add().("target", tst.addr) |
| 203 | |
| 204 | up, _ := urlscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/.Parsescip-go gomod github.com/golang/go/src go1.25.0 `net/url`/Parse().(u) |
| 205 | up.RawQueryscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/URL#RawQuery. = v.Encodescip-go gomod github.com/golang/go/src go1.25.0 `net/url`/Values#Encode().() |
| 206 | u = up.Stringscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/URL#String().() |
| 207 | |
| 208 | gotStatusCode, body := downloadURLWithStatusCodescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/downloadURLWithStatusCode().(t, u) |
| 209 | |
| 210 | if gotStatusCode != tst.wantStatusCode { |
| 211 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#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 != httpscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/.StatusOKscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/StatusOK. { |
| 217 | return |
| 218 | } |
| 219 | |
| 220 | for _, want := range tst.wants { |
| 221 | if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(body, want) { |
| 222 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("url: %s want metrics to include %q, have:\n%s", u, want, body) |
| 223 | break |
| 224 | } |
| 225 | } |
| 226 | ts.Closescip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/Server#Close().() |
| 227 | }) |
| 228 | } |
| 229 | } |