github.com/mrgossett/heapster@v0.18.2/integration/model_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 integration
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  	"time"
    21  
    22  	cadvisor "github.com/google/cadvisor/info/v1"
    23  	"github.com/stretchr/testify/assert"
    24  
    25  	"k8s.io/heapster/manager"
    26  	model_api "k8s.io/heapster/model"
    27  	"k8s.io/heapster/sinks"
    28  	sink_api "k8s.io/heapster/sinks/api"
    29  	"k8s.io/heapster/sinks/cache"
    30  	source_api "k8s.io/heapster/sources/api"
    31  )
    32  
    33  type testSource struct {
    34  	createTimestamp time.Time
    35  }
    36  
    37  const (
    38  	podCount         = 10
    39  	testNamespace    = "testnamespace"
    40  	loadAverageMilli = 300
    41  )
    42  
    43  func (t testSource) buildPods(start time.Time) []source_api.Pod {
    44  
    45  	timeElapsed := start.Sub(t.createTimestamp)
    46  
    47  	result := []source_api.Pod{}
    48  	for i := 0; i < podCount; i++ {
    49  		stat := source_api.ContainerStats{
    50  			ContainerStats: cadvisor.ContainerStats{
    51  				Timestamp: start.Add(time.Millisecond * 500),
    52  				Cpu: cadvisor.CpuStats{
    53  					Usage: cadvisor.CpuUsage{
    54  						Total:  uint64(loadAverageMilli * 1000000 * timeElapsed.Seconds()),
    55  						User:   uint64(loadAverageMilli * 1000000 * timeElapsed.Seconds()),
    56  						System: 0,
    57  					},
    58  					LoadAverage: loadAverageMilli,
    59  				},
    60  			},
    61  		}
    62  
    63  		pod := source_api.Pod{
    64  			PodMetadata: source_api.PodMetadata{
    65  				Name:         fmt.Sprintf("pod-%d", i),
    66  				Namespace:    testNamespace,
    67  				NamespaceUID: testNamespace + "UID",
    68  				ID:           fmt.Sprintf("pid-%d", i),
    69  				Hostname:     fmt.Sprintf("node-%d", i),
    70  				Status:       "Running",
    71  				PodIP:        fmt.Sprintf("10.0.0.%d", i),
    72  			},
    73  			Containers: []source_api.Container{
    74  				{
    75  					Hostname:   fmt.Sprintf("node-%d", i),
    76  					ExternalID: fmt.Sprintf("cont-%d", i),
    77  					Name:       "cont",
    78  					Spec: source_api.ContainerSpec{
    79  						ContainerSpec: cadvisor.ContainerSpec{
    80  							HasCpu: true,
    81  							Cpu: cadvisor.CpuSpec{
    82  								Limit:    500,
    83  								MaxLimit: 600,
    84  							},
    85  						},
    86  					},
    87  					Stats: []*source_api.ContainerStats{&stat},
    88  				},
    89  			},
    90  		}
    91  		result = append(result, pod)
    92  	}
    93  	return result
    94  }
    95  
    96  func (t testSource) GetInfo(start, end time.Time) (source_api.AggregateData, error) {
    97  	return source_api.AggregateData{
    98  		Pods: t.buildPods(start),
    99  	}, nil
   100  }
   101  
   102  func (t testSource) DebugInfo() string {
   103  	return "test-debug-info"
   104  }
   105  
   106  func (t testSource) Name() string {
   107  	return "test-source"
   108  }
   109  
   110  func newTestSource() source_api.Source {
   111  	return &testSource{
   112  		createTimestamp: time.Now().Add(-10 * time.Second),
   113  	}
   114  }
   115  
   116  func TestModelMetricPassing(t *testing.T) {
   117  	if testing.Short() {
   118  		t.Skip("skipping heapster model integration test.")
   119  	}
   120  	assert := assert.New(t)
   121  	resolution := 2 * time.Second
   122  
   123  	sources := []source_api.Source{newTestSource()}
   124  	cache := cache.NewCache(time.Hour, time.Hour)
   125  	assert.NotNil(cache)
   126  	sinkManager, err := sinks.NewExternalSinkManager([]sink_api.ExternalSink{}, cache, resolution)
   127  	assert.NoError(err)
   128  
   129  	manager, err := manager.NewManager(sources, sinkManager, resolution, time.Hour, cache, true, resolution, resolution)
   130  	assert.NoError(err)
   131  	start := time.Now()
   132  
   133  	manager.Start()
   134  	defer manager.Stop()
   135  	time.Sleep(10 * time.Second)
   136  
   137  	model := manager.GetModel()
   138  	pods := model.GetPods(testNamespace)
   139  	assert.Equal(podCount, len(pods))
   140  
   141  	metrics, _, err := model.GetPodMetric(model_api.PodMetricRequest{
   142  		NamespaceName: testNamespace,
   143  		PodName:       "pod-0",
   144  		MetricRequest: model_api.MetricRequest{
   145  			Start:      start,
   146  			End:        time.Time{},
   147  			MetricName: "cpu-usage",
   148  		},
   149  	})
   150  	assert.NoError(err)
   151  	//TODO: Expect more than 1 metric once #551 is fixed
   152  	assert.NotEmpty(metrics)
   153  	assert.InEpsilon(loadAverageMilli, metrics[0].Value, 50)
   154  }