github.com/timstclair/heapster@v0.20.0-alpha1/metrics/processors/pod_aggregator_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 processors 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 TestPodAggregator(t *testing.T) { 27 batch := core.DataBatch{ 28 Timestamp: time.Now(), 29 MetricSets: map[string]*core.MetricSet{ 30 core.PodContainerKey("ns1", "pod1", "c1"): { 31 Labels: map[string]string{ 32 core.LabelMetricSetType.Key: core.MetricSetTypePodContainer, 33 core.LabelPodName.Key: "pod1", 34 core.LabelNamespaceName.Key: "ns1", 35 }, 36 MetricValues: map[string]core.MetricValue{ 37 "m1": { 38 ValueType: core.ValueInt64, 39 MetricType: core.MetricGauge, 40 IntValue: 10, 41 }, 42 "m2": { 43 ValueType: core.ValueInt64, 44 MetricType: core.MetricGauge, 45 IntValue: 222, 46 }, 47 }, 48 }, 49 50 core.PodContainerKey("ns1", "pod1", "c2"): { 51 Labels: map[string]string{ 52 core.LabelMetricSetType.Key: core.MetricSetTypePodContainer, 53 core.LabelPodName.Key: "pod1", 54 core.LabelNamespaceName.Key: "ns1", 55 }, 56 MetricValues: map[string]core.MetricValue{ 57 "m1": { 58 ValueType: core.ValueInt64, 59 MetricType: core.MetricGauge, 60 IntValue: 100, 61 }, 62 "m3": { 63 ValueType: core.ValueInt64, 64 MetricType: core.MetricGauge, 65 IntValue: 30, 66 }, 67 }, 68 }, 69 }, 70 } 71 processor := PodAggregator{} 72 result, err := processor.Process(&batch) 73 assert.NoError(t, err) 74 pod, found := result.MetricSets[core.PodKey("ns1", "pod1")] 75 assert.True(t, found) 76 77 m1, found := pod.MetricValues["m1"] 78 assert.True(t, found) 79 assert.Equal(t, 110, m1.IntValue) 80 81 m2, found := pod.MetricValues["m2"] 82 assert.True(t, found) 83 assert.Equal(t, 222, m2.IntValue) 84 85 m3, found := pod.MetricValues["m3"] 86 assert.True(t, found) 87 assert.Equal(t, 30, m3.IntValue) 88 89 labelPodName, found := pod.Labels[core.LabelPodName.Key] 90 assert.True(t, found) 91 assert.Equal(t, "pod1", labelPodName) 92 93 labelNsName, found := pod.Labels[core.LabelNamespaceName.Key] 94 assert.True(t, found) 95 assert.Equal(t, "ns1", labelNsName) 96 }