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