2014-06-12 16:15:24 +08:00
|
|
|
// +build linux
|
|
|
|
|
2015-01-27 20:54:19 +08:00
|
|
|
package libcontainer
|
2014-06-12 16:15:24 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
2015-01-27 20:54:19 +08:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-06-12 16:15:24 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
2017-05-10 05:38:27 +08:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2014-06-12 16:15:24 +08:00
|
|
|
)
|
|
|
|
|
2015-12-08 23:33:47 +08:00
|
|
|
type notifyFunc func(paths map[string]string) (<-chan struct{}, error)
|
|
|
|
|
|
|
|
func testMemoryNotification(t *testing.T, evName string, notify notifyFunc, targ string) {
|
|
|
|
memoryPath, err := ioutil.TempDir("", "testmemnotification-"+evName)
|
2015-01-27 20:54:19 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-12-08 23:33:47 +08:00
|
|
|
evFile := filepath.Join(memoryPath, evName)
|
2015-01-27 20:54:19 +08:00
|
|
|
eventPath := filepath.Join(memoryPath, "cgroup.event_control")
|
2015-12-08 23:33:47 +08:00
|
|
|
if err := ioutil.WriteFile(evFile, []byte{}, 0700); err != nil {
|
2015-01-27 20:54:19 +08:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(eventPath, []byte{}, 0700); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-02-07 13:12:27 +08:00
|
|
|
paths := map[string]string{
|
|
|
|
"memory": memoryPath,
|
2015-01-27 20:54:19 +08:00
|
|
|
}
|
2015-12-08 23:33:47 +08:00
|
|
|
ch, err := notify(paths)
|
2014-06-12 16:15:24 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("expected no error, got:", err)
|
|
|
|
}
|
|
|
|
|
2015-01-27 20:54:19 +08:00
|
|
|
data, err := ioutil.ReadFile(eventPath)
|
2014-06-12 16:15:24 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("couldn't read event control file:", err)
|
|
|
|
}
|
|
|
|
|
2015-12-08 23:33:47 +08:00
|
|
|
var eventFd, evFd int
|
|
|
|
var arg string
|
|
|
|
if targ != "" {
|
|
|
|
_, err = fmt.Sscanf(string(data), "%d %d %s", &eventFd, &evFd, &arg)
|
|
|
|
} else {
|
|
|
|
_, err = fmt.Sscanf(string(data), "%d %d", &eventFd, &evFd)
|
|
|
|
}
|
|
|
|
if err != nil || arg != targ {
|
2014-06-12 16:15:24 +08:00
|
|
|
t.Fatalf("invalid control data %q: %s", data, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// re-open the eventfd
|
2017-05-10 05:38:27 +08:00
|
|
|
efd, err := unix.Dup(eventFd)
|
2014-06-12 16:15:24 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("unable to reopen eventfd:", err)
|
|
|
|
}
|
2017-05-10 05:38:27 +08:00
|
|
|
defer unix.Close(efd)
|
2014-06-12 16:15:24 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("unable to dup event fd:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(buf, 1)
|
|
|
|
|
2017-05-10 05:38:27 +08:00
|
|
|
if _, err := unix.Write(efd, buf); err != nil {
|
2014-06-12 16:15:24 +08:00
|
|
|
t.Fatal("unable to write to eventfd:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2015-12-08 23:33:47 +08:00
|
|
|
case <-ch:
|
2014-06-12 16:15:24 +08:00
|
|
|
case <-time.After(100 * time.Millisecond):
|
2015-12-08 23:33:47 +08:00
|
|
|
t.Fatal("no notification on channel after 100ms")
|
2014-06-12 16:15:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// simulate what happens when a cgroup is destroyed by cleaning up and then
|
|
|
|
// writing to the eventfd.
|
2015-01-27 20:54:19 +08:00
|
|
|
if err := os.RemoveAll(memoryPath); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-05-10 05:38:27 +08:00
|
|
|
if _, err := unix.Write(efd, buf); err != nil {
|
2014-06-12 16:15:24 +08:00
|
|
|
t.Fatal("unable to write to eventfd:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// give things a moment to shut down
|
|
|
|
select {
|
2015-12-08 23:33:47 +08:00
|
|
|
case _, ok := <-ch:
|
2014-06-12 16:15:24 +08:00
|
|
|
if ok {
|
2015-12-08 23:33:47 +08:00
|
|
|
t.Fatal("expected no notification to be triggered")
|
2014-06-12 16:15:24 +08:00
|
|
|
}
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
}
|
|
|
|
|
2017-05-10 05:38:27 +08:00
|
|
|
if _, _, err := unix.Syscall(unix.SYS_FCNTL, uintptr(evFd), unix.F_GETFD, 0); err != unix.EBADF {
|
|
|
|
t.Errorf("expected event control to be closed, but received error %s", err.Error())
|
2014-06-12 16:15:24 +08:00
|
|
|
}
|
|
|
|
|
2017-05-10 05:38:27 +08:00
|
|
|
if _, _, err := unix.Syscall(unix.SYS_FCNTL, uintptr(eventFd), unix.F_GETFD, 0); err != unix.EBADF {
|
|
|
|
t.Errorf("expected event fd to be closed, but received error %s", err.Error())
|
2014-06-12 16:15:24 +08:00
|
|
|
}
|
|
|
|
}
|
2015-12-08 23:33:47 +08:00
|
|
|
|
|
|
|
func TestNotifyOnOOM(t *testing.T) {
|
|
|
|
f := func(paths map[string]string) (<-chan struct{}, error) {
|
|
|
|
return notifyOnOOM(paths)
|
|
|
|
}
|
|
|
|
|
|
|
|
testMemoryNotification(t, "memory.oom_control", f, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNotifyMemoryPressure(t *testing.T) {
|
|
|
|
tests := map[PressureLevel]string{
|
|
|
|
LowPressure: "low",
|
|
|
|
MediumPressure: "medium",
|
|
|
|
CriticalPressure: "critical",
|
|
|
|
}
|
|
|
|
|
|
|
|
for level, arg := range tests {
|
|
|
|
f := func(paths map[string]string) (<-chan struct{}, error) {
|
|
|
|
return notifyMemoryPressure(paths, level)
|
|
|
|
}
|
|
|
|
|
|
|
|
testMemoryNotification(t, "memory.pressure_level", f, arg)
|
|
|
|
}
|
|
|
|
}
|