| 1 | package exporter |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/prometheus/client_golang/prometheus" |
| 9 | ) |
| 10 | |
| 11 | func TestCreateClientTLSConfig(t *testing.T) { |
| 12 | for _, test := range []struct { |
| 13 | name string |
| 14 | options Options |
| 15 | expectSuccess bool |
| 16 | }{ |
| 17 | // positive tests |
| 18 | {"no_options", Options{}, true}, |
| 19 | {"skip_verificaton", Options{ |
| 20 | SkipTLSVerification: true}, true}, |
| 21 | {"load_client_keypair", Options{ |
| 22 | ClientCertFile: "../contrib/tls/redis.crt", |
| 23 | ClientKeyFile: "../contrib/tls/redis.key"}, true}, |
| 24 | {"load_ca_cert", Options{ |
| 25 | CaCertFile: "../contrib/tls/ca.crt"}, true}, |
| 26 | {"load_system_certs", Options{}, true}, |
| 27 | |
| 28 | // negative tests |
| 29 | {"nonexisting_client_files", Options{ |
| 30 | ClientCertFile: "/nonexisting/file", |
| 31 | ClientKeyFile: "/nonexisting/file"}, false}, |
| 32 | {"nonexisting_ca_file", Options{ |
| 33 | CaCertFile: "/nonexisting/file"}, false}, |
| 34 | } { |
| 35 | t.Run(test.name, func(t *testing.T) { |
| 36 | e := getTestExporterWithOptions(test.options) |
| 37 | |
| 38 | _, err := e.CreateClientTLSConfig() |
| 39 | if test.expectSuccess && err != nil { |
| 40 | t.Errorf("Expected success for test: %s, got err: %s", test.name, err) |
| 41 | return |
| 42 | } |
| 43 | }) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func TestValkeyTLSScheme(t *testing.T) { |
| 48 | for _, host := range []string{ |
| 49 | os.Getenv("TEST_REDIS7_TLS_URI"), |
| 50 | os.Getenv("TEST_VALKEY8_TLS_URI"), |
| 51 | } { |
| 52 | t.Run(host, func(t *testing.T) { |
| 53 | |
| 54 | e, _ := NewRedisExporter(host, |
| 55 | Options{ |
| 56 | SkipTLSVerification: true, |
| 57 | ClientCertFile: "../contrib/tls/redis.crt", |
| 58 | ClientKeyFile: "../contrib/tls/redis.key", |
| 59 | }, |
| 60 | ) |
| 61 | c, err := e.connectToRedis() |
| 62 | if err != nil { |
| 63 | t.Fatalf("connectToRedis() err: %s", err) |
| 64 | } |
| 65 | |
| 66 | if _, err := c.Do("PING", ""); err != nil { |
| 67 | t.Errorf("PING err: %s", err) |
| 68 | } |
| 69 | |
| 70 | c.Close() |
| 71 | |
| 72 | chM := make(chan prometheus.Metric) |
| 73 | go func() { |
| 74 | e.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.Desc().String() |
| 89 | for i := range tsts { |
| 90 | if strings.Contains(desc, tsts[i].in) { |
| 91 | tsts[i].found = true |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | }) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func TestCreateServerTLSConfig(t *testing.T) { |
| 101 | e := getTestExporter() |
| 102 | |
| 103 | // positive tests |
| 104 | _, err := e.CreateServerTLSConfig("../contrib/tls/redis.crt", "../contrib/tls/redis.key", "", "TLS1.1") |
| 105 | if err != nil { |
| 106 | t.Errorf("CreateServerTLSConfig() err: %s", err) |
| 107 | } |
| 108 | _, err = e.CreateServerTLSConfig("../contrib/tls/redis.crt", "../contrib/tls/redis.key", "../contrib/tls/ca.crt", "TLS1.0") |
| 109 | if err != nil { |
| 110 | t.Errorf("CreateServerTLSConfig() err: %s", err) |
| 111 | } |
| 112 | |
| 113 | // negative tests |
| 114 | _, err = e.CreateServerTLSConfig("/nonexisting/file", "/nonexisting/file", "", "TLS1.1") |
| 115 | if err == nil { |
| 116 | t.Errorf("Expected CreateServerTLSConfig() to fail") |
| 117 | } |
| 118 | _, err = e.CreateServerTLSConfig("/nonexisting/file", "/nonexisting/file", "/nonexisting/file", "TLS1.2") |
| 119 | if err == nil { |
| 120 | t.Errorf("Expected CreateServerTLSConfig() to fail") |
| 121 | } |
| 122 | _, err = e.CreateServerTLSConfig("../contrib/tls/redis.crt", "../contrib/tls/redis.key", "/nonexisting/file", "TLS1.3") |
| 123 | if err == nil { |
| 124 | t.Errorf("Expected CreateServerTLSConfig() to fail") |
| 125 | } |
| 126 | _, err = e.CreateServerTLSConfig("../contrib/tls/redis.crt", "../contrib/tls/redis.key", "../contrib/tls/ca.crt", "TLSX") |
| 127 | if err == nil { |
| 128 | t.Errorf("Expected CreateServerTLSConfig() to fail") |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | func TestGetServerCertificateFunc(t *testing.T) { |
| 133 | // positive test |
| 134 | _, err := GetServerCertificateFunc("../contrib/tls/ca.crt", "../contrib/tls/ca.key")(nil) |
| 135 | if err != nil { |
| 136 | t.Errorf("GetServerCertificateFunc() err: %s", err) |
| 137 | } |
| 138 | |
| 139 | // negative test |
| 140 | _, err = GetServerCertificateFunc("/nonexisting/file", "/nonexisting/file")(nil) |
| 141 | if err == nil { |
| 142 | t.Errorf("Expected GetServerCertificateFunc() to fail") |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | func TestGetConfigForClientFunc(t *testing.T) { |
| 147 | // positive test |
| 148 | _, err := GetConfigForClientFunc("../contrib/tls/redis.crt", "../contrib/tls/redis.key", "../contrib/tls/ca.crt")(nil) |
| 149 | if err != nil { |
| 150 | t.Errorf("GetConfigForClientFunc() err: %s", err) |
| 151 | } |
| 152 | |
| 153 | // negative test |
| 154 | _, err = GetConfigForClientFunc("/nonexisting/file", "/nonexisting/file", "/nonexisting/file")(nil) |
| 155 | if err == nil { |
| 156 | t.Errorf("Expected GetConfigForClientFunc() to fail") |
| 157 | } |
| 158 | } |