github.com/spotahome/redis-operator@v1.2.4/metrics/metrics_test.go (about) 1 package metrics_test 2 3 import ( 4 "io" 5 "net/http" 6 "net/http/httptest" 7 "testing" 8 9 "github.com/prometheus/client_golang/prometheus" 10 "github.com/prometheus/client_golang/prometheus/promhttp" 11 12 "github.com/stretchr/testify/assert" 13 14 "github.com/spotahome/redis-operator/metrics" 15 ) 16 17 func TestPrometheusMetrics(t *testing.T) { 18 19 tests := []struct { 20 name string 21 addMetrics func(rec metrics.Recorder) 22 expMetrics []string 23 expCode int 24 }{ 25 { 26 name: "Setting OK should give an OK", 27 addMetrics: func(rec metrics.Recorder) { 28 rec.SetClusterOK("testns", "test") 29 }, 30 expMetrics: []string{ 31 `my_metrics_controller_cluster_ok{name="test",namespace="testns"} 1`, 32 }, 33 expCode: http.StatusOK, 34 }, 35 { 36 name: "Setting Error should give an Error", 37 addMetrics: func(rec metrics.Recorder) { 38 rec.SetClusterError("testns", "test") 39 }, 40 expMetrics: []string{ 41 `my_metrics_controller_cluster_ok{name="test",namespace="testns"} 0`, 42 }, 43 expCode: http.StatusOK, 44 }, 45 { 46 name: "Setting Error after ok should give an Error", 47 addMetrics: func(rec metrics.Recorder) { 48 rec.SetClusterOK("testns", "test") 49 rec.SetClusterError("testns", "test") 50 }, 51 expMetrics: []string{ 52 `my_metrics_controller_cluster_ok{name="test",namespace="testns"} 0`, 53 }, 54 expCode: http.StatusOK, 55 }, 56 { 57 name: "Setting OK after Error should give an OK", 58 addMetrics: func(rec metrics.Recorder) { 59 rec.SetClusterError("testns", "test") 60 rec.SetClusterOK("testns", "test") 61 }, 62 expMetrics: []string{ 63 `my_metrics_controller_cluster_ok{name="test",namespace="testns"} 1`, 64 }, 65 expCode: http.StatusOK, 66 }, 67 { 68 name: "Multiple clusters should appear", 69 addMetrics: func(rec metrics.Recorder) { 70 rec.SetClusterOK("testns", "test") 71 rec.SetClusterOK("testns", "test2") 72 }, 73 expMetrics: []string{ 74 `my_metrics_controller_cluster_ok{name="test",namespace="testns"} 1`, 75 `my_metrics_controller_cluster_ok{name="test2",namespace="testns"} 1`, 76 }, 77 expCode: http.StatusOK, 78 }, 79 { 80 name: "Same name on different namespaces should appear", 81 addMetrics: func(rec metrics.Recorder) { 82 rec.SetClusterOK("testns1", "test") 83 rec.SetClusterOK("testns2", "test") 84 }, 85 expMetrics: []string{ 86 `my_metrics_controller_cluster_ok{name="test",namespace="testns1"} 1`, 87 `my_metrics_controller_cluster_ok{name="test",namespace="testns2"} 1`, 88 }, 89 expCode: http.StatusOK, 90 }, 91 { 92 name: "Deleting a cluster should remove it", 93 addMetrics: func(rec metrics.Recorder) { 94 rec.SetClusterOK("testns1", "test") 95 rec.DeleteCluster("testns1", "test") 96 }, 97 expMetrics: []string{}, 98 expCode: http.StatusOK, 99 }, 100 { 101 name: "Deleting a cluster should remove only the desired one", 102 addMetrics: func(rec metrics.Recorder) { 103 rec.SetClusterOK("testns1", "test") 104 rec.SetClusterOK("testns2", "test") 105 rec.DeleteCluster("testns1", "test") 106 }, 107 expMetrics: []string{ 108 `my_metrics_controller_cluster_ok{name="test",namespace="testns2"} 1`, 109 }, 110 expCode: http.StatusOK, 111 }, 112 } 113 114 for _, test := range tests { 115 t.Run(test.name, func(t *testing.T) { 116 assert := assert.New(t) 117 118 // Create the muxer for testing. 119 reg := prometheus.NewRegistry() 120 rec := metrics.NewRecorder("my_metrics", reg) 121 122 // Add metrics to prometheus. 123 test.addMetrics(rec) 124 125 // Make the request to the metrics. 126 h := promhttp.HandlerFor(reg, promhttp.HandlerOpts{}) 127 w := httptest.NewRecorder() 128 h.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/metrics", nil)) 129 130 resp := w.Result() 131 if assert.Equal(test.expCode, resp.StatusCode) { 132 body, _ := io.ReadAll(resp.Body) 133 // Check all the metrics are present. 134 for _, expMetric := range test.expMetrics { 135 assert.Contains(string(body), expMetric) 136 } 137 } 138 }) 139 } 140 }