config_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package runconfig
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "github.com/docker/docker/nat"
  7. )
  8. func parse(t *testing.T, args string) (*Config, *HostConfig, error) {
  9. config, hostConfig, _, err := parseRun(strings.Split(args+" ubuntu bash", " "), nil)
  10. return config, hostConfig, err
  11. }
  12. func mustParse(t *testing.T, args string) (*Config, *HostConfig) {
  13. config, hostConfig, err := parse(t, args)
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. return config, hostConfig
  18. }
  19. // check if (a == c && b == d) || (a == d && b == c)
  20. // because maps are randomized
  21. func compareRandomizedStrings(a, b, c, d string) error {
  22. if a == c && b == d {
  23. return nil
  24. }
  25. if a == d && b == c {
  26. return nil
  27. }
  28. return fmt.Errorf("strings don't match")
  29. }
  30. func TestParseRunLinks(t *testing.T) {
  31. if _, hostConfig := mustParse(t, "--link a:b"); len(hostConfig.Links) == 0 || hostConfig.Links[0] != "a:b" {
  32. t.Fatalf("Error parsing links. Expected []string{\"a:b\"}, received: %v", hostConfig.Links)
  33. }
  34. if _, hostConfig := mustParse(t, "--link a:b --link c:d"); len(hostConfig.Links) < 2 || hostConfig.Links[0] != "a:b" || hostConfig.Links[1] != "c:d" {
  35. t.Fatalf("Error parsing links. Expected []string{\"a:b\", \"c:d\"}, received: %v", hostConfig.Links)
  36. }
  37. if _, hostConfig := mustParse(t, ""); len(hostConfig.Links) != 0 {
  38. t.Fatalf("Error parsing links. No link expected, received: %v", hostConfig.Links)
  39. }
  40. if _, _, err := parse(t, "--link a"); err == nil {
  41. t.Fatalf("Error parsing links. `--link a` should be an error but is not")
  42. }
  43. if _, _, err := parse(t, "--link"); err == nil {
  44. t.Fatalf("Error parsing links. `--link` should be an error but is not")
  45. }
  46. }
  47. func TestParseRunAttach(t *testing.T) {
  48. if config, _ := mustParse(t, "-a stdin"); !config.AttachStdin || config.AttachStdout || config.AttachStderr {
  49. t.Fatalf("Error parsing attach flags. Expect only Stdin enabled. Received: in: %v, out: %v, err: %v", config.AttachStdin, config.AttachStdout, config.AttachStderr)
  50. }
  51. if config, _ := mustParse(t, "-a stdin -a stdout"); !config.AttachStdin || !config.AttachStdout || config.AttachStderr {
  52. 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)
  53. }
  54. if config, _ := mustParse(t, "-a stdin -a stdout -a stderr"); !config.AttachStdin || !config.AttachStdout || !config.AttachStderr {
  55. t.Fatalf("Error parsing attach flags. Expect all attach enabled. Received: in: %v, out: %v, err: %v", config.AttachStdin, config.AttachStdout, config.AttachStderr)
  56. }
  57. if config, _ := mustParse(t, ""); config.AttachStdin || !config.AttachStdout || !config.AttachStderr {
  58. t.Fatalf("Error parsing attach flags. Expect Stdin disabled. Received: in: %v, out: %v, err: %v", config.AttachStdin, config.AttachStdout, config.AttachStderr)
  59. }
  60. if _, _, err := parse(t, "-a"); err == nil {
  61. t.Fatalf("Error parsing attach flags, `-a` should be an error but is not")
  62. }
  63. if _, _, err := parse(t, "-a invalid"); err == nil {
  64. t.Fatalf("Error parsing attach flags, `-a invalid` should be an error but is not")
  65. }
  66. if _, _, err := parse(t, "-a invalid -a stdout"); err == nil {
  67. t.Fatalf("Error parsing attach flags, `-a stdout -a invalid` should be an error but is not")
  68. }
  69. if _, _, err := parse(t, "-a stdout -a stderr -d"); err == nil {
  70. t.Fatalf("Error parsing attach flags, `-a stdout -a stderr -d` should be an error but is not")
  71. }
  72. if _, _, err := parse(t, "-a stdin -d"); err == nil {
  73. t.Fatalf("Error parsing attach flags, `-a stdin -d` should be an error but is not")
  74. }
  75. if _, _, err := parse(t, "-a stdout -d"); err == nil {
  76. t.Fatalf("Error parsing attach flags, `-a stdout -d` should be an error but is not")
  77. }
  78. if _, _, err := parse(t, "-a stderr -d"); err == nil {
  79. t.Fatalf("Error parsing attach flags, `-a stderr -d` should be an error but is not")
  80. }
  81. if _, _, err := parse(t, "-d --rm"); err == nil {
  82. t.Fatalf("Error parsing attach flags, `-d --rm` should be an error but is not")
  83. }
  84. }
  85. func TestParseRunVolumes(t *testing.T) {
  86. if config, hostConfig := mustParse(t, "-v /tmp"); hostConfig.Binds != nil {
  87. t.Fatalf("Error parsing volume flags, `-v /tmp` should not mount-bind anything. Received %v", hostConfig.Binds)
  88. } else if _, exists := config.Volumes["/tmp"]; !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 /tmp -v /var"); hostConfig.Binds != nil {
  92. t.Fatalf("Error parsing volume flags, `-v /tmp -v /var` should not mount-bind anything. Received %v", hostConfig.Binds)
  93. } else if _, exists := config.Volumes["/tmp"]; !exists {
  94. t.Fatalf("Error parsing volume flags, `-v /tmp` is missing from volumes. Recevied %v", config.Volumes)
  95. } else if _, exists := config.Volumes["/var"]; !exists {
  96. t.Fatalf("Error parsing volume flags, `-v /var` is missing from volumes. Received %v", config.Volumes)
  97. }
  98. if _, hostConfig := mustParse(t, "-v /hostTmp:/containerTmp"); hostConfig.Binds == nil || hostConfig.Binds[0] != "/hostTmp:/containerTmp" {
  99. t.Fatalf("Error parsing volume flags, `-v /hostTmp:/containerTmp` should mount-bind /hostTmp into /containeTmp. Received %v", hostConfig.Binds)
  100. }
  101. if _, hostConfig := mustParse(t, "-v /hostTmp:/containerTmp -v /hostVar:/containerVar"); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], "/hostTmp:/containerTmp", "/hostVar:/containerVar") != nil {
  102. 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)
  103. }
  104. if _, hostConfig := mustParse(t, "-v /hostTmp:/containerTmp:ro -v /hostVar:/containerVar:rw"); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], "/hostTmp:/containerTmp:ro", "/hostVar:/containerVar:rw") != nil {
  105. 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)
  106. }
  107. if config, hostConfig := mustParse(t, "-v /hostTmp:/containerTmp -v /containerVar"); hostConfig.Binds == nil || len(hostConfig.Binds) > 1 || hostConfig.Binds[0] != "/hostTmp:/containerTmp" {
  108. t.Fatalf("Error parsing volume flags, `-v /hostTmp:/containerTmp -v /containerVar` should mount-bind only /hostTmp into /containeTmp. Received %v", hostConfig.Binds)
  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. }
  145. func TestCompare(t *testing.T) {
  146. volumes1 := make(map[string]struct{})
  147. volumes1["/test1"] = struct{}{}
  148. config1 := Config{
  149. PortSpecs: []string{"1111:1111", "2222:2222"},
  150. Env: []string{"VAR1=1", "VAR2=2"},
  151. Volumes: volumes1,
  152. }
  153. config3 := Config{
  154. PortSpecs: []string{"0000:0000", "2222:2222"},
  155. Env: []string{"VAR1=1", "VAR2=2"},
  156. Volumes: volumes1,
  157. }
  158. volumes2 := make(map[string]struct{})
  159. volumes2["/test2"] = struct{}{}
  160. config5 := Config{
  161. PortSpecs: []string{"0000:0000", "2222:2222"},
  162. Env: []string{"VAR1=1", "VAR2=2"},
  163. Volumes: volumes2,
  164. }
  165. if Compare(&config1, &config3) {
  166. t.Fatalf("Compare should return false, PortSpecs are different")
  167. }
  168. if Compare(&config1, &config5) {
  169. t.Fatalf("Compare should return false, Volumes are different")
  170. }
  171. if !Compare(&config1, &config1) {
  172. t.Fatalf("Compare should return true")
  173. }
  174. }
  175. func TestMerge(t *testing.T) {
  176. volumesImage := make(map[string]struct{})
  177. volumesImage["/test1"] = struct{}{}
  178. volumesImage["/test2"] = struct{}{}
  179. configImage := &Config{
  180. PortSpecs: []string{"1111:1111", "2222:2222"},
  181. Env: []string{"VAR1=1", "VAR2=2"},
  182. Volumes: volumesImage,
  183. }
  184. volumesUser := make(map[string]struct{})
  185. volumesUser["/test3"] = struct{}{}
  186. configUser := &Config{
  187. PortSpecs: []string{"3333:2222", "3333:3333"},
  188. Env: []string{"VAR2=3", "VAR3=3"},
  189. Volumes: volumesUser,
  190. }
  191. if err := Merge(configUser, configImage); err != nil {
  192. t.Error(err)
  193. }
  194. if len(configUser.ExposedPorts) != 3 {
  195. t.Fatalf("Expected 3 ExposedPorts, 1111, 2222 and 3333, found %d", len(configUser.ExposedPorts))
  196. }
  197. for portSpecs := range configUser.ExposedPorts {
  198. if portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" {
  199. t.Fatalf("Expected 1111 or 2222 or 3333, found %s", portSpecs)
  200. }
  201. }
  202. if len(configUser.Env) != 3 {
  203. t.Fatalf("Expected 3 env var, VAR1=1, VAR2=3 and VAR3=3, found %d", len(configUser.Env))
  204. }
  205. for _, env := range configUser.Env {
  206. if env != "VAR1=1" && env != "VAR2=3" && env != "VAR3=3" {
  207. t.Fatalf("Expected VAR1=1 or VAR2=3 or VAR3=3, found %s", env)
  208. }
  209. }
  210. if len(configUser.Volumes) != 3 {
  211. t.Fatalf("Expected 3 volumes, /test1, /test2 and /test3, found %d", len(configUser.Volumes))
  212. }
  213. for v := range configUser.Volumes {
  214. if v != "/test1" && v != "/test2" && v != "/test3" {
  215. t.Fatalf("Expected /test1 or /test2 or /test3, found %s", v)
  216. }
  217. }
  218. ports, _, err := nat.ParsePortSpecs([]string{"0000"})
  219. if err != nil {
  220. t.Error(err)
  221. }
  222. configImage2 := &Config{
  223. ExposedPorts: ports,
  224. }
  225. if err := Merge(configUser, configImage2); err != nil {
  226. t.Error(err)
  227. }
  228. if len(configUser.ExposedPorts) != 4 {
  229. t.Fatalf("Expected 4 ExposedPorts, 0000, 1111, 2222 and 3333, found %d", len(configUser.ExposedPorts))
  230. }
  231. for portSpecs := range configUser.ExposedPorts {
  232. if portSpecs.Port() != "0000" && portSpecs.Port() != "1111" && portSpecs.Port() != "2222" && portSpecs.Port() != "3333" {
  233. t.Fatalf("Expected 0000 or 1111 or 2222 or 3333, found %s", portSpecs)
  234. }
  235. }
  236. }