github.com/timstclair/heapster@v0.20.0-alpha1/metrics/sinks/kafka/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 kafka 16 17 import ( 18 "fmt" 19 "testing" 20 "time" 21 22 "github.com/optiopay/kafka/proto" 23 "github.com/stretchr/testify/assert" 24 "k8s.io/heapster/metrics/core" 25 ) 26 27 type msgProducedToKafka struct { 28 message string 29 } 30 31 type fakeKafkaProducer struct { 32 msgs []msgProducedToKafka 33 } 34 35 type fakeKafkaSink struct { 36 core.DataSink 37 fakeProducer *fakeKafkaProducer 38 } 39 40 func NewFakeKafkaProducer() *fakeKafkaProducer { 41 return &fakeKafkaProducer{[]msgProducedToKafka{}} 42 } 43 44 func (producer *fakeKafkaProducer) Produce(topic string, partition int32, messages ...*proto.Message) (int64, error) { 45 for _, msg := range messages { 46 producer.msgs = append(producer.msgs, msgProducedToKafka{string(msg.Value)}) 47 } 48 return 0, nil 49 } 50 51 // Returns a fake kafka sink. 52 func NewFakeSink() fakeKafkaSink { 53 producer := NewFakeKafkaProducer() 54 fakeTimeSeriesTopic := "kafkaTime-test-topic" 55 return fakeKafkaSink{ 56 &kafkaSink{ 57 producer: producer, 58 dataTopic: fakeTimeSeriesTopic, 59 }, 60 producer, 61 } 62 } 63 64 func TestStoreDataEmptyInput(t *testing.T) { 65 fakeSink := NewFakeSink() 66 dataBatch := core.DataBatch{} 67 fakeSink.ExportData(&dataBatch) 68 assert.Equal(t, 0, len(fakeSink.fakeProducer.msgs)) 69 } 70 71 func TestStoreMultipleDataInput(t *testing.T) { 72 fakeSink := NewFakeSink() 73 timestamp := time.Now() 74 75 l := make(map[string]string) 76 l["namespace_id"] = "123" 77 l["container_name"] = "/system.slice/-.mount" 78 l[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd" 79 80 l2 := make(map[string]string) 81 l2["namespace_id"] = "123" 82 l2["container_name"] = "/system.slice/dbus.service" 83 l2[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd" 84 85 l3 := make(map[string]string) 86 l3["namespace_id"] = "123" 87 l3[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd" 88 89 l4 := make(map[string]string) 90 l4["namespace_id"] = "" 91 l4[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd" 92 93 l5 := make(map[string]string) 94 l5["namespace_id"] = "123" 95 l5[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd" 96 97 metricSet1 := core.MetricSet{ 98 Labels: l, 99 MetricValues: map[string]core.MetricValue{ 100 "/system.slice/-.mount//cpu/limit": { 101 ValueType: core.ValueInt64, 102 MetricType: core.MetricCumulative, 103 IntValue: 123456, 104 }, 105 }, 106 } 107 108 metricSet2 := core.MetricSet{ 109 Labels: l2, 110 MetricValues: map[string]core.MetricValue{ 111 "/system.slice/dbus.service//cpu/usage": { 112 ValueType: core.ValueInt64, 113 MetricType: core.MetricCumulative, 114 IntValue: 123456, 115 }, 116 }, 117 } 118 119 metricSet3 := core.MetricSet{ 120 Labels: l3, 121 MetricValues: map[string]core.MetricValue{ 122 "test/metric/1": { 123 ValueType: core.ValueInt64, 124 MetricType: core.MetricCumulative, 125 IntValue: 123456, 126 }, 127 }, 128 } 129 130 metricSet4 := core.MetricSet{ 131 Labels: l4, 132 MetricValues: map[string]core.MetricValue{ 133 "test/metric/1": { 134 ValueType: core.ValueInt64, 135 MetricType: core.MetricCumulative, 136 IntValue: 123456, 137 }, 138 }, 139 } 140 141 metricSet5 := core.MetricSet{ 142 Labels: l5, 143 MetricValues: map[string]core.MetricValue{ 144 "removeme": { 145 ValueType: core.ValueInt64, 146 MetricType: core.MetricCumulative, 147 IntValue: 123456, 148 }, 149 }, 150 } 151 152 data := core.DataBatch{ 153 Timestamp: timestamp, 154 MetricSets: map[string]*core.MetricSet{ 155 "pod1": &metricSet1, 156 "pod2": &metricSet2, 157 "pod3": &metricSet3, 158 "pod4": &metricSet4, 159 "pod5": &metricSet5, 160 }, 161 } 162 163 timeStr, err := timestamp.UTC().MarshalJSON() 164 assert.NoError(t, err) 165 166 fakeSink.ExportData(&data) 167 168 //expect msg string 169 assert.Equal(t, 5, len(fakeSink.fakeProducer.msgs)) 170 171 var expectMsgTemplate = [5]string{ 172 `{"MetricsName":"/system.slice/-.mount//cpu/limit","MetricsValue":{"value":123456},"MetricsTimestamp":%s,"MetricsTags":{"container_name":"/system.slice/-.mount","namespace_id":"123","pod_id":"aaaa-bbbb-cccc-dddd"}}`, 173 `{"MetricsName":"/system.slice/dbus.service//cpu/usage","MetricsValue":{"value":123456},"MetricsTimestamp":%s,"MetricsTags":{"container_name":"/system.slice/dbus.service","namespace_id":"123","pod_id":"aaaa-bbbb-cccc-dddd"}}`, 174 `{"MetricsName":"test/metric/1","MetricsValue":{"value":123456},"MetricsTimestamp":%s,"MetricsTags":{"namespace_id":"123","pod_id":"aaaa-bbbb-cccc-dddd"}}`, 175 `{"MetricsName":"test/metric/1","MetricsValue":{"value":123456},"MetricsTimestamp":%s,"MetricsTags":{"namespace_id":"","pod_id":"aaaa-bbbb-cccc-dddd"}}`, 176 `{"MetricsName":"removeme","MetricsValue":{"value":123456},"MetricsTimestamp":%s,"MetricsTags":{"namespace_id":"123","pod_id":"aaaa-bbbb-cccc-dddd"}}`, 177 } 178 179 msgsString := fmt.Sprintf("%s", fakeSink.fakeProducer.msgs) 180 181 for _, mgsTemplate := range expectMsgTemplate { 182 expectMsg := fmt.Sprintf(mgsTemplate, timeStr) 183 assert.Contains(t, msgsString, expectMsg) 184 } 185 186 }