forked from kubernetes-retired/heapster
/
cadvisor_test.go
103 lines (95 loc) · 3.52 KB
/
cadvisor_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2014 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sources
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/heapster/sources/api"
"k8s.io/heapster/sources/datasource"
"k8s.io/heapster/sources/nodes"
)
type fakeDataSource struct {
f func(host datasource.Host, start, end time.Time, resolution time.Duration) (subcontainers []*api.Container, root *api.Container, err error)
}
func (self *fakeDataSource) GetAllContainers(host datasource.Host, start, end time.Time, resolution time.Duration, align bool) (subcontainers []*api.Container, root *api.Container, err error) {
return self.f(host, start, end, resolution)
}
func TestBasicSuccess(t *testing.T) {
cadvisorApi := &fakeDataSource{
f: func(host datasource.Host, start, end time.Time, resolution time.Duration) (subcontainers []*api.Container, root *api.Container, err error) {
return nil, nil, nil
},
}
source := &cadvisorSource{
nodesApi: &fakeNodesApi{nodes.NodeList{}},
cadvisorPort: 8080,
cadvisorApi: cadvisorApi,
}
data, err := source.GetInfo(time.Now(), time.Now().Add(time.Minute), time.Second, false)
require.NoError(t, err)
require.Equal(t, api.AggregateData{Pods: nil, Containers: nil, Machine: nil}, data)
}
func TestWorkflowSuccess(t *testing.T) {
type cadvisorData struct {
subcontainers []*api.Container
root *api.Container
}
hostA := datasource.Host{IP: "1.1.1.1", Port: 8080, Resource: ""}
hostB := datasource.Host{IP: "1.1.1.2", Port: 8080, Resource: ""}
expectedData := map[datasource.Host]cadvisorData{
hostA: {
subcontainers: []*api.Container{{Name: "/a"}},
root: &api.Container{Name: "/"},
},
hostB: {
subcontainers: []*api.Container{{Name: "/b"}},
root: &api.Container{Name: "/"},
},
}
cadvisorApi := &fakeDataSource{
f: func(host datasource.Host, start, end time.Time, resolution time.Duration) (subcontainers []*api.Container, root *api.Container, err error) {
data, exists := expectedData[host]
if !exists {
return nil, nil, fmt.Errorf("unexpected host: %+v", host)
}
return data.subcontainers, data.root, nil
},
}
nodeList := nodes.NodeList{
Items: map[nodes.Host]nodes.Info{
nodes.Host("a"): {InternalIP: "1.1.1.1"},
nodes.Host("b"): {InternalIP: "1.1.1.2"},
},
}
source := &cadvisorSource{
nodesApi: &fakeNodesApi{nodeList},
cadvisorPort: 8080,
cadvisorApi: cadvisorApi,
}
data, err := source.GetInfo(time.Now(), time.Now().Add(time.Minute), time.Second, false)
require.NoError(t, err)
assert.Len(t, data.Containers, 2)
assert.Len(t, data.Machine, 2)
assert.Equal(t, data.Machine[0].Name, expectedData[hostA].root.Name)
assert.Equal(t, data.Machine[1].Name, expectedData[hostB].root.Name)
containerNames := []string{
data.Containers[0].Name,
data.Containers[1].Name,
}
assert.Contains(t, containerNames, expectedData[hostA].subcontainers[0].Name)
assert.Contains(t, containerNames, expectedData[hostB].subcontainers[0].Name)
}