summaryrefslogtreecommitdiff
path: root/pkg/rootless/rootless_test.go
blob: ef574099cade38176d5e06141978d97b1073588e (plain)
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
package rootless

import (
	"reflect"
	"testing"

	"github.com/opencontainers/runc/libcontainer/user"
	spec "github.com/opencontainers/runtime-spec/specs-go"
)

func TestMaybeSplitMappings(t *testing.T) {
	mappings := []spec.LinuxIDMapping{
		{
			ContainerID: 0,
			HostID:      0,
			Size:        2,
		},
	}
	desiredMappings := []spec.LinuxIDMapping{
		{
			ContainerID: 0,
			HostID:      0,
			Size:        1,
		},
		{
			ContainerID: 1,
			HostID:      1,
			Size:        1,
		},
	}
	availableMappings := []user.IDMap{
		{
			ID:       1,
			ParentID: 1000000,
			Count:    65536,
		},
		{
			ID:       0,
			ParentID: 1000,
			Count:    1,
		},
	}
	newMappings := MaybeSplitMappings(mappings, availableMappings)
	if !reflect.DeepEqual(newMappings, desiredMappings) {
		t.Fatal("wrong mappings generated")
	}

	mappings = []spec.LinuxIDMapping{
		{
			ContainerID: 0,
			HostID:      0,
			Size:        2,
		},
	}
	desiredMappings = []spec.LinuxIDMapping{
		{
			ContainerID: 0,
			HostID:      0,
			Size:        2,
		},
	}
	availableMappings = []user.IDMap{
		{
			ID:       0,
			ParentID: 1000000,
			Count:    65536,
		},
	}
	newMappings = MaybeSplitMappings(mappings, availableMappings)

	if !reflect.DeepEqual(newMappings, desiredMappings) {
		t.Fatal("wrong mappings generated")
	}

	mappings = []spec.LinuxIDMapping{
		{
			ContainerID: 0,
			HostID:      0,
			Size:        1,
		},
	}
	desiredMappings = []spec.LinuxIDMapping{
		{
			ContainerID: 0,
			HostID:      0,
			Size:        1,
		},
	}
	availableMappings = []user.IDMap{
		{
			ID:       10000,
			ParentID: 10000,
			Count:    65536,
		},
	}

	newMappings = MaybeSplitMappings(mappings, availableMappings)
	if !reflect.DeepEqual(newMappings, desiredMappings) {
		t.Fatal("wrong mappings generated")
	}
}