github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/events/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  	kube_api "k8s.io/api/core/v1"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	util "k8s.io/client-go/util/testing"
    28  	influxdb_common "k8s.io/heapster/common/influxdb"
    29  	"k8s.io/heapster/events/core"
    30  )
    31  
    32  type fakeInfluxDBEventSink struct {
    33  	core.EventSink
    34  	fakeDbClient *influxdb_common.FakeInfluxDBClient
    35  }
    36  
    37  func NewFakeSink() fakeInfluxDBEventSink {
    38  	return fakeInfluxDBEventSink{
    39  		&influxdbSink{
    40  			client: influxdb_common.Client,
    41  			c:      influxdb_common.Config,
    42  		},
    43  		influxdb_common.Client,
    44  	}
    45  }
    46  
    47  func TestStoreDataEmptyInput(t *testing.T) {
    48  	fakeSink := NewFakeSink()
    49  	eventBatch := core.EventBatch{}
    50  	fakeSink.ExportEvents(&eventBatch)
    51  	assert.Equal(t, 0, len(fakeSink.fakeDbClient.Pnts))
    52  }
    53  
    54  func TestStoreMultipleDataInput(t *testing.T) {
    55  	fakeSink := NewFakeSink()
    56  	timestamp := time.Now()
    57  
    58  	now := time.Now()
    59  	event1 := kube_api.Event{
    60  		Message:        "event1",
    61  		Count:          100,
    62  		LastTimestamp:  metav1.NewTime(now),
    63  		FirstTimestamp: metav1.NewTime(now),
    64  	}
    65  
    66  	event2 := kube_api.Event{
    67  		Message:        "event2",
    68  		Count:          101,
    69  		LastTimestamp:  metav1.NewTime(now),
    70  		FirstTimestamp: metav1.NewTime(now),
    71  	}
    72  
    73  	data := core.EventBatch{
    74  		Timestamp: timestamp,
    75  		Events: []*kube_api.Event{
    76  			&event1,
    77  			&event2,
    78  		},
    79  	}
    80  
    81  	fakeSink.ExportEvents(&data)
    82  	assert.Equal(t, 2, len(fakeSink.fakeDbClient.Pnts))
    83  }
    84  
    85  func TestCreateInfluxdbSink(t *testing.T) {
    86  	handler := util.FakeHandler{
    87  		StatusCode:   200,
    88  		RequestBody:  "",
    89  		ResponseBody: "",
    90  		T:            t,
    91  	}
    92  	server := httptest.NewServer(&handler)
    93  	defer server.Close()
    94  
    95  	stubInfluxDBUrl, err := url.Parse(server.URL)
    96  	assert.NoError(t, err)
    97  
    98  	//create influxdb sink
    99  	sink, err := CreateInfluxdbSink(stubInfluxDBUrl)
   100  	assert.NoError(t, err)
   101  
   102  	//check sink name
   103  	assert.Equal(t, sink.Name(), "InfluxDB Sink")
   104  }