commands_unit_test.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package docker
  2. import (
  3. "github.com/dotcloud/docker/runconfig"
  4. "strings"
  5. "testing"
  6. )
  7. func parse(t *testing.T, args string) (*runconfig.Config, *runconfig.HostConfig, error) {
  8. config, hostConfig, _, err := runconfig.Parse(strings.Split(args+" ubuntu bash", " "), nil)
  9. return config, hostConfig, err
  10. }
  11. func mustParse(t *testing.T, args string) (*runconfig.Config, *runconfig.HostConfig) {
  12. config, hostConfig, err := parse(t, args)
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. return config, hostConfig
  17. }
  18. func TestParseRunLinks(t *testing.T) {
  19. if _, hostConfig := mustParse(t, "-link a:b"); len(hostConfig.Links) == 0 || hostConfig.Links[0] != "a:b" {
  20. t.Fatalf("Error parsing links. Expected []string{\"a:b\"}, received: %v", hostConfig.Links)
  21. }
  22. if _, hostConfig := mustParse(t, "-link a:b -link c:d"); len(hostConfig.Links) < 2 || hostConfig.Links[0] != "a:b" || hostConfig.Links[1] != "c:d" {
  23. t.Fatalf("Error parsing links. Expected []string{\"a:b\", \"c:d\"}, received: %v", hostConfig.Links)
  24. }
  25. if _, hostConfig := mustParse(t, ""); len(hostConfig.Links) != 0 {
  26. t.Fatalf("Error parsing links. No link expected, received: %v", hostConfig.Links)
  27. }
  28. if _, _, err := parse(t, "-link a"); err == nil {
  29. t.Fatalf("Error parsing links. `-link a` should be an error but is not")
  30. }
  31. if _, _, err := parse(t, "-link"); err == nil {
  32. t.Fatalf("Error parsing links. `-link` should be an error but is not")
  33. }
  34. }
  35. func TestParseRunAttach(t *testing.T) {
  36. if config, _ := mustParse(t, "-a stdin"); !config.AttachStdin || config.AttachStdout || config.AttachStderr {
  37. t.Fatalf("Error parsing attach flags. Expect only Stdin enabled. Received: in: %v, out: %v, err: %v", config.AttachStdin, config.AttachStdout, config.AttachStderr)
  38. }
  39. if config, _ := mustParse(t, "-a stdin -a stdout"); !config.AttachStdin || !config.AttachStdout || config.AttachStderr {
  40. t.Fatalf("Error parsing attach flags. Expect only Stdin and Stdout enabled. Received: in: %v, out: %v, err: %v", config.AttachStdin, config.AttachStdout, config.AttachStderr)
  41. }
  42. if config, _ := mustParse(t, "-a stdin -a stdout -a stderr"); !config.AttachStdin || !config.AttachStdout || !config.AttachStderr {
  43. t.Fatalf("Error parsing attach flags. Expect all attach enabled. Received: in: %v, out: %v, err: %v", config.AttachStdin, config.AttachStdout, config.AttachStderr)
  44. }
  45. if config, _ := mustParse(t, ""); config.AttachStdin || !config.AttachStdout || !config.AttachStderr {
  46. t.Fatalf("Error parsing attach flags. Expect Stdin disabled. Received: in: %v, out: %v, err: %v", config.AttachStdin, config.AttachStdout, config.AttachStderr)
  47. }
  48. if _, _, err := parse(t, "-a"); err == nil {
  49. t.Fatalf("Error parsing attach flags, `-a` should be an error but is not")
  50. }
  51. if _, _, err := parse(t, "-a invalid"); err == nil {
  52. t.Fatalf("Error parsing attach flags, `-a invalid` should be an error but is not")
  53. }
  54. if _, _, err := parse(t, "-a invalid -a stdout"); err == nil {
  55. t.Fatalf("Error parsing attach flags, `-a stdout -a invalid` should be an error but is not")
  56. }
  57. if _, _, err := parse(t, "-a stdout -a stderr -d"); err == nil {
  58. t.Fatalf("Error parsing attach flags, `-a stdout -a stderr -d` should be an error but is not")
  59. }
  60. if _, _, err := parse(t, "-a stdin -d"); err == nil {
  61. t.Fatalf("Error parsing attach flags, `-a stdin -d` should be an error but is not")
  62. }
  63. if _, _, err := parse(t, "-a stdout -d"); err == nil {
  64. t.Fatalf("Error parsing attach flags, `-a stdout -d` should be an error but is not")
  65. }
  66. if _, _, err := parse(t, "-a stderr -d"); err == nil {
  67. t.Fatalf("Error parsing attach flags, `-a stderr -d` should be an error but is not")
  68. }
  69. if _, _, err := parse(t, "-d -rm"); err == nil {
  70. t.Fatalf("Error parsing attach flags, `-d -rm` should be an error but is not")
  71. }
  72. }
  73. func TestParseRunVolumes(t *testing.T) {
  74. if config, hostConfig := mustParse(t, "-v /tmp"); hostConfig.Binds != nil {
  75. t.Fatalf("Error parsing volume flags, `-v /tmp` should not mount-bind anything. Received %v", hostConfig.Binds)
  76. } else if _, exists := config.Volumes["/tmp"]; !exists {
  77. t.Fatalf("Error parsing volume flags, `-v /tmp` is missing from volumes. Received %v", config.Volumes)
  78. }
  79. if config, hostConfig := mustParse(t, "-v /tmp -v /var"); hostConfig.Binds != nil {
  80. t.Fatalf("Error parsing volume flags, `-v /tmp -v /var` should not mount-bind anything. Received %v", hostConfig.Binds)
  81. } else if _, exists := config.Volumes["/tmp"]; !exists {
  82. t.Fatalf("Error parsing volume flags, `-v /tmp` is missing from volumes. Recevied %v", config.Volumes)
  83. } else if _, exists := config.Volumes["/var"]; !exists {
  84. t.Fatalf("Error parsing volume flags, `-v /var` is missing from volumes. Received %v", config.Volumes)
  85. }
  86. if config, hostConfig := mustParse(t, "-v /hostTmp:/containerTmp"); hostConfig.Binds == nil || hostConfig.Binds[0] != "/hostTmp:/containerTmp" {
  87. t.Fatalf("Error parsing volume flags, `-v /hostTmp:/containerTmp` should mount-bind /hostTmp into /containeTmp. Received %v", hostConfig.Binds)
  88. } else if _, exists := config.Volumes["/containerTmp"]; !exists {
  89. t.Fatalf("Error parsing volume flags, `-v /tmp` is missing from volumes. Received %v", config.Volumes)
  90. }
  91. if config, hostConfig := mustParse(t, "-v /hostTmp:/containerTmp -v /hostVar:/containerVar"); hostConfig.Binds == nil || hostConfig.Binds[0] != "/hostTmp:/containerTmp" || hostConfig.Binds[1] != "/hostVar:/containerVar" {
  92. t.Fatalf("Error parsing volume flags, `-v /hostTmp:/containerTmp -v /hostVar:/containerVar` should mount-bind /hostTmp into /containeTmp and /hostVar into /hostContainer. Received %v", hostConfig.Binds)
  93. } else if _, exists := config.Volumes["/containerTmp"]; !exists {
  94. t.Fatalf("Error parsing volume flags, `-v /containerTmp` is missing from volumes. Received %v", config.Volumes)
  95. } else if _, exists := config.Volumes["/containerVar"]; !exists {
  96. t.Fatalf("Error parsing volume flags, `-v /containerVar` is missing from volumes. Received %v", config.Volumes)
  97. }
  98. if config, hostConfig := mustParse(t, "-v /hostTmp:/containerTmp:ro -v /hostVar:/containerVar:rw"); hostConfig.Binds == nil || hostConfig.Binds[0] != "/hostTmp:/containerTmp:ro" || hostConfig.Binds[1] != "/hostVar:/containerVar:rw" {
  99. t.Fatalf("Error parsing volume flags, `-v /hostTmp:/containerTmp:ro -v /hostVar:/containerVar:rw` should mount-bind /hostTmp into /containeTmp and /hostVar into /hostContainer. Received %v", hostConfig.Binds)
  100. } else if _, exists := config.Volumes["/containerTmp"]; !exists {
  101. t.Fatalf("Error parsing volume flags, `-v /containerTmp` is missing from volumes. Received %v", config.Volumes)
  102. } else if _, exists := config.Volumes["/containerVar"]; !exists {
  103. t.Fatalf("Error parsing volume flags, `-v /containerVar` is missing from volumes. Received %v", config.Volumes)
  104. }
  105. if config, hostConfig := mustParse(t, "-v /hostTmp:/containerTmp -v /containerVar"); hostConfig.Binds == nil || len(hostConfig.Binds) > 1 || hostConfig.Binds[0] != "/hostTmp:/containerTmp" {
  106. t.Fatalf("Error parsing volume flags, `-v /hostTmp:/containerTmp -v /containerVar` should mount-bind only /hostTmp into /containeTmp. Received %v", hostConfig.Binds)
  107. } else if _, exists := config.Volumes["/containerTmp"]; !exists {
  108. t.Fatalf("Error parsing volume flags, `-v /containerTmp` is missing from volumes. Received %v", config.Volumes)
  109. } else if _, exists := config.Volumes["/containerVar"]; !exists {
  110. t.Fatalf("Error parsing volume flags, `-v /containerVar` is missing from volumes. Received %v", config.Volumes)
  111. }
  112. if config, hostConfig := mustParse(t, ""); hostConfig.Binds != nil {
  113. t.Fatalf("Error parsing volume flags, without volume, nothing should be mount-binded. Received %v", hostConfig.Binds)
  114. } else if len(config.Volumes) != 0 {
  115. t.Fatalf("Error parsing volume flags, without volume, no volume should be present. Received %v", config.Volumes)
  116. }
  117. if _, _, err := parse(t, "-v /"); err == nil {
  118. t.Fatalf("Expected error, but got none")
  119. }
  120. if _, _, err := parse(t, "-v /:/"); err == nil {
  121. t.Fatalf("Error parsing volume flags, `-v /:/` should fail but didn't")
  122. }
  123. if _, _, err := parse(t, "-v"); err == nil {
  124. t.Fatalf("Error parsing volume flags, `-v` should fail but didn't")
  125. }
  126. if _, _, err := parse(t, "-v /tmp:"); err == nil {
  127. t.Fatalf("Error parsing volume flags, `-v /tmp:` should fail but didn't")
  128. }
  129. if _, _, err := parse(t, "-v /tmp:ro"); err == nil {
  130. t.Fatalf("Error parsing volume flags, `-v /tmp:ro` should fail but didn't")
  131. }
  132. if _, _, err := parse(t, "-v /tmp::"); err == nil {
  133. t.Fatalf("Error parsing volume flags, `-v /tmp::` should fail but didn't")
  134. }
  135. if _, _, err := parse(t, "-v :"); err == nil {
  136. t.Fatalf("Error parsing volume flags, `-v :` should fail but didn't")
  137. }
  138. if _, _, err := parse(t, "-v ::"); err == nil {
  139. t.Fatalf("Error parsing volume flags, `-v ::` should fail but didn't")
  140. }
  141. if _, _, err := parse(t, "-v /tmp:/tmp:/tmp:/tmp"); err == nil {
  142. t.Fatalf("Error parsing volume flags, `-v /tmp:/tmp:/tmp:/tmp` should fail but didn't")
  143. }
  144. }