utils_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package docker
  2. import (
  3. "github.com/dotcloud/docker/utils"
  4. "io"
  5. "io/ioutil"
  6. "os"
  7. "path"
  8. "strings"
  9. "testing"
  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, error) {
  82. config, hostConfig, _, err := ParseRun(args, nil)
  83. if err != nil {
  84. return nil, nil, err
  85. }
  86. config.Image = GetTestImage(r).ID
  87. c, err := NewBuilder(r).Create(config)
  88. if err != nil {
  89. t.Fatal(err)
  90. return nil, nil, err
  91. }
  92. return c, hostConfig, nil
  93. }
  94. // Create a test container, start it, wait for it to complete, destroy it,
  95. // and return its standard output as a string.
  96. // The image name (eg. the XXX in []string{"-i", "-t", "XXX", "bash"}, is dynamically replaced by the current test image.
  97. // If t is not nil, call t.Fatal() at the first error. Otherwise return errors normally.
  98. func runContainer(r *Runtime, args []string, t *testing.T) (output string, err error) {
  99. defer func() {
  100. if err != nil && t != nil {
  101. t.Fatal(err)
  102. }
  103. }()
  104. container, hostConfig, err := mkContainer(r, args, t)
  105. if err != nil {
  106. return "", err
  107. }
  108. defer r.Destroy(container)
  109. stdout, err := container.StdoutPipe()
  110. if err != nil {
  111. return "", err
  112. }
  113. defer stdout.Close()
  114. if err := container.Start(hostConfig); err != nil {
  115. return "", err
  116. }
  117. container.Wait()
  118. data, err := ioutil.ReadAll(stdout)
  119. if err != nil {
  120. return "", err
  121. }
  122. output = string(data)
  123. return
  124. }
  125. func TestMergeConfig(t *testing.T) {
  126. volumesImage := make(map[string]struct{})
  127. volumesImage["/test1"] = struct{}{}
  128. volumesImage["/test2"] = struct{}{}
  129. configImage := &Config{
  130. Dns: []string{"1.1.1.1", "2.2.2.2"},
  131. PortSpecs: []string{"1111:1111", "2222:2222"},
  132. Env: []string{"VAR1=1", "VAR2=2"},
  133. Volumes: volumesImage,
  134. }
  135. volumesUser := make(map[string]struct{})
  136. volumesUser["/test3"] = struct{}{}
  137. configUser := &Config{
  138. Dns: []string{"3.3.3.3"},
  139. PortSpecs: []string{"3333:2222", "3333:3333"},
  140. Env: []string{"VAR2=3", "VAR3=3"},
  141. Volumes: volumesUser,
  142. }
  143. MergeConfig(configUser, configImage)
  144. if len(configUser.Dns) != 3 {
  145. t.Fatalf("Expected 3 dns, 1.1.1.1, 2.2.2.2 and 3.3.3.3, found %d", len(configUser.Dns))
  146. }
  147. for _, dns := range configUser.Dns {
  148. if dns != "1.1.1.1" && dns != "2.2.2.2" && dns != "3.3.3.3" {
  149. t.Fatalf("Expected 1.1.1.1 or 2.2.2.2 or 3.3.3.3, found %s", dns)
  150. }
  151. }
  152. if len(configUser.PortSpecs) != 3 {
  153. t.Fatalf("Expected 3 portSpecs, 1111:1111, 3333:2222 and 3333:3333, found %d", len(configUser.PortSpecs))
  154. }
  155. for _, portSpecs := range configUser.PortSpecs {
  156. if portSpecs != "1111:1111" && portSpecs != "3333:2222" && portSpecs != "3333:3333" {
  157. t.Fatalf("Expected 1111:1111 or 3333:2222 or 3333:3333, found %s", portSpecs)
  158. }
  159. }
  160. if len(configUser.Env) != 3 {
  161. t.Fatalf("Expected 3 env var, VAR1=1, VAR2=3 and VAR3=3, found %d", len(configUser.Env))
  162. }
  163. for _, env := range configUser.Env {
  164. if env != "VAR1=1" && env != "VAR2=3" && env != "VAR3=3" {
  165. t.Fatalf("Expected VAR1=1 or VAR2=3 or VAR3=3, found %s", env)
  166. }
  167. }
  168. if len(configUser.Volumes) != 3 {
  169. t.Fatalf("Expected 3 volumes, /test1, /test2 and /test3, found %d", len(configUser.Volumes))
  170. }
  171. for v, _ := range configUser.Volumes {
  172. if v != "/test1" && v != "/test2" && v != "/test3" {
  173. t.Fatalf("Expected /test1 or /test2 or /test3, found %s", v)
  174. }
  175. }
  176. }