2
0

docker_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package worker
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "testing"
  9. "time"
  10. units "github.com/docker/go-units"
  11. "github.com/codeskyblue/go-sh"
  12. . "github.com/smartystreets/goconvey/convey"
  13. )
  14. func cmdRun(p string, args []string) {
  15. cmd := exec.Command(p, args...)
  16. out, err := cmd.CombinedOutput()
  17. if err != nil {
  18. logger.Debugf("cmdRun failed %s", err)
  19. return
  20. }
  21. logger.Debugf("cmdRun: ", string(out))
  22. }
  23. func getDockerByName(name string) (string, error) {
  24. // docker ps -f 'name=$name' --format '{{.Names}}'
  25. out, err := sh.Command(
  26. "docker", "ps", "-a",
  27. "--filter", "name="+name,
  28. "--format", "{{.Names}}",
  29. ).Output()
  30. if err == nil {
  31. logger.Debugf("docker ps: '%s'", string(out))
  32. }
  33. return string(out), err
  34. }
  35. func TestDocker(t *testing.T) {
  36. Convey("Docker Should Work", t, func(ctx C) {
  37. tmpDir, err := ioutil.TempDir("", "tunasync")
  38. defer os.RemoveAll(tmpDir)
  39. So(err, ShouldBeNil)
  40. cmdScript := filepath.Join(tmpDir, "cmd.sh")
  41. tmpFile := filepath.Join(tmpDir, "log_file")
  42. expectedOutput := "HELLO_WORLD"
  43. c := cmdConfig{
  44. name: "tuna-docker",
  45. upstreamURL: "http://mirrors.tuna.moe/",
  46. command: "/bin/cmd.sh",
  47. workingDir: tmpDir,
  48. logDir: tmpDir,
  49. logFile: tmpFile,
  50. interval: 600 * time.Second,
  51. env: map[string]string{
  52. "TEST_CONTENT": expectedOutput,
  53. },
  54. }
  55. cmdScriptContent := `#!/bin/sh
  56. echo ${TEST_CONTENT}
  57. sleep 20
  58. `
  59. err = ioutil.WriteFile(cmdScript, []byte(cmdScriptContent), 0755)
  60. So(err, ShouldBeNil)
  61. provider, err := newCmdProvider(c)
  62. So(err, ShouldBeNil)
  63. d := &dockerHook{
  64. emptyHook: emptyHook{
  65. provider: provider,
  66. },
  67. image: "alpine:3.8",
  68. volumes: []string{
  69. fmt.Sprintf("%s:%s", cmdScript, "/bin/cmd.sh"),
  70. },
  71. memoryLimit: 512 * units.MiB,
  72. }
  73. provider.AddHook(d)
  74. So(provider.Docker(), ShouldNotBeNil)
  75. err = d.preExec()
  76. So(err, ShouldBeNil)
  77. cmdRun("docker", []string{"images"})
  78. exitedErr := make(chan error, 1)
  79. go func() {
  80. err = provider.Run(make(chan empty, 1))
  81. logger.Debugf("provider.Run() exited")
  82. if err != nil {
  83. logger.Errorf("provider.Run() failed: %v", err)
  84. }
  85. exitedErr <- err
  86. }()
  87. // Wait for docker running
  88. for wait := 0; wait < 8; wait++ {
  89. names, err := getDockerByName(d.Name())
  90. So(err, ShouldBeNil)
  91. if names != "" {
  92. break
  93. }
  94. time.Sleep(1 * time.Second)
  95. }
  96. // cmdRun("ps", []string{"aux"})
  97. // assert container running
  98. names, err := getDockerByName(d.Name())
  99. So(err, ShouldBeNil)
  100. So(names, ShouldEqual, d.Name()+"\n")
  101. err = provider.Terminate()
  102. So(err, ShouldBeNil)
  103. // cmdRun("ps", []string{"aux"})
  104. <-exitedErr
  105. // container should be terminated and removed
  106. names, err = getDockerByName(d.Name())
  107. So(err, ShouldBeNil)
  108. So(names, ShouldEqual, "")
  109. // check log content
  110. loggedContent, err := ioutil.ReadFile(provider.LogFile())
  111. So(err, ShouldBeNil)
  112. So(string(loggedContent), ShouldEqual, expectedOutput+"\n")
  113. d.postExec()
  114. })
  115. }