cgroup_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. cg := newCgroupHook(provider, "/sys/fs/cgroup", "tunasync", "cpu", 0)
  68. provider.AddHook(cg)
  69. err = cg.preExec()
  70. if err != nil {
  71. logger.Errorf("Failed to create cgroup")
  72. return
  73. }
  74. So(err, ShouldBeNil)
  75. go func() {
  76. err := provider.Run(make(chan empty, 1))
  77. ctx.So(err, ShouldNotBeNil)
  78. }()
  79. time.Sleep(1 * time.Second)
  80. // Deamon should be started
  81. daemonPidBytes, err := ioutil.ReadFile(bgPidfile)
  82. So(err, ShouldBeNil)
  83. daemonPid := strings.Trim(string(daemonPidBytes), " \n")
  84. logger.Debug("daemon pid: %s", daemonPid)
  85. procDir := filepath.Join("/proc", daemonPid)
  86. _, err = os.Stat(procDir)
  87. So(err, ShouldBeNil)
  88. err = provider.Terminate()
  89. So(err, ShouldBeNil)
  90. // Deamon won't be killed
  91. _, err = os.Stat(procDir)
  92. So(err, ShouldBeNil)
  93. // Deamon can be killed by cgroup killer
  94. cg.postExec()
  95. _, err = os.Stat(procDir)
  96. So(os.IsNotExist(err), ShouldBeTrue)
  97. })
  98. Convey("Rsync Memory Should Be Limited", t, func() {
  99. tmpDir, err := ioutil.TempDir("", "tunasync")
  100. defer os.RemoveAll(tmpDir)
  101. So(err, ShouldBeNil)
  102. scriptFile := filepath.Join(tmpDir, "myrsync")
  103. tmpFile := filepath.Join(tmpDir, "log_file")
  104. c := rsyncConfig{
  105. name: "tuna-cgroup",
  106. upstreamURL: "rsync://rsync.tuna.moe/tuna/",
  107. rsyncCmd: scriptFile,
  108. workingDir: tmpDir,
  109. logDir: tmpDir,
  110. logFile: tmpFile,
  111. useIPv6: true,
  112. interval: 600 * time.Second,
  113. }
  114. provider, err := newRsyncProvider(c)
  115. So(err, ShouldBeNil)
  116. cg := newCgroupHook(provider, "/sys/fs/cgroup", "tunasync", "cpu", 512 * units.MiB)
  117. provider.AddHook(cg)
  118. err = cg.preExec()
  119. if err != nil {
  120. logger.Errorf("Failed to create cgroup")
  121. return
  122. }
  123. if cg.subsystem == "memory" {
  124. memoLimit, err := ioutil.ReadFile(filepath.Join(cg.basePath, "memory", cg.baseGroup, provider.Name(), "memory.limit_in_bytes"))
  125. So(err, ShouldBeNil)
  126. So(strings.Trim(string(memoLimit), "\n"), ShouldEqual, strconv.Itoa(512*1024*1024))
  127. }
  128. cg.postExec()
  129. })
  130. }