docker_test.go 2.7 KB

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