| 1 | package exporter |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/prometheus/client_golang/prometheus" |
| 10 | dto "github.com/prometheus/client_model/go" |
| 11 | ) |
| 12 | |
| 13 | func newRdbTestExporter() *Exporter { |
| 14 | return &Exporter{ |
| 15 | options: Options{Namespace: "test"}, |
| 16 | metricDescriptions: map[string]*prometheus.Desc{ |
| 17 | "rdb_current_size_bytes": newMetricDescr("test", "rdb_current_size_bytes", "test metric", nil), |
| 18 | }, |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | func collectRdbSizeMetric(t *testing.T, e *Exporter, config map[string]string) (found bool, value float64) { |
| 23 | t.Helper() |
| 24 | ch := make(chan prometheus.Metric, 10) |
| 25 | e.extractRdbFileSizeMetric(ch, config) |
| 26 | close(ch) |
| 27 | |
| 28 | for m := range ch { |
| 29 | if !strings.Contains(m.Desc().String(), "rdb_current_size_bytes") { |
| 30 | continue |
| 31 | } |
| 32 | found = true |
| 33 | d := &dto.Metric{} |
| 34 | if err := m.Write(d); err == nil && d.GetGauge() != nil { |
| 35 | value = d.GetGauge().GetValue() |
| 36 | } |
| 37 | } |
| 38 | return found, value |
| 39 | } |
| 40 | |
| 41 | func TestExtractRdbFileSizeMetricNoOutput(t *testing.T) { |
| 42 | tsts := []struct { |
| 43 | name string |
| 44 | config map[string]string |
| 45 | }{ |
| 46 | {"empty config", map[string]string{}}, |
| 47 | {"missing dir and dbfilename", map[string]string{"maxmemory": "1000000", "timeout": "300"}}, |
| 48 | } |
| 49 | |
| 50 | for _, tst := range tsts { |
| 51 | t.Run(tst.name, func(t *testing.T) { |
| 52 | if found, _ := collectRdbSizeMetric(t, newRdbTestExporter(), tst.config); found { |
| 53 | t.Errorf("expected no metric, got one") |
| 54 | } |
| 55 | }) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func TestExtractRdbFileSizeMetricNonExistentFile(t *testing.T) { |
| 60 | found, val := collectRdbSizeMetric(t, newRdbTestExporter(), map[string]string{ |
| 61 | "dir": "/tmp/nonexistent_redis_dir_12345", |
| 62 | "dbfilename": "dump.rdb", |
| 63 | }) |
| 64 | if !found { |
| 65 | t.Fatal("expected metric to be produced for non-existent file") |
| 66 | } |
| 67 | if val != 0 { |
| 68 | t.Errorf("expected value 0 for non-existent file, got %f", val) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | func TestExtractRdbFileSizeMetricRegularFile(t *testing.T) { |
| 73 | tsts := []struct { |
| 74 | name string |
| 75 | size int64 |
| 76 | }{ |
| 77 | {"small file", 21}, |
| 78 | {"large file", 10 * 1024 * 1024}, |
| 79 | } |
| 80 | |
| 81 | for _, tst := range tsts { |
| 82 | t.Run(tst.name, func(t *testing.T) { |
| 83 | tmpDir := t.TempDir() |
| 84 | f, err := os.Create(filepath.Join(tmpDir, "dump.rdb")) |
| 85 | if err != nil { |
| 86 | t.Fatalf("failed to create test file: %s", err) |
| 87 | } |
| 88 | if err := f.Truncate(tst.size); err != nil { |
| 89 | f.Close() |
| 90 | t.Fatalf("failed to truncate file: %s", err) |
| 91 | } |
| 92 | f.Close() |
| 93 | |
| 94 | found, val := collectRdbSizeMetric(t, newRdbTestExporter(), map[string]string{ |
| 95 | "dir": tmpDir, |
| 96 | "dbfilename": "dump.rdb", |
| 97 | }) |
| 98 | if !found { |
| 99 | t.Fatal("expected metric to be produced") |
| 100 | } |
| 101 | if val != float64(tst.size) { |
| 102 | t.Errorf("expected value %d, got %f", tst.size, val) |
| 103 | } |
| 104 | }) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func TestExtractRdbFileSizeMetricUnsafeDbfilename(t *testing.T) { |
| 109 | tsts := []struct { |
| 110 | name string |
| 111 | dbfilename string |
| 112 | }{ |
| 113 | {"parent traversal", "../../../etc/passwd"}, |
| 114 | {"absolute path", "/etc/passwd"}, |
| 115 | {"subdir path", "sub/dump.rdb"}, |
| 116 | {"dot", "."}, |
| 117 | {"dotdot", ".."}, |
| 118 | } |
| 119 | |
| 120 | for _, tst := range tsts { |
| 121 | t.Run(tst.name, func(t *testing.T) { |
| 122 | if found, _ := collectRdbSizeMetric(t, newRdbTestExporter(), map[string]string{ |
| 123 | "dir": "/tmp", |
| 124 | "dbfilename": tst.dbfilename, |
| 125 | }); found { |
| 126 | t.Errorf("expected no metric for unsafe dbfilename %q, got one", tst.dbfilename) |
| 127 | } |
| 128 | }) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | func TestExtractRdbFileSizeMetricDirectory(t *testing.T) { |
| 133 | tmpDir := t.TempDir() |
| 134 | if err := os.Mkdir(filepath.Join(tmpDir, "subdir"), 0755); err != nil { |
| 135 | t.Fatalf("failed to create test directory: %s", err) |
| 136 | } |
| 137 | |
| 138 | if found, _ := collectRdbSizeMetric(t, newRdbTestExporter(), map[string]string{ |
| 139 | "dir": tmpDir, |
| 140 | "dbfilename": "subdir", |
| 141 | }); found { |
| 142 | t.Error("expected no metric for a directory, got one") |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | func TestExtractRdbFileSizeMetricIntegration(t *testing.T) { |
| 147 | if os.Getenv("TEST_REDIS_URI") == "" { |
| 148 | t.Skipf("TEST_REDIS_URI not set - skipping") |
| 149 | } |
| 150 | |
| 151 | tsts := []struct { |
| 152 | name string |
| 153 | opt Options |
| 154 | wantMetric bool |
| 155 | }{ |
| 156 | {"enabled", Options{Namespace: "test", InclRdbFileSizeMetric: true, ConfigCommandName: "CONFIG"}, true}, |
| 157 | {"disabled", Options{Namespace: "test", InclRdbFileSizeMetric: false, ConfigCommandName: "CONFIG"}, false}, |
| 158 | {"config command disabled", Options{Namespace: "test", InclRdbFileSizeMetric: true, ConfigCommandName: "-"}, false}, |
| 159 | } |
| 160 | |
| 161 | for _, tst := range tsts { |
| 162 | t.Run(tst.name, func(t *testing.T) { |
| 163 | e, err := NewRedisExporter(os.Getenv("TEST_REDIS_URI"), tst.opt) |
| 164 | if err != nil { |
| 165 | t.Fatalf("NewRedisExporter() failed: %s", err) |
| 166 | } |
| 167 | |
| 168 | ch := make(chan prometheus.Metric, 100) |
| 169 | go func() { |
| 170 | e.Collect(ch) |
| 171 | close(ch) |
| 172 | }() |
| 173 | |
| 174 | found := false |
| 175 | var value float64 |
| 176 | for m := range ch { |
| 177 | if !strings.Contains(m.Desc().String(), "rdb_current_size_bytes") { |
| 178 | continue |
| 179 | } |
| 180 | found = true |
| 181 | d := &dto.Metric{} |
| 182 | if err := m.Write(d); err == nil && d.GetGauge() != nil { |
| 183 | value = d.GetGauge().GetValue() |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | if found != tst.wantMetric { |
| 188 | t.Errorf("metric present = %v, want %v", found, tst.wantMetric) |
| 189 | } |
| 190 | if found && value < 0 { |
| 191 | t.Errorf("rdb_current_size_bytes should not be negative, got %f", value) |
| 192 | } |
| 193 | }) |
| 194 | } |
| 195 | } |