github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/events/sinks/honeycomb/honeycomb_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 honeycomb
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"net/url"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	kube_api "k8s.io/api/core/v1"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	honeycomb_common "k8s.io/heapster/common/honeycomb"
    27  	"k8s.io/heapster/events/core"
    28  )
    29  
    30  type fakeHoneycombEventSink struct {
    31  	core.EventSink
    32  	fakeDbClient *honeycomb_common.FakeHoneycombClient
    33  }
    34  
    35  func NewFakeSink() fakeHoneycombEventSink {
    36  	fakeClient := honeycomb_common.NewFakeHoneycombClient()
    37  	return fakeHoneycombEventSink{
    38  		&honeycombSink{
    39  			client: fakeClient,
    40  		},
    41  		fakeClient,
    42  	}
    43  }
    44  
    45  func TestStoreDataEmptyInput(t *testing.T) {
    46  	fakeSink := NewFakeSink()
    47  	eventBatch := core.EventBatch{}
    48  	fakeSink.ExportEvents(&eventBatch)
    49  	assert.Equal(t, 0, len(fakeSink.fakeDbClient.BatchPoints))
    50  }
    51  
    52  func TestStoreMultipleDataInput(t *testing.T) {
    53  	fakeSink := NewFakeSink()
    54  	timestamp := time.Now()
    55  
    56  	now := time.Now()
    57  	event1 := kube_api.Event{
    58  		Message:        "event1",
    59  		Count:          100,
    60  		LastTimestamp:  metav1.NewTime(now),
    61  		FirstTimestamp: metav1.NewTime(now),
    62  	}
    63  
    64  	event2 := kube_api.Event{
    65  		Message:        "event2",
    66  		Count:          101,
    67  		LastTimestamp:  metav1.NewTime(now),
    68  		FirstTimestamp: metav1.NewTime(now),
    69  	}
    70  
    71  	data := core.EventBatch{
    72  		Timestamp: timestamp,
    73  		Events: []*kube_api.Event{
    74  			&event1,
    75  			&event2,
    76  		},
    77  	}
    78  
    79  	fakeSink.ExportEvents(&data)
    80  	assert.Equal(t, 2, len(fakeSink.fakeDbClient.BatchPoints))
    81  }
    82  
    83  func TestCreateHoneycombSink(t *testing.T) {
    84  	stubHoneycombURL, err := url.Parse("?dataset=testdataset&writekey=testwritekey")
    85  	assert.NoError(t, err)
    86  
    87  	//create honeycomb sink
    88  	sink, err := NewHoneycombSink(stubHoneycombURL)
    89  	assert.NoError(t, err)
    90  
    91  	//check sink name
    92  	assert.Equal(t, sink.Name(), "Honeycomb Sink")
    93  }