1414package collector
1515
1616import (
17- "fmt "
17+ "io "
1818 "net/http"
1919 "net/http/httptest"
2020 "net/url"
21+ "os"
22+ "path"
23+ "strings"
2124 "testing"
2225
2326 "github.com/go-kit/log"
27+ "github.com/prometheus/client_golang/prometheus/testutil"
2428)
2529
2630func TestSLM (t * testing.T ) {
@@ -31,35 +35,104 @@ func TestSLM(t *testing.T) {
3135 // curl -XPUT http://127.0.0.1:9200/_slm/policy/everything -H 'Content-Type: application/json' -d '{"schedule":"0 */15 * * * ?","name":"<everything-{now/d}>","repository":"my_repository","config":{"indices":".*","include_global_state":true,"ignore_unavailable":true},"retention":{"expire_after":"7d"}}'
3236 // curl http://127.0.0.1:9200/_slm/stats (Numbers manually tweaked)
3337
34- tcs := map [string ]string {
35- "7.15.0" : `{"retention_runs":9,"retention_failed":0,"retention_timed_out":0,"retention_deletion_time":"1.2m","retention_deletion_time_millis":72491,"total_snapshots_taken":103,"total_snapshots_failed":2,"total_snapshots_deleted":20,"total_snapshot_deletion_failures":0,"policy_stats":[{"policy":"everything","snapshots_taken":50,"snapshots_failed":2,"snapshots_deleted":20,"snapshot_deletion_failures":0}]}` ,
38+ tests := []struct {
39+ name string
40+ file string
41+ want string
42+ }{
43+ {
44+ name : "7.15.0" ,
45+ file : "7.15.0.json" ,
46+ want : `# HELP elasticsearch_slm_stats_operation_mode Operating status of SLM
47+ # TYPE elasticsearch_slm_stats_operation_mode gauge
48+ elasticsearch_slm_stats_operation_mode{operation_mode="RUNNING"} 0
49+ elasticsearch_slm_stats_operation_mode{operation_mode="STOPPED"} 0
50+ elasticsearch_slm_stats_operation_mode{operation_mode="STOPPING"} 0
51+ # HELP elasticsearch_slm_stats_retention_deletion_time_seconds Retention run deletion time
52+ # TYPE elasticsearch_slm_stats_retention_deletion_time_seconds gauge
53+ elasticsearch_slm_stats_retention_deletion_time_seconds 72.491
54+ # HELP elasticsearch_slm_stats_retention_failed_total Total failed retention runs
55+ # TYPE elasticsearch_slm_stats_retention_failed_total counter
56+ elasticsearch_slm_stats_retention_failed_total 0
57+ # HELP elasticsearch_slm_stats_retention_runs_total Total retention runs
58+ # TYPE elasticsearch_slm_stats_retention_runs_total counter
59+ elasticsearch_slm_stats_retention_runs_total 9
60+ # HELP elasticsearch_slm_stats_retention_timed_out_total Total timed out retention runs
61+ # TYPE elasticsearch_slm_stats_retention_timed_out_total counter
62+ elasticsearch_slm_stats_retention_timed_out_total 0
63+ # HELP elasticsearch_slm_stats_snapshot_deletion_failures_total Total snapshot deletion failures
64+ # TYPE elasticsearch_slm_stats_snapshot_deletion_failures_total counter
65+ elasticsearch_slm_stats_snapshot_deletion_failures_total{policy="everything"} 0
66+ # HELP elasticsearch_slm_stats_snapshots_deleted_total Total snapshots deleted
67+ # TYPE elasticsearch_slm_stats_snapshots_deleted_total counter
68+ elasticsearch_slm_stats_snapshots_deleted_total{policy="everything"} 20
69+ # HELP elasticsearch_slm_stats_snapshots_failed_total Total snapshots failed
70+ # TYPE elasticsearch_slm_stats_snapshots_failed_total counter
71+ elasticsearch_slm_stats_snapshots_failed_total{policy="everything"} 2
72+ # HELP elasticsearch_slm_stats_snapshots_taken_total Total snapshots taken
73+ # TYPE elasticsearch_slm_stats_snapshots_taken_total counter
74+ elasticsearch_slm_stats_snapshots_taken_total{policy="everything"} 50
75+ # HELP elasticsearch_slm_stats_total_snapshot_deletion_failures_total Total snapshot deletion failures
76+ # TYPE elasticsearch_slm_stats_total_snapshot_deletion_failures_total counter
77+ elasticsearch_slm_stats_total_snapshot_deletion_failures_total 0
78+ # HELP elasticsearch_slm_stats_total_snapshots_deleted_total Total snapshots deleted
79+ # TYPE elasticsearch_slm_stats_total_snapshots_deleted_total counter
80+ elasticsearch_slm_stats_total_snapshots_deleted_total 20
81+ # HELP elasticsearch_slm_stats_total_snapshots_failed_total Total snapshots failed
82+ # TYPE elasticsearch_slm_stats_total_snapshots_failed_total counter
83+ elasticsearch_slm_stats_total_snapshots_failed_total 2
84+ # HELP elasticsearch_slm_stats_total_snapshots_taken_total Total snapshots taken
85+ # TYPE elasticsearch_slm_stats_total_snapshots_taken_total counter
86+ elasticsearch_slm_stats_total_snapshots_taken_total 103
87+ ` ,
88+ },
3689 }
37- for ver , out := range tcs {
38- ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
39- fmt .Fprintln (w , out )
40- }))
41- defer ts .Close ()
4290
43- u , err := url .Parse (ts .URL )
44- if err != nil {
45- t .Fatalf ("Failed to parse URL: %s" , err )
46- }
47- s := NewSLM (log .NewNopLogger (), http .DefaultClient , u )
48- stats , err := s .fetchAndDecodeSLMStats ()
49- if err != nil {
50- t .Fatalf ("Failed to fetch or decode snapshots stats: %s" , err )
51- }
52- t .Logf ("[%s] SLM Response: %+v" , ver , stats )
53- slmStats := stats
54- policyStats := stats .PolicyStats [0 ]
91+ for _ , tt := range tests {
92+ t .Run (tt .name , func (t * testing.T ) {
93+ fStatsPath := path .Join ("../fixtures/slm/stats/" , tt .file )
94+ fStats , err := os .Open (fStatsPath )
95+ if err != nil {
96+ t .Fatal (err )
97+ }
98+ defer fStats .Close ()
5599
56- if slmStats .TotalSnapshotsTaken != 103 {
57- t .Errorf ("Bad number of total snapshots taken" )
58- }
100+ fStatusPath := path .Join ("../fixtures/slm/status/" , tt .file )
101+ fStatus , err := os .Open (fStatusPath )
102+ if err != nil {
103+ t .Fatal (err )
104+ }
105+ defer fStatus .Close ()
106+
107+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
108+ switch r .RequestURI {
109+ case "/_slm/stats" :
110+ io .Copy (w , fStats )
111+ return
112+ case "/_slm/status" :
113+ io .Copy (w , fStatus )
114+ return
115+ }
116+
117+ http .Error (w , "Not Found" , http .StatusNotFound )
118+ }))
119+ defer ts .Close ()
120+
121+ u , err := url .Parse (ts .URL )
122+ if err != nil {
123+ t .Fatalf ("Failed to parse URL: %s" , err )
124+ }
125+
126+ s := NewSLM (log .NewNopLogger (), http .DefaultClient , u )
127+ if err != nil {
128+ t .Fatal (err )
129+ }
130+
131+ if err := testutil .CollectAndCompare (s , strings .NewReader (tt .want )); err != nil {
132+ t .Fatal (err )
133+ }
134+ })
59135
60- if policyStats .SnapshotsTaken != 50 {
61- t .Errorf ("Bad number of policy snapshots taken" )
62- }
63136 }
64137
65138}
0 commit comments