Skip to content

Commit 79b0e15

Browse files
authored
Merge pull request #935 from sysadmind/shards-test
Add tests for shards collector
2 parents 7783607 + 8103328 commit 79b0e15

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

collector/shards_test.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright 2024 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package collector
15+
16+
import (
17+
"io"
18+
"net/http"
19+
"net/http/httptest"
20+
"net/url"
21+
"os"
22+
"path"
23+
"strings"
24+
"testing"
25+
26+
"github.com/go-kit/log"
27+
"github.com/prometheus/client_golang/prometheus/testutil"
28+
)
29+
30+
func TestShards(t *testing.T) {
31+
// Testcases created using:
32+
// docker run --rm -d -p 9200:9200 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:$VERSION
33+
// curl -XPUT http://localhost:9200/testindex
34+
// curl -XPUT http://localhost:9200/otherindex
35+
// curl http://localhost:9200/_cat/shards?format=json > fixtures/shards/$VERSION.json
36+
37+
tests := []struct {
38+
name string
39+
file string
40+
want string
41+
}{
42+
{
43+
name: "7.15.0",
44+
file: "7.15.0.json",
45+
want: `# HELP elasticsearch_node_shards_json_parse_failures Number of errors while parsing JSON.
46+
# TYPE elasticsearch_node_shards_json_parse_failures counter
47+
elasticsearch_node_shards_json_parse_failures 0
48+
# HELP elasticsearch_node_shards_total Total shards per node
49+
# TYPE elasticsearch_node_shards_total gauge
50+
elasticsearch_node_shards_total{cluster="unknown_cluster",node="35dfca79831a"} 3
51+
`,
52+
},
53+
}
54+
55+
for _, tt := range tests {
56+
t.Run(tt.name, func(t *testing.T) {
57+
f, err := os.Open(path.Join("../fixtures/shards/", tt.file))
58+
if err != nil {
59+
t.Fatal(err)
60+
}
61+
defer f.Close()
62+
63+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
64+
io.Copy(w, f)
65+
}))
66+
defer ts.Close()
67+
68+
u, err := url.Parse(ts.URL)
69+
if err != nil {
70+
t.Fatalf("Failed to parse URL: %s", err)
71+
}
72+
73+
s := NewShards(log.NewNopLogger(), http.DefaultClient, u)
74+
if err != nil {
75+
t.Fatal(err)
76+
}
77+
78+
if err := testutil.CollectAndCompare(s, strings.NewReader(tt.want)); err != nil {
79+
t.Fatal(err)
80+
}
81+
})
82+
83+
}
84+
}

fixtures/shards/7.15.0.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"index":".geoip_databases","shard":"0","prirep":"p","state":"STARTED","docs":"38","store":"36.4mb","ip":"172.17.0.2","node":"35dfca79831a"},{"index":"otherindex","shard":"0","prirep":"p","state":"STARTED","docs":"0","store":"208b","ip":"172.17.0.2","node":"35dfca79831a"},{"index":"otherindex","shard":"0","prirep":"r","state":"UNASSIGNED","docs":null,"store":null,"ip":null,"node":null},{"index":"testindex","shard":"0","prirep":"p","state":"STARTED","docs":"0","store":"208b","ip":"172.17.0.2","node":"35dfca79831a"},{"index":"testindex","shard":"0","prirep":"r","state":"UNASSIGNED","docs":null,"store":null,"ip":null,"node":null}]

0 commit comments

Comments
 (0)