github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/metrics/processors/node_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 TestNodeAggregate(t *testing.T) { 27 batch := core.DataBatch{ 28 Timestamp: time.Now(), 29 MetricSets: map[string]*core.MetricSet{ 30 core.PodKey("ns1", "pod1"): { 31 Labels: map[string]string{ 32 core.LabelMetricSetType.Key: core.MetricSetTypePod, 33 core.LabelNamespaceName.Key: "ns1", 34 core.LabelNodename.Key: "h1", 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.PodKey("ns1", "pod2"): { 51 Labels: map[string]string{ 52 core.LabelMetricSetType.Key: core.MetricSetTypePod, 53 core.LabelNamespaceName.Key: "ns1", 54 core.LabelNodename.Key: "h1", 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.NodeKey("h1"): { 71 Labels: map[string]string{ 72 core.LabelMetricSetType.Key: core.MetricSetTypeNode, 73 core.LabelNodename.Key: "h1", 74 }, 75 MetricValues: map[string]core.MetricValue{}, 76 }, 77 }, 78 } 79 processor := NodeAggregator{ 80 MetricsToAggregate: []string{"m1", "m3"}, 81 } 82 result, err := processor.Process(&batch) 83 assert.NoError(t, err) 84 node, found := result.MetricSets[core.NodeKey("h1")] 85 assert.True(t, found) 86 87 m1, found := node.MetricValues["m1"] 88 assert.True(t, found) 89 assert.Equal(t, int64(110), m1.IntValue) 90 91 m3, found := node.MetricValues["m3"] 92 assert.True(t, found) 93 assert.Equal(t, int64(30), m3.IntValue) 94 }