github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/metrics/sinks/statsd/driver_test.go (about) 1 // Copyright 2016 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 statsd 16 17 import ( 18 "fmt" 19 "net/url" 20 "strings" 21 "testing" 22 "time" 23 24 "github.com/stretchr/testify/assert" 25 "k8s.io/heapster/metrics/core" 26 ) 27 28 const ( 29 driverUrl = "udp://127.0.0.1:4125?protocolType=influxstatsd&allowedLabels=tag1,tag3" 30 ) 31 32 func TestDriverName(t *testing.T) { 33 url, err := url.Parse(driverUrl) 34 assert.NoError(t, err) 35 36 sink, err := NewStatsdSink(url) 37 assert.NoError(t, err) 38 assert.NotNil(t, sink) 39 40 assert.Equal(t, "StatsD Sink", sink.Name()) 41 } 42 43 func TestDriverExportData(t *testing.T) { 44 url, err := url.Parse(driverUrl) 45 assert.NoError(t, err) 46 47 client := &dummyStatsdClientImpl{messages: nil} 48 sink, err := NewStatsdSinkWithClient(url, client) 49 assert.NoError(t, err) 50 assert.NotNil(t, sink) 51 52 timestamp := time.Now() 53 54 m1 := "test.metric.1" 55 m2 := "test.metric.2" 56 m3 := "test.metric.3" 57 m4 := "test.metric.4" 58 59 var labels = map[string]string{ 60 "tag1": "value1", 61 "tag2": "value2", 62 "tag3": "value3", 63 } 64 65 labelStr := "tag1=value1,tag3=value3" 66 expectedMsgs := [...]string{ 67 fmt.Sprintf("%s,%s:1|g\n", m1, labelStr), 68 fmt.Sprintf("%s,%s:2|g\n", m2, labelStr), 69 fmt.Sprintf("%s,%s:3|g\n", m3, labelStr), 70 fmt.Sprintf("%s,%s:4|g\n", m4, labelStr), 71 } 72 metricSet1 := core.MetricSet{ 73 Labels: labels, 74 MetricValues: map[string]core.MetricValue{ 75 m1: { 76 ValueType: core.ValueInt64, 77 MetricType: core.MetricGauge, 78 IntValue: 1, 79 }, 80 m2: { 81 ValueType: core.ValueInt64, 82 MetricType: core.MetricGauge, 83 IntValue: 2, 84 }, 85 }, 86 } 87 88 metricSet2 := core.MetricSet{ 89 Labels: labels, 90 MetricValues: map[string]core.MetricValue{ 91 m3: { 92 ValueType: core.ValueInt64, 93 MetricType: core.MetricGauge, 94 IntValue: 3, 95 }, 96 m4: { 97 ValueType: core.ValueInt64, 98 MetricType: core.MetricGauge, 99 IntValue: 4, 100 }, 101 }, 102 } 103 104 dataBatch := &core.DataBatch{ 105 Timestamp: timestamp, 106 MetricSets: map[string]*core.MetricSet{ 107 "pod1": &metricSet1, 108 "pod2": &metricSet2, 109 }, 110 } 111 112 sink.ExportData(dataBatch) 113 114 res := strings.Join(client.messages, "\n") + "\n" 115 for _, expectedMsg := range expectedMsgs[:] { 116 assert.Contains(t, res, expectedMsg) 117 } 118 }