github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/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 core.PodKey("ns1", "pod2"): { 71 Labels: map[string]string{ 72 core.LabelMetricSetType.Key: core.MetricSetTypePod, 73 core.LabelPodName.Key: "pod2", 74 core.LabelNamespaceName.Key: "ns1", 75 }, 76 MetricValues: map[string]core.MetricValue{ 77 "m1": { 78 ValueType: core.ValueInt64, 79 MetricType: core.MetricGauge, 80 IntValue: 100, 81 }, 82 }, 83 }, 84 85 core.PodContainerKey("ns1", "pod2", "c1"): { 86 Labels: map[string]string{ 87 core.LabelMetricSetType.Key: core.MetricSetTypePodContainer, 88 core.LabelPodName.Key: "pod2", 89 core.LabelNamespaceName.Key: "ns1", 90 }, 91 MetricValues: map[string]core.MetricValue{ 92 "m1": { 93 ValueType: core.ValueInt64, 94 MetricType: core.MetricGauge, 95 IntValue: 10, 96 }, 97 "m2": { 98 ValueType: core.ValueInt64, 99 MetricType: core.MetricGauge, 100 IntValue: 20, 101 }, 102 }, 103 }, 104 }, 105 } 106 processor := PodAggregator{} 107 result, err := processor.Process(&batch) 108 assert.NoError(t, err) 109 pod, found := result.MetricSets[core.PodKey("ns1", "pod1")] 110 assert.True(t, found) 111 112 m1, found := pod.MetricValues["m1"] 113 assert.True(t, found) 114 assert.Equal(t, int64(110), m1.IntValue) 115 116 m2, found := pod.MetricValues["m2"] 117 assert.True(t, found) 118 assert.Equal(t, int64(222), m2.IntValue) 119 120 m3, found := pod.MetricValues["m3"] 121 assert.True(t, found) 122 assert.Equal(t, int64(30), m3.IntValue) 123 124 labelPodName, found := pod.Labels[core.LabelPodName.Key] 125 assert.True(t, found) 126 assert.Equal(t, "pod1", labelPodName) 127 128 labelNsName, found := pod.Labels[core.LabelNamespaceName.Key] 129 assert.True(t, found) 130 assert.Equal(t, "ns1", labelNsName) 131 132 pod, found = result.MetricSets[core.PodKey("ns1", "pod2")] 133 assert.True(t, found) 134 135 m1, found = pod.MetricValues["m1"] 136 assert.True(t, found) 137 assert.Equal(t, int64(100), m1.IntValue) 138 139 m2, found = pod.MetricValues["m2"] 140 assert.True(t, found) 141 assert.Equal(t, int64(20), m2.IntValue) 142 143 }