github.com/timstclair/heapster@v0.20.0-alpha1/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  	"encoding/json"
    19  	"fmt"
    20  	"testing"
    21  	"time"
    22  
    23  	riemann_api "github.com/bigdatadev/goryman"
    24  	"github.com/stretchr/testify/assert"
    25  	"k8s.io/heapster/metrics/core"
    26  )
    27  
    28  type eventSendToRiemann struct {
    29  	event string
    30  }
    31  
    32  type fakeRiemannClient struct {
    33  	events []eventSendToRiemann
    34  }
    35  
    36  type fakeRiemannSink struct {
    37  	core.DataSink
    38  	fakeRiemannClient *fakeRiemannClient
    39  }
    40  
    41  func NewFakeRiemannClient() *fakeRiemannClient {
    42  	return &fakeRiemannClient{[]eventSendToRiemann{}}
    43  }
    44  
    45  func (client *fakeRiemannClient) Connect() error {
    46  	return nil
    47  }
    48  
    49  func (client *fakeRiemannClient) Close() error {
    50  	return nil
    51  }
    52  
    53  func (client *fakeRiemannClient) SendEvent(e *riemann_api.Event) error {
    54  	eventsJson, _ := json.Marshal(e)
    55  	client.events = append(client.events, eventSendToRiemann{event: string(eventsJson)})
    56  	return nil
    57  }
    58  
    59  // Returns a fake kafka sink.
    60  func NewFakeSink() fakeRiemannSink {
    61  	riemannClient := NewFakeRiemannClient()
    62  	c := riemannConfig{
    63  		host:  "riemann-heapster:5555",
    64  		ttl:   60.0,
    65  		state: "",
    66  		tags:  make([]string, 0),
    67  	}
    68  
    69  	return fakeRiemannSink{
    70  		&riemannSink{
    71  			client: riemannClient,
    72  			config: c,
    73  		},
    74  		riemannClient,
    75  	}
    76  }
    77  
    78  func TestStoreDataEmptyInput(t *testing.T) {
    79  	fakeSink := NewFakeSink()
    80  	dataBatch := core.DataBatch{}
    81  	fakeSink.ExportData(&dataBatch)
    82  	assert.Equal(t, 0, len(fakeSink.fakeRiemannClient.events))
    83  }
    84  
    85  func TestStoreMultipleDataInput(t *testing.T) {
    86  	fakeSink := NewFakeSink()
    87  	timestamp := time.Now()
    88  
    89  	l := make(map[string]string)
    90  	l["namespace_id"] = "123"
    91  	l["container_name"] = "/system.slice/-.mount"
    92  	l[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd"
    93  
    94  	l2 := make(map[string]string)
    95  	l2["namespace_id"] = "123"
    96  	l2["container_name"] = "/system.slice/dbus.service"
    97  	l2[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd"
    98  
    99  	l3 := make(map[string]string)
   100  	l3["namespace_id"] = "123"
   101  	l3[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd"
   102  
   103  	l4 := make(map[string]string)
   104  	l4["namespace_id"] = ""
   105  	l4[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd"
   106  
   107  	l5 := make(map[string]string)
   108  	l5["namespace_id"] = "123"
   109  	l5[core.LabelPodId.Key] = "aaaa-bbbb-cccc-dddd"
   110  
   111  	metricSet1 := core.MetricSet{
   112  		Labels: l,
   113  		MetricValues: map[string]core.MetricValue{
   114  			"/system.slice/-.mount//cpu/limit": {
   115  				ValueType:  core.ValueInt64,
   116  				MetricType: core.MetricCumulative,
   117  				IntValue:   123456,
   118  			},
   119  		},
   120  	}
   121  
   122  	metricSet2 := core.MetricSet{
   123  		Labels: l2,
   124  		MetricValues: map[string]core.MetricValue{
   125  			"/system.slice/dbus.service//cpu/usage": {
   126  				ValueType:  core.ValueInt64,
   127  				MetricType: core.MetricCumulative,
   128  				IntValue:   123456,
   129  			},
   130  		},
   131  	}
   132  
   133  	metricSet3 := core.MetricSet{
   134  		Labels: l3,
   135  		MetricValues: map[string]core.MetricValue{
   136  			"test/metric/1": {
   137  				ValueType:  core.ValueInt64,
   138  				MetricType: core.MetricCumulative,
   139  				IntValue:   123456,
   140  			},
   141  		},
   142  	}
   143  
   144  	metricSet4 := core.MetricSet{
   145  		Labels: l4,
   146  		MetricValues: map[string]core.MetricValue{
   147  			"test/metric/1": {
   148  				ValueType:  core.ValueInt64,
   149  				MetricType: core.MetricCumulative,
   150  				IntValue:   123456,
   151  			},
   152  		},
   153  	}
   154  
   155  	metricSet5 := core.MetricSet{
   156  		Labels: l5,
   157  		MetricValues: map[string]core.MetricValue{
   158  			"removeme": {
   159  				ValueType:  core.ValueInt64,
   160  				MetricType: core.MetricCumulative,
   161  				IntValue:   123456,
   162  			},
   163  		},
   164  	}
   165  
   166  	data := core.DataBatch{
   167  		Timestamp: timestamp,
   168  		MetricSets: map[string]*core.MetricSet{
   169  			"pod1": &metricSet1,
   170  			"pod2": &metricSet2,
   171  			"pod3": &metricSet3,
   172  			"pod4": &metricSet4,
   173  			"pod5": &metricSet5,
   174  		},
   175  	}
   176  
   177  	timeValue := timestamp.Unix()
   178  
   179  	fakeSink.ExportData(&data)
   180  
   181  	//expect msg string
   182  	assert.Equal(t, 5, len(fakeSink.fakeRiemannClient.events))
   183  
   184  	var expectEventsTemplate = [5]string{
   185  		`{"Ttl":60,"Time":%d,"Tags":[],"Host":"","State":"","Service":"/system.slice/-.mount//cpu/limit","Metric":123456,"Description":"","Attributes":{"container_name":"/system.slice/-.mount","namespace_id":"123","pod_id":"aaaa-bbbb-cccc-dddd"}}`,
   186  		`{"Ttl":60,"Time":%d,"Tags":[],"Host":"","State":"","Service":"/system.slice/dbus.service//cpu/usage","Metric":123456,"Description":"","Attributes":{"container_name":"/system.slice/dbus.service","namespace_id":"123","pod_id":"aaaa-bbbb-cccc-dddd"}}`,
   187  		`{"Ttl":60,"Time":%d,"Tags":[],"Host":"","State":"","Service":"test/metric/1","Metric":123456,"Description":"","Attributes":{"namespace_id":"123","pod_id":"aaaa-bbbb-cccc-dddd"}}`,
   188  		`{"Ttl":60,"Time":%d,"Tags":[],"Host":"","State":"","Service":"test/metric/1","Metric":123456,"Description":"","Attributes":{"namespace_id":"","pod_id":"aaaa-bbbb-cccc-dddd"}}`,
   189  		`{"Ttl":60,"Time":%d,"Tags":[],"Host":"","State":"","Service":"removeme","Metric":123456,"Description":"","Attributes":{"namespace_id":"123","pod_id":"aaaa-bbbb-cccc-dddd"}}`,
   190  	}
   191  
   192  	eventsString := fmt.Sprintf("%s", fakeSink.fakeRiemannClient.events)
   193  	for _, evtTemplate := range expectEventsTemplate {
   194  		expectEvt := fmt.Sprintf(evtTemplate, timeValue)
   195  		assert.Contains(t, eventsString, expectEvt)
   196  	}
   197  }