utils_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package docker
  2. import (
  3. "io"
  4. "io/ioutil"
  5. "os"
  6. "path"
  7. "strings"
  8. "testing"
  9. "github.com/dotcloud/docker/utils"
  10. )
  11. // This file contains utility functions for docker's unit test suite.
  12. // It has to be named XXX_test.go, apparently, in other to access private functions
  13. // from other XXX_test.go functions.
  14. // Create a temporary runtime suitable for unit testing.
  15. // Call t.Fatal() at the first error.
  16. func mkRuntime(f Fataler) *Runtime {
  17. runtime, err := newTestRuntime()
  18. if err != nil {
  19. f.Fatal(err)
  20. }
  21. return runtime
  22. }
  23. // A common interface to access the Fatal method of
  24. // both testing.B and testing.T.
  25. type Fataler interface {
  26. Fatal(args ...interface{})
  27. }
  28. func newTestRuntime() (*Runtime, error) {
  29. root, err := ioutil.TempDir("", "docker-test")
  30. if err != nil {
  31. return nil, err
  32. }
  33. if err := os.Remove(root); err != nil {
  34. return nil, err
  35. }
  36. if err := utils.CopyDirectory(unitTestStoreBase, root); err != nil {
  37. return nil, err
  38. }
  39. runtime, err := NewRuntimeFromDirectory(root, false)
  40. if err != nil {
  41. return nil, err
  42. }
  43. runtime.UpdateCapabilities(true)
  44. return runtime, nil
  45. }
  46. // Write `content` to the file at path `dst`, creating it if necessary,
  47. // as well as any missing directories.
  48. // The file is truncated if it already exists.
  49. // Call t.Fatal() at the first error.
  50. func writeFile(dst, content string, t *testing.T) {
  51. // Create subdirectories if necessary
  52. if err := os.MkdirAll(path.Dir(dst), 0700); err != nil && !os.IsExist(err) {
  53. t.Fatal(err)
  54. }
  55. f, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700)
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. // Write content (truncate if it exists)
  60. if _, err := io.Copy(f, strings.NewReader(content)); err != nil {
  61. t.Fatal(err)
  62. }
  63. }
  64. // Return the contents of file at path `src`.
  65. // Call t.Fatal() at the first error (including if the file doesn't exist)
  66. func readFile(src string, t *testing.T) (content string) {
  67. f, err := os.Open(src)
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. data, err := ioutil.ReadAll(f)
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. return string(data)
  76. }
  77. // Create a test container from the given runtime `r` and run arguments `args`.
  78. // The image name (eg. the XXX in []string{"-i", "-t", "XXX", "bash"}, is dynamically replaced by the current test image.
  79. // The caller is responsible for destroying the container.
  80. // Call t.Fatal() at the first error.
  81. func mkContainer(r *Runtime, args []string, t *testing.T) (*Container, *HostConfig) {
  82. config, hostConfig, _, err := ParseRun(args, nil)
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. config.Image = GetTestImage(r).ID
  87. c, err := NewBuilder(r).Create(config)
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. return c, hostConfig
  92. }
  93. // Create a test container, start it, wait for it to complete, destroy it,
  94. // and return its standard output as a string.
  95. // The image name (eg. the XXX in []string{"-i", "-t", "XXX", "bash"}, is dynamically replaced by the current test image.
  96. // If t is not nil, call t.Fatal() at the first error. Otherwise return errors normally.
  97. func runContainer(r *Runtime, args []string, t *testing.T) (output string, err error) {
  98. defer func() {
  99. if err != nil && t != nil {
  100. t.Fatal(err)
  101. }
  102. }()
  103. container, hostConfig := mkContainer(r, args, t)
  104. defer r.Destroy(container)
  105. stdout, err := container.StdoutPipe()
  106. if err != nil {
  107. return "", err
  108. }
  109. defer stdout.Close()
  110. if err := container.Start(hostConfig); err != nil {
  111. return "", err
  112. }
  113. container.Wait()
  114. data, err := ioutil.ReadAll(stdout)
  115. if err != nil {
  116. return "", err
  117. }
  118. output = string(data)
  119. return
  120. }