|
| 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 | +} |
0 commit comments