@@ -89,6 +89,9 @@ func TestCadvisorListPodStats(t *testing.T) {
89
89
seedPod1Container = 4000
90
90
seedPod2Infra = 5000
91
91
seedPod2Container = 6000
92
+ seedPod3Infra = 7000
93
+ seedPod3Container0 = 8000
94
+ seedPod3Container1 = 8001
92
95
seedEphemeralVolume1 = 10000
93
96
seedEphemeralVolume2 = 10001
94
97
seedPersistentVolume1 = 20000
@@ -98,12 +101,15 @@ func TestCadvisorListPodStats(t *testing.T) {
98
101
pName0 = "pod0"
99
102
pName1 = "pod1"
100
103
pName2 = "pod0" // ensure pName2 conflicts with pName0, but is in a different namespace
104
+ pName3 = "pod3"
101
105
)
102
106
const (
103
107
cName00 = "c0"
104
108
cName01 = "c1"
105
109
cName10 = "c0" // ensure cName10 conflicts with cName02, but is in a different pod
106
110
cName20 = "c1" // ensure cName20 conflicts with cName01, but is in a different pod + namespace
111
+ cName30 = "c0-init"
112
+ cName31 = "c1"
107
113
)
108
114
const (
109
115
rootfsCapacity = uint64 (10000000 )
@@ -119,6 +125,7 @@ func TestCadvisorListPodStats(t *testing.T) {
119
125
prf0 := statsapi.PodReference {Name : pName0 , Namespace : namespace0 , UID : "UID" + pName0 }
120
126
prf1 := statsapi.PodReference {Name : pName1 , Namespace : namespace0 , UID : "UID" + pName1 }
121
127
prf2 := statsapi.PodReference {Name : pName2 , Namespace : namespace2 , UID : "UID" + pName2 }
128
+ prf3 := statsapi.PodReference {Name : pName3 , Namespace : namespace0 , UID : "UID" + pName3 }
122
129
infos := map [string ]cadvisorapiv2.ContainerInfo {
123
130
"/" : getTestContainerInfo (seedRoot , "" , "" , "" ),
124
131
"/docker-daemon" : getTestContainerInfo (seedRuntime , "" , "" , "" ),
@@ -136,6 +143,10 @@ func TestCadvisorListPodStats(t *testing.T) {
136
143
"/pod2-c0" : getTestContainerInfo (seedPod2Container , pName2 , namespace2 , cName20 ),
137
144
"/kubepods/burstable/podUIDpod0" : getTestContainerInfo (seedPod0Infra , pName0 , namespace0 , leaky .PodInfraContainerName ),
138
145
"/kubepods/podUIDpod1" : getTestContainerInfo (seedPod1Infra , pName1 , namespace0 , leaky .PodInfraContainerName ),
146
+ // Pod3 - Namespace0
147
+ "/pod3-i" : getTestContainerInfo (seedPod3Infra , pName3 , namespace0 , leaky .PodInfraContainerName ),
148
+ "/pod3-c0-init" : getTestContainerInfo (seedPod3Container0 , pName3 , namespace0 , cName30 ),
149
+ "/pod3-c1" : getTestContainerInfo (seedPod3Container1 , pName3 , namespace0 , cName31 ),
139
150
}
140
151
141
152
freeRootfsInodes := rootfsInodesFree
@@ -169,6 +180,21 @@ func TestCadvisorListPodStats(t *testing.T) {
169
180
info .Spec .Memory .Limit = memoryLimitOverride
170
181
infos [name ] = info
171
182
}
183
+ // any container for which cadvisor should return no stats (as might be the case for an exited init container)
184
+ nostatsOverrides := []string {
185
+ "/pod3-c0-init" ,
186
+ }
187
+ for _ , name := range nostatsOverrides {
188
+ info , found := infos [name ]
189
+ if ! found {
190
+ t .Errorf ("No container defined with name %v" , name )
191
+ }
192
+ info .Spec .Memory = cadvisorapiv2.MemorySpec {}
193
+ info .Spec .Cpu = cadvisorapiv2.CpuSpec {}
194
+ info .Spec .HasMemory = false
195
+ info .Spec .HasCpu = false
196
+ infos [name ] = info
197
+ }
172
198
173
199
options := cadvisorapiv2.RequestOptions {
174
200
IdType : cadvisorapiv2 .TypeName ,
@@ -197,18 +223,20 @@ func TestCadvisorListPodStats(t *testing.T) {
197
223
p0Time := metav1 .Now ()
198
224
p1Time := metav1 .Now ()
199
225
p2Time := metav1 .Now ()
226
+ p3Time := metav1 .Now ()
200
227
mockStatus := new (statustest.MockStatusProvider )
201
228
mockStatus .On ("GetPodStatus" , types .UID ("UID" + pName0 )).Return (v1.PodStatus {StartTime : & p0Time }, true )
202
229
mockStatus .On ("GetPodStatus" , types .UID ("UID" + pName1 )).Return (v1.PodStatus {StartTime : & p1Time }, true )
203
230
mockStatus .On ("GetPodStatus" , types .UID ("UID" + pName2 )).Return (v1.PodStatus {StartTime : & p2Time }, true )
231
+ mockStatus .On ("GetPodStatus" , types .UID ("UID" + pName3 )).Return (v1.PodStatus {StartTime : & p3Time }, true )
204
232
205
233
resourceAnalyzer := & fakeResourceAnalyzer {podVolumeStats : volumeStats }
206
234
207
235
p := NewCadvisorStatsProvider (mockCadvisor , resourceAnalyzer , nil , nil , mockRuntime , mockStatus )
208
236
pods , err := p .ListPodStats ()
209
237
assert .NoError (t , err )
210
238
211
- assert .Equal (t , 3 , len (pods ))
239
+ assert .Equal (t , 4 , len (pods ))
212
240
indexPods := make (map [statsapi.PodReference ]statsapi.PodStats , len (pods ))
213
241
for _ , pod := range pods {
214
242
indexPods [pod .PodRef ] = pod
@@ -261,6 +289,24 @@ func TestCadvisorListPodStats(t *testing.T) {
261
289
checkCPUStats (t , "Pod2Container0" , seedPod2Container , con .CPU )
262
290
checkMemoryStats (t , "Pod2Container0" , seedPod2Container , infos ["/pod2-c0" ], con .Memory )
263
291
checkNetworkStats (t , "Pod2" , seedPod2Infra , ps .Network )
292
+
293
+ // Validate Pod3 Results
294
+
295
+ ps , found = indexPods [prf3 ]
296
+ assert .True (t , found )
297
+ assert .Len (t , ps .Containers , 2 )
298
+ indexCon = make (map [string ]statsapi.ContainerStats , len (ps .Containers ))
299
+ for _ , con := range ps .Containers {
300
+ indexCon [con .Name ] = con
301
+ }
302
+ con = indexCon [cName31 ]
303
+ assert .Equal (t , cName31 , con .Name )
304
+ checkCPUStats (t , "Pod3Container1" , seedPod3Container1 , con .CPU )
305
+ checkMemoryStats (t , "Pod3Container1" , seedPod3Container1 , infos ["/pod3-c1" ], con .Memory )
306
+ con = indexCon [cName30 ]
307
+ assert .Equal (t , cName30 , con .Name )
308
+ checkEmptyCPUStats (t , "Pod3Container0" , seedPod3Container0 , con .CPU )
309
+ checkEmptyMemoryStats (t , "Pod3Container0" , seedPod3Container0 , infos ["/pod3-c0-init" ], con .Memory )
264
310
}
265
311
266
312
func TestCadvisorListPodCPUAndMemoryStats (t * testing.T ) {
0 commit comments