github.com/google/cloudprober@v0.11.3/metrics/testutils/testutils_test.go (about) 1 // Copyright 2019 The Cloudprober Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package testutils 16 17 import ( 18 "testing" 19 "time" 20 21 "github.com/google/cloudprober/metrics" 22 ) 23 24 func TestMetricsFromChannel(t *testing.T) { 25 dataChan := make(chan *metrics.EventMetrics, 10) 26 27 var ts [2]time.Time 28 29 ts[0] = time.Now() 30 time.Sleep(time.Millisecond) 31 ts[1] = time.Now() 32 33 // Put 2 EventMetrics, get 2 34 dataChan <- metrics.NewEventMetrics(ts[0]) 35 dataChan <- metrics.NewEventMetrics(ts[1]) 36 37 ems, err := MetricsFromChannel(dataChan, 2, time.Second) 38 if err != nil { 39 t.Error(err) 40 } 41 42 for i := range ems { 43 if ems[i].Timestamp != ts[i] { 44 t.Errorf("First EventMetrics has unexpected timestamp. Got=%s, Expected=%s", ems[i], ts[i]) 45 } 46 } 47 48 // Put 2 EventMetrics, try to get 3 49 dataChan <- metrics.NewEventMetrics(ts[0]) 50 dataChan <- metrics.NewEventMetrics(ts[1]) 51 52 ems, err = MetricsFromChannel(dataChan, 3, time.Second) 53 if err == nil { 54 t.Error("expected error got none") 55 } 56 57 for i := range ems { 58 if ems[i].Timestamp != ts[i] { 59 t.Errorf("First EventMetrics has unexpected timestamp. Got=%s, Expected=%s", ems[i], ts[i]) 60 } 61 } 62 } 63 64 func TestMetricsMap(t *testing.T) { 65 var ems []*metrics.EventMetrics 66 expectedValues := map[string][]int64{ 67 "success": []int64{99, 98}, 68 "total": []int64{100, 100}, 69 } 70 ems = append(ems, metrics.NewEventMetrics(time.Now()). 71 AddMetric("success", metrics.NewInt(99)). 72 AddMetric("total", metrics.NewInt(100)). 73 AddLabel("dst", "target1")) 74 ems = append(ems, metrics.NewEventMetrics(time.Now()). 75 AddMetric("success", metrics.NewInt(98)). 76 AddMetric("total", metrics.NewInt(100)). 77 AddLabel("dst", "target2")) 78 79 metricsMap := MetricsMap(ems) 80 81 for _, m := range []string{"success", "total"} { 82 if metricsMap[m] == nil { 83 t.Errorf("didn't get metric %s in metrics map", m) 84 } 85 } 86 87 for i, tgt := range []string{"target1", "target2"} { 88 for _, m := range []string{"success", "total"} { 89 if len(metricsMap[m][tgt]) != 1 { 90 t.Errorf("Wrong number of values for metric (%s) for target (%s) from the command output. Got=%d, Expected=1", m, tgt, len(metricsMap[m][tgt])) 91 } 92 val := metricsMap[m][tgt][0].Metric(m).(metrics.NumValue).Int64() 93 if val != expectedValues[m][i] { 94 t.Errorf("Wrong metric value for target (%s) from the command output. Got=%d, Expected=%d", m, val, expectedValues[m][i]) 95 } 96 } 97 } 98 }