github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/spec/spec_test.go (about)

     1  package createconfig
     2  
     3  import (
     4  	"runtime"
     5  	"testing"
     6  
     7  	"github.com/containers/common/pkg/sysinfo"
     8  	"github.com/containers/podman/v2/pkg/cgroups"
     9  	"github.com/containers/podman/v2/pkg/rootless"
    10  	"github.com/containers/storage"
    11  	"github.com/containers/storage/pkg/idtools"
    12  	"github.com/docker/go-units"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  var (
    17  	sysInfo = sysinfo.New(true)
    18  )
    19  
    20  // Make createconfig to test with
    21  func makeTestCreateConfig() *CreateConfig {
    22  	cc := new(CreateConfig)
    23  	cc.Resources = CreateResourceConfig{}
    24  	cc.User.IDMappings = new(storage.IDMappingOptions)
    25  	cc.User.IDMappings.UIDMap = []idtools.IDMap{}
    26  	cc.User.IDMappings.GIDMap = []idtools.IDMap{}
    27  
    28  	return cc
    29  }
    30  
    31  func doCommonSkipChecks(t *testing.T) {
    32  	// The default configuration of podman enables seccomp, which is not available on non-Linux systems.
    33  	// Thus, any tests that use the default seccomp setting would fail.
    34  	// Skip the tests on non-Linux platforms rather than explicitly disable seccomp in the test and possibly affect the test result.
    35  	if runtime.GOOS != "linux" {
    36  		t.Skip("seccomp, which is enabled by default, is only supported on Linux")
    37  	}
    38  
    39  	if rootless.IsRootless() {
    40  		isCgroupV2, err := cgroups.IsCgroup2UnifiedMode()
    41  		if err != nil {
    42  			t.Errorf("unexpected error: %v", err)
    43  		}
    44  
    45  		if !isCgroupV2 {
    46  			t.Skip("cgroups v1 cannot be used when rootless")
    47  		}
    48  	}
    49  }
    50  
    51  // TestPIDsLimit verifies the given pid-limit is correctly defined in the spec
    52  func TestPIDsLimit(t *testing.T) {
    53  	doCommonSkipChecks(t)
    54  
    55  	if !sysInfo.PidsLimit {
    56  		t.Skip("running test not supported by the host system")
    57  	}
    58  
    59  	cc := makeTestCreateConfig()
    60  	cc.Resources.PidsLimit = 22
    61  
    62  	spec, err := cc.createConfigToOCISpec(nil, nil)
    63  	assert.NoError(t, err)
    64  
    65  	assert.Equal(t, spec.Linux.Resources.Pids.Limit, int64(22))
    66  }
    67  
    68  // TestBLKIOWeightDevice verifies the given blkio weight is correctly set in the
    69  // spec.
    70  func TestBLKIOWeightDevice(t *testing.T) {
    71  	doCommonSkipChecks(t)
    72  
    73  	if !sysInfo.BlkioWeightDevice {
    74  		t.Skip("running test not supported by the host system")
    75  	}
    76  
    77  	cc := makeTestCreateConfig()
    78  	cc.Resources.BlkioWeightDevice = []string{"/dev/zero:100"}
    79  
    80  	spec, err := cc.createConfigToOCISpec(nil, nil)
    81  	assert.NoError(t, err)
    82  
    83  	// /dev/zero is guaranteed 1,5 by the Linux kernel
    84  	assert.Equal(t, spec.Linux.Resources.BlockIO.WeightDevice[0].Major, int64(1))
    85  	assert.Equal(t, spec.Linux.Resources.BlockIO.WeightDevice[0].Minor, int64(5))
    86  	assert.Equal(t, *(spec.Linux.Resources.BlockIO.WeightDevice[0].Weight), uint16(100))
    87  }
    88  
    89  // TestMemorySwap verifies that the given swap memory limit is correctly set in
    90  // the spec.
    91  func TestMemorySwap(t *testing.T) {
    92  	doCommonSkipChecks(t)
    93  
    94  	if !sysInfo.SwapLimit {
    95  		t.Skip("running test not supported by the host system")
    96  	}
    97  
    98  	swapLimit, err := units.RAMInBytes("45m")
    99  	assert.NoError(t, err)
   100  
   101  	cc := makeTestCreateConfig()
   102  	cc.Resources.MemorySwap = swapLimit
   103  
   104  	spec, err := cc.createConfigToOCISpec(nil, nil)
   105  	assert.NoError(t, err)
   106  
   107  	assert.Equal(t, *(spec.Linux.Resources.Memory.Swap), swapLimit)
   108  }