forked from google/cadvisor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer_test.go
314 lines (260 loc) · 9.08 KB
/
container_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// 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.
// Per-container manager.
package manager
import (
"fmt"
"reflect"
"sync"
"testing"
"time"
"github.com/google/cadvisor/cache/memory"
"github.com/google/cadvisor/collector"
"github.com/google/cadvisor/container"
containertest "github.com/google/cadvisor/container/testing"
info "github.com/google/cadvisor/info/v1"
itest "github.com/google/cadvisor/info/v1/test"
"github.com/google/cadvisor/accelerators"
"github.com/mindprince/gonvml"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
clock "k8s.io/utils/clock/testing"
)
const (
containerName = "/container"
testLongHousekeeping = time.Second
)
// Create a containerData instance for a test.
func setupContainerData(t *testing.T, spec info.ContainerSpec) (*containerData, *containertest.MockContainerHandler, *memory.InMemoryCache, *clock.FakeClock) {
mockHandler := containertest.NewMockContainerHandler(containerName)
mockHandler.On("GetSpec").Return(
spec,
nil,
)
memoryCache := memory.New(60, nil)
fakeClock := clock.NewFakeClock(time.Now())
ret, err := newContainerData(containerName, memoryCache, mockHandler, false, &collector.GenericCollectorManager{}, 60*time.Second, true, fakeClock)
if err != nil {
t.Fatal(err)
}
return ret, mockHandler, memoryCache, fakeClock
}
// Create a containerData instance for a test and add a default GetSpec mock.
func newTestContainerData(t *testing.T) (*containerData, *containertest.MockContainerHandler, *memory.InMemoryCache, *clock.FakeClock) {
return setupContainerData(t, itest.GenerateRandomContainerSpec(4))
}
func TestUpdateSubcontainers(t *testing.T) {
subcontainers := []info.ContainerReference{
{Name: "/container/ee0103"},
{Name: "/container/abcd"},
{Name: "/container/something"},
}
cd, mockHandler, _, _ := newTestContainerData(t)
mockHandler.On("ListContainers", container.ListSelf).Return(
subcontainers,
nil,
)
err := cd.updateSubcontainers()
if err != nil {
t.Fatal(err)
}
if len(cd.info.Subcontainers) != len(subcontainers) {
t.Errorf("Received %v subcontainers, should be %v", len(cd.info.Subcontainers), len(subcontainers))
}
for _, sub := range cd.info.Subcontainers {
found := false
for _, sub2 := range subcontainers {
if sub.Name == sub2.Name {
found = true
}
}
if !found {
t.Errorf("Received unknown sub container %v", sub)
}
}
mockHandler.AssertExpectations(t)
}
func TestUpdateSubcontainersWithError(t *testing.T) {
cd, mockHandler, _, _ := newTestContainerData(t)
mockHandler.On("ListContainers", container.ListSelf).Return(
[]info.ContainerReference{},
fmt.Errorf("some error"),
)
mockHandler.On("Exists").Return(true)
assert.NotNil(t, cd.updateSubcontainers())
assert.Empty(t, cd.info.Subcontainers, "subcontainers should not be populated on failure")
mockHandler.AssertExpectations(t)
}
func TestUpdateSubcontainersWithErrorOnDeadContainer(t *testing.T) {
cd, mockHandler, _, _ := newTestContainerData(t)
mockHandler.On("ListContainers", container.ListSelf).Return(
[]info.ContainerReference{},
fmt.Errorf("some error"),
)
mockHandler.On("Exists").Return(false)
assert.Nil(t, cd.updateSubcontainers())
mockHandler.AssertExpectations(t)
}
func checkNumStats(t *testing.T, memoryCache *memory.InMemoryCache, numStats int) {
var empty time.Time
stats, err := memoryCache.RecentStats(containerName, empty, empty, -1)
require.Nil(t, err)
assert.Len(t, stats, numStats)
}
func TestUpdateStats(t *testing.T) {
statsList := itest.GenerateRandomStats(1, 4, 1*time.Second)
stats := statsList[0]
cd, mockHandler, memoryCache, _ := newTestContainerData(t)
mockHandler.On("GetStats").Return(
stats,
nil,
)
err := cd.updateStats()
if err != nil {
t.Fatal(err)
}
checkNumStats(t, memoryCache, 1)
mockHandler.AssertExpectations(t)
}
func TestUpdateSpec(t *testing.T) {
spec := itest.GenerateRandomContainerSpec(4)
cd, mockHandler, _, _ := newTestContainerData(t)
mockHandler.On("GetSpec").Return(
spec,
nil,
)
err := cd.updateSpec()
if err != nil {
t.Fatal(err)
}
mockHandler.AssertExpectations(t)
}
func TestGetInfo(t *testing.T) {
spec := itest.GenerateRandomContainerSpec(4)
subcontainers := []info.ContainerReference{
{Name: "/container/ee0103"},
{Name: "/container/abcd"},
{Name: "/container/something"},
}
cd, mockHandler, _, _ := setupContainerData(t, spec)
mockHandler.On("ListContainers", container.ListSelf).Return(
subcontainers,
nil,
)
mockHandler.Aliases = []string{"a1", "a2"}
info, err := cd.GetInfo(true)
if err != nil {
t.Fatal(err)
}
mockHandler.AssertExpectations(t)
if len(info.Subcontainers) != len(subcontainers) {
t.Errorf("Received %v subcontainers, should be %v", len(info.Subcontainers), len(subcontainers))
}
for _, sub := range info.Subcontainers {
found := false
for _, sub2 := range subcontainers {
if sub.Name == sub2.Name {
found = true
}
}
if !found {
t.Errorf("Received unknown sub container %v", sub)
}
}
if !reflect.DeepEqual(spec, info.Spec) {
t.Errorf("received wrong container spec")
}
if info.Name != mockHandler.Name {
t.Errorf("received wrong container name: received %v; should be %v", info.Name, mockHandler.Name)
}
}
func TestUpdateNvidiaStats(t *testing.T) {
cd, _, _, _ := newTestContainerData(t)
stats := info.ContainerStats{}
// When there are no devices, we should not get an error and stats should not change.
cd.nvidiaCollector = accelerators.NewNvidiaCollector([]gonvml.Device{})
err := cd.nvidiaCollector.UpdateStats(&stats)
assert.Nil(t, err)
assert.Equal(t, info.ContainerStats{}, stats)
// This is an impossible situation (there are devices but nvml is not initialized).
// Here I am testing that the CGo gonvml library doesn't panic when passed bad
// input and instead returns an error.
cd.nvidiaCollector = accelerators.NewNvidiaCollector([]gonvml.Device{{}, {}})
err = cd.nvidiaCollector.UpdateStats(&stats)
assert.NotNil(t, err)
assert.Equal(t, info.ContainerStats{}, stats)
}
func TestOnDemandHousekeeping(t *testing.T) {
statsList := itest.GenerateRandomStats(1, 4, 1*time.Second)
stats := statsList[0]
cd, mockHandler, memoryCache, fakeClock := newTestContainerData(t)
mockHandler.On("GetStats").Return(stats, nil)
defer cd.Stop()
// 0 seconds should always trigger an update
go cd.OnDemandHousekeeping(0 * time.Second)
cd.housekeepingTick(fakeClock.NewTimer(time.Minute).C(), testLongHousekeeping)
fakeClock.Step(2 * time.Second)
// This should return without requiring a housekeepingTick because stats have been updated recently enough
cd.OnDemandHousekeeping(3 * time.Second)
go cd.OnDemandHousekeeping(1 * time.Second)
cd.housekeepingTick(fakeClock.NewTimer(time.Minute).C(), testLongHousekeeping)
checkNumStats(t, memoryCache, 2)
mockHandler.AssertExpectations(t)
}
func TestConcurrentOnDemandHousekeeping(t *testing.T) {
statsList := itest.GenerateRandomStats(1, 4, 1*time.Second)
stats := statsList[0]
cd, mockHandler, memoryCache, fakeClock := newTestContainerData(t)
mockHandler.On("GetStats").Return(stats, nil)
defer cd.Stop()
numConcurrentCalls := 5
var waitForHousekeeping sync.WaitGroup
waitForHousekeeping.Add(numConcurrentCalls)
onDemandCache := []chan struct{}{}
for i := 0; i < numConcurrentCalls; i++ {
go func() {
cd.OnDemandHousekeeping(0 * time.Second)
waitForHousekeeping.Done()
}()
// Wait for work to be queued
onDemandCache = append(onDemandCache, <-cd.onDemandChan)
}
// Requeue work:
for _, ch := range onDemandCache {
cd.onDemandChan <- ch
}
go cd.housekeepingTick(fakeClock.NewTimer(time.Minute).C(), testLongHousekeeping)
// Ensure that all queued calls return with only a single call to housekeepingTick
waitForHousekeeping.Wait()
checkNumStats(t, memoryCache, 1)
mockHandler.AssertExpectations(t)
}
func TestOnDemandHousekeepingReturnsAfterStopped(t *testing.T) {
statsList := itest.GenerateRandomStats(1, 4, 1*time.Second)
stats := statsList[0]
cd, mockHandler, memoryCache, fakeClock := newTestContainerData(t)
mockHandler.On("GetStats").Return(stats, nil)
// trigger housekeeping update
go cd.OnDemandHousekeeping(0 * time.Second)
cd.housekeepingTick(fakeClock.NewTimer(time.Minute).C(), testLongHousekeeping)
checkNumStats(t, memoryCache, 1)
fakeClock.Step(2 * time.Second)
cd.Stop()
// housekeeping tick should detect stop and not store any more metrics
assert.False(t, cd.housekeepingTick(fakeClock.NewTimer(time.Minute).C(), testLongHousekeeping))
fakeClock.Step(1 * time.Second)
// on demand housekeeping should not block and return
cd.OnDemandHousekeeping(-1 * time.Second)
mockHandler.AssertExpectations(t)
}