github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/events/sinks/kafka/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 kafka
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	kube_api "k8s.io/api/core/v1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	event_core "k8s.io/heapster/events/core"
    25  )
    26  
    27  type fakeKafkaClient struct {
    28  	points []KafkaSinkPoint
    29  }
    30  
    31  type fakeKafkaSink struct {
    32  	event_core.EventSink
    33  	fakeClient *fakeKafkaClient
    34  }
    35  
    36  func NewFakeKafkaClient() *fakeKafkaClient {
    37  	return &fakeKafkaClient{[]KafkaSinkPoint{}}
    38  }
    39  
    40  func (client *fakeKafkaClient) ProduceKafkaMessage(msgData interface{}) error {
    41  	if point, ok := msgData.(KafkaSinkPoint); ok {
    42  		client.points = append(client.points, point)
    43  	}
    44  
    45  	return nil
    46  }
    47  
    48  func (client *fakeKafkaClient) Name() string {
    49  	return "Apache Kafka Sink"
    50  }
    51  
    52  func (client *fakeKafkaClient) Stop() {
    53  	// nothing needs to be done.
    54  }
    55  
    56  // Returns a fake kafka sink.
    57  func NewFakeSink() fakeKafkaSink {
    58  	client := NewFakeKafkaClient()
    59  	return fakeKafkaSink{
    60  		&kafkaSink{
    61  			KafkaClient: client,
    62  		},
    63  		client,
    64  	}
    65  }
    66  
    67  func TestStoreDataEmptyInput(t *testing.T) {
    68  	fakeSink := NewFakeSink()
    69  	eventsBatch := event_core.EventBatch{}
    70  	fakeSink.ExportEvents(&eventsBatch)
    71  	assert.Equal(t, 0, len(fakeSink.fakeClient.points))
    72  }
    73  
    74  func TestStoreMultipleEventsInput(t *testing.T) {
    75  	fakeSink := NewFakeSink()
    76  	timestamp := time.Now()
    77  	now := time.Now()
    78  	event1 := kube_api.Event{
    79  		Message:        "event1",
    80  		Count:          100,
    81  		LastTimestamp:  metav1.NewTime(now),
    82  		FirstTimestamp: metav1.NewTime(now),
    83  	}
    84  	event2 := kube_api.Event{
    85  		Message:        "event2",
    86  		Count:          101,
    87  		LastTimestamp:  metav1.NewTime(now),
    88  		FirstTimestamp: metav1.NewTime(now),
    89  	}
    90  	data := event_core.EventBatch{
    91  		Timestamp: timestamp,
    92  		Events: []*kube_api.Event{
    93  			&event1,
    94  			&event2,
    95  		},
    96  	}
    97  	fakeSink.ExportEvents(&data)
    98  	// expect msg string
    99  	assert.Equal(t, 2, len(fakeSink.fakeClient.points))
   100  
   101  }