github.com/timstclair/heapster@v0.20.0-alpha1/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  )
    28  
    29  func TestPodEnricher(t *testing.T) {
    30  	batch := &core.DataBatch{
    31  		Timestamp: time.Now(),
    32  		MetricSets: map[string]*core.MetricSet{
    33  			core.PodContainerKey("ns1", "pod1", "c1"): {
    34  				Labels: map[string]string{
    35  					core.LabelMetricSetType.Key: core.MetricSetTypePodContainer,
    36  					core.LabelPodName.Key:       "pod1",
    37  					core.LabelNamespaceName.Key: "ns1",
    38  					core.LabelContainerName.Key: "c1",
    39  				},
    40  				MetricValues: map[string]core.MetricValue{},
    41  			},
    42  
    43  			core.PodKey("ns1", "pod1"): {
    44  				Labels: map[string]string{
    45  					core.LabelMetricSetType.Key: core.MetricSetTypePod,
    46  					core.LabelPodName.Key:       "pod1",
    47  					core.LabelNamespaceName.Key: "ns1",
    48  				},
    49  				MetricValues: map[string]core.MetricValue{},
    50  			},
    51  		},
    52  	}
    53  
    54  	pod := kube_api.Pod{
    55  		ObjectMeta: kube_api.ObjectMeta{
    56  			Name:      "pod1",
    57  			Namespace: "ns1",
    58  		},
    59  		Spec: kube_api.PodSpec{
    60  			NodeName: "node1",
    61  			Containers: []kube_api.Container{
    62  				{
    63  					Name:  "c1",
    64  					Image: "gcr.io/google_containers/pause:2.0",
    65  					Resources: kube_api.ResourceRequirements{
    66  						Requests: kube_api.ResourceList{
    67  							kube_api.ResourceCPU:    *resource.NewMilliQuantity(100, resource.DecimalSI),
    68  							kube_api.ResourceMemory: *resource.NewQuantity(555, resource.DecimalSI),
    69  						},
    70  					},
    71  				},
    72  				{
    73  					Name:  "nginx",
    74  					Image: "gcr.io/google_containers/pause:2.0",
    75  					Resources: kube_api.ResourceRequirements{
    76  						Requests: kube_api.ResourceList{
    77  							kube_api.ResourceCPU:    *resource.NewMilliQuantity(333, resource.DecimalSI),
    78  							kube_api.ResourceMemory: *resource.NewQuantity(1000, resource.DecimalSI),
    79  						},
    80  						Limits: kube_api.ResourceList{
    81  							kube_api.ResourceCPU:    *resource.NewMilliQuantity(2222, resource.DecimalSI),
    82  							kube_api.ResourceMemory: *resource.NewQuantity(3333, resource.DecimalSI),
    83  						},
    84  					},
    85  				},
    86  			},
    87  		},
    88  	}
    89  
    90  	addPodInfo(&pod, batch)
    91  
    92  	podAggregator := PodAggregator{}
    93  	var err error
    94  	batch, err = podAggregator.Process(batch)
    95  	assert.NoError(t, err)
    96  
    97  	podMs, found := batch.MetricSets[core.PodKey("ns1", "pod1")]
    98  	assert.True(t, found)
    99  	checkRequests(t, podMs, 433, 1555)
   100  	checkLimits(t, podMs, 2222, 3333)
   101  
   102  	containerMs, found := batch.MetricSets[core.PodContainerKey("ns1", "pod1", "c1")]
   103  	assert.True(t, found)
   104  	checkRequests(t, containerMs, 100, 555)
   105  	checkLimits(t, containerMs, 0, 0)
   106  }
   107  
   108  func checkRequests(t *testing.T, ms *core.MetricSet, cpu, mem int64) {
   109  	cpuVal, found := ms.MetricValues[core.MetricCpuRequest.Name]
   110  	assert.True(t, found)
   111  	assert.Equal(t, cpu, cpuVal.IntValue)
   112  
   113  	memVal, found := ms.MetricValues[core.MetricMemoryRequest.Name]
   114  	assert.True(t, found)
   115  	assert.Equal(t, mem, memVal.IntValue)
   116  }
   117  
   118  func checkLimits(t *testing.T, ms *core.MetricSet, cpu, mem int64) {
   119  	cpuVal, found := ms.MetricValues[core.MetricCpuLimit.Name]
   120  	assert.True(t, found)
   121  	assert.Equal(t, cpu, cpuVal.IntValue)
   122  
   123  	memVal, found := ms.MetricValues[core.MetricMemoryLimit.Name]
   124  	assert.True(t, found)
   125  	assert.Equal(t, mem, memVal.IntValue)
   126  }