Skip to content

Commit 9465190

Browse files
committed
chore(test): clean up the cgroupsv2 checks
This is to bring clarity while developing a new test. Signed-off-by: Pablo Barbáchano <[email protected]>
1 parent 98235b7 commit 9465190

File tree

1 file changed

+39
-92
lines changed

1 file changed

+39
-92
lines changed

tests/integration_tests/security/test_jail.py

Lines changed: 39 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,12 @@ def sys_setup_cgroups():
216216
yield cgroup_version
217217

218218

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):
220220
"""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+
221225
for cgroup in cgroups:
222226
controller = cgroup.split(".")[0]
223227
file_name, value = cgroup.split("=")
@@ -231,55 +235,37 @@ def check_cgroups_v1(cgroups, cgroup_location, jailer_id, parent_cgroup=FC_BINAR
231235
assert open(tasks_file, "r", encoding="utf-8").readline().strip().isdigit()
232236

233237

234-
def check_cgroups_v2(cgroups, cgroup_location, jailer_id, parent_cgroup=FC_BINARY_NAME):
238+
def check_cgroups_v2(vm):
235239
"""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:
242249
controller = cgroup.split(".")[0]
243250
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"
257262
)
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"
271268
)
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()
283269

284270

285271
def get_cpus(node):
@@ -326,18 +312,10 @@ def test_cgroups(test_microvm_with_api, sys_setup_cgroups):
326312

327313
test_microvm.spawn()
328314

329-
# We assume sysfs cgroups are mounted here.
330-
sys_cgroup = "/sys/fs/cgroup"
331-
assert os.path.isdir(sys_cgroup)
332-
333315
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)
337317
else:
338-
check_cgroups_v2(
339-
test_microvm.jailer.cgroups, sys_cgroup, test_microvm.jailer.jailer_id
340-
)
318+
check_cgroups_v2(test_microvm)
341319

342320

343321
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):
362340

363341
test_microvm.spawn()
364342

365-
# We assume sysfs cgroups are mounted here.
366-
sys_cgroup = "/sys/fs/cgroup"
367-
assert os.path.isdir(sys_cgroup)
368-
369343
if test_microvm.jailer.cgroup_ver == 1:
370344
check_cgroups_v1(
371345
test_microvm.jailer.cgroups,
372-
sys_cgroup,
373346
test_microvm.jailer.jailer_id,
374347
test_microvm.jailer.parent_cgroup,
375348
)
376349
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)
383351

384352

385353
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):
397365

398366
test_microvm.spawn()
399367

400-
# We assume sysfs cgroups are mounted here.
401-
sys_cgroup = "/sys/fs/cgroup"
402-
assert os.path.isdir(sys_cgroup)
403-
404368
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)
408370
else:
409-
check_cgroups_v2(
410-
test_microvm.jailer.cgroups, sys_cgroup, test_microvm.jailer.jailer_id
411-
)
371+
check_cgroups_v2(test_microvm)
412372

413373

414374
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):
424384

425385
test_microvm.spawn()
426386

427-
# We assume sysfs cgroups are mounted here.
428-
sys_cgroup = "/sys/fs/cgroup"
429-
assert os.path.isdir(sys_cgroup)
430-
431387
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)
435389
else:
436-
check_cgroups_v2(
437-
test_microvm.jailer.cgroups, sys_cgroup, test_microvm.jailer.jailer_id
438-
)
390+
check_cgroups_v2(test_microvm)
439391

440392

441393
@pytest.mark.skipif(
@@ -450,13 +402,8 @@ def test_v1_default_cgroups(test_microvm_with_api):
450402

451403
test_microvm.spawn()
452404

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)
456406

457-
check_cgroups_v1(
458-
test_microvm.jailer.cgroups, sys_cgroup, test_microvm.jailer.jailer_id
459-
)
460407

461408

462409
def test_args_default_resource_limits(test_microvm_with_api):

0 commit comments

Comments
 (0)