github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/events/sinks/riemann/driver_test.go (about)

     1  // Copyright 2017 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 riemann
    16  
    17  import (
    18  	"strconv"
    19  	"testing"
    20  	"time"
    21  
    22  	pb "github.com/golang/protobuf/proto"
    23  	"github.com/riemann/riemann-go-client/proto"
    24  	"github.com/stretchr/testify/assert"
    25  	kube_api "k8s.io/api/core/v1"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	riemannCommon "k8s.io/heapster/common/riemann"
    28  	"k8s.io/heapster/events/core"
    29  )
    30  
    31  type fakeRiemannClient struct {
    32  	events []proto.Event
    33  }
    34  
    35  type fakeRiemannSink struct {
    36  	core.EventSink
    37  	fakeRiemannClient *fakeRiemannClient
    38  }
    39  
    40  func NewFakeRiemannClient() *fakeRiemannClient {
    41  	return &fakeRiemannClient{[]proto.Event{}}
    42  }
    43  
    44  func (client *fakeRiemannClient) Connect(timeout int32) error {
    45  	return nil
    46  }
    47  
    48  func (client *fakeRiemannClient) Close() error {
    49  	return nil
    50  }
    51  
    52  func (client *fakeRiemannClient) Send(e *proto.Msg) (*proto.Msg, error) {
    53  	msg := &proto.Msg{Ok: pb.Bool(true)}
    54  	for _, event := range e.Events {
    55  		client.events = append(client.events, *event)
    56  	}
    57  	// always returns a Ok msg
    58  	return msg, nil
    59  }
    60  
    61  // Returns a fake Riemann sink.
    62  func NewFakeSink() fakeRiemannSink {
    63  	riemannClient := NewFakeRiemannClient()
    64  	c := riemannCommon.RiemannConfig{
    65  		Host:      "riemann-heapster:5555",
    66  		Ttl:       60.0,
    67  		State:     "",
    68  		Tags:      []string{"heapster"},
    69  		BatchSize: 1000,
    70  	}
    71  
    72  	return fakeRiemannSink{
    73  		&RiemannSink{
    74  			client: riemannClient,
    75  			config: c,
    76  		},
    77  		riemannClient,
    78  	}
    79  }
    80  
    81  func TestStoreDataEmptyInput(t *testing.T) {
    82  	fakeSink := NewFakeSink()
    83  	dataBatch := core.EventBatch{}
    84  	fakeSink.ExportEvents(&dataBatch)
    85  	assert.Equal(t, 0, len(fakeSink.fakeRiemannClient.events))
    86  }
    87  
    88  func TestStoreMultipleDataInput(t *testing.T) {
    89  	fakeSink := NewFakeSink()
    90  	timestamp := time.Now()
    91  
    92  	data := core.EventBatch{
    93  		Timestamp: timestamp,
    94  		Events: []*kube_api.Event{
    95  			{
    96  				Message: "event1",
    97  				Type:    "Normal",
    98  				Count:   100,
    99  				Source: kube_api.EventSource{
   100  					Host:      "riemann",
   101  					Component: "component",
   102  				},
   103  				LastTimestamp:  metav1.NewTime(timestamp),
   104  				FirstTimestamp: metav1.NewTime(timestamp),
   105  			},
   106  			{
   107  				Message: "event2",
   108  				Type:    "Warning",
   109  				Count:   101,
   110  				Source: kube_api.EventSource{
   111  					Host:      "riemann",
   112  					Component: "component",
   113  				},
   114  				LastTimestamp:  metav1.NewTime(timestamp),
   115  				FirstTimestamp: metav1.NewTime(timestamp),
   116  			},
   117  			{
   118  				Message: "event3",
   119  				Count:   102,
   120  				Source: kube_api.EventSource{
   121  					Host:      "riemann1",
   122  					Component: "component1",
   123  				},
   124  				Reason: "because",
   125  				InvolvedObject: kube_api.ObjectReference{
   126  					Kind:            "kind",
   127  					Namespace:       "kube-system",
   128  					Name:            "objectname",
   129  					UID:             "uuid",
   130  					APIVersion:      "v1",
   131  					ResourceVersion: "v2",
   132  					FieldPath:       "/foo",
   133  				},
   134  			},
   135  		},
   136  	}
   137  	fakeSink.ExportEvents(&data)
   138  	// expect msg string
   139  	assert.Equal(t, 3, len(fakeSink.fakeRiemannClient.events))
   140  	timeValue := timestamp.Unix()
   141  	var expectedEvents = []*proto.Event{
   142  		{
   143  			Host:         pb.String("riemann"),
   144  			Time:         pb.Int64(timeValue),
   145  			Ttl:          pb.Float32(60),
   146  			MetricSint64: pb.Int64(100),
   147  			Service:      pb.String("."),
   148  			Description:  pb.String("event1"),
   149  			State:        pb.String("ok"),
   150  			Tags:         []string{"heapster"},
   151  			Attributes: []*proto.Attribute{
   152  				{
   153  					Key:   pb.String("api-version"),
   154  					Value: pb.String(""),
   155  				},
   156  				{
   157  					Key:   pb.String("component"),
   158  					Value: pb.String("component"),
   159  				},
   160  				{
   161  					Key:   pb.String("field-path"),
   162  					Value: pb.String(""),
   163  				},
   164  				{
   165  					Key:   pb.String("first-timestamp"),
   166  					Value: pb.String(strconv.FormatInt(timeValue, 10)),
   167  				},
   168  				{
   169  					Key:   pb.String("last-timestamp"),
   170  					Value: pb.String(strconv.FormatInt(timeValue, 10)),
   171  				},
   172  				{
   173  					Key:   pb.String("name"),
   174  					Value: pb.String(""),
   175  				},
   176  				{
   177  					Key:   pb.String("namespace"),
   178  					Value: pb.String(""),
   179  				},
   180  				{
   181  					Key:   pb.String("resource-version"),
   182  					Value: pb.String(""),
   183  				},
   184  				{
   185  					Key:   pb.String("uid"),
   186  					Value: pb.String(""),
   187  				},
   188  			},
   189  		},
   190  		{
   191  			Host:         pb.String("riemann"),
   192  			Time:         pb.Int64(timeValue),
   193  			Ttl:          pb.Float32(60),
   194  			MetricSint64: pb.Int64(101),
   195  			Service:      pb.String("."),
   196  			Description:  pb.String("event2"),
   197  			State:        pb.String("warning"),
   198  			Tags:         []string{"heapster"},
   199  			Attributes: []*proto.Attribute{
   200  				{
   201  					Key:   pb.String("api-version"),
   202  					Value: pb.String(""),
   203  				},
   204  				{
   205  					Key:   pb.String("component"),
   206  					Value: pb.String("component"),
   207  				},
   208  				{
   209  					Key:   pb.String("field-path"),
   210  					Value: pb.String(""),
   211  				},
   212  				{
   213  					Key:   pb.String("first-timestamp"),
   214  					Value: pb.String(strconv.FormatInt(timeValue, 10)),
   215  				},
   216  				{
   217  					Key:   pb.String("last-timestamp"),
   218  					Value: pb.String(strconv.FormatInt(timeValue, 10)),
   219  				},
   220  				{
   221  					Key:   pb.String("name"),
   222  					Value: pb.String(""),
   223  				},
   224  				{
   225  					Key:   pb.String("namespace"),
   226  					Value: pb.String(""),
   227  				},
   228  				{
   229  					Key:   pb.String("resource-version"),
   230  					Value: pb.String(""),
   231  				},
   232  				{
   233  					Key:   pb.String("uid"),
   234  					Value: pb.String(""),
   235  				},
   236  			},
   237  		},
   238  		{
   239  			Host:         pb.String("riemann1"),
   240  			Time:         pb.Int64(timeValue),
   241  			Ttl:          pb.Float32(60),
   242  			MetricSint64: pb.Int64(102),
   243  			Service:      pb.String("kind.because"),
   244  			Description:  pb.String("event3"),
   245  			State:        pb.String("warning"),
   246  			Tags:         []string{"heapster"},
   247  			Attributes: []*proto.Attribute{
   248  				{
   249  					Key:   pb.String("api-version"),
   250  					Value: pb.String("v1"),
   251  				},
   252  				{
   253  					Key:   pb.String("component"),
   254  					Value: pb.String("component1"),
   255  				},
   256  				{
   257  					Key:   pb.String("field-path"),
   258  					Value: pb.String("/foo"),
   259  				},
   260  				{
   261  					Key:   pb.String("first-timestamp"),
   262  					Value: pb.String(""),
   263  				},
   264  				{
   265  					Key:   pb.String("last-timestamp"),
   266  					Value: pb.String(""),
   267  				},
   268  				{
   269  					Key:   pb.String("name"),
   270  					Value: pb.String("objectname"),
   271  				},
   272  				{
   273  					Key:   pb.String("namespace"),
   274  					Value: pb.String("kube-system"),
   275  				},
   276  				{
   277  					Key:   pb.String("resource-version"),
   278  					Value: pb.String("v2"),
   279  				},
   280  				{
   281  					Key:   pb.String("uid"),
   282  					Value: pb.String("uuid"),
   283  				},
   284  			},
   285  		},
   286  	}
   287  	for _, expectedEvent := range expectedEvents {
   288  		found := false
   289  		for _, sinkEvent := range fakeSink.fakeRiemannClient.events {
   290  			if pb.Equal(expectedEvent, &sinkEvent) {
   291  				found = true
   292  			}
   293  		}
   294  		if !found {
   295  			t.Error("Error, event not found in sink")
   296  		}
   297  	}
   298  }