commands_unit_test.go 8.7 KB

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