utils_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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) {
  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. }
  121. func TestCompareConfig(t *testing.T) {
  122. volumes1 := make(map[string]struct{})
  123. volumes1["/test1"] = struct{}{}
  124. config1 := Config{
  125. Dns: []string{"1.1.1.1", "2.2.2.2"},
  126. PortSpecs: []string{"1111:1111", "2222:2222"},
  127. Env: []string{"VAR1=1", "VAR2=2"},
  128. VolumesFrom: "11111111",
  129. Volumes: volumes1,
  130. }
  131. config2 := Config{
  132. Dns: []string{"0.0.0.0", "2.2.2.2"},
  133. PortSpecs: []string{"1111:1111", "2222:2222"},
  134. Env: []string{"VAR1=1", "VAR2=2"},
  135. VolumesFrom: "11111111",
  136. Volumes: volumes1,
  137. }
  138. config3 := Config{
  139. Dns: []string{"1.1.1.1", "2.2.2.2"},
  140. PortSpecs: []string{"0000:0000", "2222:2222"},
  141. Env: []string{"VAR1=1", "VAR2=2"},
  142. VolumesFrom: "11111111",
  143. Volumes: volumes1,
  144. }
  145. config4 := Config{
  146. Dns: []string{"1.1.1.1", "2.2.2.2"},
  147. PortSpecs: []string{"0000:0000", "2222:2222"},
  148. Env: []string{"VAR1=1", "VAR2=2"},
  149. VolumesFrom: "22222222",
  150. Volumes: volumes1,
  151. }
  152. volumes2 := make(map[string]struct{})
  153. volumes2["/test2"] = struct{}{}
  154. config5 := Config{
  155. Dns: []string{"1.1.1.1", "2.2.2.2"},
  156. PortSpecs: []string{"0000:0000", "2222:2222"},
  157. Env: []string{"VAR1=1", "VAR2=2"},
  158. VolumesFrom: "11111111",
  159. Volumes: volumes2,
  160. }
  161. if CompareConfig(&config1, &config2) {
  162. t.Fatalf("CompareConfig should return false, Dns are different")
  163. }
  164. if CompareConfig(&config1, &config3) {
  165. t.Fatalf("CompareConfig should return false, PortSpecs are different")
  166. }
  167. if CompareConfig(&config1, &config4) {
  168. t.Fatalf("CompareConfig should return false, VolumesFrom are different")
  169. }
  170. if CompareConfig(&config1, &config5) {
  171. t.Fatalf("CompareConfig should return false, Volumes are different")
  172. }
  173. if !CompareConfig(&config1, &config1) {
  174. t.Fatalf("CompareConfig should return true")
  175. }
  176. }
  177. func TestMergeConfig(t *testing.T) {
  178. volumesImage := make(map[string]struct{})
  179. volumesImage["/test1"] = struct{}{}
  180. volumesImage["/test2"] = struct{}{}
  181. configImage := &Config{
  182. Dns: []string{"1.1.1.1", "2.2.2.2"},
  183. PortSpecs: []string{"1111:1111", "2222:2222"},
  184. Env: []string{"VAR1=1", "VAR2=2"},
  185. VolumesFrom: "1111",
  186. Volumes: volumesImage,
  187. }
  188. volumesUser := make(map[string]struct{})
  189. volumesUser["/test3"] = struct{}{}
  190. configUser := &Config{
  191. Dns: []string{"3.3.3.3"},
  192. PortSpecs: []string{"2222:3333", "3333:3333"},
  193. Env: []string{"VAR2=3", "VAR3=3"},
  194. Volumes: volumesUser,
  195. }
  196. MergeConfig(configUser, configImage)
  197. if len(configUser.Dns) != 3 {
  198. t.Fatalf("Expected 3 dns, 1.1.1.1, 2.2.2.2 and 3.3.3.3, found %d", len(configUser.Dns))
  199. }
  200. for _, dns := range configUser.Dns {
  201. if dns != "1.1.1.1" && dns != "2.2.2.2" && dns != "3.3.3.3" {
  202. t.Fatalf("Expected 1.1.1.1 or 2.2.2.2 or 3.3.3.3, found %s", dns)
  203. }
  204. }
  205. if len(configUser.PortSpecs) != 3 {
  206. t.Fatalf("Expected 3 portSpecs, 1111:1111, 2222:3333 and 3333:3333, found %d", len(configUser.PortSpecs))
  207. }
  208. for _, portSpecs := range configUser.PortSpecs {
  209. if portSpecs != "1111:1111" && portSpecs != "2222:3333" && portSpecs != "3333:3333" {
  210. t.Fatalf("Expected 1111:1111 or 2222:3333 or 3333:3333, found %s", portSpecs)
  211. }
  212. }
  213. if len(configUser.Env) != 3 {
  214. t.Fatalf("Expected 3 env var, VAR1=1, VAR2=3 and VAR3=3, found %d", len(configUser.Env))
  215. }
  216. for _, env := range configUser.Env {
  217. if env != "VAR1=1" && env != "VAR2=3" && env != "VAR3=3" {
  218. t.Fatalf("Expected VAR1=1 or VAR2=3 or VAR3=3, found %s", env)
  219. }
  220. }
  221. if len(configUser.Volumes) != 3 {
  222. t.Fatalf("Expected 3 volumes, /test1, /test2 and /test3, found %d", len(configUser.Volumes))
  223. }
  224. for v, _ := range configUser.Volumes {
  225. if v != "/test1" && v != "/test2" && v != "/test3" {
  226. t.Fatalf("Expected /test1 or /test2 or /test3, found %s", v)
  227. }
  228. }
  229. if configUser.VolumesFrom != "1111" {
  230. t.Fatalf("Expected VolumesFrom to be 1111, found %s", configUser.VolumesFrom)
  231. }
  232. }