@@ -216,8 +216,12 @@ def sys_setup_cgroups():
216
216
yield cgroup_version
217
217
218
218
219
- def check_cgroups_v1 (cgroups , cgroup_location , jailer_id , parent_cgroup = FC_BINARY_NAME ):
219
+ def check_cgroups_v1 (cgroups , jailer_id , parent_cgroup = FC_BINARY_NAME ):
220
220
"""Assert that every cgroupv1 in cgroups is correctly set."""
221
+ # We assume sysfs cgroups are mounted here.
222
+ cgroup_location = "/sys/fs/cgroup"
223
+ assert os .path .isdir (cgroup_location )
224
+
221
225
for cgroup in cgroups :
222
226
controller = cgroup .split ("." )[0 ]
223
227
file_name , value = cgroup .split ("=" )
@@ -231,55 +235,37 @@ def check_cgroups_v1(cgroups, cgroup_location, jailer_id, parent_cgroup=FC_BINAR
231
235
assert open (tasks_file , "r" , encoding = "utf-8" ).readline ().strip ().isdigit ()
232
236
233
237
234
- def check_cgroups_v2 (cgroups , cgroup_location , jailer_id , parent_cgroup = FC_BINARY_NAME ):
238
+ def check_cgroups_v2 (vm ):
235
239
"""Assert that every cgroupv2 in cgroups is correctly set."""
236
- cg_locations = {
237
- "root" : f"{ cgroup_location } " ,
238
- "fc" : f"{ cgroup_location } /{ parent_cgroup } " ,
239
- "jail" : f"{ cgroup_location } /{ parent_cgroup } /{ jailer_id } " ,
240
- }
241
- for cgroup in cgroups :
240
+ # We assume sysfs cgroups are mounted here.
241
+ cg_root = Path ("/sys/fs/cgroup" )
242
+ assert cg_root .is_dir ()
243
+ parent_cgroup = vm .jailer .parent_cgroup
244
+ if parent_cgroup is None :
245
+ parent_cgroup = FC_BINARY_NAME
246
+ cg_parent = cg_root / parent_cgroup
247
+ cg_jail = cg_parent / vm .jailer .jailer_id
248
+ for cgroup in vm .jailer .cgroups :
242
249
controller = cgroup .split ("." )[0 ]
243
250
file_name , value = cgroup .split ("=" )
244
- procs_file = f'{ cg_locations ["jail" ]} /cgroup.procs'
245
- file = f'{ cg_locations ["jail" ]} /{ file_name } '
246
-
247
- assert (
248
- controller
249
- in open (f'{ cg_locations ["root" ]} /cgroup.controllers' , "r" , encoding = "utf-8" )
250
- .readline ()
251
- .strip ()
252
- )
253
- assert (
254
- controller
255
- in open (
256
- f'{ cg_locations ["root" ]} /cgroup.subtree_control' , "r" , encoding = "utf-8"
251
+ procs = cg_jail .joinpath ("cgroup.procs" ).read_text ().splitlines ()
252
+ file = cg_jail / file_name
253
+
254
+ assert file .read_text ().strip () == value
255
+
256
+ assert all (x .isnumeric () for x in procs )
257
+ assert str (vm .firecracker_pid ) in procs
258
+
259
+ for cgroup in [cg_root , cg_parent , cg_jail ]:
260
+ assert controller in cgroup .joinpath ("cgroup.controllers" ).read_text (
261
+ encoding = "ascii"
257
262
)
258
- .readline ()
259
- .strip ()
260
- )
261
- assert (
262
- controller
263
- in open (f'{ cg_locations ["fc" ]} /cgroup.controllers' , "r" , encoding = "utf-8" )
264
- .readline ()
265
- .strip ()
266
- )
267
- assert (
268
- controller
269
- in open (
270
- f'{ cg_locations ["fc" ]} /cgroup.subtree_control' , "r" , encoding = "utf-8"
263
+ # don't check since there are no children cgroups
264
+ if cgroup == cg_jail :
265
+ continue
266
+ assert controller in cgroup .joinpath ("cgroup.subtree_control" ).read_text (
267
+ encoding = "ascii"
271
268
)
272
- .readline ()
273
- .strip ()
274
- )
275
- assert (
276
- controller
277
- in open (f'{ cg_locations ["jail" ]} /cgroup.controllers' , "r" , encoding = "utf-8" )
278
- .readline ()
279
- .strip ()
280
- )
281
- assert open (file , "r" , encoding = "utf-8" ).readline ().strip () == value
282
- assert open (procs_file , "r" , encoding = "utf-8" ).readline ().strip ().isdigit ()
283
269
284
270
285
271
def get_cpus (node ):
@@ -326,18 +312,10 @@ def test_cgroups(test_microvm_with_api, sys_setup_cgroups):
326
312
327
313
test_microvm .spawn ()
328
314
329
- # We assume sysfs cgroups are mounted here.
330
- sys_cgroup = "/sys/fs/cgroup"
331
- assert os .path .isdir (sys_cgroup )
332
-
333
315
if test_microvm .jailer .cgroup_ver == 1 :
334
- check_cgroups_v1 (
335
- test_microvm .jailer .cgroups , sys_cgroup , test_microvm .jailer .jailer_id
336
- )
316
+ check_cgroups_v1 (test_microvm .jailer .cgroups , test_microvm .jailer .jailer_id )
337
317
else :
338
- check_cgroups_v2 (
339
- test_microvm .jailer .cgroups , sys_cgroup , test_microvm .jailer .jailer_id
340
- )
318
+ check_cgroups_v2 (test_microvm )
341
319
342
320
343
321
def test_cgroups_custom_parent (test_microvm_with_api , sys_setup_cgroups ):
@@ -362,24 +340,14 @@ def test_cgroups_custom_parent(test_microvm_with_api, sys_setup_cgroups):
362
340
363
341
test_microvm .spawn ()
364
342
365
- # We assume sysfs cgroups are mounted here.
366
- sys_cgroup = "/sys/fs/cgroup"
367
- assert os .path .isdir (sys_cgroup )
368
-
369
343
if test_microvm .jailer .cgroup_ver == 1 :
370
344
check_cgroups_v1 (
371
345
test_microvm .jailer .cgroups ,
372
- sys_cgroup ,
373
346
test_microvm .jailer .jailer_id ,
374
347
test_microvm .jailer .parent_cgroup ,
375
348
)
376
349
else :
377
- check_cgroups_v2 (
378
- test_microvm .jailer .cgroups ,
379
- sys_cgroup ,
380
- test_microvm .jailer .jailer_id ,
381
- test_microvm .jailer .parent_cgroup ,
382
- )
350
+ check_cgroups_v2 (test_microvm )
383
351
384
352
385
353
def test_node_cgroups (test_microvm_with_api , sys_setup_cgroups ):
@@ -397,18 +365,10 @@ def test_node_cgroups(test_microvm_with_api, sys_setup_cgroups):
397
365
398
366
test_microvm .spawn ()
399
367
400
- # We assume sysfs cgroups are mounted here.
401
- sys_cgroup = "/sys/fs/cgroup"
402
- assert os .path .isdir (sys_cgroup )
403
-
404
368
if test_microvm .jailer .cgroup_ver == 1 :
405
- check_cgroups_v1 (
406
- test_microvm .jailer .cgroups , sys_cgroup , test_microvm .jailer .jailer_id
407
- )
369
+ check_cgroups_v1 (test_microvm .jailer .cgroups , test_microvm .jailer .jailer_id )
408
370
else :
409
- check_cgroups_v2 (
410
- test_microvm .jailer .cgroups , sys_cgroup , test_microvm .jailer .jailer_id
411
- )
371
+ check_cgroups_v2 (test_microvm )
412
372
413
373
414
374
def test_cgroups_without_numa (test_microvm_with_api , sys_setup_cgroups ):
@@ -424,18 +384,10 @@ def test_cgroups_without_numa(test_microvm_with_api, sys_setup_cgroups):
424
384
425
385
test_microvm .spawn ()
426
386
427
- # We assume sysfs cgroups are mounted here.
428
- sys_cgroup = "/sys/fs/cgroup"
429
- assert os .path .isdir (sys_cgroup )
430
-
431
387
if test_microvm .jailer .cgroup_ver == 1 :
432
- check_cgroups_v1 (
433
- test_microvm .jailer .cgroups , sys_cgroup , test_microvm .jailer .jailer_id
434
- )
388
+ check_cgroups_v1 (test_microvm .jailer .cgroups , test_microvm .jailer .jailer_id )
435
389
else :
436
- check_cgroups_v2 (
437
- test_microvm .jailer .cgroups , sys_cgroup , test_microvm .jailer .jailer_id
438
- )
390
+ check_cgroups_v2 (test_microvm )
439
391
440
392
441
393
@pytest .mark .skipif (
@@ -450,13 +402,8 @@ def test_v1_default_cgroups(test_microvm_with_api):
450
402
451
403
test_microvm .spawn ()
452
404
453
- # We assume sysfs cgroups are mounted here.
454
- sys_cgroup = "/sys/fs/cgroup"
455
- assert os .path .isdir (sys_cgroup )
405
+ check_cgroups_v1 (test_microvm .jailer .cgroups , test_microvm .jailer .jailer_id )
456
406
457
- check_cgroups_v1 (
458
- test_microvm .jailer .cgroups , sys_cgroup , test_microvm .jailer .jailer_id
459
- )
460
407
461
408
462
409
def test_args_default_resource_limits (test_microvm_with_api ):
0 commit comments