github.com/opencontainers/runc@v1.2.0-rc.1.0.20240520010911-492dc558cdd6/libcontainer/userns/usernsfd_linux_test.go (about)

     1  package userns
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/opencontainers/runc/libcontainer/configs"
     8  )
     9  
    10  func BenchmarkSpawnProc(b *testing.B) {
    11  	if os.Geteuid() != 0 {
    12  		b.Skip("spawning user namespaced processes requires root")
    13  	}
    14  
    15  	// We can reuse the mapping as we call spawnProc() directly.
    16  	mapping := Mapping{
    17  		UIDMappings: []configs.IDMap{
    18  			{ContainerID: 0, HostID: 1337, Size: 142},
    19  			{ContainerID: 150, HostID: 0, Size: 1},
    20  			{ContainerID: 442, HostID: 1111, Size: 12},
    21  			{ContainerID: 1000, HostID: 9999, Size: 92},
    22  			{ContainerID: 9999, HostID: 1000000, Size: 4},
    23  			// Newer kernels support more than 5 entries, but stick to 5 here.
    24  		},
    25  		GIDMappings: []configs.IDMap{
    26  			{ContainerID: 1, HostID: 2337, Size: 142},
    27  			{ContainerID: 145, HostID: 6, Size: 1},
    28  			{ContainerID: 200, HostID: 1000, Size: 12},
    29  			{ContainerID: 1000, HostID: 9888, Size: 92},
    30  			{ContainerID: 8999, HostID: 1000000, Size: 4},
    31  			// Newer kernels support more than 5 entries, but stick to 5 here.
    32  		},
    33  	}
    34  
    35  	procs := make([]*os.Process, 0, b.N)
    36  	b.Cleanup(func() {
    37  		for _, proc := range procs {
    38  			if proc != nil {
    39  				_ = proc.Kill()
    40  				_, _ = proc.Wait()
    41  			}
    42  		}
    43  	})
    44  
    45  	for i := 0; i < b.N; i++ {
    46  		proc, err := spawnProc(mapping)
    47  		if err != nil {
    48  			b.Error(err)
    49  		}
    50  		procs = append(procs, proc)
    51  	}
    52  }