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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
|
// Copyright (c) Contributors to the Apptainer project, established as
// Apptainer a Series of LF Projects LLC.
// For website terms of use, trademark policy, privacy policy and other
// project policies see https://lfprojects.org/policies
// Copyright (c) 2022-2025, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package cgroups
import (
"fmt"
"testing"
"github.com/apptainer/apptainer/e2e/internal/e2e"
"github.com/apptainer/apptainer/e2e/internal/testhelper"
"github.com/apptainer/apptainer/internal/pkg/test/tool/require"
"github.com/google/uuid"
"github.com/opencontainers/cgroups"
)
// NOTE
// ----
// Tests in this package/topic are run in a mount namespace only. There is
// no PID namespace, in order that the systemd cgroups manager functionality
// can be exercised.
//
// You must take extra care not to leave detached process etc. that will
// pollute the host PID namespace.
//
// randomName generates a random name instance or OCI container name based on a UUID.
func randomName(t *testing.T) string {
t.Helper()
id, err := uuid.NewRandom()
if err != nil {
t.Fatal(err)
}
return id.String()
}
type ctx struct {
env e2e.TestEnv
}
// instanceStats tests an instance ability to output stats
func (c *ctx) instanceStats(t *testing.T, profile e2e.Profile) {
e2e.EnsureImage(t, c.env)
// All tests require root
tests := []struct {
name string
createArgs []string
startErrorCode int
statsErrorCode int
}{
{
name: "basic stats create",
createArgs: []string{"--memory", "250M", c.env.ImagePath},
statsErrorCode: 0,
startErrorCode: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// We always expect stats output, not create
createExitFunc := []e2e.ApptainerCmdResultOp{}
instanceName := randomName(t)
// Start the instance with cgroups for stats
createArgs := append(tt.createArgs, instanceName)
c.env.RunApptainer(
t,
e2e.AsSubtest("start"),
e2e.WithProfile(profile),
e2e.WithCommand("instance start"),
e2e.WithArgs(createArgs...),
e2e.ExpectExit(tt.startErrorCode, createExitFunc...),
)
// Get stats for the instance
c.env.RunApptainer(
t,
e2e.AsSubtest("stats"),
e2e.WithProfile(profile),
e2e.WithCommand("instance stats"),
e2e.WithArgs("--no-stream", instanceName),
e2e.ExpectExit(tt.statsErrorCode,
// Header (column spacing varies by content)
e2e.ExpectOutput(e2e.ContainMatch, "INSTANCE NAME"),
e2e.ExpectOutput(e2e.ContainMatch, "CPU USAGE"),
e2e.ExpectOutput(e2e.ContainMatch, "MEM USAGE / LIMIT"),
e2e.ExpectOutput(e2e.ContainMatch, "MEM %"),
e2e.ExpectOutput(e2e.ContainMatch, "BLOCK I/O"),
e2e.ExpectOutput(e2e.ContainMatch, "PIDS"),
e2e.ExpectOutput(e2e.ContainMatch, "PIDS"),
// Instance name is visible
e2e.ExpectOutput(e2e.ContainMatch, instanceName),
// Memory limit is visible
e2e.ExpectOutput(e2e.ContainMatch, "/ 250MiB"),
),
)
c.env.RunApptainer(
t,
e2e.AsSubtest("stop"),
e2e.WithProfile(profile),
e2e.WithCommand("instance stop"),
e2e.WithArgs(instanceName),
e2e.ExpectExit(0),
)
})
}
}
// moved from INSTANCE suite, as testing with systemd cgroup manager requires
// e2e to be run without PID namespace
func (c *ctx) instanceApply(t *testing.T, profile e2e.Profile) {
e2e.EnsureImage(t, c.env)
tests := []struct {
name string
createArgs []string
execArgs []string
startErrorCode int
startErrorOut string
execErrorCode int
execErrorOut string
rootfull bool
rootless bool
}{
{
name: "nonexistent toml",
createArgs: []string{"--apply-cgroups", "testdata/cgroups/doesnotexist.toml", c.env.ImagePath},
startErrorCode: 255,
// e2e test currently only captures the error from the CLI process, not the error displayed by the
// starter process, so we check for the generic CLI error.
startErrorOut: "no such file or directory",
rootfull: true,
rootless: true,
},
{
name: "invalid toml",
createArgs: []string{"--apply-cgroups", "testdata/cgroups/invalid.toml", c.env.ImagePath},
startErrorCode: 255,
// e2e test currently only captures the error from the CLI process, not the error displayed by the
// starter process, so we check for the generic CLI error.
startErrorOut: "toml: expected character",
rootfull: true,
rootless: true,
},
{
name: "memory limit",
createArgs: []string{"--apply-cgroups", "testdata/cgroups/memory_limit.toml", c.env.ImagePath},
// We get a CLI 255 error code, not the 137 that the starter receives for an OOM kill
startErrorCode: 255,
startErrorOut: "signal: killed",
rootfull: true,
rootless: true,
},
{
name: "cpu success",
createArgs: []string{"--apply-cgroups", "testdata/cgroups/cpu_success.toml", c.env.ImagePath},
startErrorCode: 0,
execArgs: []string{"/bin/true"},
execErrorCode: 0,
rootfull: true,
rootless: true,
},
{
name: "device deny",
createArgs: []string{"--apply-cgroups", "testdata/cgroups/deny_device.toml", c.env.ImagePath},
startErrorCode: 0,
execArgs: []string{"cat", "/dev/null"},
execErrorCode: 1,
execErrorOut: "Operation not permitted",
rootfull: true,
rootless: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if profile.Privileged() && !tt.rootfull {
t.Skip()
}
if !profile.Privileged() && !tt.rootless {
t.Skip()
}
createExitFunc := []e2e.ApptainerCmdResultOp{}
if tt.startErrorOut != "" {
createExitFunc = []e2e.ApptainerCmdResultOp{e2e.ExpectError(e2e.ContainMatch, tt.startErrorOut)}
}
execExitFunc := []e2e.ApptainerCmdResultOp{}
if tt.execErrorOut != "" {
execExitFunc = []e2e.ApptainerCmdResultOp{e2e.ExpectError(e2e.ContainMatch, tt.execErrorOut)}
}
// pick up a random name
instanceName := randomName(t)
joinName := fmt.Sprintf("instance://%s", instanceName)
createArgs := append(tt.createArgs, instanceName)
c.env.RunApptainer(
t,
e2e.AsSubtest("start"),
e2e.WithProfile(profile),
e2e.WithCommand("instance start"),
e2e.WithArgs(createArgs...),
e2e.ExpectExit(tt.startErrorCode, createExitFunc...),
)
if tt.startErrorCode != 0 {
return
}
execArgs := append([]string{joinName}, tt.execArgs...)
c.env.RunApptainer(
t,
e2e.AsSubtest("exec"),
e2e.WithProfile(profile),
e2e.WithGlobalOptions("-d"),
e2e.WithCommand("exec"),
e2e.WithArgs(execArgs...),
e2e.WithDir(profile.HostUser(t).Dir),
e2e.ExpectExit(tt.execErrorCode, execExitFunc...),
)
c.env.RunApptainer(
t,
e2e.AsSubtest("stop"),
e2e.WithProfile(profile),
e2e.WithCommand("instance stop"),
e2e.WithArgs(instanceName),
e2e.ExpectExit(0),
)
})
}
}
func (c *ctx) instanceApplyRoot(t *testing.T) {
c.instanceApply(t, e2e.RootProfile)
}
func (c *ctx) instanceApplyRootless(t *testing.T) {
c.instanceApply(t, e2e.UserProfile)
}
func (c *ctx) instanceStatsRoot(t *testing.T) {
c.instanceStats(t, e2e.RootProfile)
}
func (c *ctx) instanceStatsRootless(t *testing.T) {
c.instanceStats(t, e2e.UserProfile)
}
func (c *ctx) actionApply(t *testing.T, profile e2e.Profile) {
e2e.EnsureImage(t, c.env)
tests := []struct {
name string
args []string
expectErrorCode int
expectErrorOut string
rootfull bool
rootless bool
}{
{
name: "nonexistent toml",
args: []string{"--apply-cgroups", "testdata/cgroups/doesnotexist.toml", c.env.ImagePath, "/bin/sleep", "5"},
expectErrorCode: 255,
expectErrorOut: "no such file or directory",
rootfull: true,
rootless: true,
},
{
name: "invalid toml",
args: []string{"--apply-cgroups", "testdata/cgroups/invalid.toml", c.env.ImagePath, "/bin/sleep", "5"},
expectErrorCode: 255,
expectErrorOut: "toml: expected character",
rootfull: true,
rootless: true,
},
{
name: "memory limit",
args: []string{"--apply-cgroups", "testdata/cgroups/memory_limit.toml", c.env.ImagePath, "/bin/sleep", "5"},
expectErrorCode: 137,
rootfull: true,
rootless: true,
},
{
name: "cpu success",
args: []string{"--apply-cgroups", "testdata/cgroups/cpu_success.toml", c.env.ImagePath, "/bin/true"},
expectErrorCode: 0,
rootfull: true,
// This currently fails in the e2e scenario due to the way we are using a mount namespace.
// It *does* work if you test it, directly calling the apptainer CLI.
// Reason is believed to be: https://github.com/opencontainers/runc/issues/3026
rootless: false,
},
// Device access is allowed by default.
{
name: "device allow default",
args: []string{"--apply-cgroups", "testdata/cgroups/null.toml", c.env.ImagePath, "cat", "/dev/null"},
expectErrorCode: 0,
rootfull: true,
rootless: true,
},
// Device limits are properly applied only in rootful mode. Rootless will ignore them with a warning.
{
name: "device deny",
args: []string{"--apply-cgroups", "testdata/cgroups/deny_device.toml", c.env.ImagePath, "cat", "/dev/null"},
expectErrorCode: 1,
expectErrorOut: "Operation not permitted",
rootfull: true,
rootless: false,
},
{
name: "device ignored",
args: []string{"--apply-cgroups", "testdata/cgroups/deny_device.toml", c.env.ImagePath, "cat", "/dev/null"},
expectErrorCode: 0,
expectErrorOut: "Device limits will not be applied with rootless cgroups",
rootfull: false,
rootless: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if profile.Privileged() && !tt.rootfull {
t.Skip()
}
if !profile.Privileged() && !tt.rootless {
t.Skip()
}
exitFunc := []e2e.ApptainerCmdResultOp{}
if tt.expectErrorOut != "" {
exitFunc = []e2e.ApptainerCmdResultOp{e2e.ExpectError(e2e.ContainMatch, tt.expectErrorOut)}
}
c.env.RunApptainer(
t,
e2e.WithProfile(profile),
e2e.WithCommand("exec"),
e2e.WithArgs(tt.args...),
e2e.ExpectExit(tt.expectErrorCode, exitFunc...),
)
})
}
}
func (c *ctx) actionApplyRoot(t *testing.T) {
c.actionApply(t, e2e.RootProfile)
}
func (c *ctx) actionApplyRootless(t *testing.T) {
for _, profile := range []e2e.Profile{e2e.UserProfile, e2e.UserNamespaceProfile, e2e.FakerootProfile} {
t.Run(profile.String(), func(t *testing.T) {
c.actionApply(t, profile)
})
}
}
type resourceFlagTest struct {
name string
args []string
expectErrorCode int
// cgroupsV1 - cgroupfs controller/resource to check, and content we expect to see
controllerV1 string
resourceV1 string
expectV1 string
// cgroupsV2 - delegation required when rootless
delegationV2 string
// cgroupsV2 - resource to check, and content we expect to see
resourceV2 string
expectV2 string
skipV2 bool
}
var resourceFlagTests = []resourceFlagTest{
{
name: "blkio-weight",
args: []string{"--blkio-weight", "50"},
expectErrorCode: 0,
controllerV1: "blkio",
// Could be `blkio.bfq.weight` if bfq is available. However, under
// cgroups v1 older crun will not set blkio.bfq.weight, so only test
// with blkio.weight.
// Ref: https://github.com/containers/crun/issues/1157
resourceV1: "blkio.weight",
expectV1: "50",
delegationV2: "io",
resourceV2: "io.bfq.weight",
expectV2: "default 50",
},
{
name: "cpus",
args: []string{"--cpus", "0.5"},
expectErrorCode: 0,
// 0.5 cpus = quota of 50000 with default period 100000
controllerV1: "cpu",
resourceV1: "cpu.cfs_quota_us",
expectV1: "50000",
delegationV2: "cpu",
resourceV2: "cpu.max",
expectV2: "50000 100000",
},
{
name: "cpu-shares",
args: []string{"--cpu-shares", "123"},
expectErrorCode: 0,
controllerV1: "cpu",
resourceV1: "cpu.shares",
expectV1: "123",
// Cgroups v2 has a conversion from shares to weight:
// weight = (1 + ((cpuShares-2)*9999)/262142)
delegationV2: "cpu",
resourceV2: "cpu.weight",
expectV2: "5",
},
{
name: "cpuset-cpus",
args: []string{"--cpuset-cpus", "0", "--cpuset-mems", "0"},
expectErrorCode: 0,
controllerV1: "cpuset",
resourceV1: "cpuset.cpus",
expectV1: "0",
delegationV2: "cpuset",
resourceV2: "cpuset.cpus",
expectV2: "0",
},
{
name: "cpuset-mems",
args: []string{"--cpuset-cpus", "0", "--cpuset-mems", "0"},
expectErrorCode: 0,
controllerV1: "cpuset",
resourceV1: "cpuset.mems",
expectV1: "0",
delegationV2: "cpuset",
resourceV2: "cpuset.mems",
expectV2: "0",
},
{
name: "memory",
args: []string{"--memory", "500M"},
expectErrorCode: 0,
controllerV1: "memory",
resourceV1: "memory.limit_in_bytes",
expectV1: "524288000",
delegationV2: "memory",
resourceV2: "memory.max",
expectV2: "524288000",
},
{
name: "memory-reservation",
args: []string{"--memory-reservation", "500M"},
expectErrorCode: 0,
controllerV1: "memory",
resourceV1: "memory.soft_limit_in_bytes",
expectV1: "524288000",
delegationV2: "memory",
resourceV2: "memory.low",
expectV2: "524288000",
},
{
// The CLI memory-swap value is v1 memory + swap... so this means 250M of swap
name: "memory-swap",
args: []string{"--memory-swap", "500M", "--memory", "250M"},
expectErrorCode: 0,
controllerV1: "memory",
resourceV1: "memory.memsw.limit_in_bytes",
// V1 shows the 500M combined
expectV1: "524288000",
// V2 treats the mem & swap separately... shows only 250M of swap (500M memory-swap - 250M memory)
delegationV2: "memory",
resourceV2: "memory.swap.max",
expectV2: "262144000",
},
{
name: "oom-kill-disable",
args: []string{"--oom-kill-disable"},
expectErrorCode: 0,
controllerV1: "memory",
resourceV1: "memory.oom_control",
expectV1: "oom_kill_disable 1",
// v2 relies on oom_score_adj on /proc/pid instead
skipV2: true,
},
{
name: "pids-limit",
args: []string{"--pids-limit", "123"},
expectErrorCode: 0,
controllerV1: "pids",
resourceV1: "pids.max",
expectV1: "123",
delegationV2: "pids",
resourceV2: "pids.max",
expectV2: "123",
},
}
func (c *ctx) actionFlags(t *testing.T, profile e2e.Profile) {
e2e.EnsureImage(t, c.env)
for _, tt := range resourceFlagTests {
t.Run(tt.name, func(t *testing.T) {
if cgroups.IsCgroup2UnifiedMode() {
c.actionFlagV2(t, tt, profile)
return
}
c.actionFlagV1(t, tt, profile)
})
}
}
func (c *ctx) actionFlagV1(t *testing.T, tt resourceFlagTest, profile e2e.Profile) {
// Don't try to test a resource that doesn't exist in our caller cgroup.
// E.g. some systems don't have memory.memswp, and might not have blkio.bfq
require.CgroupsResourceExists(t, tt.controllerV1, tt.resourceV1)
// Use shell in the container to find container cgroup and cat the value for the tested controller & resource.
// /proc/self/cgroup is : delimited
// controller is the 2nd field in `/proc/self/cgroup`
// cgroup path relative to root cgroup mount is the 3rd field in `/proc/self/cgroup`
shellCmd := fmt.Sprintf("cat /sys/fs/cgroup/%s$(cat /proc/self/cgroup | grep '[,:]%s[,:]' | cut -d ':' -f 3)/%s", tt.controllerV1, tt.controllerV1, tt.resourceV1)
exitFunc := []e2e.ApptainerCmdResultOp{}
if tt.expectV1 != "" {
exitFunc = []e2e.ApptainerCmdResultOp{e2e.ExpectOutput(e2e.ContainMatch, tt.expectV1)}
}
args := tt.args
args = append(args, "-B", "/sys/fs/cgroup", c.env.ImagePath, "/bin/sh", "-c", shellCmd)
c.env.RunApptainer(
t,
e2e.WithProfile(profile),
e2e.WithCommand("exec"),
e2e.WithArgs(args...),
e2e.ExpectExit(tt.expectErrorCode, exitFunc...),
)
}
func (c *ctx) actionFlagV2(t *testing.T, tt resourceFlagTest, profile e2e.Profile) {
if tt.skipV2 {
t.Skip()
}
// Don't try to test a resource that doesn't exist in our caller cgroup.
// E.g. some systems don't have io.bfq.*
require.CgroupsResourceExists(t, "", tt.resourceV2)
// In rootless mode, can only test subsystems that have been delegated
if !profile.Privileged() {
require.CgroupsV2Delegated(t, tt.delegationV2)
}
exitFunc := []e2e.ApptainerCmdResultOp{}
if tt.expectV2 != "" {
exitFunc = []e2e.ApptainerCmdResultOp{e2e.ExpectOutput(e2e.ContainMatch, tt.expectV2)}
}
// Use shell in the container to find container cgroup and cat the value for the tested controller & resource.
// /proc/self/cgroup is : delimited
// For V2 the controller is null (field 2), at index 0 (field 1)
// cgroup path relative to root cgroup mount is the 3rd field in `/proc/self/cgroup`
shellCmd := fmt.Sprintf("cat /sys/fs/cgroup$(cat /proc/self/cgroup | grep '^0::' | cut -d ':' -f 3)/%s", tt.resourceV2)
args := tt.args
args = append(args, "-B", "/sys/fs/cgroup", c.env.ImagePath, "/bin/sh", "-c", shellCmd)
c.env.RunApptainer(
t,
e2e.WithProfile(profile),
e2e.WithCommand("exec"),
e2e.WithArgs(args...),
e2e.ExpectExit(tt.expectErrorCode, exitFunc...),
)
}
func (c *ctx) actionFlagsRoot(t *testing.T) {
c.actionFlags(t, e2e.RootProfile)
}
func (c *ctx) actionFlagsRootless(t *testing.T) {
for _, profile := range []e2e.Profile{e2e.UserProfile, e2e.UserNamespaceProfile, e2e.FakerootProfile} {
t.Run(profile.String(), func(t *testing.T) {
c.actionFlags(t, profile)
})
}
}
func (c *ctx) instanceFlags(t *testing.T, profile e2e.Profile) {
e2e.EnsureImage(t, c.env)
for _, tt := range resourceFlagTests {
t.Run(tt.name, func(t *testing.T) {
if cgroups.IsCgroup2UnifiedMode() {
c.instanceFlagV2(t, tt, profile)
return
}
c.instanceFlagV1(t, tt, profile)
})
}
}
func (c *ctx) instanceFlagV1(t *testing.T, tt resourceFlagTest, profile e2e.Profile) {
// Don't try to test a resource that doesn't exist in our caller cgroup.
// E.g. some systems don't have memory.memswp, and might not have blkio.bfq
require.CgroupsResourceExists(t, tt.controllerV1, tt.resourceV1)
instanceName := randomName(t)
joinName := fmt.Sprintf("instance://%s", instanceName)
startArgs := append(tt.args, "-B", "/sys/fs/cgroup", c.env.ImagePath, instanceName)
c.env.RunApptainer(
t,
e2e.AsSubtest("start"),
e2e.WithProfile(profile),
e2e.WithCommand("instance start"),
e2e.WithArgs(startArgs...),
e2e.ExpectExit(0),
)
// Use shell in the container to find container cgroup and cat the value for the tested controller & resource.
// /proc/self/cgroup is : delimited
// controller is the 2nd field in `/proc/self/cgroup`
// cgroup path relative to root cgroup mount is the 3rd field in `/proc/self/cgroup`
shellCmd := fmt.Sprintf("cat /sys/fs/cgroup/%s$(cat /proc/self/cgroup | grep '[,:]%s[,:]' | cut -d ':' -f 3)/%s", tt.controllerV1, tt.controllerV1, tt.resourceV1)
exitFunc := []e2e.ApptainerCmdResultOp{}
if tt.expectV1 != "" {
exitFunc = []e2e.ApptainerCmdResultOp{e2e.ExpectOutput(e2e.ContainMatch, tt.expectV1)}
}
c.env.RunApptainer(
t,
e2e.AsSubtest("exec"),
e2e.WithProfile(profile),
e2e.WithCommand("exec"),
e2e.WithArgs(joinName, "/bin/sh", "-c", shellCmd),
e2e.WithDir(profile.HostUser(t).Dir),
e2e.ExpectExit(tt.expectErrorCode, exitFunc...),
)
c.env.RunApptainer(
t,
e2e.AsSubtest("stop"),
e2e.WithProfile(profile),
e2e.WithCommand("instance stop"),
e2e.WithArgs(instanceName),
e2e.ExpectExit(0),
)
}
func (c *ctx) instanceFlagV2(t *testing.T, tt resourceFlagTest, profile e2e.Profile) {
if tt.skipV2 {
t.Skip()
}
// Don't try to test a resource that doesn't exist in our caller cgroup.
// E.g. some systems don't have io.bfq.*
require.CgroupsResourceExists(t, "", tt.resourceV2)
// In rootless mode, can only test subsystems that have been delegated
if !profile.Privileged() {
require.CgroupsV2Delegated(t, tt.delegationV2)
}
instanceName := randomName(t)
joinName := fmt.Sprintf("instance://%s", instanceName)
startArgs := append(tt.args, "-B", "/sys/fs/cgroup", c.env.ImagePath, instanceName)
c.env.RunApptainer(
t,
e2e.AsSubtest("start"),
e2e.WithProfile(profile),
e2e.WithCommand("instance start"),
e2e.WithArgs(startArgs...),
e2e.ExpectExit(0),
)
// Use shell in the container to find container cgroup and cat the value for the tested controller & resource.
// /proc/self/cgroup is : delimited
// For V2 the controller is null (field 2), at index 0 (field 1)
// cgroup path relative to root cgroup mount is the 3rd field in `/proc/self/cgroup`
shellCmd := fmt.Sprintf("cat /sys/fs/cgroup$(cat /proc/self/cgroup | grep '^0::' | cut -d ':' -f 3)/%s", tt.resourceV2)
exitFunc := []e2e.ApptainerCmdResultOp{}
if tt.expectV2 != "" {
exitFunc = []e2e.ApptainerCmdResultOp{e2e.ExpectOutput(e2e.ContainMatch, tt.expectV2)}
}
execProfile := profile
if profile.String() == e2e.FakerootProfile.String() {
execProfile = e2e.UserNamespaceProfile
}
c.env.RunApptainer(
t,
e2e.AsSubtest("exec"),
e2e.WithProfile(execProfile),
e2e.WithCommand("exec"),
e2e.WithArgs(joinName, "/bin/sh", "-c", shellCmd),
e2e.WithDir(profile.HostUser(t).Dir),
e2e.ExpectExit(tt.expectErrorCode, exitFunc...),
)
c.env.RunApptainer(
t,
e2e.AsSubtest("stop"),
e2e.WithProfile(profile),
e2e.WithCommand("instance stop"),
e2e.WithArgs(instanceName),
e2e.ExpectExit(0),
)
}
func (c *ctx) instanceFlagsRoot(t *testing.T) {
c.instanceFlags(t, e2e.RootProfile)
}
func (c *ctx) instanceFlagsRootless(t *testing.T) {
for _, profile := range []e2e.Profile{e2e.UserProfile, e2e.UserNamespaceProfile, e2e.FakerootProfile} {
t.Run(profile.String(), func(t *testing.T) {
c.instanceFlags(t, profile)
})
}
}
// On cgroups v2 systems, can run rootless without resource limits with bad
// XDG_RUNTIME_DIR / DBUS_SESSION_BUS_ADDRESS. Cannot run with resource limits
// and bad env vars.
func (c *ctx) actionDbusXDG(t *testing.T) {
e2e.EnsureImage(t, c.env)
require.CgroupsV2Unified(t)
tests := []struct {
name string
args []string
expectErrorCode int
expectErrorOut string
xdgVar string
dbusVar string
}{
{
name: "bad xdg no limits",
args: []string{c.env.ImagePath, "/bin/true"},
expectErrorCode: 0,
xdgVar: "/not/a/dir",
},
{
name: "bad dbus no limits",
args: []string{c.env.ImagePath, "/bin/true"},
expectErrorCode: 0,
dbusVar: "/not/a/dbus/socket",
},
{
name: "bad xdg limits",
args: []string{"--cpus", "1", c.env.ImagePath, "/bin/true"},
expectErrorCode: 255,
expectErrorOut: "XDG_RUNTIME_DIR",
xdgVar: "/not/a/dir",
},
{
name: "bad dbus limits",
args: []string{"--cpus", "1", c.env.ImagePath, "/bin/true"},
expectErrorCode: 255,
expectErrorOut: "DBUS_SESSION_BUS_ADDRESS",
dbusVar: "/not/a/dbus/socket",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for _, profile := range []e2e.Profile{e2e.UserProfile} {
exitFunc := []e2e.ApptainerCmdResultOp{}
if tt.expectErrorOut != "" {
exitFunc = []e2e.ApptainerCmdResultOp{e2e.ExpectError(e2e.ContainMatch, tt.expectErrorOut)}
}
testEnv := []string{}
if tt.xdgVar != "" {
testEnv = append(testEnv, "XDG_RUNTIME_DIR="+tt.xdgVar)
}
if tt.dbusVar != "" {
testEnv = append(testEnv, "DBUS_SESSION_BUS_ADDRESS="+tt.dbusVar)
}
c.env.RunApptainer(
t,
e2e.AsSubtest(profile.String()),
e2e.WithProfile(profile),
e2e.WithCommand("exec"),
e2e.WithArgs(tt.args...),
e2e.WithRootlessEnv(),
e2e.WithEnv(testEnv),
e2e.ExpectExit(tt.expectErrorCode, exitFunc...),
)
}
})
}
}
// On cgroups v2 systems, can run rootless without resource limits with bad
// XDG_RUNTIME_DIR / DBUS_SESSION_BUS_ADDRESS. Cannot run with resource limits
// and bad env vars.
func (c *ctx) instanceDbusXdg(t *testing.T) {
require.CgroupsV2Unified(t)
e2e.EnsureImage(t, c.env)
// All tests require root
tests := []struct {
name string
args []string
expectErrorCode int
expectErrorOut string
xdgVar string
dbusVar string
}{
{
name: "bad xdg no limits",
args: []string{c.env.ImagePath},
expectErrorCode: 0,
xdgVar: "/not/a/dir",
},
{
name: "bad dbus no limits",
args: []string{c.env.ImagePath},
expectErrorCode: 0,
dbusVar: "/not/a/dbus/socket",
},
{
name: "bad xdg limits",
args: []string{"--cpus", "1", c.env.ImagePath},
expectErrorCode: 255,
expectErrorOut: "XDG_RUNTIME_DIR",
xdgVar: "/not/a/dir",
},
{
name: "bad dbus limits",
args: []string{"--cpus", "1", c.env.ImagePath},
expectErrorCode: 255,
expectErrorOut: "DBUS_SESSION_BUS_ADDRESS",
dbusVar: "/not/a/dbus/socket",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
instanceName := randomName(t)
exitFunc := []e2e.ApptainerCmdResultOp{}
if tt.expectErrorOut != "" {
exitFunc = []e2e.ApptainerCmdResultOp{e2e.ExpectError(e2e.ContainMatch, tt.expectErrorOut)}
}
testEnv := []string{}
if tt.xdgVar != "" {
testEnv = append(testEnv, "XDG_RUNTIME_DIR="+tt.xdgVar)
}
if tt.dbusVar != "" {
testEnv = append(testEnv, "DBUS_SESSION_BUS_ADDRESS="+tt.dbusVar)
}
createArgs := append(tt.args, instanceName)
c.env.RunApptainer(
t,
e2e.AsSubtest("start"),
e2e.WithProfile(e2e.UserProfile),
e2e.WithCommand("instance start"),
e2e.WithArgs(createArgs...),
e2e.WithRootlessEnv(),
e2e.WithEnv(testEnv),
e2e.ExpectExit(tt.expectErrorCode, exitFunc...),
)
if tt.expectErrorCode == 0 {
c.env.RunApptainer(
t,
e2e.AsSubtest("stop"),
e2e.WithProfile(e2e.UserProfile),
e2e.WithCommand("instance stop"),
e2e.WithArgs(instanceName),
e2e.ExpectExit(0),
)
}
})
}
}
// E2ETests is the main func to trigger the test suite
func E2ETests(env e2e.TestEnv) testhelper.Tests {
c := &ctx{
env: env,
}
np := testhelper.NoParallel
return testhelper.Tests{
"instance stats root": np(env.WithRootManagers(c.instanceStatsRoot)),
"instance stats rootless": np(env.WithRootlessManagers(c.instanceStatsRootless)),
"instance root cgroups": np(env.WithRootManagers(c.instanceApplyRoot)),
"instance rootless cgroups": np(env.WithRootlessManagers(c.instanceApplyRootless)),
"instance flags root cgroups": np(env.WithRootManagers(c.instanceFlagsRoot)),
"instance flags rootless cgroups": np(env.WithRootlessManagers(c.instanceFlagsRootless)),
"action root cgroups": np(env.WithRootManagers(c.actionApplyRoot)),
"action rootless cgroups": np(env.WithRootlessManagers(c.actionApplyRootless)),
"action flags root cgroups": np(env.WithRootManagers(c.actionFlagsRoot)),
"action flags rootless cgroups": np(env.WithRootlessManagers(c.actionFlagsRootless)),
"action dbus xdg": np(c.actionDbusXDG),
"instance dbus xdg": np(c.instanceDbusXdg),
}
}
|