github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/metrics/sinks/librato/librato_test.go (about) 1 // Copyright 2017 Google Inc. All Rights Reserved. 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 librato 16 17 import ( 18 "net/url" 19 "testing" 20 "time" 21 22 "github.com/stretchr/testify/assert" 23 librato_common "k8s.io/heapster/common/librato" 24 "k8s.io/heapster/metrics/core" 25 ) 26 27 type fakeLibratoDataSink struct { 28 core.DataSink 29 fakeDbClient *librato_common.FakeLibratoClient 30 } 31 32 func newRawLibratoSink() *libratoSink { 33 return &libratoSink{ 34 client: librato_common.FakeClient, 35 c: librato_common.Config, 36 } 37 } 38 39 func NewFakeSink() fakeLibratoDataSink { 40 return fakeLibratoDataSink{ 41 newRawLibratoSink(), 42 librato_common.FakeClient, 43 } 44 } 45 func TestStoreDataEmptyInput(t *testing.T) { 46 fakeSink := NewFakeSink() 47 dataBatch := core.DataBatch{} 48 fakeSink.ExportData(&dataBatch) 49 assert.Equal(t, 0, len(fakeSink.fakeDbClient.Measurements)) 50 } 51 52 func TestStoreMultipleDataInput(t *testing.T) { 53 fakeSink := NewFakeSink() 54 timestamp := time.Now() 55 56 l := make(map[string]string) 57 l["namespace_id"] = "123" 58 l["container_name"] = "/system.slice/-.mount" 59 l[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd" 60 61 l2 := make(map[string]string) 62 l2["namespace_id"] = "123" 63 l2["container_name"] = "/system.slice/dbus.service" 64 l2[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd" 65 66 l3 := make(map[string]string) 67 l3["namespace_id"] = "123" 68 l3[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd" 69 70 l4 := make(map[string]string) 71 l4["namespace_id"] = "" 72 l4[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd" 73 74 l5 := make(map[string]string) 75 l5["namespace_id"] = "123" 76 l5[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd" 77 78 metricSet1 := core.MetricSet{ 79 Labels: l, 80 MetricValues: map[string]core.MetricValue{ 81 "/system.slice/-.mount//cpu/limit": { 82 ValueType: core.ValueInt64, 83 MetricType: core.MetricCumulative, 84 IntValue: 123456, 85 }, 86 }, 87 } 88 89 metricSet2 := core.MetricSet{ 90 Labels: l2, 91 MetricValues: map[string]core.MetricValue{ 92 "/system.slice/dbus.service//cpu/usage": { 93 ValueType: core.ValueInt64, 94 MetricType: core.MetricCumulative, 95 IntValue: 123456, 96 }, 97 }, 98 } 99 100 metricSet3 := core.MetricSet{ 101 Labels: l3, 102 MetricValues: map[string]core.MetricValue{ 103 "test/metric/1": { 104 ValueType: core.ValueInt64, 105 MetricType: core.MetricCumulative, 106 IntValue: 123456, 107 }, 108 }, 109 } 110 111 metricSet4 := core.MetricSet{ 112 Labels: l4, 113 MetricValues: map[string]core.MetricValue{ 114 "test/metric/1": { 115 ValueType: core.ValueInt64, 116 MetricType: core.MetricCumulative, 117 IntValue: 123456, 118 }, 119 }, 120 } 121 122 metricSet5 := core.MetricSet{ 123 Labels: l5, 124 MetricValues: map[string]core.MetricValue{ 125 "removeme": { 126 ValueType: core.ValueFloat, 127 MetricType: core.MetricCumulative, 128 FloatValue: 1.23456, 129 }, 130 }, 131 } 132 133 data := core.DataBatch{ 134 Timestamp: timestamp, 135 MetricSets: map[string]*core.MetricSet{ 136 "pod1": &metricSet1, 137 "pod2": &metricSet2, 138 "pod3": &metricSet3, 139 "pod4": &metricSet4, 140 "pod5": &metricSet5, 141 }, 142 } 143 144 fakeSink.ExportData(&data) 145 assert.Equal(t, 5, len(fakeSink.fakeDbClient.Measurements)) 146 } 147 148 func TestCreateLibratoSink(t *testing.T) { 149 stubLibratoURL, err := url.Parse("?username=test&token=my_token") 150 assert.NoError(t, err) 151 152 //create influxdb sink 153 sink, err := CreateLibratoSink(stubLibratoURL) 154 assert.NoError(t, err) 155 156 //check sink name 157 assert.Equal(t, sink.Name(), "Librato Sink") 158 } 159 160 func checkMetricVal(expected, actual core.MetricValue) bool { 161 if expected.ValueType != actual.ValueType { 162 return false 163 } 164 165 // only check the relevant value type 166 switch expected.ValueType { 167 case core.ValueFloat: 168 return expected.FloatValue == actual.FloatValue 169 case core.ValueInt64: 170 return expected.IntValue == actual.IntValue 171 default: 172 return expected == actual 173 } 174 }