镜像自地址
https://github.com/tuna/tunasync.git
已同步 2025-12-08 07:26:47 +00:00
比较提交
4 次代码提交
| 作者 | SHA1 | 提交日期 | |
|---|---|---|---|
|
|
fddb793ca1 | ||
|
|
c41d7a4038 | ||
|
|
8b0ef2bb53 | ||
|
|
b25be80670 |
7
.github/workflows/tunasync.yml
vendored
7
.github/workflows/tunasync.yml
vendored
@@ -36,10 +36,9 @@ jobs:
|
|||||||
|
|
||||||
- name: Setup test dependencies
|
- name: Setup test dependencies
|
||||||
run: |
|
run: |
|
||||||
sudo apt update
|
sudo apt-get update
|
||||||
sudo apt install -y cgroup-bin
|
sudo apt-get install -y cgroup-bin
|
||||||
/usr/bin/docker pull alpine
|
docker pull alpine:3.8
|
||||||
/usr/bin/docker images
|
|
||||||
lssubsys -am
|
lssubsys -am
|
||||||
sudo cgcreate -a $USER -t $USER -g cpu:tunasync
|
sudo cgcreate -a $USER -t $USER -g cpu:tunasync
|
||||||
sudo cgcreate -a $USER -t $USER -g memory:tunasync
|
sudo cgcreate -a $USER -t $USER -g memory:tunasync
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
package internal
|
package internal
|
||||||
|
|
||||||
const Version string = "0.4.1"
|
const Version string = "0.4.2"
|
||||||
|
|||||||
127
worker/docker_test.go
普通文件
127
worker/docker_test.go
普通文件
@@ -0,0 +1,127 @@
|
|||||||
|
package worker
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/codeskyblue/go-sh"
|
||||||
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
|
)
|
||||||
|
|
||||||
|
func cmdRun(p string, args []string) {
|
||||||
|
cmd := exec.Command(p, args...)
|
||||||
|
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
logger.Debugf("cmdRun failed %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
logger.Debugf("cmdRun: ", string(out))
|
||||||
|
}
|
||||||
|
|
||||||
|
func getDockerByName(name string) (string, error) {
|
||||||
|
// docker ps -f 'name=$name' --format '{{.Names}}'
|
||||||
|
out, err := sh.Command(
|
||||||
|
"docker", "ps", "-a",
|
||||||
|
"--filter", "name="+name,
|
||||||
|
"--format", "{{.Names}}",
|
||||||
|
).Output()
|
||||||
|
if err == nil {
|
||||||
|
logger.Debugf("docker ps: '%s'", string(out))
|
||||||
|
}
|
||||||
|
return string(out), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDocker(t *testing.T) {
|
||||||
|
Convey("Docker Should Work", t, func(ctx C) {
|
||||||
|
tmpDir, err := ioutil.TempDir("", "tunasync")
|
||||||
|
defer os.RemoveAll(tmpDir)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
cmdScript := filepath.Join(tmpDir, "cmd.sh")
|
||||||
|
tmpFile := filepath.Join(tmpDir, "log_file")
|
||||||
|
expectedOutput := "HELLO_WORLD"
|
||||||
|
|
||||||
|
c := cmdConfig{
|
||||||
|
name: "tuna-docker",
|
||||||
|
upstreamURL: "http://mirrors.tuna.moe/",
|
||||||
|
command: "/bin/cmd.sh",
|
||||||
|
workingDir: tmpDir,
|
||||||
|
logDir: tmpDir,
|
||||||
|
logFile: tmpFile,
|
||||||
|
interval: 600 * time.Second,
|
||||||
|
env: map[string]string{
|
||||||
|
"TEST_CONTENT": expectedOutput,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
cmdScriptContent := `#!/bin/sh
|
||||||
|
echo ${TEST_CONTENT}
|
||||||
|
sleep 20
|
||||||
|
`
|
||||||
|
err = ioutil.WriteFile(cmdScript, []byte(cmdScriptContent), 0755)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
|
provider, err := newCmdProvider(c)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
|
d := &dockerHook{
|
||||||
|
emptyHook: emptyHook{
|
||||||
|
provider: provider,
|
||||||
|
},
|
||||||
|
image: "alpine:3.8",
|
||||||
|
volumes: []string{
|
||||||
|
fmt.Sprintf("%s:%s", cmdScript, "/bin/cmd.sh"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
provider.AddHook(d)
|
||||||
|
So(provider.Docker(), ShouldNotBeNil)
|
||||||
|
|
||||||
|
err = d.preExec()
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
|
cmdRun("docker", []string{"images"})
|
||||||
|
exitedErr := make(chan error, 1)
|
||||||
|
go func() {
|
||||||
|
err = provider.Run()
|
||||||
|
logger.Debugf("provider.Run() exited")
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("provider.Run() failed: %v", err)
|
||||||
|
}
|
||||||
|
exitedErr <- err
|
||||||
|
}()
|
||||||
|
cmdRun("ps", []string{"aux"})
|
||||||
|
|
||||||
|
// Wait for docker running
|
||||||
|
time.Sleep(8 * time.Second)
|
||||||
|
|
||||||
|
cmdRun("ps", []string{"aux"})
|
||||||
|
|
||||||
|
// assert container running
|
||||||
|
names, err := getDockerByName(d.Name())
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
// So(names, ShouldEqual, d.Name()+"\n")
|
||||||
|
|
||||||
|
err = provider.Terminate()
|
||||||
|
// So(err, ShouldBeNil)
|
||||||
|
|
||||||
|
cmdRun("ps", []string{"aux"})
|
||||||
|
<-exitedErr
|
||||||
|
|
||||||
|
// container should be terminated and removed
|
||||||
|
names, err = getDockerByName(d.Name())
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(names, ShouldEqual, "")
|
||||||
|
|
||||||
|
// check log content
|
||||||
|
loggedContent, err := ioutil.ReadFile(provider.LogFile())
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(string(loggedContent), ShouldEqual, expectedOutput+"\n")
|
||||||
|
|
||||||
|
d.postExec()
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -458,14 +458,14 @@ exit 0
|
|||||||
fmt.Sprintf(
|
fmt.Sprintf(
|
||||||
"-aHvh --no-o --no-g --stats --exclude .~tmp~/ --safe-links "+
|
"-aHvh --no-o --no-g --stats --exclude .~tmp~/ --safe-links "+
|
||||||
"--timeout=120 --contimeout=120 --exclude dists/ -6 "+
|
"--timeout=120 --contimeout=120 --exclude dists/ -6 "+
|
||||||
"--exclude-from %s --delete-excluded --cache %s %s",
|
"--exclude-from %s %s %s",
|
||||||
provider.excludeFile, provider.upstreamURL, provider.WorkingDir(),
|
provider.excludeFile, provider.upstreamURL, provider.WorkingDir(),
|
||||||
),
|
),
|
||||||
targetDir,
|
targetDir,
|
||||||
fmt.Sprintf(
|
fmt.Sprintf(
|
||||||
"-aHvh --no-o --no-g --stats --exclude .~tmp~/ "+
|
"-aHvh --no-o --no-g --stats --exclude .~tmp~/ "+
|
||||||
"--delete --delete-after --delay-updates --safe-links "+
|
"--delete --delete-after --delay-updates --safe-links "+
|
||||||
"--timeout=120 --contimeout=120 -6 --exclude-from %s --delete-excluded --cache %s %s",
|
"--timeout=120 --contimeout=120 --delete-excluded --cache -6 --exclude-from %s %s %s",
|
||||||
provider.excludeFile, provider.upstreamURL, provider.WorkingDir(),
|
provider.excludeFile, provider.upstreamURL, provider.WorkingDir(),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
@@ -497,7 +497,7 @@ exit 0
|
|||||||
expectedOutput := fmt.Sprintf(
|
expectedOutput := fmt.Sprintf(
|
||||||
"-aHvh --no-o --no-g --stats --exclude .~tmp~/ --safe-links "+
|
"-aHvh --no-o --no-g --stats --exclude .~tmp~/ --safe-links "+
|
||||||
"--timeout=120 --contimeout=120 --exclude dists/ -6 "+
|
"--timeout=120 --contimeout=120 --exclude dists/ -6 "+
|
||||||
"--exclude-from %s --delete-excluded --cache %s %s\n",
|
"--exclude-from %s %s %s\n",
|
||||||
provider.excludeFile, provider.upstreamURL, provider.WorkingDir(),
|
provider.excludeFile, provider.upstreamURL, provider.WorkingDir(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ func newCmdJob(provider mirrorProvider, cmdAndArgs []string, workingDir string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *cmdJob) Start() error {
|
func (c *cmdJob) Start() error {
|
||||||
// logger.Debugf("Command start: %v", c.cmd.Args)
|
logger.Debugf("Command start: %v", c.cmd.Args)
|
||||||
c.finished = make(chan empty, 1)
|
c.finished = make(chan empty, 1)
|
||||||
return c.cmd.Start()
|
return c.cmd.Start()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -106,6 +106,9 @@ func (p *twoStageRsyncProvider) Options(stage int) ([]string, error) {
|
|||||||
|
|
||||||
} else if stage == 2 {
|
} else if stage == 2 {
|
||||||
options = append(options, p.stage2Options...)
|
options = append(options, p.stage2Options...)
|
||||||
|
if p.extraOptions != nil {
|
||||||
|
options = append(options, p.extraOptions...)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return []string{}, fmt.Errorf("Invalid stage: %d", stage)
|
return []string{}, fmt.Errorf("Invalid stage: %d", stage)
|
||||||
}
|
}
|
||||||
@@ -117,9 +120,6 @@ func (p *twoStageRsyncProvider) Options(stage int) ([]string, error) {
|
|||||||
if p.excludeFile != "" {
|
if p.excludeFile != "" {
|
||||||
options = append(options, "--exclude-from", p.excludeFile)
|
options = append(options, "--exclude-from", p.excludeFile)
|
||||||
}
|
}
|
||||||
if p.extraOptions != nil {
|
|
||||||
options = append(options, p.extraOptions...)
|
|
||||||
}
|
|
||||||
|
|
||||||
return options, nil
|
return options, nil
|
||||||
}
|
}
|
||||||
|
|||||||
在新工单中引用
屏蔽一个用户