github.com/jonaz/heapster@v1.3.0-beta.0.0.20170208112634-cd3c15ca3d29/metrics/processors/pod_based_enricher_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  	kube_api "k8s.io/kubernetes/pkg/api"
    26  	"k8s.io/kubernetes/pkg/api/resource"
    27  	"k8s.io/kubernetes/pkg/client/cache"
    28  )
    29  
    30  var batches = []*core.DataBatch{
    31  	{
    32  		Timestamp: time.Now(),
    33  		MetricSets: map[string]*core.MetricSet{
    34  			core.PodContainerKey("ns1", "pod1", "c1"): {
    35  				Labels: map[string]string{
    36  					core.LabelMetricSetType.Key: core.MetricSetTypePodContainer,
    37  					core.LabelPodName.Key:       "pod1",
    38  					core.LabelNamespaceName.Key: "ns1",
    39  					core.LabelContainerName.Key: "c1",
    40  				},
    41  				MetricValues: map[string]core.MetricValue{},
    42  			},
    43  
    44  			core.PodKey("ns1", "pod1"): {
    45  				Labels: map[string]string{
    46  					core.LabelMetricSetType.Key: core.MetricSetTypePod,
    47  					core.LabelPodName.Key:       "pod1",
    48  					core.LabelNamespaceName.Key: "ns1",
    49  				},
    50  				MetricValues: map[string]core.MetricValue{},
    51  			},
    52  		},
    53  	},
    54  	{
    55  		Timestamp: time.Now(),
    56  		MetricSets: map[string]*core.MetricSet{
    57  			core.PodContainerKey("ns1", "pod1", "c1"): {
    58  				Labels: map[string]string{
    59  					core.LabelMetricSetType.Key: core.MetricSetTypePodContainer,
    60  					core.LabelPodName.Key:       "pod1",
    61  					core.LabelNamespaceName.Key: "ns1",
    62  					core.LabelContainerName.Key: "c1",
    63  				},
    64  				MetricValues: map[string]core.MetricValue{},
    65  			},
    66  		},
    67  	},
    68  	{
    69  		Timestamp: time.Now(),
    70  		MetricSets: map[string]*core.MetricSet{
    71  			core.PodKey("ns1", "pod1"): {
    72  				Labels: map[string]string{
    73  					core.LabelMetricSetType.Key: core.MetricSetTypePod,
    74  					core.LabelPodName.Key:       "pod1",
    75  					core.LabelNamespaceName.Key: "ns1",
    76  				},
    77  				MetricValues: map[string]core.MetricValue{},
    78  			},
    79  		},
    80  	},
    81  }
    82  
    83  func TestPodEnricher(t *testing.T) {
    84  	pod := kube_api.Pod{
    85  		ObjectMeta: kube_api.ObjectMeta{
    86  			Name:      "pod1",
    87  			Namespace: "ns1",
    88  		},
    89  		Spec: kube_api.PodSpec{
    90  			NodeName: "node1",
    91  			Containers: []kube_api.Container{
    92  				{
    93  					Name:  "c1",
    94  					Image: "gcr.io/google_containers/pause:2.0",
    95  					Resources: kube_api.ResourceRequirements{
    96  						Requests: kube_api.ResourceList{
    97  							kube_api.ResourceCPU:    *resource.NewMilliQuantity(100, resource.DecimalSI),
    98  							kube_api.ResourceMemory: *resource.NewQuantity(555, resource.DecimalSI),
    99  						},
   100  					},
   101  				},
   102  				{
   103  					Name:  "nginx",
   104  					Image: "gcr.io/google_containers/pause:2.0",
   105  					Resources: kube_api.ResourceRequirements{
   106  						Requests: kube_api.ResourceList{
   107  							kube_api.ResourceCPU:    *resource.NewMilliQuantity(333, resource.DecimalSI),
   108  							kube_api.ResourceMemory: *resource.NewQuantity(1000, resource.DecimalSI),
   109  						},
   110  						Limits: kube_api.ResourceList{
   111  							kube_api.ResourceCPU:    *resource.NewMilliQuantity(2222, resource.DecimalSI),
   112  							kube_api.ResourceMemory: *resource.NewQuantity(3333, resource.DecimalSI),
   113  						},
   114  					},
   115  				},
   116  			},
   117  		},
   118  	}
   119  
   120  	store := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
   121  	podLister := &cache.StoreToPodLister{Indexer: store}
   122  	podLister.Indexer.Add(&pod)
   123  	podBasedEnricher := PodBasedEnricher{podLister: podLister}
   124  
   125  	var err error
   126  	for _, batch := range batches {
   127  		batch, err = podBasedEnricher.Process(batch)
   128  		assert.NoError(t, err)
   129  
   130  		podAggregator := PodAggregator{}
   131  		batch, err = podAggregator.Process(batch)
   132  		assert.NoError(t, err)
   133  
   134  		podMs, found := batch.MetricSets[core.PodKey("ns1", "pod1")]
   135  		assert.True(t, found)
   136  		checkRequests(t, podMs, 433, 1555)
   137  		checkLimits(t, podMs, 2222, 3333)
   138  
   139  		containerMs, found := batch.MetricSets[core.PodContainerKey("ns1", "pod1", "c1")]
   140  		assert.True(t, found)
   141  		checkRequests(t, containerMs, 100, 555)
   142  		checkLimits(t, containerMs, 0, 0)
   143  	}
   144  }
   145  
   146  func checkRequests(t *testing.T, ms *core.MetricSet, cpu, mem int64) {
   147  	cpuVal, found := ms.MetricValues[core.MetricCpuRequest.Name]
   148  	assert.True(t, found)
   149  	assert.Equal(t, cpu, cpuVal.IntValue)
   150  
   151  	memVal, found := ms.MetricValues[core.MetricMemoryRequest.Name]
   152  	assert.True(t, found)
   153  	assert.Equal(t, mem, memVal.IntValue)
   154  }
   155  
   156  func checkLimits(t *testing.T, ms *core.MetricSet, cpu, mem int64) {
   157  	cpuVal, found := ms.MetricValues[core.MetricCpuLimit.Name]
   158  	assert.True(t, found)
   159  	assert.Equal(t, cpu, cpuVal.IntValue)
   160  
   161  	memVal, found := ms.MetricValues[core.MetricMemoryLimit.Name]
   162  	assert.True(t, found)
   163  	assert.Equal(t, mem, memVal.IntValue)
   164  }