docker_test.go 2.1 KB

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