github.com/timstclair/heapster@v0.20.0-alpha1/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 metricsink
    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 TestGetMetrics(t *testing.T) {
    27  	now := time.Now()
    28  	key := core.PodKey("ns1", "pod1")
    29  	otherKey := core.PodKey("ns1", "other")
    30  
    31  	batch1 := core.DataBatch{
    32  		Timestamp: now.Add(-180 * time.Second),
    33  		MetricSets: map[string]*core.MetricSet{
    34  			key: {
    35  				Labels: map[string]string{
    36  					core.LabelMetricSetType.Key: core.MetricSetTypePod,
    37  					core.LabelPodNamespace.Key:  "ns1",
    38  				},
    39  				MetricValues: map[string]core.MetricValue{
    40  					"m1": {
    41  						ValueType:  core.ValueInt64,
    42  						MetricType: core.MetricGauge,
    43  						IntValue:   60,
    44  					},
    45  					"m2": {
    46  						ValueType:  core.ValueInt64,
    47  						MetricType: core.MetricGauge,
    48  						IntValue:   666,
    49  					},
    50  				},
    51  			},
    52  		},
    53  	}
    54  
    55  	batch2 := core.DataBatch{
    56  		Timestamp: now.Add(-60 * time.Second),
    57  		MetricSets: map[string]*core.MetricSet{
    58  			key: {
    59  				Labels: map[string]string{
    60  					core.LabelMetricSetType.Key: core.MetricSetTypePod,
    61  					core.LabelPodNamespace.Key:  "ns1",
    62  				},
    63  				MetricValues: map[string]core.MetricValue{
    64  					"m1": {
    65  						ValueType:  core.ValueInt64,
    66  						MetricType: core.MetricGauge,
    67  						IntValue:   40,
    68  					},
    69  					"m2": {
    70  						ValueType:  core.ValueInt64,
    71  						MetricType: core.MetricGauge,
    72  						IntValue:   444,
    73  					},
    74  				},
    75  			},
    76  		},
    77  	}
    78  
    79  	batch3 := core.DataBatch{
    80  		Timestamp: now.Add(-20 * time.Second),
    81  		MetricSets: map[string]*core.MetricSet{
    82  			key: {
    83  				Labels: map[string]string{
    84  					core.LabelMetricSetType.Key: core.MetricSetTypePod,
    85  					core.LabelPodNamespace.Key:  "ns1",
    86  				},
    87  				MetricValues: map[string]core.MetricValue{
    88  					"m1": {
    89  						ValueType:  core.ValueInt64,
    90  						MetricType: core.MetricGauge,
    91  						IntValue:   20,
    92  					},
    93  					"m2": {
    94  						ValueType:  core.ValueInt64,
    95  						MetricType: core.MetricGauge,
    96  						IntValue:   222,
    97  					},
    98  				},
    99  			},
   100  			otherKey: {
   101  				Labels: map[string]string{
   102  					core.LabelMetricSetType.Key: core.MetricSetTypePod,
   103  					core.LabelPodNamespace.Key:  "ns1",
   104  				},
   105  				MetricValues: map[string]core.MetricValue{
   106  					"m1": {
   107  						ValueType:  core.ValueInt64,
   108  						MetricType: core.MetricGauge,
   109  						IntValue:   123,
   110  					},
   111  				},
   112  			},
   113  		},
   114  	}
   115  
   116  	metrics := NewMetricSink(45*time.Second, 120*time.Second, []string{"m1"})
   117  	metrics.ExportData(&batch1)
   118  	metrics.ExportData(&batch2)
   119  	metrics.ExportData(&batch3)
   120  
   121  	//batch1 is discarded by long store
   122  	result1 := metrics.GetMetric("m1", []string{key}, now.Add(-120*time.Second), now)
   123  	assert.Equal(t, 2, len(result1[key]))
   124  	assert.Equal(t, 40, result1[key][0].MetricValue.IntValue)
   125  	assert.Equal(t, 20, result1[key][1].MetricValue.IntValue)
   126  	assert.Equal(t, 1, len(metrics.GetMetric("m1", []string{otherKey}, now.Add(-120*time.Second), now)[otherKey]))
   127  
   128  	//batch1 is discarded by long store and batch2 doesn't belong to time window
   129  	assert.Equal(t, 1, len(metrics.GetMetric("m1", []string{key}, now.Add(-30*time.Second), now)[key]))
   130  
   131  	//batch1 and batch1 are discarded by short store
   132  	assert.Equal(t, 1, len(metrics.GetMetric("m2", []string{key}, now.Add(-120*time.Second), now)[key]))
   133  
   134  	//nothing is in time window
   135  	assert.Equal(t, 0, len(metrics.GetMetric("m2", []string{key}, now.Add(-10*time.Second), now)[key]))
   136  
   137  	metricNames := metrics.GetMetricNames(key)
   138  	assert.Equal(t, 2, len(metricNames))
   139  	assert.Contains(t, metricNames, "m1")
   140  	assert.Contains(t, metricNames, "m2")
   141  }
   142  
   143  func TestGetNames(t *testing.T) {
   144  	now := time.Now()
   145  	key := core.PodKey("ns1", "pod1")
   146  	otherKey := core.PodKey("ns1", "other")
   147  
   148  	batch := core.DataBatch{
   149  		Timestamp: now.Add(-20 * time.Second),
   150  		MetricSets: map[string]*core.MetricSet{
   151  			key: {
   152  				Labels: map[string]string{
   153  					core.LabelMetricSetType.Key: core.MetricSetTypePod,
   154  					core.LabelPodNamespace.Key:  "ns1",
   155  					core.LabelNamespaceName.Key: "ns1",
   156  					core.LabelPodName.Key:       "pod1",
   157  				},
   158  				MetricValues: map[string]core.MetricValue{
   159  					"m1": {
   160  						ValueType:  core.ValueInt64,
   161  						MetricType: core.MetricGauge,
   162  						IntValue:   20,
   163  					},
   164  					"m2": {
   165  						ValueType:  core.ValueInt64,
   166  						MetricType: core.MetricGauge,
   167  						IntValue:   222,
   168  					},
   169  				},
   170  			},
   171  			otherKey: {
   172  				Labels: map[string]string{
   173  					core.LabelMetricSetType.Key: core.MetricSetTypePod,
   174  					core.LabelPodNamespace.Key:  "ns2",
   175  					core.LabelNamespaceName.Key: "ns2",
   176  					core.LabelPodName.Key:       "pod2",
   177  				},
   178  				MetricValues: map[string]core.MetricValue{
   179  					"m1": {
   180  						ValueType:  core.ValueInt64,
   181  						MetricType: core.MetricGauge,
   182  						IntValue:   123,
   183  					},
   184  				},
   185  			},
   186  		},
   187  	}
   188  
   189  	metrics := NewMetricSink(45*time.Second, 120*time.Second, []string{"m1"})
   190  	metrics.ExportData(&batch)
   191  
   192  	assert.Contains(t, metrics.GetPods(), "ns1/pod1")
   193  	assert.Contains(t, metrics.GetPods(), "ns2/pod2")
   194  	assert.Contains(t, metrics.GetPodsFromNamespace("ns1"), "pod1")
   195  	assert.NotContains(t, metrics.GetPodsFromNamespace("ns1"), "pod2")
   196  	assert.Contains(t, metrics.GetMetricSetKeys(), key)
   197  	assert.Contains(t, metrics.GetMetricSetKeys(), otherKey)
   198  }