github.com/jonaz/heapster@v1.3.0-beta.0.0.20170208112634-cd3c15ca3d29/metrics/sinks/elasticsearch/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 elasticsearch
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"gopkg.in/olivere/elastic.v3"
    25  	esCommon "k8s.io/heapster/common/elasticsearch"
    26  	"k8s.io/heapster/metrics/core"
    27  )
    28  
    29  type fakeESSink struct {
    30  	core.DataSink
    31  	savedData map[string][]string
    32  }
    33  
    34  var FakeESSink fakeESSink
    35  
    36  func SaveDataIntoES_Stub(date time.Time, typeName string, sinkData []interface{}) error {
    37  	for _, data := range sinkData {
    38  		jsonItems, err := json.Marshal(data)
    39  		if err != nil {
    40  			return fmt.Errorf("failed to transform the items to json : %s", err)
    41  		}
    42  
    43  		if FakeESSink.savedData[typeName] == nil {
    44  			FakeESSink.savedData[typeName] = []string{}
    45  		}
    46  
    47  		FakeESSink.savedData[typeName] = append(FakeESSink.savedData[typeName], string(jsonItems))
    48  	}
    49  	return nil
    50  }
    51  
    52  // Returns a fake ES sink.
    53  func NewFakeSink() fakeESSink {
    54  	savedData := make(map[string][]string)
    55  	return fakeESSink{
    56  		&elasticSearchSink{
    57  			saveData:  SaveDataIntoES_Stub,
    58  			flushData: func() error { return nil },
    59  			esSvc: esCommon.ElasticSearchService{
    60  				EsClient:    &elastic.Client{},
    61  				ClusterName: esCommon.ESClusterName,
    62  			},
    63  		},
    64  		savedData,
    65  	}
    66  }
    67  
    68  func TestStoreDataEmptyInput(t *testing.T) {
    69  	FakeESSink := NewFakeSink()
    70  	dataBatch := core.DataBatch{}
    71  	FakeESSink.ExportData(&dataBatch)
    72  	assert.Equal(t, 0, len(FakeESSink.savedData))
    73  }
    74  
    75  func TestStoreMultipleDataInput(t *testing.T) {
    76  	timestamp := time.Now()
    77  
    78  	l := make(map[string]string)
    79  	l["namespace_id"] = "123"
    80  	l["container_name"] = "/system.slice/-.mount"
    81  	l[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd"
    82  
    83  	l2 := make(map[string]string)
    84  	l2["namespace_id"] = "123"
    85  	l2["container_name"] = "/system.slice/dbus.service"
    86  	l2[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd"
    87  
    88  	l3 := make(map[string]string)
    89  	l3["namespace_id"] = "123"
    90  	l3[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd"
    91  
    92  	l4 := make(map[string]string)
    93  	l4["namespace_id"] = ""
    94  	l4[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd"
    95  
    96  	l5 := make(map[string]string)
    97  	l5["namespace_id"] = "123"
    98  	l5[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd"
    99  
   100  	metricSet1 := core.MetricSet{
   101  		Labels: l,
   102  		MetricValues: map[string]core.MetricValue{
   103  			"/system.slice/-.mount//cpu/limit": {
   104  				ValueType:  core.ValueInt64,
   105  				MetricType: core.MetricCumulative,
   106  				IntValue:   123456,
   107  			},
   108  		},
   109  	}
   110  
   111  	metricSet2 := core.MetricSet{
   112  		Labels: l2,
   113  		MetricValues: map[string]core.MetricValue{
   114  			"/system.slice/dbus.service//cpu/usage": {
   115  				ValueType:  core.ValueInt64,
   116  				MetricType: core.MetricCumulative,
   117  				IntValue:   123456,
   118  			},
   119  		},
   120  	}
   121  
   122  	metricSet3 := core.MetricSet{
   123  		Labels: l3,
   124  		MetricValues: map[string]core.MetricValue{
   125  			"test/metric/1": {
   126  				ValueType:  core.ValueInt64,
   127  				MetricType: core.MetricCumulative,
   128  				IntValue:   123456,
   129  			},
   130  		},
   131  	}
   132  
   133  	metricSet4 := core.MetricSet{
   134  		Labels: l4,
   135  		MetricValues: map[string]core.MetricValue{
   136  			"test/metric/1": {
   137  				ValueType:  core.ValueInt64,
   138  				MetricType: core.MetricCumulative,
   139  				IntValue:   123456,
   140  			},
   141  		},
   142  	}
   143  
   144  	metricSet5 := core.MetricSet{
   145  		Labels: l5,
   146  		MetricValues: map[string]core.MetricValue{
   147  			"removeme": {
   148  				ValueType:  core.ValueInt64,
   149  				MetricType: core.MetricCumulative,
   150  				IntValue:   123456,
   151  			},
   152  		},
   153  	}
   154  
   155  	metricSet6 := core.MetricSet{
   156  		Labels: l,
   157  		MetricValues: map[string]core.MetricValue{
   158  			"cpu/usage": {
   159  				ValueType:  core.ValueInt64,
   160  				MetricType: core.MetricGauge,
   161  				IntValue:   123456,
   162  			},
   163  			"cpu/limit": {
   164  				ValueType:  core.ValueInt64,
   165  				MetricType: core.MetricGauge,
   166  				IntValue:   223456,
   167  			},
   168  		},
   169  	}
   170  
   171  	data := core.DataBatch{
   172  		Timestamp: timestamp,
   173  		MetricSets: map[string]*core.MetricSet{
   174  			"pod1": &metricSet1,
   175  			"pod2": &metricSet2,
   176  			"pod3": &metricSet3,
   177  			"pod4": &metricSet4,
   178  			"pod5": &metricSet5,
   179  			"pod6": &metricSet6,
   180  		},
   181  	}
   182  
   183  	timeStr, err := timestamp.UTC().MarshalJSON()
   184  	assert.NoError(t, err)
   185  
   186  	FakeESSink = NewFakeSink()
   187  	FakeESSink.ExportData(&data)
   188  
   189  	//expect msg string
   190  	assert.Equal(t, 2, len(FakeESSink.savedData))
   191  
   192  	var expectMsgTemplate = [6]string{
   193  		`{"GeneralMetricsTimestamp":%s,"MetricsTags":{"cluster_name":"default","namespace_id":"","pod_id":"aaaa-bbbb-cccc-dddd"},"MetricsName":"test/metric/1","MetricsValue":{"value":123456}}`,
   194  		`{"GeneralMetricsTimestamp":%s,"MetricsTags":{"cluster_name":"default","namespace_id":"123","pod_id":"aaaa-bbbb-cccc-dddd"},"MetricsName":"removeme","MetricsValue":{"value":123456}}`,
   195  		`{"GeneralMetricsTimestamp":%s,"MetricsTags":{"cluster_name":"default","container_name":"/system.slice/-.mount","namespace_id":"123","pod_id":"aaaa-bbbb-cccc-dddd"},"MetricsName":"/system.slice/-.mount//cpu/limit","MetricsValue":{"value":123456}}`,
   196  		`{"GeneralMetricsTimestamp":%s,"MetricsTags":{"cluster_name":"default","container_name":"/system.slice/dbus.service","namespace_id":"123","pod_id":"aaaa-bbbb-cccc-dddd"},"MetricsName":"/system.slice/dbus.service//cpu/usage","MetricsValue":{"value":123456}}`,
   197  		`{"GeneralMetricsTimestamp":%s,"MetricsTags":{"cluster_name":"default","namespace_id":"123","pod_id":"aaaa-bbbb-cccc-dddd"},"MetricsName":"test/metric/1","MetricsValue":{"value":123456}}`,
   198  		`{"CpuMetricsTimestamp":%s,"Metrics":{"cpu/limit":{"value":223456},"cpu/usage":{"value":123456}},"MetricsTags":{"cluster_name":"default","container_name":"/system.slice/-.mount","namespace_id":"123","pod_id":"aaaa-bbbb-cccc-dddd"}}`,
   199  	}
   200  
   201  	msgsString := fmt.Sprintf("%s", FakeESSink.savedData)
   202  
   203  	for _, mgsTemplate := range expectMsgTemplate {
   204  		expectMsg := fmt.Sprintf(mgsTemplate, timeStr)
   205  		assert.Contains(t, msgsString, expectMsg)
   206  	}
   207  
   208  	FakeESSink = NewFakeSink()
   209  }