github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/metrics/sinks/honeycomb/honeycomb_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 honeycomb
    16  
    17  import (
    18  	"net/url"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  	honeycomb_common "k8s.io/heapster/common/honeycomb"
    24  	"k8s.io/heapster/metrics/core"
    25  )
    26  
    27  type fakeHoneycombDataSink struct {
    28  	core.DataSink
    29  	fakeDbClient *honeycomb_common.FakeHoneycombClient
    30  }
    31  
    32  func newRawHoneycombSink(client honeycomb_common.Client) *honeycombSink {
    33  	return &honeycombSink{
    34  		client: client,
    35  	}
    36  }
    37  
    38  func NewFakeSink() fakeHoneycombDataSink {
    39  	fakeClient := honeycomb_common.NewFakeHoneycombClient()
    40  	return fakeHoneycombDataSink{
    41  		newRawHoneycombSink(fakeClient),
    42  		fakeClient,
    43  	}
    44  }
    45  
    46  func TestStoreDataEmptyInput(t *testing.T) {
    47  	fakeSink := NewFakeSink()
    48  	dataBatch := core.DataBatch{}
    49  	fakeSink.ExportData(&dataBatch)
    50  	assert.Equal(t, 0, len(fakeSink.fakeDbClient.BatchPoints))
    51  }
    52  
    53  func TestStoreMultipleDataInput(t *testing.T) {
    54  	fakeSink := NewFakeSink()
    55  	timestamp := time.Now()
    56  
    57  	l := make(map[string]string)
    58  	l["namespace_id"] = "123"
    59  	l["container_name"] = "/system.slice/-.mount"
    60  	l[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd"
    61  
    62  	l2 := make(map[string]string)
    63  	l2["namespace_id"] = "123"
    64  	l2["container_name"] = "/system.slice/dbus.service"
    65  	l2[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd"
    66  
    67  	l3 := make(map[string]string)
    68  	l3["namespace_id"] = "123"
    69  	l3[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd"
    70  
    71  	l4 := make(map[string]string)
    72  	l4["namespace_id"] = ""
    73  	l4[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd"
    74  
    75  	l5 := make(map[string]string)
    76  	l5["namespace_id"] = "123"
    77  	l5[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd"
    78  
    79  	metricSet1 := core.MetricSet{
    80  		Labels: l,
    81  		MetricValues: map[string]core.MetricValue{
    82  			"/system.slice/-.mount//cpu/limit": {
    83  				ValueType:  core.ValueInt64,
    84  				MetricType: core.MetricCumulative,
    85  				IntValue:   123456,
    86  			},
    87  		},
    88  	}
    89  
    90  	metricSet2 := core.MetricSet{
    91  		Labels: l2,
    92  		MetricValues: map[string]core.MetricValue{
    93  			"/system.slice/dbus.service//cpu/usage": {
    94  				ValueType:  core.ValueInt64,
    95  				MetricType: core.MetricCumulative,
    96  				IntValue:   123456,
    97  			},
    98  		},
    99  	}
   100  
   101  	metricSet3 := core.MetricSet{
   102  		Labels: l3,
   103  		MetricValues: map[string]core.MetricValue{
   104  			"test/metric/1": {
   105  				ValueType:  core.ValueInt64,
   106  				MetricType: core.MetricCumulative,
   107  				IntValue:   123456,
   108  			},
   109  		},
   110  	}
   111  
   112  	metricSet4 := core.MetricSet{
   113  		Labels: l4,
   114  		MetricValues: map[string]core.MetricValue{
   115  			"test/metric/1": {
   116  				ValueType:  core.ValueInt64,
   117  				MetricType: core.MetricCumulative,
   118  				IntValue:   123456,
   119  			},
   120  		},
   121  	}
   122  
   123  	metricSet5 := core.MetricSet{
   124  		Labels: l5,
   125  		MetricValues: map[string]core.MetricValue{
   126  			"removeme": {
   127  				ValueType:  core.ValueFloat,
   128  				MetricType: core.MetricCumulative,
   129  				FloatValue: 1.23456,
   130  			},
   131  		},
   132  	}
   133  
   134  	data := core.DataBatch{
   135  		Timestamp: timestamp,
   136  		MetricSets: map[string]*core.MetricSet{
   137  			"pod1": &metricSet1,
   138  			"pod2": &metricSet2,
   139  			"pod3": &metricSet3,
   140  			"pod4": &metricSet4,
   141  			"pod5": &metricSet5,
   142  		},
   143  	}
   144  
   145  	fakeSink.ExportData(&data)
   146  	assert.Equal(t, 5, len(fakeSink.fakeDbClient.BatchPoints))
   147  }
   148  
   149  func TestCreateHoneycombSink(t *testing.T) {
   150  	stubHoneycombURL, err := url.Parse("?dataset=testdataset&writekey=testwritekey")
   151  	assert.NoError(t, err)
   152  
   153  	//create honeycomb sink
   154  	sink, err := NewHoneycombSink(stubHoneycombURL)
   155  	assert.NoError(t, err)
   156  
   157  	//check sink name
   158  	assert.Equal(t, sink.Name(), "Honeycomb Sink")
   159  }