github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/metrics/sinks/elasticsearch/driver_test.go (about) 1 // Copyright 2015 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 elasticsearch 16 17 import ( 18 "encoding/json" 19 "fmt" 20 "testing" 21 "time" 22 23 "github.com/stretchr/testify/assert" 24 esCommon "k8s.io/heapster/common/elasticsearch" 25 "k8s.io/heapster/metrics/core" 26 ) 27 28 type fakeESSink struct { 29 core.DataSink 30 savedData map[string][]string 31 } 32 33 var FakeESSink fakeESSink 34 35 func SaveDataIntoES_Stub(date time.Time, typeName string, sinkData []interface{}) error { 36 for _, data := range sinkData { 37 jsonItems, err := json.Marshal(data) 38 if err != nil { 39 return fmt.Errorf("failed to transform the items to json : %s", err) 40 } 41 42 if FakeESSink.savedData[typeName] == nil { 43 FakeESSink.savedData[typeName] = []string{} 44 } 45 46 FakeESSink.savedData[typeName] = append(FakeESSink.savedData[typeName], string(jsonItems)) 47 } 48 return nil 49 } 50 51 // Returns a fake ES sink. 52 func NewFakeSink() fakeESSink { 53 savedData := make(map[string][]string) 54 return fakeESSink{ 55 &elasticSearchSink{ 56 saveData: SaveDataIntoES_Stub, 57 flushData: func() error { return nil }, 58 esSvc: esCommon.ElasticSearchService{ 59 EsClient: esCommon.NewMockClient(), 60 ClusterName: esCommon.ESClusterName, 61 }, 62 }, 63 savedData, 64 } 65 } 66 67 func TestStoreDataEmptyInput(t *testing.T) { 68 FakeESSink = NewFakeSink() 69 dataBatch := core.DataBatch{} 70 FakeESSink.ExportData(&dataBatch) 71 assert.Equal(t, 0, len(FakeESSink.savedData)) 72 } 73 74 func TestStoreMultipleDataInput(t *testing.T) { 75 timestamp := time.Now() 76 77 l := make(map[string]string) 78 l["namespace_id"] = "123" 79 l["container_name"] = "/system.slice/-.mount" 80 l[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd" 81 82 l2 := make(map[string]string) 83 l2["namespace_id"] = "123" 84 l2["container_name"] = "/system.slice/dbus.service" 85 l2[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd" 86 87 l3 := make(map[string]string) 88 l3["namespace_id"] = "123" 89 l3[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd" 90 91 l4 := make(map[string]string) 92 l4["namespace_id"] = "" 93 l4[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd" 94 95 l5 := make(map[string]string) 96 l5["namespace_id"] = "123" 97 l5[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd" 98 99 metricSet1 := core.MetricSet{ 100 Labels: l, 101 MetricValues: map[string]core.MetricValue{ 102 "/system.slice/-.mount//cpu/limit": { 103 ValueType: core.ValueInt64, 104 MetricType: core.MetricCumulative, 105 IntValue: 123456, 106 }, 107 }, 108 } 109 110 metricSet2 := core.MetricSet{ 111 Labels: l2, 112 MetricValues: map[string]core.MetricValue{ 113 "/system.slice/dbus.service//cpu/usage": { 114 ValueType: core.ValueInt64, 115 MetricType: core.MetricCumulative, 116 IntValue: 123456, 117 }, 118 }, 119 } 120 121 metricSet3 := core.MetricSet{ 122 Labels: l3, 123 MetricValues: map[string]core.MetricValue{ 124 "test/metric/1": { 125 ValueType: core.ValueInt64, 126 MetricType: core.MetricCumulative, 127 IntValue: 123456, 128 }, 129 }, 130 } 131 132 metricSet4 := core.MetricSet{ 133 Labels: l4, 134 MetricValues: map[string]core.MetricValue{ 135 "test/metric/1": { 136 ValueType: core.ValueInt64, 137 MetricType: core.MetricCumulative, 138 IntValue: 123456, 139 }, 140 }, 141 } 142 143 metricSet5 := core.MetricSet{ 144 Labels: l5, 145 MetricValues: map[string]core.MetricValue{ 146 "removeme": { 147 ValueType: core.ValueInt64, 148 MetricType: core.MetricCumulative, 149 IntValue: 123456, 150 }, 151 }, 152 } 153 154 metricSet6 := core.MetricSet{ 155 Labels: l, 156 MetricValues: map[string]core.MetricValue{ 157 "cpu/usage": { 158 ValueType: core.ValueInt64, 159 MetricType: core.MetricGauge, 160 IntValue: 123456, 161 }, 162 "cpu/limit": { 163 ValueType: core.ValueInt64, 164 MetricType: core.MetricGauge, 165 IntValue: 223456, 166 }, 167 }, 168 } 169 170 data := core.DataBatch{ 171 Timestamp: timestamp, 172 MetricSets: map[string]*core.MetricSet{ 173 "pod1": &metricSet1, 174 "pod2": &metricSet2, 175 "pod3": &metricSet3, 176 "pod4": &metricSet4, 177 "pod5": &metricSet5, 178 "pod6": &metricSet6, 179 }, 180 } 181 182 timeStr, err := timestamp.UTC().MarshalJSON() 183 assert.NoError(t, err) 184 185 FakeESSink = NewFakeSink() 186 FakeESSink.ExportData(&data) 187 188 //expect msg string 189 assert.Equal(t, 2, len(FakeESSink.savedData)) 190 191 var expectMsgTemplate = [6]string{ 192 `{"GeneralMetricsTimestamp":%s,"MetricsTags":{"cluster_name":"default","namespace_id":"","pod_id":"aaaa-bbbb-cccc-dddd"},"MetricsName":"test/metric/1","MetricsValue":{"value":123456}}`, 193 `{"GeneralMetricsTimestamp":%s,"MetricsTags":{"cluster_name":"default","namespace_id":"123","pod_id":"aaaa-bbbb-cccc-dddd"},"MetricsName":"removeme","MetricsValue":{"value":123456}}`, 194 `{"GeneralMetricsTimestamp":%s,"MetricsTags":{"cluster_name":"default","container_name":"/system.slice/-.mount","namespace_id":"123","pod_id":"aaaa-bbbb-cccc-dddd"},"MetricsName":"/system.slice/-.mount//cpu/limit","MetricsValue":{"value":123456}}`, 195 `{"GeneralMetricsTimestamp":%s,"MetricsTags":{"cluster_name":"default","container_name":"/system.slice/dbus.service","namespace_id":"123","pod_id":"aaaa-bbbb-cccc-dddd"},"MetricsName":"/system.slice/dbus.service//cpu/usage","MetricsValue":{"value":123456}}`, 196 `{"GeneralMetricsTimestamp":%s,"MetricsTags":{"cluster_name":"default","namespace_id":"123","pod_id":"aaaa-bbbb-cccc-dddd"},"MetricsName":"test/metric/1","MetricsValue":{"value":123456}}`, 197 `{"CpuMetricsTimestamp":%s,"Metrics":{"cpu/limit":{"value":223456},"cpu/usage":{"value":123456}},"MetricsTags":{"cluster_name":"default","container_name":"/system.slice/-.mount","namespace_id":"123","pod_id":"aaaa-bbbb-cccc-dddd"}}`, 198 } 199 200 msgsString := fmt.Sprintf("%s", FakeESSink.savedData) 201 202 for _, mgsTemplate := range expectMsgTemplate { 203 expectMsg := fmt.Sprintf(mgsTemplate, timeStr) 204 assert.Contains(t, msgsString, expectMsg) 205 } 206 }