| 1 | package exporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/ |
| 2 | |
| 3 | import ( |
| 4 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 5 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 6 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 7 | |
| 8 | "github.com/prometheus/client_golang/prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/" |
| 9 | ) |
| 10 | |
| 11 | func TestCreateClientTLSConfigscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestCreateClientTLSConfig().(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#) { |
| 12 | for _, test := range []struct { |
| 13 | name string |
| 14 | options Optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options# |
| 15 | expectSuccess bool |
| 16 | }{ |
| 17 | // positive tests |
| 18 | {"no_options", Optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#{}, true}, |
| 19 | {"skip_verificaton", Optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#{ |
| 20 | SkipTLSVerificationscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#SkipTLSVerification.: true}, true}, |
| 21 | {"load_client_keypair", Optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#{ |
| 22 | ClientCertFilescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#ClientCertFile.: "../contrib/tls/redis.crt", |
| 23 | ClientKeyFilescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#ClientKeyFile.: "../contrib/tls/redis.key"}, true}, |
| 24 | {"load_ca_cert", Optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#{ |
| 25 | CaCertFilescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#CaCertFile.: "../contrib/tls/ca.crt"}, true}, |
| 26 | {"load_system_certs", Optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#{}, true}, |
| 27 | |
| 28 | // negative tests |
| 29 | {"nonexisting_client_files", Optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#{ |
| 30 | ClientCertFilescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#ClientCertFile.: "/nonexisting/file", |
| 31 | ClientKeyFilescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#ClientKeyFile.: "/nonexisting/file"}, false}, |
| 32 | {"nonexisting_ca_file", Optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#{ |
| 33 | CaCertFilescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#CaCertFile.: "/nonexisting/file"}, false}, |
| 34 | } { |
| 35 | t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(test.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 | e := getTestExporterWithOptionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/getTestExporterWithOptions().(test.options) |
| 37 | |
| 38 | _, err := e.CreateClientTLSConfigscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#CreateClientTLSConfig().() |
| 39 | if test.expectSuccess && err != nil { |
| 40 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected success for test: %s, got err: %s", test.name, err) |
| 41 | return |
| 42 | } |
| 43 | }) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func TestValkeyTLSSchemescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestValkeyTLSScheme().(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 | for _, host := range []string{ |
| 49 | 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_REDIS7_TLS_URI"), |
| 50 | 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_VALKEY8_TLS_URI"), |
| 51 | } { |
| 52 | t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(host, 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#) { |
| 53 | |
| 54 | e, _ := NewRedisExporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/NewRedisExporter().(host, |
| 55 | Optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#{ |
| 56 | SkipTLSVerificationscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#SkipTLSVerification.: true, |
| 57 | ClientCertFilescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#ClientCertFile.: "../contrib/tls/redis.crt", |
| 58 | ClientKeyFilescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#ClientKeyFile.: "../contrib/tls/redis.key", |
| 59 | }, |
| 60 | ) |
| 61 | c, err := e.connectToRedisscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#connectToRedis().() |
| 62 | if err != nil { |
| 63 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("connectToRedis() err: %s", err) |
| 64 | } |
| 65 | |
| 66 | if _, err := c.Doscip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/Conn#Do().("PING", ""); err != nil { |
| 67 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("PING err: %s", err) |
| 68 | } |
| 69 | |
| 70 | c.Closescip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/Conn#Close().() |
| 71 | |
| 72 | chM := make(chan prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.Metricscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Metric#) |
| 73 | go func() { |
| 74 | e.Collectscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#Collect().(chM) |
| 75 | close(chM) |
| 76 | }() |
| 77 | |
| 78 | tsts := []struct { |
| 79 | in string |
| 80 | found bool |
| 81 | }{ |
| 82 | {in: "db_keys"}, |
| 83 | {in: "commands_total"}, |
| 84 | {in: "total_connections_received"}, |
| 85 | {in: "used_memory"}, |
| 86 | } |
| 87 | for m := range chM { |
| 88 | desc := m.Descscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Metric#Desc().().Stringscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Desc#String().() |
| 89 | for i := range tsts { |
| 90 | 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().(desc, tsts[i].in) { |
| 91 | tsts[i].found = true |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | }) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func TestCreateServerTLSConfigscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestCreateServerTLSConfig().(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#) { |
| 101 | e := getTestExporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/getTestExporter().() |
| 102 | |
| 103 | // positive tests |
| 104 | _, err := e.CreateServerTLSConfigscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#CreateServerTLSConfig().("../contrib/tls/redis.crt", "../contrib/tls/redis.key", "", "TLS1.1") |
| 105 | if err != nil { |
| 106 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("CreateServerTLSConfig() err: %s", err) |
| 107 | } |
| 108 | _, err = e.CreateServerTLSConfigscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#CreateServerTLSConfig().("../contrib/tls/redis.crt", "../contrib/tls/redis.key", "../contrib/tls/ca.crt", "TLS1.0") |
| 109 | if err != nil { |
| 110 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("CreateServerTLSConfig() err: %s", err) |
| 111 | } |
| 112 | |
| 113 | // negative tests |
| 114 | _, err = e.CreateServerTLSConfigscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#CreateServerTLSConfig().("/nonexisting/file", "/nonexisting/file", "", "TLS1.1") |
| 115 | if err == nil { |
| 116 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected CreateServerTLSConfig() to fail") |
| 117 | } |
| 118 | _, err = e.CreateServerTLSConfigscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#CreateServerTLSConfig().("/nonexisting/file", "/nonexisting/file", "/nonexisting/file", "TLS1.2") |
| 119 | if err == nil { |
| 120 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected CreateServerTLSConfig() to fail") |
| 121 | } |
| 122 | _, err = e.CreateServerTLSConfigscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#CreateServerTLSConfig().("../contrib/tls/redis.crt", "../contrib/tls/redis.key", "/nonexisting/file", "TLS1.3") |
| 123 | if err == nil { |
| 124 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected CreateServerTLSConfig() to fail") |
| 125 | } |
| 126 | _, err = e.CreateServerTLSConfigscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#CreateServerTLSConfig().("../contrib/tls/redis.crt", "../contrib/tls/redis.key", "../contrib/tls/ca.crt", "TLSX") |
| 127 | if err == nil { |
| 128 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected CreateServerTLSConfig() to fail") |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | func TestGetServerCertificateFuncscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestGetServerCertificateFunc().(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#) { |
| 133 | // positive test |
| 134 | _, err := GetServerCertificateFuncscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/GetServerCertificateFunc().("../contrib/tls/ca.crt", "../contrib/tls/ca.key")(nil) |
| 135 | if err != nil { |
| 136 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("GetServerCertificateFunc() err: %s", err) |
| 137 | } |
| 138 | |
| 139 | // negative test |
| 140 | _, err = GetServerCertificateFuncscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/GetServerCertificateFunc().("/nonexisting/file", "/nonexisting/file")(nil) |
| 141 | if err == nil { |
| 142 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected GetServerCertificateFunc() to fail") |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | func TestGetConfigForClientFuncscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestGetConfigForClientFunc().(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#) { |
| 147 | // positive test |
| 148 | _, err := GetConfigForClientFuncscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/GetConfigForClientFunc().("../contrib/tls/redis.crt", "../contrib/tls/redis.key", "../contrib/tls/ca.crt")(nil) |
| 149 | if err != nil { |
| 150 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("GetConfigForClientFunc() err: %s", err) |
| 151 | } |
| 152 | |
| 153 | // negative test |
| 154 | _, err = GetConfigForClientFuncscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/GetConfigForClientFunc().("/nonexisting/file", "/nonexisting/file", "/nonexisting/file")(nil) |
| 155 | if err == nil { |
| 156 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected GetConfigForClientFunc() to fail") |
| 157 | } |
| 158 | } |