utils_test.go 2.8 KB

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