Skip to content

Commit 9a1186d

Browse files
author
Mrunal Patel
authored
Merge pull request #1619 from fntlnz/spec-linux-testing
WIP: Better testsuite for specconv
2 parents c9b649d + c0e6e12 commit 9a1186d

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed

libcontainer/specconv/spec_linux_test.go

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,175 @@
33
package specconv
44

55
import (
6+
"os"
67
"testing"
78

89
"github.com/opencontainers/runc/libcontainer/configs/validate"
910
"github.com/opencontainers/runtime-spec/specs-go"
1011
)
1112

13+
func TestLinuxCgroupWithMemoryResource(t *testing.T) {
14+
cgroupsPath := "/user/cgroups/path/id"
15+
16+
spec := &specs.Spec{}
17+
devices := []specs.LinuxDeviceCgroup{
18+
{
19+
Allow: false,
20+
Access: "rwm",
21+
},
22+
}
23+
24+
limit := int64(100)
25+
reservation := int64(50)
26+
swap := int64(20)
27+
kernel := int64(40)
28+
kernelTCP := int64(45)
29+
swappiness := uint64(1)
30+
swappinessPtr := &swappiness
31+
disableOOMKiller := true
32+
resources := &specs.LinuxResources{
33+
Devices: devices,
34+
Memory: &specs.LinuxMemory{
35+
Limit: &limit,
36+
Reservation: &reservation,
37+
Swap: &swap,
38+
Kernel: &kernel,
39+
KernelTCP: &kernelTCP,
40+
Swappiness: swappinessPtr,
41+
DisableOOMKiller: &disableOOMKiller,
42+
},
43+
}
44+
spec.Linux = &specs.Linux{
45+
CgroupsPath: cgroupsPath,
46+
Resources: resources,
47+
}
48+
49+
opts := &CreateOpts{
50+
CgroupName: "ContainerID",
51+
UseSystemdCgroup: false,
52+
Spec: spec,
53+
}
54+
55+
cgroup, err := createCgroupConfig(opts)
56+
if err != nil {
57+
t.Errorf("Couldn't create Cgroup config: %v", err)
58+
}
59+
60+
if cgroup.Path != cgroupsPath {
61+
t.Errorf("Wrong cgroupsPath, expected '%s' got '%s'", cgroupsPath, cgroup.Path)
62+
}
63+
if cgroup.Resources.Memory != limit {
64+
t.Errorf("Expected to have %d as memory limit, got %d", limit, cgroup.Resources.Memory)
65+
}
66+
if cgroup.Resources.MemoryReservation != reservation {
67+
t.Errorf("Expected to have %d as memory reservation, got %d", reservation, cgroup.Resources.MemoryReservation)
68+
}
69+
if cgroup.Resources.MemorySwap != swap {
70+
t.Errorf("Expected to have %d as swap, got %d", swap, cgroup.Resources.MemorySwap)
71+
}
72+
if cgroup.Resources.KernelMemory != kernel {
73+
t.Errorf("Expected to have %d as Kernel Memory, got %d", kernel, cgroup.Resources.KernelMemory)
74+
}
75+
if cgroup.Resources.KernelMemoryTCP != kernelTCP {
76+
t.Errorf("Expected to have %d as TCP Kernel Memory, got %d", kernelTCP, cgroup.Resources.KernelMemoryTCP)
77+
}
78+
if cgroup.Resources.MemorySwappiness != swappinessPtr {
79+
t.Errorf("Expected to have %d as memory swappiness, got %d", swappinessPtr, cgroup.Resources.MemorySwappiness)
80+
}
81+
if cgroup.Resources.OomKillDisable != disableOOMKiller {
82+
t.Errorf("The OOMKiller should be enabled")
83+
}
84+
}
85+
86+
func TestLinuxCgroupSystemd(t *testing.T) {
87+
cgroupsPath := "parent:scopeprefix:name"
88+
89+
spec := &specs.Spec{}
90+
spec.Linux = &specs.Linux{
91+
CgroupsPath: cgroupsPath,
92+
}
93+
94+
opts := &CreateOpts{
95+
UseSystemdCgroup: true,
96+
Spec: spec,
97+
}
98+
99+
cgroup, err := createCgroupConfig(opts)
100+
101+
if err != nil {
102+
t.Errorf("Couldn't create Cgroup config: %v", err)
103+
}
104+
105+
expectedParent := "parent"
106+
if cgroup.Parent != expectedParent {
107+
t.Errorf("Expected to have %s as Parent instead of %s", expectedParent, cgroup.Parent)
108+
}
109+
110+
expectedScopePrefix := "scopeprefix"
111+
if cgroup.ScopePrefix != expectedScopePrefix {
112+
t.Errorf("Expected to have %s as ScopePrefix instead of %s", expectedScopePrefix, cgroup.ScopePrefix)
113+
}
114+
115+
expectedName := "name"
116+
if cgroup.Name != expectedName {
117+
t.Errorf("Expected to have %s as Name instead of %s", expectedName, cgroup.Name)
118+
}
119+
}
120+
121+
func TestLinuxCgroupSystemdWithEmptyPath(t *testing.T) {
122+
cgroupsPath := ""
123+
124+
spec := &specs.Spec{}
125+
spec.Linux = &specs.Linux{
126+
CgroupsPath: cgroupsPath,
127+
}
128+
129+
opts := &CreateOpts{
130+
CgroupName: "ContainerID",
131+
UseSystemdCgroup: true,
132+
Spec: spec,
133+
}
134+
135+
cgroup, err := createCgroupConfig(opts)
136+
137+
if err != nil {
138+
t.Errorf("Couldn't create Cgroup config: %v", err)
139+
}
140+
141+
expectedParent := "system.slice"
142+
if cgroup.Parent != expectedParent {
143+
t.Errorf("Expected to have %s as Parent instead of %s", expectedParent, cgroup.Parent)
144+
}
145+
146+
expectedScopePrefix := "runc"
147+
if cgroup.ScopePrefix != expectedScopePrefix {
148+
t.Errorf("Expected to have %s as ScopePrefix instead of %s", expectedScopePrefix, cgroup.ScopePrefix)
149+
}
150+
151+
if cgroup.Name != opts.CgroupName {
152+
t.Errorf("Expected to have %s as Name instead of %s", opts.CgroupName, cgroup.Name)
153+
}
154+
}
155+
156+
func TestLinuxCgroupSystemdWithInvalidPath(t *testing.T) {
157+
cgroupsPath := "/user/cgroups/path/id"
158+
159+
spec := &specs.Spec{}
160+
spec.Linux = &specs.Linux{
161+
CgroupsPath: cgroupsPath,
162+
}
163+
164+
opts := &CreateOpts{
165+
CgroupName: "ContainerID",
166+
UseSystemdCgroup: true,
167+
Spec: spec,
168+
}
169+
170+
_, err := createCgroupConfig(opts)
171+
if err == nil {
172+
t.Error("Expected to produce an error if not using the correct format for cgroup paths belonging to systemd")
173+
}
174+
}
12175
func TestLinuxCgroupsPathSpecified(t *testing.T) {
13176
cgroupsPath := "/user/cgroups/path/id"
14177

@@ -97,6 +260,10 @@ func TestDupNamespaces(t *testing.T) {
97260
}
98261

99262
func TestRootlessSpecconvValidate(t *testing.T) {
263+
if _, err := os.Stat("/proc/self/ns/user"); os.IsNotExist(err) {
264+
t.Skip("userns is unsupported")
265+
}
266+
100267
spec := Example()
101268
spec.Root.Path = "/"
102269
ToRootless(spec)

0 commit comments

Comments
 (0)