|
| 1 | +// Copyright 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 cluster |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "net/http" |
| 19 | + "net/http/httptest" |
| 20 | + "net/url" |
| 21 | + "os" |
| 22 | + "reflect" |
| 23 | + "testing" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/prometheus/common/promslog" |
| 27 | +) |
| 28 | + |
| 29 | +func TestInfoProvider_GetInfo(t *testing.T) { |
| 30 | + timesURLCalled := 0 |
| 31 | + expectedInfo := Info{ |
| 32 | + ClusterName: "test-cluster-1", |
| 33 | + } |
| 34 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 35 | + if r.URL.Path != "/" { |
| 36 | + http.NotFound(w, r) |
| 37 | + return |
| 38 | + } |
| 39 | + w.Header().Set("Content-Type", "application/json") |
| 40 | + timesURLCalled++ |
| 41 | + _, _ = w.Write([]byte(`{ |
| 42 | + "name": "test-node-abcd", |
| 43 | + "cluster_name": "test-cluster-1", |
| 44 | + "cluster_uuid": "r1bT9sBrR7S9-CamE41Qqg", |
| 45 | + "version": { |
| 46 | + "number": "5.6.9", |
| 47 | + "build_hash": "877a590", |
| 48 | + "build_date": "2018-04-12T16:25:14.838Z", |
| 49 | + "build_snapshot": false, |
| 50 | + "lucene_version": "6.6.1" |
| 51 | + } |
| 52 | + }`)) |
| 53 | + })) |
| 54 | + tsURL, err := url.Parse(ts.URL) |
| 55 | + if err != nil { |
| 56 | + t.Fatalf("failed to parse test server URL: %v", err) |
| 57 | + } |
| 58 | + defer ts.Close() |
| 59 | + |
| 60 | + i := NewInfoProvider(promslog.New(&promslog.Config{Writer: os.Stdout}), http.DefaultClient, tsURL, time.Second) |
| 61 | + |
| 62 | + if timesURLCalled != 0 { |
| 63 | + t.Errorf("expected no initial URL calls, got %d", timesURLCalled) |
| 64 | + } |
| 65 | + |
| 66 | + got, err := i.GetInfo(context.Background()) |
| 67 | + if err != nil { |
| 68 | + t.Errorf("InfoProvider.GetInfo() error = %v, wantErr %v", err, false) |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + if !reflect.DeepEqual(got, expectedInfo) { |
| 73 | + t.Errorf("InfoProvider.GetInfo() = %v, want %v", got, expectedInfo) |
| 74 | + } |
| 75 | + |
| 76 | + if timesURLCalled != 1 { |
| 77 | + t.Errorf("expected URL to be called once, got %d", timesURLCalled) |
| 78 | + } |
| 79 | + |
| 80 | + // Call again to ensure cached value is returned |
| 81 | + got, err = i.GetInfo(context.Background()) |
| 82 | + if err != nil { |
| 83 | + t.Errorf("InfoProvider.GetInfo() error on second call = %v, wantErr %v", err, false) |
| 84 | + return |
| 85 | + } |
| 86 | + if !reflect.DeepEqual(got, expectedInfo) { |
| 87 | + t.Errorf("InfoProvider.GetInfo() on second call = %v, want %v", got, expectedInfo) |
| 88 | + } |
| 89 | + if timesURLCalled != 1 { |
| 90 | + t.Errorf("expected URL to be called only once, got %d", timesURLCalled) |
| 91 | + } |
| 92 | + |
| 93 | + // Call again after delay to ensure we refresh the cache |
| 94 | + time.Sleep(2 * time.Second) |
| 95 | + got, err = i.GetInfo(context.Background()) |
| 96 | + if err != nil { |
| 97 | + t.Errorf("InfoProvider.GetInfo() error on second call = %v, wantErr %v", err, false) |
| 98 | + return |
| 99 | + } |
| 100 | + if !reflect.DeepEqual(got, expectedInfo) { |
| 101 | + t.Errorf("InfoProvider.GetInfo() on second call = %v, want %v", got, expectedInfo) |
| 102 | + } |
| 103 | + if timesURLCalled != 2 { |
| 104 | + t.Errorf("expected URL to be called only once, got %d", timesURLCalled) |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments