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