|
14 | 14 | package collector |
15 | 15 |
|
16 | 16 | import ( |
17 | | - "fmt" |
| 17 | + "io" |
18 | 18 | "net/http" |
19 | 19 | "net/http/httptest" |
20 | 20 | "net/url" |
| 21 | + "os" |
| 22 | + "path" |
| 23 | + "strings" |
21 | 24 | "testing" |
22 | 25 |
|
| 26 | + "github.com/prometheus/client_golang/prometheus/testutil" |
23 | 27 | "github.com/prometheus/common/promslog" |
24 | 28 | ) |
25 | 29 |
|
@@ -54,53 +58,65 @@ func TestIndicesSettings(t *testing.T) { |
54 | 58 |
|
55 | 59 | // curl http://localhost:9200/_all/_settings |
56 | 60 |
|
57 | | - tcs := map[string]string{ |
58 | | - "6.5.4": `{"viber":{"settings":{"index":{"creation_date":"1618593207186","number_of_shards":"5","number_of_replicas":"1","uuid":"lWg86KTARzO3r7lELytT1Q","version":{"created":"6050499"},"provided_name":"viber"}}},"instagram":{"settings":{"index":{"mapping":{"total_fields":{"limit":"10000"}},"number_of_shards":"5","blocks":{"read_only_allow_delete":"true"},"provided_name":"instagram","creation_date":"1618593203353","number_of_replicas":"1","uuid":"msb6eG7aT8GmNe-a4oyVtQ","version":{"created":"6050499"}}}},"twitter":{"settings":{"index":{"number_of_shards":"5","blocks":{"read_only_allow_delete":"true"},"provided_name":"twitter","creation_date":"1618593193641","number_of_replicas":"1","uuid":"YRUT8t4aSkKsNmGl7K3y4Q","version":{"created":"6050499"}}}},"facebook":{"settings":{"index":{"creation_date":"1618593199101","number_of_shards":"5","number_of_replicas":"1","uuid":"trZhb_YOTV-RWKitTYw81A","version":{"created":"6050499"},"provided_name":"facebook"}}}}`, |
| 61 | + tests := []struct { |
| 62 | + name string |
| 63 | + file string |
| 64 | + want string |
| 65 | + }{ |
| 66 | + { |
| 67 | + name: "6.5.4", |
| 68 | + file: "6.5.4.json", |
| 69 | + want: `# HELP elasticsearch_indices_settings_creation_timestamp_seconds index setting creation_date |
| 70 | + # TYPE elasticsearch_indices_settings_creation_timestamp_seconds gauge |
| 71 | + elasticsearch_indices_settings_creation_timestamp_seconds{index="facebook"} 1.618593199101e+09 |
| 72 | + elasticsearch_indices_settings_creation_timestamp_seconds{index="instagram"} 1.618593203353e+09 |
| 73 | + elasticsearch_indices_settings_creation_timestamp_seconds{index="twitter"} 1.618593193641e+09 |
| 74 | + elasticsearch_indices_settings_creation_timestamp_seconds{index="viber"} 1.618593207186e+09 |
| 75 | + # HELP elasticsearch_indices_settings_replicas index setting number_of_replicas |
| 76 | + # TYPE elasticsearch_indices_settings_replicas gauge |
| 77 | + elasticsearch_indices_settings_replicas{index="facebook"} 1 |
| 78 | + elasticsearch_indices_settings_replicas{index="instagram"} 1 |
| 79 | + elasticsearch_indices_settings_replicas{index="twitter"} 1 |
| 80 | + elasticsearch_indices_settings_replicas{index="viber"} 1 |
| 81 | + # HELP elasticsearch_indices_settings_stats_read_only_indices Current number of read only indices within cluster |
| 82 | + # TYPE elasticsearch_indices_settings_stats_read_only_indices gauge |
| 83 | + elasticsearch_indices_settings_stats_read_only_indices 2 |
| 84 | + # HELP elasticsearch_indices_settings_total_fields index mapping setting for total_fields |
| 85 | + # TYPE elasticsearch_indices_settings_total_fields gauge |
| 86 | + elasticsearch_indices_settings_total_fields{index="facebook"} 1000 |
| 87 | + elasticsearch_indices_settings_total_fields{index="instagram"} 10000 |
| 88 | + elasticsearch_indices_settings_total_fields{index="twitter"} 1000 |
| 89 | + elasticsearch_indices_settings_total_fields{index="viber"} 1000 |
| 90 | + `, |
| 91 | + }, |
59 | 92 | } |
60 | | - for ver, out := range tcs { |
61 | | - for hn, handler := range map[string]http.Handler{ |
62 | | - "plain": http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
63 | | - fmt.Fprintln(w, out) |
64 | | - }), |
65 | | - } { |
66 | | - ts := httptest.NewServer(handler) |
| 93 | + |
| 94 | + for _, tt := range tests { |
| 95 | + t.Run(tt.name, func(t *testing.T) { |
| 96 | + f, err := os.Open(path.Join("../fixtures/indices_settings", tt.file)) |
| 97 | + if err != nil { |
| 98 | + t.Fatal(err) |
| 99 | + } |
| 100 | + defer f.Close() |
| 101 | + |
| 102 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 103 | + io.Copy(w, f) |
| 104 | + })) |
67 | 105 | defer ts.Close() |
68 | 106 |
|
69 | 107 | u, err := url.Parse(ts.URL) |
70 | 108 | if err != nil { |
71 | | - t.Fatalf("Failed to parse URL: %s", err) |
| 109 | + t.Fatal(err) |
72 | 110 | } |
| 111 | + |
73 | 112 | c := NewIndicesSettings(promslog.NewNopLogger(), http.DefaultClient, u) |
74 | | - nsr, err := c.fetchAndDecodeIndicesSettings() |
75 | 113 | if err != nil { |
76 | | - t.Fatalf("Failed to fetch or decode indices settings: %s", err) |
77 | | - } |
78 | | - t.Logf("[%s/%s] All Indices Settings Response: %+v", hn, ver, nsr) |
79 | | - // if nsr.Cluster.Routing.Allocation.Enabled != "ALL" { |
80 | | - // t.Errorf("Wrong setting for cluster routing allocation enabled") |
81 | | - // } |
82 | | - var counter int |
83 | | - var totalFields int |
84 | | - for key, value := range nsr { |
85 | | - if value.Settings.IndexInfo.Blocks.ReadOnly == "true" { |
86 | | - counter++ |
87 | | - if key != "instagram" && key != "twitter" { |
88 | | - t.Errorf("Wrong read_only index") |
89 | | - } |
90 | | - } |
91 | | - if value.Settings.IndexInfo.Mapping.TotalFields.Limit == "10000" { |
92 | | - totalFields++ |
93 | | - if key != "instagram" { |
94 | | - t.Errorf("Expected 10000 total_fields only for instagram") |
95 | | - } |
96 | | - } |
| 114 | + t.Fatal(err) |
97 | 115 | } |
98 | | - if counter != 2 { |
99 | | - t.Errorf("Wrong number of read_only indexes") |
100 | | - } |
101 | | - if totalFields != 1 { |
102 | | - t.Errorf(("Wrong number of total_fields found")) |
| 116 | + |
| 117 | + if err := testutil.CollectAndCompare(c, strings.NewReader(tt.want)); err != nil { |
| 118 | + t.Fatal(err) |
103 | 119 | } |
104 | | - } |
| 120 | + }) |
105 | 121 | } |
106 | 122 | } |
0 commit comments