github.com/jonaz/heapster@v1.3.0-beta.0.0.20170208112634-cd3c15ca3d29/metrics/sinks/metric/metric_sink_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 metric
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  
    23  	"k8s.io/heapster/metrics/core"
    24  )
    25  
    26  func makeBatches(now time.Time, key, otherKey string) (core.DataBatch, core.DataBatch, core.DataBatch) {
    27  	batch1 := core.DataBatch{
    28  		Timestamp: now.Add(-180 * time.Second),
    29  		MetricSets: map[string]*core.MetricSet{
    30  			key: {
    31  				Labels: map[string]string{
    32  					core.LabelMetricSetType.Key: core.MetricSetTypePod,
    33  					core.LabelPodNamespace.Key:  "ns1",
    34  				},
    35  				MetricValues: map[string]core.MetricValue{
    36  					"m1": {
    37  						ValueType:  core.ValueInt64,
    38  						MetricType: core.MetricGauge,
    39  						IntValue:   60,
    40  					},
    41  					"m2": {
    42  						ValueType:  core.ValueInt64,
    43  						MetricType: core.MetricGauge,
    44  						IntValue:   666,
    45  					},
    46  				},
    47  			},
    48  		},
    49  	}
    50  
    51  	batch2 := core.DataBatch{
    52  		Timestamp: now.Add(-60 * time.Second),
    53  		MetricSets: map[string]*core.MetricSet{
    54  			key: {
    55  				Labels: map[string]string{
    56  					core.LabelMetricSetType.Key: core.MetricSetTypePod,
    57  					core.LabelPodNamespace.Key:  "ns1",
    58  				},
    59  				MetricValues: map[string]core.MetricValue{
    60  					"m1": {
    61  						ValueType:  core.ValueInt64,
    62  						MetricType: core.MetricGauge,
    63  						IntValue:   40,
    64  					},
    65  					"m2": {
    66  						ValueType:  core.ValueInt64,
    67  						MetricType: core.MetricGauge,
    68  						IntValue:   444,
    69  					},
    70  				},
    71  				LabeledMetrics: []core.LabeledMetric{
    72  					{
    73  						Name:   "somelblmetric",
    74  						Labels: map[string]string{"lbl1": "val1.1", "lbl2": "val2.1"},
    75  						MetricValue: core.MetricValue{
    76  							ValueType:  core.ValueInt64,
    77  							MetricType: core.MetricGauge,
    78  							IntValue:   8675,
    79  						},
    80  					},
    81  					{
    82  						Name:   "otherlblmetric",
    83  						Labels: map[string]string{"lbl1": "val1.1", "lbl2": "val2.1"},
    84  						MetricValue: core.MetricValue{
    85  							ValueType:  core.ValueInt64,
    86  							MetricType: core.MetricGauge,
    87  							IntValue:   1234,
    88  						},
    89  					},
    90  				},
    91  			},
    92  		},
    93  	}
    94  
    95  	batch3 := core.DataBatch{
    96  		Timestamp: now.Add(-20 * time.Second),
    97  		MetricSets: map[string]*core.MetricSet{
    98  			key: {
    99  				Labels: map[string]string{
   100  					core.LabelMetricSetType.Key: core.MetricSetTypePod,
   101  					core.LabelPodNamespace.Key:  "ns1",
   102  				},
   103  				MetricValues: map[string]core.MetricValue{
   104  					"m1": {
   105  						ValueType:  core.ValueInt64,
   106  						MetricType: core.MetricGauge,
   107  						IntValue:   20,
   108  					},
   109  					"m2": {
   110  						ValueType:  core.ValueInt64,
   111  						MetricType: core.MetricGauge,
   112  						IntValue:   222,
   113  					},
   114  				},
   115  				LabeledMetrics: []core.LabeledMetric{
   116  					{
   117  						Name:   "somelblmetric",
   118  						Labels: map[string]string{"lbl1": "val1.1", "lbl2": "val2.1"},
   119  						MetricValue: core.MetricValue{
   120  							ValueType:  core.ValueInt64,
   121  							MetricType: core.MetricGauge,
   122  							IntValue:   309,
   123  						},
   124  					},
   125  					{
   126  						Name:   "somelblmetric",
   127  						Labels: map[string]string{"lbl1": "val1.2", "lbl2": "val2.1"},
   128  						MetricValue: core.MetricValue{
   129  							ValueType:  core.ValueInt64,
   130  							MetricType: core.MetricGauge,
   131  							IntValue:   5678,
   132  						},
   133  					},
   134  				},
   135  			},
   136  			otherKey: {
   137  				Labels: map[string]string{
   138  					core.LabelMetricSetType.Key: core.MetricSetTypePod,
   139  					core.LabelPodNamespace.Key:  "ns1",
   140  				},
   141  				MetricValues: map[string]core.MetricValue{
   142  					"m1": {
   143  						ValueType:  core.ValueInt64,
   144  						MetricType: core.MetricGauge,
   145  						IntValue:   123,
   146  					},
   147  				},
   148  			},
   149  		},
   150  	}
   151  
   152  	return batch1, batch2, batch3
   153  }
   154  
   155  func TestGetMetrics(t *testing.T) {
   156  	now := time.Now()
   157  	key := core.PodKey("ns1", "pod1")
   158  	otherKey := core.PodKey("ns1", "other")
   159  
   160  	batch1, batch2, batch3 := makeBatches(now, key, otherKey)
   161  
   162  	metrics := NewMetricSink(45*time.Second, 120*time.Second, []string{"m1"})
   163  	metrics.ExportData(&batch1)
   164  	metrics.ExportData(&batch2)
   165  	metrics.ExportData(&batch3)
   166  
   167  	//batch1 is discarded by long store
   168  	result1 := metrics.GetMetric("m1", []string{key}, now.Add(-120*time.Second), now)
   169  	assert.Equal(t, 2, len(result1[key]))
   170  	assert.Equal(t, int64(40), result1[key][0].MetricValue.IntValue)
   171  	assert.Equal(t, int64(20), result1[key][1].MetricValue.IntValue)
   172  	assert.Equal(t, 1, len(metrics.GetMetric("m1", []string{otherKey}, now.Add(-120*time.Second), now)[otherKey]))
   173  
   174  	//batch1 is discarded by long store and batch2 doesn't belong to time window
   175  	assert.Equal(t, 1, len(metrics.GetMetric("m1", []string{key}, now.Add(-30*time.Second), now)[key]))
   176  
   177  	//batch1 and batch1 are discarded by short store
   178  	assert.Equal(t, 1, len(metrics.GetMetric("m2", []string{key}, now.Add(-120*time.Second), now)[key]))
   179  
   180  	//nothing is in time window
   181  	assert.Equal(t, 0, len(metrics.GetMetric("m2", []string{key}, now.Add(-10*time.Second), now)[key]))
   182  
   183  	metricNames := metrics.GetMetricNames(key)
   184  	assert.Equal(t, 2, len(metricNames))
   185  	assert.Contains(t, metricNames, "m1")
   186  	assert.Contains(t, metricNames, "m2")
   187  }
   188  
   189  func TestGetLabeledMetrics(t *testing.T) {
   190  	now := time.Now().UTC()
   191  	key := core.PodKey("ns1", "pod1")
   192  	otherKey := core.PodKey("ns1", "other")
   193  
   194  	batch1, batch2, batch3 := makeBatches(now, key, otherKey)
   195  
   196  	metrics := NewMetricSink(45*time.Second, 120*time.Second, []string{"m1"})
   197  	metrics.ExportData(&batch1)
   198  	metrics.ExportData(&batch2)
   199  	metrics.ExportData(&batch3)
   200  
   201  	result := metrics.GetLabeledMetric("somelblmetric", map[string]string{"lbl1": "val1.1", "lbl2": "val2.1"}, []string{key}, now.Add(-120*time.Second), now)
   202  
   203  	assert.Equal(t, []core.TimestampedMetricValue{
   204  		{
   205  			Timestamp: now.Add(-20 * time.Second),
   206  			MetricValue: core.MetricValue{
   207  				ValueType:  core.ValueInt64,
   208  				MetricType: core.MetricGauge,
   209  				IntValue:   309,
   210  			},
   211  		},
   212  	}, result[key])
   213  }
   214  
   215  func TestGetNames(t *testing.T) {
   216  	now := time.Now()
   217  	key := core.PodKey("ns1", "pod1")
   218  	otherKey := core.PodKey("ns1", "other")
   219  
   220  	batch := core.DataBatch{
   221  		Timestamp: now.Add(-20 * time.Second),
   222  		MetricSets: map[string]*core.MetricSet{
   223  			key: {
   224  				Labels: map[string]string{
   225  					core.LabelMetricSetType.Key: core.MetricSetTypePod,
   226  					core.LabelPodNamespace.Key:  "ns1",
   227  					core.LabelNamespaceName.Key: "ns1",
   228  					core.LabelPodName.Key:       "pod1",
   229  				},
   230  				MetricValues: map[string]core.MetricValue{
   231  					"m1": {
   232  						ValueType:  core.ValueInt64,
   233  						MetricType: core.MetricGauge,
   234  						IntValue:   20,
   235  					},
   236  					"m2": {
   237  						ValueType:  core.ValueInt64,
   238  						MetricType: core.MetricGauge,
   239  						IntValue:   222,
   240  					},
   241  				},
   242  			},
   243  			otherKey: {
   244  				Labels: map[string]string{
   245  					core.LabelMetricSetType.Key: core.MetricSetTypePod,
   246  					core.LabelPodNamespace.Key:  "ns2",
   247  					core.LabelNamespaceName.Key: "ns2",
   248  					core.LabelPodName.Key:       "pod2",
   249  				},
   250  				MetricValues: map[string]core.MetricValue{
   251  					"m1": {
   252  						ValueType:  core.ValueInt64,
   253  						MetricType: core.MetricGauge,
   254  						IntValue:   123,
   255  					},
   256  				},
   257  			},
   258  		},
   259  	}
   260  
   261  	metrics := NewMetricSink(45*time.Second, 120*time.Second, []string{"m1"})
   262  	metrics.ExportData(&batch)
   263  
   264  	assert.Contains(t, metrics.GetPods(), "ns1/pod1")
   265  	assert.Contains(t, metrics.GetPods(), "ns2/pod2")
   266  	assert.Contains(t, metrics.GetPodsFromNamespace("ns1"), "pod1")
   267  	assert.NotContains(t, metrics.GetPodsFromNamespace("ns1"), "pod2")
   268  	assert.Contains(t, metrics.GetMetricSetKeys(), key)
   269  	assert.Contains(t, metrics.GetMetricSetKeys(), otherKey)
   270  }