github.com/caesarxuchao/heapster@v1.1.0/events/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  	"github.com/olivere/elastic"
    21  	"github.com/stretchr/testify/assert"
    22  	esCommon "k8s.io/heapster/common/elasticsearch"
    23  	"k8s.io/heapster/events/core"
    24  	kube_api "k8s.io/kubernetes/pkg/api"
    25  	kube_api_unversioned "k8s.io/kubernetes/pkg/api/unversioned"
    26  	"testing"
    27  	"time"
    28  )
    29  
    30  type dataSavedToES struct {
    31  	data string
    32  }
    33  
    34  type fakeESSink struct {
    35  	core.EventSink
    36  	savedData []dataSavedToES
    37  }
    38  
    39  var FakeESSink fakeESSink
    40  
    41  func SaveDataIntoES_Stub(esClient *elastic.Client, indexName string, typeName string, sinkData interface{}) error {
    42  	jsonItems, err := json.Marshal(sinkData)
    43  	if err != nil {
    44  		return fmt.Errorf("failed to transform the items to json : %s", err)
    45  	}
    46  	FakeESSink.savedData = append(FakeESSink.savedData, dataSavedToES{string(jsonItems)})
    47  	return nil
    48  }
    49  
    50  // Returns a fake ES sink.
    51  func NewFakeSink() fakeESSink {
    52  	var ESClient elastic.Client
    53  	fakeSinkESNodes := make([]string, 2)
    54  	savedData := make([]dataSavedToES, 0)
    55  	return fakeESSink{
    56  		&elasticSearchSink{
    57  			esClient:     &ESClient,
    58  			saveDataFunc: SaveDataIntoES_Stub,
    59  			esConfig: esCommon.ElasticSearchConfig{
    60  				Index:        "heapster-metric-index",
    61  				NeedAuthen:   false,
    62  				EsUserName:   "admin",
    63  				EsUserSecret: "admin",
    64  				EsNodes:      fakeSinkESNodes,
    65  			},
    66  		},
    67  		savedData,
    68  	}
    69  }
    70  
    71  func TestStoreDataEmptyInput(t *testing.T) {
    72  	fakeSink := NewFakeSink()
    73  	dataBatch := core.EventBatch{}
    74  	fakeSink.ExportEvents(&dataBatch)
    75  	assert.Equal(t, 0, len(fakeSink.savedData))
    76  }
    77  
    78  func TestStoreMultipleDataInput(t *testing.T) {
    79  	fakeSink := NewFakeSink()
    80  	timestamp := time.Now()
    81  	now := time.Now()
    82  	event1 := kube_api.Event{
    83  		Message:        "event1",
    84  		Count:          100,
    85  		LastTimestamp:  kube_api_unversioned.NewTime(now),
    86  		FirstTimestamp: kube_api_unversioned.NewTime(now),
    87  	}
    88  	event2 := kube_api.Event{
    89  		Message:        "event2",
    90  		Count:          101,
    91  		LastTimestamp:  kube_api_unversioned.NewTime(now),
    92  		FirstTimestamp: kube_api_unversioned.NewTime(now),
    93  	}
    94  	data := core.EventBatch{
    95  		Timestamp: timestamp,
    96  		Events: []*kube_api.Event{
    97  			&event1,
    98  			&event2,
    99  		},
   100  	}
   101  	fakeSink.ExportEvents(&data)
   102  	// expect msg string
   103  	assert.Equal(t, 2, len(FakeESSink.savedData))
   104  	FakeESSink = fakeESSink{}
   105  }