Skip to content

Commit 2d0a06d

Browse files
authored
Merge pull request containerd#3146 from Ace-Tang/add-test
test: add custom cgroup test
2 parents e7b6fea + f7f6dd7 commit 2d0a06d

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

daemon_config_linux_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
package containerd
1818

1919
import (
20+
"bufio"
2021
"bytes"
2122
"context"
23+
"fmt"
2224
"io/ioutil"
2325
"os"
2426
"os/exec"
2527
"path/filepath"
28+
"strings"
2629
"syscall"
2730
"testing"
2831
"time"
@@ -170,3 +173,81 @@ func TestDaemonRuntimeRoot(t *testing.T) {
170173
}
171174
<-status
172175
}
176+
177+
// code most copy from https://github.com/opencontainers/runc
178+
func getCgroupPath() (map[string]string, error) {
179+
cgroupPath := make(map[string]string)
180+
f, err := os.Open("/proc/self/mountinfo")
181+
if err != nil {
182+
return nil, err
183+
}
184+
defer f.Close()
185+
186+
scanner := bufio.NewScanner(f)
187+
for scanner.Scan() {
188+
text := scanner.Text()
189+
fields := strings.Split(text, " ")
190+
// Safe as mountinfo encodes mountpoints with spaces as \040.
191+
index := strings.Index(text, " - ")
192+
postSeparatorFields := strings.Fields(text[index+3:])
193+
numPostFields := len(postSeparatorFields)
194+
195+
// This is an error as we can't detect if the mount is for "cgroup"
196+
if numPostFields == 0 {
197+
continue
198+
}
199+
200+
if postSeparatorFields[0] == "cgroup" {
201+
// Check that the mount is properly formatted.
202+
if numPostFields < 3 {
203+
continue
204+
}
205+
cgroupPath[filepath.Base(fields[4])] = fields[4]
206+
}
207+
}
208+
209+
return cgroupPath, nil
210+
}
211+
212+
// TestDaemonCustomCgroup ensures plugin.cgroup.path is not ignored
213+
func TestDaemonCustomCgroup(t *testing.T) {
214+
cgroupPath, err := getCgroupPath()
215+
if err != nil {
216+
t.Fatal(err)
217+
}
218+
if len(cgroupPath) == 0 {
219+
t.Skip("skip TestDaemonCustomCgroup since no cgroup path available")
220+
}
221+
222+
customCgroup := fmt.Sprintf("%d", time.Now().Nanosecond())
223+
configTOML := `
224+
[cgroup]
225+
path = "` + customCgroup + `"`
226+
227+
_, _, cleanup := newDaemonWithConfig(t, configTOML)
228+
229+
defer func() {
230+
// do cgroup path clean
231+
for _, v := range cgroupPath {
232+
if _, err := os.Stat(filepath.Join(v, customCgroup)); err == nil {
233+
if err := os.RemoveAll(filepath.Join(v, customCgroup)); err != nil {
234+
t.Logf("failed to remove cgroup path %s", filepath.Join(v, customCgroup))
235+
}
236+
}
237+
}
238+
}()
239+
240+
defer cleanup()
241+
242+
for k, v := range cgroupPath {
243+
if k == "rmda" {
244+
continue
245+
}
246+
path := filepath.Join(v, customCgroup)
247+
if _, err := os.Stat(path); err != nil {
248+
if os.IsNotExist(err) {
249+
t.Fatalf("custom cgroup path %s should exist, actually not", path)
250+
}
251+
}
252+
}
253+
}

0 commit comments

Comments
 (0)