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