cgroup_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package worker
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. "strconv"
  7. "strings"
  8. "testing"
  9. "time"
  10. units "github.com/docker/go-units"
  11. . "github.com/smartystreets/goconvey/convey"
  12. )
  13. func TestCgroup(t *testing.T) {
  14. Convey("Cgroup Should Work", t, func(ctx C) {
  15. tmpDir, err := ioutil.TempDir("", "tunasync")
  16. defer os.RemoveAll(tmpDir)
  17. So(err, ShouldBeNil)
  18. cmdScript := filepath.Join(tmpDir, "cmd.sh")
  19. daemonScript := filepath.Join(tmpDir, "daemon.sh")
  20. tmpFile := filepath.Join(tmpDir, "log_file")
  21. bgPidfile := filepath.Join(tmpDir, "bg.pid")
  22. c := cmdConfig{
  23. name: "tuna-cgroup",
  24. upstreamURL: "http://mirrors.tuna.moe/",
  25. command: cmdScript + " " + daemonScript,
  26. workingDir: tmpDir,
  27. logDir: tmpDir,
  28. logFile: tmpFile,
  29. interval: 600 * time.Second,
  30. env: map[string]string{
  31. "BG_PIDFILE": bgPidfile,
  32. },
  33. }
  34. cmdScriptContent := `#!/bin/bash
  35. redirect-std() {
  36. [[ -t 0 ]] && exec </dev/null
  37. [[ -t 1 ]] && exec >/dev/null
  38. [[ -t 2 ]] && exec 2>/dev/null
  39. }
  40. # close all non-std* fds
  41. close-fds() {
  42. eval exec {3..255}\>\&-
  43. }
  44. # full daemonization of external command with setsid
  45. daemonize() {
  46. (
  47. redirect-std
  48. cd /
  49. close-fds
  50. exec setsid "$@"
  51. ) &
  52. }
  53. echo $$
  54. daemonize $@
  55. sleep 5
  56. `
  57. daemonScriptContent := `#!/bin/bash
  58. echo $$ > $BG_PIDFILE
  59. sleep 30
  60. `
  61. err = ioutil.WriteFile(cmdScript, []byte(cmdScriptContent), 0755)
  62. So(err, ShouldBeNil)
  63. err = ioutil.WriteFile(daemonScript, []byte(daemonScriptContent), 0755)
  64. So(err, ShouldBeNil)
  65. provider, err := newCmdProvider(c)
  66. So(err, ShouldBeNil)
  67. cgcf := cgroupConfig{BasePath: "/sys/fs/cgroup", Group: "tunasync", Subsystem: "cpu"}
  68. cg := newCgroupHook(provider, cgcf, 0)
  69. provider.AddHook(cg)
  70. err = cg.preExec()
  71. if err != nil {
  72. logger.Errorf("Failed to create cgroup")
  73. return
  74. }
  75. So(err, ShouldBeNil)
  76. go func() {
  77. err := provider.Run(make(chan empty, 1))
  78. ctx.So(err, ShouldNotBeNil)
  79. }()
  80. time.Sleep(1 * time.Second)
  81. // Deamon should be started
  82. daemonPidBytes, err := ioutil.ReadFile(bgPidfile)
  83. So(err, ShouldBeNil)
  84. daemonPid := strings.Trim(string(daemonPidBytes), " \n")
  85. logger.Debug("daemon pid: %s", daemonPid)
  86. procDir := filepath.Join("/proc", daemonPid)
  87. _, err = os.Stat(procDir)
  88. So(err, ShouldBeNil)
  89. err = provider.Terminate()
  90. So(err, ShouldBeNil)
  91. // Deamon won't be killed
  92. _, err = os.Stat(procDir)
  93. So(err, ShouldBeNil)
  94. // Deamon can be killed by cgroup killer
  95. cg.postExec()
  96. _, err = os.Stat(procDir)
  97. So(os.IsNotExist(err), ShouldBeTrue)
  98. })
  99. Convey("Rsync Memory Should Be Limited", t, func() {
  100. tmpDir, err := ioutil.TempDir("", "tunasync")
  101. defer os.RemoveAll(tmpDir)
  102. So(err, ShouldBeNil)
  103. scriptFile := filepath.Join(tmpDir, "myrsync")
  104. tmpFile := filepath.Join(tmpDir, "log_file")
  105. c := rsyncConfig{
  106. name: "tuna-cgroup",
  107. upstreamURL: "rsync://rsync.tuna.moe/tuna/",
  108. rsyncCmd: scriptFile,
  109. workingDir: tmpDir,
  110. logDir: tmpDir,
  111. logFile: tmpFile,
  112. useIPv6: true,
  113. interval: 600 * time.Second,
  114. }
  115. provider, err := newRsyncProvider(c)
  116. So(err, ShouldBeNil)
  117. cgcf := cgroupConfig{BasePath: "/sys/fs/cgroup", Group: "tunasync", Subsystem: "cpu"}
  118. cg := newCgroupHook(provider, cgcf, 512 * units.MiB)
  119. provider.AddHook(cg)
  120. err = cg.preExec()
  121. if err != nil {
  122. logger.Errorf("Failed to create cgroup")
  123. return
  124. }
  125. if cg.subsystem == "memory" {
  126. memoLimit, err := ioutil.ReadFile(filepath.Join(cg.basePath, "memory", cg.baseGroup, provider.Name(), "memory.limit_in_bytes"))
  127. So(err, ShouldBeNil)
  128. So(strings.Trim(string(memoLimit), "\n"), ShouldEqual, strconv.Itoa(512*1024*1024))
  129. }
  130. cg.postExec()
  131. })
  132. }