| 1 | package exporter |
| 2 | |
| 3 | import ( |
| 4 | "net/http/httptest" |
| 5 | "net/url" |
| 6 | "os" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestHostVariations(t *testing.T) { |
| 12 | host := strings.ReplaceAll(os.Getenv("TEST_REDIS_URI"), "redis://", "") |
| 13 | |
| 14 | for _, prefix := range []string{"", "redis://", "tcp://", ""} { |
| 15 | e, _ := NewRedisExporter(prefix+host, Options{SkipTLSVerification: true}) |
| 16 | c, err := e.connectToRedis() |
| 17 | if err != nil { |
| 18 | t.Errorf("connectToRedis() err: %s", err) |
| 19 | continue |
| 20 | } |
| 21 | |
| 22 | if _, err := c.Do("PING", ""); err != nil { |
| 23 | t.Errorf("PING err: %s", err) |
| 24 | } |
| 25 | |
| 26 | c.Close() |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func TestValkeyScheme(t *testing.T) { |
| 31 | host := os.Getenv("TEST_VALKEY8_URI") |
| 32 | |
| 33 | e, _ := NewRedisExporter(host, Options{SkipTLSVerification: true}) |
| 34 | c, err := e.connectToRedis() |
| 35 | if err != nil { |
| 36 | t.Fatalf("connectToRedis() err: %s", err) |
| 37 | } |
| 38 | |
| 39 | if _, err := c.Do("PING", ""); err != nil { |
| 40 | t.Errorf("PING err: %s", err) |
| 41 | } |
| 42 | |
| 43 | c.Close() |
| 44 | } |
| 45 | |
| 46 | func TestPasswordProtectedInstance(t *testing.T) { |
| 47 | userAddr := os.Getenv("TEST_USER_PWD_REDIS_URI") |
| 48 | if userAddr == "" { |
| 49 | t.Skipf("Skipping TestHTTPScrapeWithPasswordFile, missing env variables") |
| 50 | } |
| 51 | |
| 52 | parsedPassword := "" |
| 53 | parsed, err := url.Parse(userAddr) |
| 54 | if err == nil && parsed.User != nil { |
| 55 | parsedPassword, _ = parsed.User.Password() |
| 56 | } |
| 57 | |
| 58 | tsts := []struct { |
| 59 | name string |
| 60 | addr string |
| 61 | user string |
| 62 | pwd string |
| 63 | }{ |
| 64 | { |
| 65 | name: "TEST_PWD_REDIS_URI", |
| 66 | addr: os.Getenv("TEST_PWD_REDIS_URI"), |
| 67 | }, |
| 68 | { |
| 69 | name: "TEST_USER_PWD_REDIS_URI", |
| 70 | addr: userAddr, |
| 71 | }, |
| 72 | { |
| 73 | name: "parsed-TEST_USER_PWD_REDIS_URI", |
| 74 | addr: parsed.Host, |
| 75 | user: parsed.User.Username(), |
| 76 | pwd: parsedPassword, |
| 77 | }, |
| 78 | } |
| 79 | |
| 80 | for _, tst := range tsts { |
| 81 | t.Run(tst.name, func(t *testing.T) { |
| 82 | e, _ := NewRedisExporter( |
| 83 | tst.addr, |
| 84 | Options{ |
| 85 | Namespace: "test", |
| 86 | User: tst.user, |
| 87 | Password: tst.pwd, |
| 88 | }) |
| 89 | ts := httptest.NewServer(e) |
| 90 | defer ts.Close() |
| 91 | |
| 92 | body := downloadURL(t, ts.URL+"/metrics") |
| 93 | if !strings.Contains(body, "test_up 1") { |
| 94 | t.Errorf(`%s - response to /metric doesn't contain "test_up 1"`, tst) |
| 95 | } |
| 96 | }) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func TestPasswordInvalid(t *testing.T) { |
| 101 | if os.Getenv("TEST_PWD_REDIS_URI") == "" { |
| 102 | t.Skipf("TEST_PWD_REDIS_URI not set - skipping") |
| 103 | } |
| 104 | |
| 105 | testPwd := "redis-password" |
| 106 | uri := strings.Replace(os.Getenv("TEST_PWD_REDIS_URI"), testPwd, "wrong-pwd", -1) |
| 107 | |
| 108 | e, _ := NewRedisExporter(uri, Options{Namespace: "test"}) |
| 109 | ts := httptest.NewServer(e) |
| 110 | defer ts.Close() |
| 111 | |
| 112 | want := `test_exporter_last_scrape_error{err="dial redis: unknown network redis"} 1` |
| 113 | body := downloadURL(t, ts.URL+"/metrics") |
| 114 | if !strings.Contains(body, want) { |
| 115 | t.Errorf(`error, expected string "%s" in body, got body: \n\n%s`, want, body) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | func TestConnectToClusterUsingPasswordFile(t *testing.T) { |
| 120 | clusterUri := os.Getenv("TEST_VALKEY_CLUSTER_PASSWORD_URI") |
| 121 | if clusterUri == "" { |
| 122 | t.Skipf("TEST_VALKEY_CLUSTER_PASSWORD_URI is not set") |
| 123 | } |
| 124 | passMap := map[string]string{clusterUri: "redis-password"} |
| 125 | wrongPassMap := map[string]string{"redis://redis-cluster-password-wrong:7006": "redis-password"} |
| 126 | |
| 127 | tsts := []struct { |
| 128 | name string |
| 129 | isCluster bool |
| 130 | passMap map[string]string |
| 131 | refreshError bool |
| 132 | }{ |
| 133 | {name: "ConnectToCluster using password file with cluster mode", isCluster: true, passMap: passMap, refreshError: false}, |
| 134 | {name: "ConnectToCluster using password file without cluster mode", isCluster: false, passMap: passMap, refreshError: false}, |
| 135 | {name: "ConnectToCluster using password file with cluster mode failed", isCluster: false, passMap: wrongPassMap, refreshError: true}, |
| 136 | } |
| 137 | for _, tst := range tsts { |
| 138 | t.Run(tst.name, func(t *testing.T) { |
| 139 | e, _ := NewRedisExporter(clusterUri, Options{ |
| 140 | SkipTLSVerification: true, |
| 141 | PasswordMap: tst.passMap, |
| 142 | IsCluster: tst.isCluster, |
| 143 | }) |
| 144 | _, err := e.connectToRedisCluster() |
| 145 | t.Logf("connectToRedisCluster() err: %s", err) |
| 146 | if err != nil && strings.Contains(err.Error(), "Cluster refresh failed:") && !tst.refreshError { |
| 147 | t.Fatalf("Test Cluster connection Failed error") |
| 148 | } |
| 149 | if !tst.refreshError && err != nil { |
| 150 | t.Fatalf("Test Cluster connection Failed, err: %s", err) |
| 151 | } |
| 152 | }) |
| 153 | } |
| 154 | } |