github.com/containers/podman/v4@v4.9.4/pkg/rootless/rootless_test.go (about)

     1  package rootless
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/opencontainers/runc/libcontainer/user"
     8  	spec "github.com/opencontainers/runtime-spec/specs-go"
     9  )
    10  
    11  func TestMaybeSplitMappings(t *testing.T) {
    12  	mappings := []spec.LinuxIDMapping{
    13  		{
    14  			ContainerID: 0,
    15  			HostID:      0,
    16  			Size:        2,
    17  		},
    18  	}
    19  	desiredMappings := []spec.LinuxIDMapping{
    20  		{
    21  			ContainerID: 0,
    22  			HostID:      0,
    23  			Size:        1,
    24  		},
    25  		{
    26  			ContainerID: 1,
    27  			HostID:      1,
    28  			Size:        1,
    29  		},
    30  	}
    31  	availableMappings := []user.IDMap{
    32  		{
    33  			ID:       1,
    34  			ParentID: 1000000,
    35  			Count:    65536,
    36  		},
    37  		{
    38  			ID:       0,
    39  			ParentID: 1000,
    40  			Count:    1,
    41  		},
    42  	}
    43  	newMappings := MaybeSplitMappings(mappings, availableMappings)
    44  	if !reflect.DeepEqual(newMappings, desiredMappings) {
    45  		t.Fatal("wrong mappings generated")
    46  	}
    47  
    48  	mappings = []spec.LinuxIDMapping{
    49  		{
    50  			ContainerID: 0,
    51  			HostID:      0,
    52  			Size:        2,
    53  		},
    54  	}
    55  	desiredMappings = []spec.LinuxIDMapping{
    56  		{
    57  			ContainerID: 0,
    58  			HostID:      0,
    59  			Size:        2,
    60  		},
    61  	}
    62  	availableMappings = []user.IDMap{
    63  		{
    64  			ID:       0,
    65  			ParentID: 1000000,
    66  			Count:    65536,
    67  		},
    68  	}
    69  	newMappings = MaybeSplitMappings(mappings, availableMappings)
    70  
    71  	if !reflect.DeepEqual(newMappings, desiredMappings) {
    72  		t.Fatal("wrong mappings generated")
    73  	}
    74  
    75  	mappings = []spec.LinuxIDMapping{
    76  		{
    77  			ContainerID: 0,
    78  			HostID:      0,
    79  			Size:        1,
    80  		},
    81  	}
    82  	desiredMappings = []spec.LinuxIDMapping{
    83  		{
    84  			ContainerID: 0,
    85  			HostID:      0,
    86  			Size:        1,
    87  		},
    88  	}
    89  	availableMappings = []user.IDMap{
    90  		{
    91  			ID:       10000,
    92  			ParentID: 10000,
    93  			Count:    65536,
    94  		},
    95  	}
    96  
    97  	newMappings = MaybeSplitMappings(mappings, availableMappings)
    98  	if !reflect.DeepEqual(newMappings, desiredMappings) {
    99  		t.Fatal("wrong mappings generated")
   100  	}
   101  
   102  	mappings = []spec.LinuxIDMapping{
   103  		{
   104  			ContainerID: 0,
   105  			HostID:      0,
   106  			Size:        4,
   107  		},
   108  	}
   109  	desiredMappings = []spec.LinuxIDMapping{
   110  		{
   111  			ContainerID: 0,
   112  			HostID:      0,
   113  			Size:        1,
   114  		},
   115  		{
   116  			ContainerID: 1,
   117  			HostID:      1,
   118  			Size:        1,
   119  		},
   120  		{
   121  			ContainerID: 2,
   122  			HostID:      2,
   123  			Size:        1,
   124  		},
   125  		{
   126  			ContainerID: 3,
   127  			HostID:      3,
   128  			Size:        1,
   129  		},
   130  	}
   131  	availableMappings = []user.IDMap{
   132  		{
   133  			ID:       0,
   134  			ParentID: 0,
   135  			Count:    1,
   136  		},
   137  		{
   138  			ID:       1,
   139  			ParentID: 1,
   140  			Count:    1,
   141  		},
   142  		{
   143  			ID:       2,
   144  			ParentID: 2,
   145  			Count:    1,
   146  		},
   147  		{
   148  			ID:       3,
   149  			ParentID: 3,
   150  			Count:    1,
   151  		},
   152  	}
   153  
   154  	newMappings = MaybeSplitMappings(mappings, availableMappings)
   155  	if !reflect.DeepEqual(newMappings, desiredMappings) {
   156  		t.Fatal("wrong mappings generated")
   157  	}
   158  }