github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/metrics/sinks/riemann/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 riemann
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	pb "github.com/golang/protobuf/proto"
    22  	"github.com/riemann/riemann-go-client"
    23  	"github.com/riemann/riemann-go-client/proto"
    24  	"github.com/stretchr/testify/assert"
    25  	riemannCommon "k8s.io/heapster/common/riemann"
    26  	"k8s.io/heapster/metrics/core"
    27  )
    28  
    29  type fakeRiemannClient struct {
    30  	events []proto.Event
    31  }
    32  
    33  type fakeRiemannSink struct {
    34  	core.DataSink
    35  	fakeRiemannClient *fakeRiemannClient
    36  }
    37  
    38  func NewFakeRiemannClient() *fakeRiemannClient {
    39  	return &fakeRiemannClient{[]proto.Event{}}
    40  }
    41  
    42  func (client *fakeRiemannClient) Connect(timeout int32) error {
    43  	return nil
    44  }
    45  
    46  func (client *fakeRiemannClient) Close() error {
    47  	return nil
    48  }
    49  
    50  func (client *fakeRiemannClient) Send(e *proto.Msg) (*proto.Msg, error) {
    51  	msg := &proto.Msg{Ok: pb.Bool(true)}
    52  	for _, event := range e.Events {
    53  		client.events = append(client.events, *event)
    54  	}
    55  	// always returns a Ok msg
    56  	return msg, nil
    57  }
    58  
    59  // Returns a fake Riemann sink.
    60  func NewFakeSink() fakeRiemannSink {
    61  	riemannClient := NewFakeRiemannClient()
    62  	c := riemannCommon.RiemannConfig{
    63  		Host:      "riemann-heapster:5555",
    64  		Ttl:       60.0,
    65  		State:     "",
    66  		Tags:      []string{"heapster"},
    67  		BatchSize: 1000,
    68  	}
    69  
    70  	return fakeRiemannSink{
    71  		&RiemannSink{
    72  			client: riemannClient,
    73  			config: c,
    74  		},
    75  		riemannClient,
    76  	}
    77  }
    78  
    79  func TestAppendEvent(t *testing.T) {
    80  	c := riemannCommon.RiemannConfig{
    81  		Host:      "riemann-heapster:5555",
    82  		Ttl:       60.0,
    83  		State:     "",
    84  		Tags:      make([]string, 0),
    85  		BatchSize: 1000,
    86  	}
    87  	sink := &RiemannSink{
    88  		client: nil,
    89  		config: c,
    90  	}
    91  	var events []riemanngo.Event
    92  	labels := map[string]string{
    93  		"foo": "bar",
    94  	}
    95  	events = appendEvent(events, sink, "riemann", "service1", 10, labels, 1)
    96  	events = appendEvent(events, sink, "riemann", "service1", 10.1, labels, 1)
    97  	assert.Equal(t, 2, len(events))
    98  	assert.Equal(t, events[0], riemanngo.Event{
    99  		Host:    "riemann",
   100  		Service: "service1",
   101  		Metric:  10,
   102  		Time:    1,
   103  		Ttl:     60,
   104  		Tags:    []string{},
   105  		Attributes: map[string]string{
   106  			"foo": "bar",
   107  		},
   108  	})
   109  	assert.Equal(t, events[1], riemanngo.Event{
   110  		Host:    "riemann",
   111  		Service: "service1",
   112  		Metric:  10.1,
   113  		Time:    1,
   114  		Ttl:     60,
   115  		Tags:    []string{},
   116  		Attributes: map[string]string{
   117  			"foo": "bar",
   118  		},
   119  	})
   120  }
   121  
   122  func TestAppendEventFull(t *testing.T) {
   123  	riemannClient := NewFakeRiemannClient()
   124  	c := riemannCommon.RiemannConfig{
   125  		Host:      "riemann-heapster:5555",
   126  		Ttl:       60.0,
   127  		State:     "",
   128  		Tags:      make([]string, 0),
   129  		BatchSize: 1000,
   130  	}
   131  	fakeSink := RiemannSink{
   132  		client: riemannClient,
   133  		config: c,
   134  	}
   135  
   136  	var events []riemanngo.Event
   137  	for i := 0; i < 999; i++ {
   138  		events = appendEvent(events, &fakeSink, "riemann", "service1", 10, map[string]string{}, 1)
   139  	}
   140  	assert.Equal(t, 999, len(events))
   141  	// batch size = 1000
   142  	events = appendEvent(events, &fakeSink, "riemann", "service1", 10, map[string]string{}, 1)
   143  	assert.Equal(t, 0, len(events))
   144  }
   145  
   146  func TestStoreDataEmptyInput(t *testing.T) {
   147  	fakeSink := NewFakeSink()
   148  	dataBatch := core.DataBatch{}
   149  	fakeSink.ExportData(&dataBatch)
   150  	assert.Equal(t, 0, len(fakeSink.fakeRiemannClient.events))
   151  }
   152  
   153  func TestStoreMultipleDataInput(t *testing.T) {
   154  	fakeSink := NewFakeSink()
   155  	timestamp := time.Now()
   156  
   157  	l := make(map[string]string)
   158  	l["namespace_id"] = "123"
   159  	l[core.LabelHostname.Key] = "riemann"
   160  	l["container_name"] = "/system.slice/-.mount"
   161  	l[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd"
   162  
   163  	l2 := make(map[string]string)
   164  	l2["namespace_id"] = "123"
   165  	l2[core.LabelHostname.Key] = "riemann"
   166  	l2["container_name"] = "/system.slice/dbus.service"
   167  	l2[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd"
   168  
   169  	l3 := make(map[string]string)
   170  	l3["namespace_id"] = "123"
   171  	l3[core.LabelHostname.Key] = "riemann"
   172  	l3[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd"
   173  
   174  	l4 := make(map[string]string)
   175  	l4["namespace_id"] = ""
   176  	l4[core.LabelHostname.Key] = "riemann"
   177  	l4[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd"
   178  
   179  	l5 := make(map[string]string)
   180  	l5["namespace_id"] = "123"
   181  	l5[core.LabelHostname.Key] = "riemann"
   182  	l5[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd"
   183  
   184  	metricValue := core.MetricValue{
   185  		IntValue:   int64(10),
   186  		FloatValue: 10.0,
   187  		MetricType: 1,
   188  		ValueType:  0,
   189  	}
   190  	metricSet1 := core.MetricSet{
   191  		Labels: l,
   192  		MetricValues: map[string]core.MetricValue{
   193  			"/system.slice/-.mount//cpu/limit": {
   194  				ValueType:  core.ValueInt64,
   195  				MetricType: core.MetricCumulative,
   196  				IntValue:   123456,
   197  			},
   198  		},
   199  		LabeledMetrics: []core.LabeledMetric{
   200  			{
   201  				Name: "labeledmetric",
   202  				Labels: map[string]string{
   203  					"foo": "bar",
   204  				},
   205  				MetricValue: metricValue,
   206  			},
   207  		},
   208  	}
   209  	metricSet2 := core.MetricSet{
   210  		Labels: l2,
   211  		MetricValues: map[string]core.MetricValue{
   212  			"/system.slice/dbus.service//cpu/usage": {
   213  				ValueType:  core.ValueInt64,
   214  				MetricType: core.MetricCumulative,
   215  				IntValue:   123456,
   216  			},
   217  		},
   218  	}
   219  
   220  	metricSet3 := core.MetricSet{
   221  		Labels: l3,
   222  		MetricValues: map[string]core.MetricValue{
   223  			"test/metric/1": {
   224  				ValueType:  core.ValueInt64,
   225  				MetricType: core.MetricCumulative,
   226  				IntValue:   123456,
   227  			},
   228  		},
   229  	}
   230  
   231  	metricSet4 := core.MetricSet{
   232  		Labels: l4,
   233  		MetricValues: map[string]core.MetricValue{
   234  			"test/metric/2": {
   235  				ValueType:  core.ValueInt64,
   236  				MetricType: core.MetricCumulative,
   237  				IntValue:   12345,
   238  			},
   239  		},
   240  	}
   241  
   242  	metricSet5 := core.MetricSet{
   243  		Labels: l5,
   244  		MetricValues: map[string]core.MetricValue{
   245  			"removeme": {
   246  				ValueType:  core.ValueInt64,
   247  				MetricType: core.MetricCumulative,
   248  				IntValue:   123456,
   249  			},
   250  		},
   251  	}
   252  
   253  	data := core.DataBatch{
   254  		Timestamp: timestamp,
   255  		MetricSets: map[string]*core.MetricSet{
   256  			"pod1": &metricSet1,
   257  			"pod2": &metricSet2,
   258  			"pod3": &metricSet3,
   259  			"pod4": &metricSet4,
   260  			"pod5": &metricSet5,
   261  		},
   262  	}
   263  
   264  	timeValue := timestamp.Unix()
   265  	fakeSink.ExportData(&data)
   266  
   267  	assert.Equal(t, 6, len(fakeSink.fakeRiemannClient.events))
   268  	var expectedEvents = []*proto.Event{
   269  		{
   270  			Host:         pb.String("riemann"),
   271  			Time:         pb.Int64(timeValue),
   272  			Ttl:          pb.Float32(60),
   273  			MetricSint64: pb.Int64(10),
   274  			Service:      pb.String("labeledmetric"),
   275  			Tags:         []string{"heapster"},
   276  			Attributes: []*proto.Attribute{
   277  				{
   278  					Key:   pb.String("container_name"),
   279  					Value: pb.String("/system.slice/-.mount"),
   280  				},
   281  				{
   282  					Key:   pb.String("foo"),
   283  					Value: pb.String("bar"),
   284  				},
   285  				{
   286  					Key:   pb.String("hostname"),
   287  					Value: pb.String("riemann"),
   288  				},
   289  				{
   290  					Key:   pb.String("namespace_id"),
   291  					Value: pb.String("123"),
   292  				},
   293  				{
   294  					Key:   pb.String("pod_id"),
   295  					Value: pb.String("aaaa-bbbb-cccc-dddd"),
   296  				},
   297  			},
   298  		},
   299  		{
   300  			Host:         pb.String("riemann"),
   301  			Time:         pb.Int64(timeValue),
   302  			Ttl:          pb.Float32(60),
   303  			MetricSint64: pb.Int64(123456),
   304  			Service:      pb.String("/system.slice/-.mount//cpu/limit"),
   305  			Tags:         []string{"heapster"},
   306  			Attributes: []*proto.Attribute{
   307  				{
   308  					Key:   pb.String("container_name"),
   309  					Value: pb.String("/system.slice/-.mount"),
   310  				},
   311  				{
   312  					Key:   pb.String("hostname"),
   313  					Value: pb.String("riemann"),
   314  				},
   315  				{
   316  					Key:   pb.String("namespace_id"),
   317  					Value: pb.String("123"),
   318  				},
   319  				{
   320  					Key:   pb.String("pod_id"),
   321  					Value: pb.String("aaaa-bbbb-cccc-dddd"),
   322  				},
   323  			},
   324  		},
   325  		{
   326  			Host:         pb.String("riemann"),
   327  			Time:         pb.Int64(timeValue),
   328  			Ttl:          pb.Float32(60),
   329  			MetricSint64: pb.Int64(123456),
   330  			Service:      pb.String("/system.slice/dbus.service//cpu/usage"),
   331  			Tags:         []string{"heapster"},
   332  			Attributes: []*proto.Attribute{
   333  				{
   334  					Key:   pb.String("container_name"),
   335  					Value: pb.String("/system.slice/dbus.service"),
   336  				},
   337  				{
   338  					Key:   pb.String("hostname"),
   339  					Value: pb.String("riemann"),
   340  				},
   341  				{
   342  					Key:   pb.String("namespace_id"),
   343  					Value: pb.String("123"),
   344  				},
   345  				{
   346  					Key:   pb.String("pod_id"),
   347  					Value: pb.String("aaaa-bbbb-cccc-dddd"),
   348  				},
   349  			},
   350  		},
   351  		{
   352  			Host:         pb.String("riemann"),
   353  			Time:         pb.Int64(timeValue),
   354  			Ttl:          pb.Float32(60),
   355  			MetricSint64: pb.Int64(123456),
   356  			Service:      pb.String("test/metric/1"),
   357  			Tags:         []string{"heapster"},
   358  			Attributes: []*proto.Attribute{
   359  				{
   360  					Key:   pb.String("hostname"),
   361  					Value: pb.String("riemann"),
   362  				},
   363  				{
   364  					Key:   pb.String("namespace_id"),
   365  					Value: pb.String("123"),
   366  				},
   367  				{
   368  					Key:   pb.String("pod_id"),
   369  					Value: pb.String("aaaa-bbbb-cccc-dddd"),
   370  				},
   371  			},
   372  		},
   373  		{
   374  			Host:         pb.String("riemann"),
   375  			Time:         pb.Int64(timeValue),
   376  			Ttl:          pb.Float32(60),
   377  			MetricSint64: pb.Int64(12345),
   378  			Service:      pb.String("test/metric/2"),
   379  			Tags:         []string{"heapster"},
   380  			Attributes: []*proto.Attribute{
   381  				{
   382  					Key:   pb.String("hostname"),
   383  					Value: pb.String("riemann"),
   384  				},
   385  				{
   386  					Key:   pb.String("namespace_id"),
   387  					Value: pb.String(""),
   388  				},
   389  				{
   390  					Key:   pb.String("pod_id"),
   391  					Value: pb.String("aaaa-bbbb-cccc-dddd"),
   392  				},
   393  			},
   394  		},
   395  		{
   396  			Host:         pb.String("riemann"),
   397  			Time:         pb.Int64(timeValue),
   398  			Ttl:          pb.Float32(60),
   399  			MetricSint64: pb.Int64(123456),
   400  			Service:      pb.String("removeme"),
   401  			Tags:         []string{"heapster"},
   402  			Attributes: []*proto.Attribute{
   403  				{
   404  					Key:   pb.String("hostname"),
   405  					Value: pb.String("riemann"),
   406  				},
   407  				{
   408  					Key:   pb.String("namespace_id"),
   409  					Value: pb.String("123"),
   410  				},
   411  				{
   412  					Key:   pb.String("pod_id"),
   413  					Value: pb.String("aaaa-bbbb-cccc-dddd"),
   414  				},
   415  			},
   416  		},
   417  	}
   418  
   419  	for _, expectedEvent := range expectedEvents {
   420  		found := false
   421  		for _, sinkEvent := range fakeSink.fakeRiemannClient.events {
   422  			if pb.Equal(expectedEvent, &sinkEvent) {
   423  				found = true
   424  			}
   425  		}
   426  		if !found {
   427  			t.Error("Error, event not found in sink")
   428  		}
   429  	}
   430  }