Skip to content

Commit b0f63df

Browse files
authored
Add tests for clusterinfo collector (prometheus-community#792)
* Add tests for clusterinfo collector Signed-off-by: Joe Adams <[email protected]> * Add license header Signed-off-by: Joe Adams <[email protected]> --------- Signed-off-by: Joe Adams <[email protected]>
1 parent 620c886 commit b0f63df

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

collector/cluster_info_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright 2023 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+
"strings"
23+
"testing"
24+
25+
"github.com/go-kit/log"
26+
"github.com/prometheus/client_golang/prometheus/testutil"
27+
)
28+
29+
func TestClusterInfo(t *testing.T) {
30+
// Testcases created using:
31+
// docker run -p 9200:9200 -e "discovery.type=single-node" elasticsearch:${VERSION}
32+
// curl http://localhost:9200/ > fixtures/cluster_info/${VERSION}.json
33+
34+
tests := []struct {
35+
name string
36+
file string
37+
want string
38+
}{
39+
{
40+
name: "2.4.5",
41+
file: "../fixtures/clusterinfo/2.4.5.json",
42+
want: `# HELP elasticsearch_version Elasticsearch version information.
43+
# TYPE elasticsearch_version gauge
44+
elasticsearch_version{build_date="",build_hash="c849dd13904f53e63e88efc33b2ceeda0b6a1276",cluster="elasticsearch",cluster_uuid="3qps7bcWTqyzV49ApmPVfw",lucene_version="5.5.4",version="2.4.5"} 1
45+
`,
46+
},
47+
{
48+
name: "5.4.2",
49+
file: "../fixtures/clusterinfo/5.4.2.json",
50+
want: `# HELP elasticsearch_version Elasticsearch version information.
51+
# TYPE elasticsearch_version gauge
52+
elasticsearch_version{build_date="2017-06-15T02:29:28.122Z",build_hash="929b078",cluster="elasticsearch",cluster_uuid="kbqi7yhQT-WlPdGL2m0xJg",lucene_version="6.5.1",version="5.4.2"} 1
53+
`,
54+
},
55+
{
56+
name: "7.13.1",
57+
file: "../fixtures/clusterinfo/7.13.1.json",
58+
want: `# HELP elasticsearch_version Elasticsearch version information.
59+
# TYPE elasticsearch_version gauge
60+
elasticsearch_version{build_date="2021-05-28T17:40:59.346932922Z",build_hash="9a7758028e4ea59bcab41c12004603c5a7dd84a9",cluster="docker-cluster",cluster_uuid="aCMrCY1VQpqJ6U4Sw_xdiw",lucene_version="8.8.2",version="7.13.1"} 1
61+
`,
62+
},
63+
}
64+
65+
for _, tt := range tests {
66+
t.Run(tt.name, func(t *testing.T) {
67+
f, err := os.Open(tt.file)
68+
if err != nil {
69+
t.Fatal(err)
70+
}
71+
defer f.Close()
72+
73+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
74+
io.Copy(w, f)
75+
}))
76+
defer ts.Close()
77+
78+
u, err := url.Parse(ts.URL)
79+
if err != nil {
80+
t.Fatal(err)
81+
}
82+
83+
c, err := NewClusterInfo(log.NewNopLogger(), u, http.DefaultClient)
84+
if err != nil {
85+
t.Fatal(err)
86+
}
87+
88+
if err := testutil.CollectAndCompare(wrapCollector{c}, strings.NewReader(tt.want)); err != nil {
89+
t.Fatal(err)
90+
}
91+
})
92+
}
93+
}

fixtures/clusterinfo/2.4.5.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name" : "Mys-Tech",
3+
"cluster_name" : "elasticsearch",
4+
"cluster_uuid" : "3qps7bcWTqyzV49ApmPVfw",
5+
"version" : {
6+
"number" : "2.4.5",
7+
"build_hash" : "c849dd13904f53e63e88efc33b2ceeda0b6a1276",
8+
"build_timestamp" : "2017-04-24T16:18:17Z",
9+
"build_snapshot" : false,
10+
"lucene_version" : "5.5.4"
11+
},
12+
"tagline" : "You Know, for Search"
13+
}

fixtures/clusterinfo/5.4.2.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name" : "gOHPUga",
3+
"cluster_name" : "elasticsearch",
4+
"cluster_uuid" : "kbqi7yhQT-WlPdGL2m0xJg",
5+
"version" : {
6+
"number" : "5.4.2",
7+
"build_hash" : "929b078",
8+
"build_date" : "2017-06-15T02:29:28.122Z",
9+
"build_snapshot" : false,
10+
"lucene_version" : "6.5.1"
11+
},
12+
"tagline" : "You Know, for Search"
13+
}

fixtures/clusterinfo/7.13.1.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name" : "e0630cfd8e1e",
3+
"cluster_name" : "docker-cluster",
4+
"cluster_uuid" : "aCMrCY1VQpqJ6U4Sw_xdiw",
5+
"version" : {
6+
"number" : "7.13.1",
7+
"build_flavor" : "default",
8+
"build_type" : "docker",
9+
"build_hash" : "9a7758028e4ea59bcab41c12004603c5a7dd84a9",
10+
"build_date" : "2021-05-28T17:40:59.346932922Z",
11+
"build_snapshot" : false,
12+
"lucene_version" : "8.8.2",
13+
"minimum_wire_compatibility_version" : "6.8.0",
14+
"minimum_index_compatibility_version" : "6.0.0-beta1"
15+
},
16+
"tagline" : "You Know, for Search"
17+
}

0 commit comments

Comments
 (0)