(t *testing.T)
| 87 | } |
| 88 | |
| 89 | func TestCgroup(t *testing.T) { |
| 90 | var cgcf *cgroupConfig |
| 91 | Convey("init cgroup", t, func(ctx C) { |
| 92 | _, useCurrentCgroup := os.LookupEnv("USECURCGROUP") |
| 93 | cgcf = &cgroupConfig{BasePath: "/sys/fs/cgroup", Group: "tunasync", Subsystem: "cpu"} |
| 94 | if useCurrentCgroup { |
| 95 | cgcf.Group = "" |
| 96 | } |
| 97 | err := initCgroup(cgcf) |
| 98 | So(err, ShouldBeNil) |
| 99 | if cgcf.isUnified { |
| 100 | So(cgcf.cgMgrV2, ShouldNotBeNil) |
| 101 | } else { |
| 102 | So(cgcf.cgMgrV1, ShouldNotBeNil) |
| 103 | } |
| 104 | |
| 105 | Convey("Cgroup Should Work", func(ctx C) { |
| 106 | tmpDir, err := os.MkdirTemp("", "tunasync") |
| 107 | defer os.RemoveAll(tmpDir) |
| 108 | So(err, ShouldBeNil) |
| 109 | cmdScript := filepath.Join(tmpDir, "cmd.sh") |
| 110 | daemonScript := filepath.Join(tmpDir, "daemon.sh") |
| 111 | tmpFile := filepath.Join(tmpDir, "log_file") |
| 112 | bgPidfile := filepath.Join(tmpDir, "bg.pid") |
| 113 | |
| 114 | c := cmdConfig{ |
| 115 | name: "tuna-cgroup", |
| 116 | upstreamURL: "http://mirrors.tuna.moe/", |
| 117 | command: cmdScript + " " + daemonScript, |
| 118 | workingDir: tmpDir, |
| 119 | logDir: tmpDir, |
| 120 | logFile: tmpFile, |
| 121 | interval: 600 * time.Second, |
| 122 | env: map[string]string{ |
| 123 | "BG_PIDFILE": bgPidfile, |
| 124 | }, |
| 125 | } |
| 126 | cmdScriptContent := `#!/bin/bash |
| 127 | redirect-std() { |
| 128 | [[ -t 0 ]] && exec </dev/null |
| 129 | [[ -t 1 ]] && exec >/dev/null |
| 130 | [[ -t 2 ]] && exec 2>/dev/null |
| 131 | } |
| 132 | |
| 133 | # close all non-std* fds |
| 134 | close-fds() { |
| 135 | eval exec {3..255}\>\&- |
| 136 | } |
| 137 | |
| 138 | # full daemonization of external command with setsid |
| 139 | daemonize() { |
| 140 | ( |
| 141 | redirect-std |
| 142 | cd / |
| 143 | close-fds |
| 144 | exec setsid "$@" |
| 145 | ) & |
| 146 | } |
nothing calls this directly
no test coverage detected