github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/metrics/processors/cluster_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 TestClusterAggregate(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.MetricSetTypeNamespace, 33 core.LabelNamespaceName.Key: "ns1", 34 }, 35 MetricValues: map[string]core.MetricValue{ 36 "m1": { 37 ValueType: core.ValueInt64, 38 MetricType: core.MetricGauge, 39 IntValue: 10, 40 }, 41 "m2": { 42 ValueType: core.ValueInt64, 43 MetricType: core.MetricGauge, 44 IntValue: 222, 45 }, 46 }, 47 }, 48 49 core.PodKey("ns1", "pod2"): { 50 Labels: map[string]string{ 51 core.LabelMetricSetType.Key: core.MetricSetTypeNamespace, 52 core.LabelNamespaceName.Key: "ns1", 53 }, 54 MetricValues: map[string]core.MetricValue{ 55 "m1": { 56 ValueType: core.ValueInt64, 57 MetricType: core.MetricGauge, 58 IntValue: 100, 59 }, 60 "m3": { 61 ValueType: core.ValueInt64, 62 MetricType: core.MetricGauge, 63 IntValue: 30, 64 }, 65 }, 66 }, 67 }, 68 } 69 processor := ClusterAggregator{ 70 MetricsToAggregate: []string{"m1", "m3"}, 71 } 72 result, err := processor.Process(&batch) 73 assert.NoError(t, err) 74 cluster, found := result.MetricSets[core.ClusterKey()] 75 assert.True(t, found) 76 77 m1, found := cluster.MetricValues["m1"] 78 assert.True(t, found) 79 assert.Equal(t, int64(110), m1.IntValue) 80 81 m3, found := cluster.MetricValues["m3"] 82 assert.True(t, found) 83 assert.Equal(t, int64(30), m3.IntValue) 84 }