github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/metrics/sinks/wavefront/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 wavefront
    16  
    17  import (
    18  	"github.com/stretchr/testify/assert"
    19  	"k8s.io/heapster/metrics/core"
    20  	"net/url"
    21  	"strconv"
    22  	"strings"
    23  	"testing"
    24  	"time"
    25  )
    26  
    27  var (
    28  	fakeNodeIp  = "192.168.1.23"
    29  	fakePodName = "redis-test"
    30  	fakePodUid  = "redis-test-uid"
    31  	fakeLabel   = map[string]string{
    32  		"name":                   "redis",
    33  		"io.kubernetes.pod.name": "default/redis-test",
    34  		"pod_id":                 fakePodUid,
    35  		"namespace_name":         "default",
    36  		"pod_name":               fakePodName,
    37  		"container_name":         "redis",
    38  		"container_base_image":   "kubernetes/redis:v1",
    39  		"namespace_id":           "namespace-test-uid",
    40  		"host_id":                fakeNodeIp,
    41  		"hostname":               fakeNodeIp,
    42  	}
    43  )
    44  
    45  func NewFakeWavefrontSink() *wavefrontSink {
    46  	return &wavefrontSink{
    47  		testMode:          true,
    48  		ClusterName:       "testCluster",
    49  		IncludeLabels:     false,
    50  		IncludeContainers: true,
    51  	}
    52  }
    53  
    54  func TestStoreTimeseriesEmptyInput(t *testing.T) {
    55  	fakeSink := NewFakeWavefrontSink()
    56  	db := core.DataBatch{}
    57  	fakeSink.ExportData(&db)
    58  	assert.Equal(t, 0, len(fakeSink.testReceivedLines))
    59  }
    60  
    61  func TestStoreTimeseriesMultipleTimeseriesInput(t *testing.T) {
    62  	fakeSink := NewFakeWavefrontSink()
    63  	batch := generateFakeBatch()
    64  	fakeSink.ExportData(batch)
    65  	assert.Equal(t, len(batch.MetricSets), len(fakeSink.testReceivedLines))
    66  }
    67  func TestName(t *testing.T) {
    68  	fakeSink := NewFakeWavefrontSink()
    69  	name := fakeSink.Name()
    70  	assert.Equal(t, name, "Wavefront Sink")
    71  }
    72  
    73  func TestNewMetricName(t *testing.T) {
    74  	fakeSink := NewFakeWavefrontSink()
    75  	batch := generateFakeBatch()
    76  	fakeSink.ExportData(batch)
    77  	name := "cpu/usage"
    78  	mtype := "pod_container"
    79  	newName := fakeSink.cleanMetricName(mtype, name)
    80  	assert.Equal(t, "pod_container.cpu.usage", newName)
    81  }
    82  
    83  func TestValidateLines(t *testing.T) {
    84  	fakeSink := NewFakeWavefrontSink()
    85  	batch := generateFakeBatch()
    86  	fakeSink.ExportData(batch)
    87  
    88  	//validate each line received from the fake batch
    89  	for _, line := range fakeSink.testReceivedLines {
    90  		parts := strings.Split(strings.TrimSpace(line), " ")
    91  
    92  		//second part should always be the numeric metric value
    93  		_, err := strconv.ParseFloat(parts[1], 64)
    94  		assert.NoError(t, err)
    95  
    96  		//third part should always be the epoch timestamp (a count of seconds)
    97  		_, err = strconv.ParseInt(parts[2], 0, 64)
    98  		assert.NoError(t, err)
    99  
   100  		//the fourth part should be the source tag
   101  		isSourceTag := strings.HasPrefix(parts[3], "source=")
   102  		assert.True(t, isSourceTag)
   103  
   104  		//all remaining parts are tags and must be key value pairs (containing "=")
   105  		tags := parts[4:]
   106  		for _, v := range tags {
   107  			assert.True(t, strings.Contains(v, "="))
   108  		}
   109  	}
   110  }
   111  
   112  func TestCreateWavefrontSinkWithNoEmptyInputs(t *testing.T) {
   113  	fakeUrl := "wavefront-proxy:2878?clusterName=testCluster&prefix=testPrefix&includeLabels=true&includeContainers=true"
   114  	uri, _ := url.Parse(fakeUrl)
   115  	sink, err := NewWavefrontSink(uri)
   116  	assert.NoError(t, err)
   117  	assert.NotNil(t, sink)
   118  	wfSink, ok := sink.(*wavefrontSink)
   119  	assert.Equal(t, true, ok)
   120  	assert.Equal(t, "wavefront-proxy:2878", wfSink.ProxyAddress)
   121  	assert.Equal(t, "testCluster", wfSink.ClusterName)
   122  	assert.Equal(t, "testPrefix", wfSink.Prefix)
   123  	assert.Equal(t, true, wfSink.IncludeLabels)
   124  	assert.Equal(t, true, wfSink.IncludeContainers)
   125  }
   126  
   127  func generateFakeBatch() *core.DataBatch {
   128  	batch := core.DataBatch{
   129  		Timestamp:  time.Now(),
   130  		MetricSets: map[string]*core.MetricSet{},
   131  	}
   132  
   133  	batch.MetricSets["m1"] = generateMetricSet("cpu/limit", core.MetricGauge, 1000)
   134  	batch.MetricSets["m2"] = generateMetricSet("cpu/usage", core.MetricCumulative, 43363664)
   135  	batch.MetricSets["m3"] = generateMetricSet("filesystem/limit", core.MetricGauge, 42241163264)
   136  	batch.MetricSets["m4"] = generateMetricSet("filesystem/usage", core.MetricGauge, 32768)
   137  	batch.MetricSets["m5"] = generateMetricSet("memory/limit", core.MetricGauge, -1)
   138  	batch.MetricSets["m6"] = generateMetricSet("memory/usage", core.MetricGauge, 487424)
   139  	batch.MetricSets["m7"] = generateMetricSet("memory/working_set", core.MetricGauge, 491520)
   140  	batch.MetricSets["m8"] = generateMetricSet("uptime", core.MetricCumulative, 910823)
   141  	return &batch
   142  }
   143  
   144  func generateMetricSet(name string, metricType core.MetricType, value int64) *core.MetricSet {
   145  	set := &core.MetricSet{
   146  		Labels: fakeLabel,
   147  		MetricValues: map[string]core.MetricValue{
   148  			name: {
   149  				MetricType: metricType,
   150  				ValueType:  core.ValueInt64,
   151  				IntValue:   value,
   152  			},
   153  		},
   154  	}
   155  	return set
   156  }