update_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package service
  2. import (
  3. "reflect"
  4. "sort"
  5. "testing"
  6. "time"
  7. "github.com/docker/docker/api/types/container"
  8. mounttypes "github.com/docker/docker/api/types/mount"
  9. "github.com/docker/docker/api/types/swarm"
  10. "github.com/docker/docker/pkg/testutil/assert"
  11. )
  12. func TestUpdateServiceArgs(t *testing.T) {
  13. flags := newUpdateCommand(nil).Flags()
  14. flags.Set("args", "the \"new args\"")
  15. spec := &swarm.ServiceSpec{}
  16. cspec := &spec.TaskTemplate.ContainerSpec
  17. cspec.Args = []string{"old", "args"}
  18. updateService(flags, spec)
  19. assert.EqualStringSlice(t, cspec.Args, []string{"the", "new args"})
  20. }
  21. func TestUpdateLabels(t *testing.T) {
  22. flags := newUpdateCommand(nil).Flags()
  23. flags.Set("label-add", "toadd=newlabel")
  24. flags.Set("label-rm", "toremove")
  25. labels := map[string]string{
  26. "toremove": "thelabeltoremove",
  27. "tokeep": "value",
  28. }
  29. updateLabels(flags, &labels)
  30. assert.Equal(t, len(labels), 2)
  31. assert.Equal(t, labels["tokeep"], "value")
  32. assert.Equal(t, labels["toadd"], "newlabel")
  33. }
  34. func TestUpdateLabelsRemoveALabelThatDoesNotExist(t *testing.T) {
  35. flags := newUpdateCommand(nil).Flags()
  36. flags.Set("label-rm", "dne")
  37. labels := map[string]string{"foo": "theoldlabel"}
  38. updateLabels(flags, &labels)
  39. assert.Equal(t, len(labels), 1)
  40. }
  41. func TestUpdatePlacement(t *testing.T) {
  42. flags := newUpdateCommand(nil).Flags()
  43. flags.Set("constraint-add", "node=toadd")
  44. flags.Set("constraint-rm", "node!=toremove")
  45. placement := &swarm.Placement{
  46. Constraints: []string{"node!=toremove", "container=tokeep"},
  47. }
  48. updatePlacement(flags, placement)
  49. assert.Equal(t, len(placement.Constraints), 2)
  50. assert.Equal(t, placement.Constraints[0], "container=tokeep")
  51. assert.Equal(t, placement.Constraints[1], "node=toadd")
  52. }
  53. func TestUpdateEnvironment(t *testing.T) {
  54. flags := newUpdateCommand(nil).Flags()
  55. flags.Set("env-add", "toadd=newenv")
  56. flags.Set("env-rm", "toremove")
  57. envs := []string{"toremove=theenvtoremove", "tokeep=value"}
  58. updateEnvironment(flags, &envs)
  59. assert.Equal(t, len(envs), 2)
  60. // Order has been removed in updateEnvironment (map)
  61. sort.Strings(envs)
  62. assert.Equal(t, envs[0], "toadd=newenv")
  63. assert.Equal(t, envs[1], "tokeep=value")
  64. }
  65. func TestUpdateEnvironmentWithDuplicateValues(t *testing.T) {
  66. flags := newUpdateCommand(nil).Flags()
  67. flags.Set("env-add", "foo=newenv")
  68. flags.Set("env-add", "foo=dupe")
  69. flags.Set("env-rm", "foo")
  70. envs := []string{"foo=value"}
  71. updateEnvironment(flags, &envs)
  72. assert.Equal(t, len(envs), 0)
  73. }
  74. func TestUpdateEnvironmentWithDuplicateKeys(t *testing.T) {
  75. // Test case for #25404
  76. flags := newUpdateCommand(nil).Flags()
  77. flags.Set("env-add", "A=b")
  78. envs := []string{"A=c"}
  79. updateEnvironment(flags, &envs)
  80. assert.Equal(t, len(envs), 1)
  81. assert.Equal(t, envs[0], "A=b")
  82. }
  83. func TestUpdateGroups(t *testing.T) {
  84. flags := newUpdateCommand(nil).Flags()
  85. flags.Set("group-add", "wheel")
  86. flags.Set("group-add", "docker")
  87. flags.Set("group-rm", "root")
  88. flags.Set("group-add", "foo")
  89. flags.Set("group-rm", "docker")
  90. groups := []string{"bar", "root"}
  91. updateGroups(flags, &groups)
  92. assert.Equal(t, len(groups), 3)
  93. assert.Equal(t, groups[0], "bar")
  94. assert.Equal(t, groups[1], "foo")
  95. assert.Equal(t, groups[2], "wheel")
  96. }
  97. func TestUpdateMounts(t *testing.T) {
  98. flags := newUpdateCommand(nil).Flags()
  99. flags.Set("mount-add", "type=volume,source=vol2,target=/toadd")
  100. flags.Set("mount-rm", "/toremove")
  101. mounts := []mounttypes.Mount{
  102. {Target: "/toremove", Source: "vol1", Type: mounttypes.TypeBind},
  103. {Target: "/tokeep", Source: "vol3", Type: mounttypes.TypeBind},
  104. }
  105. updateMounts(flags, &mounts)
  106. assert.Equal(t, len(mounts), 2)
  107. assert.Equal(t, mounts[0].Target, "/toadd")
  108. assert.Equal(t, mounts[1].Target, "/tokeep")
  109. }
  110. func TestUpdateMountsWithDuplicateMounts(t *testing.T) {
  111. flags := newUpdateCommand(nil).Flags()
  112. flags.Set("mount-add", "type=volume,source=vol4,target=/toadd")
  113. mounts := []mounttypes.Mount{
  114. {Target: "/tokeep1", Source: "vol1", Type: mounttypes.TypeBind},
  115. {Target: "/toadd", Source: "vol2", Type: mounttypes.TypeBind},
  116. {Target: "/tokeep2", Source: "vol3", Type: mounttypes.TypeBind},
  117. }
  118. updateMounts(flags, &mounts)
  119. assert.Equal(t, len(mounts), 3)
  120. assert.Equal(t, mounts[0].Target, "/tokeep1")
  121. assert.Equal(t, mounts[1].Target, "/tokeep2")
  122. assert.Equal(t, mounts[2].Target, "/toadd")
  123. }
  124. func TestUpdatePorts(t *testing.T) {
  125. flags := newUpdateCommand(nil).Flags()
  126. flags.Set("publish-add", "1000:1000")
  127. flags.Set("publish-rm", "333/udp")
  128. portConfigs := []swarm.PortConfig{
  129. {TargetPort: 333, Protocol: swarm.PortConfigProtocolUDP},
  130. {TargetPort: 555},
  131. }
  132. err := updatePorts(flags, &portConfigs)
  133. assert.Equal(t, err, nil)
  134. assert.Equal(t, len(portConfigs), 2)
  135. // Do a sort to have the order (might have changed by map)
  136. targetPorts := []int{int(portConfigs[0].TargetPort), int(portConfigs[1].TargetPort)}
  137. sort.Ints(targetPorts)
  138. assert.Equal(t, targetPorts[0], 555)
  139. assert.Equal(t, targetPorts[1], 1000)
  140. }
  141. func TestUpdatePortsDuplicateEntries(t *testing.T) {
  142. // Test case for #25375
  143. flags := newUpdateCommand(nil).Flags()
  144. flags.Set("publish-add", "80:80")
  145. portConfigs := []swarm.PortConfig{
  146. {TargetPort: 80, PublishedPort: 80},
  147. }
  148. err := updatePorts(flags, &portConfigs)
  149. assert.Equal(t, err, nil)
  150. assert.Equal(t, len(portConfigs), 1)
  151. assert.Equal(t, portConfigs[0].TargetPort, uint32(80))
  152. }
  153. func TestUpdatePortsDuplicateKeys(t *testing.T) {
  154. // Test case for #25375
  155. flags := newUpdateCommand(nil).Flags()
  156. flags.Set("publish-add", "80:20")
  157. portConfigs := []swarm.PortConfig{
  158. {TargetPort: 80, PublishedPort: 80},
  159. }
  160. err := updatePorts(flags, &portConfigs)
  161. assert.Equal(t, err, nil)
  162. assert.Equal(t, len(portConfigs), 1)
  163. assert.Equal(t, portConfigs[0].TargetPort, uint32(20))
  164. }
  165. func TestUpdatePortsConflictingFlags(t *testing.T) {
  166. // Test case for #25375
  167. flags := newUpdateCommand(nil).Flags()
  168. flags.Set("publish-add", "80:80")
  169. flags.Set("publish-add", "80:20")
  170. portConfigs := []swarm.PortConfig{
  171. {TargetPort: 80, PublishedPort: 80},
  172. }
  173. err := updatePorts(flags, &portConfigs)
  174. assert.Error(t, err, "conflicting port mapping")
  175. }
  176. func TestUpdateHealthcheckTable(t *testing.T) {
  177. type test struct {
  178. flags [][2]string
  179. initial *container.HealthConfig
  180. expected *container.HealthConfig
  181. err string
  182. }
  183. testCases := []test{
  184. {
  185. flags: [][2]string{{"no-healthcheck", "true"}},
  186. initial: &container.HealthConfig{Test: []string{"CMD-SHELL", "cmd1"}, Retries: 10},
  187. expected: &container.HealthConfig{Test: []string{"NONE"}},
  188. },
  189. {
  190. flags: [][2]string{{"health-cmd", "cmd1"}},
  191. initial: &container.HealthConfig{Test: []string{"NONE"}},
  192. expected: &container.HealthConfig{Test: []string{"CMD-SHELL", "cmd1"}},
  193. },
  194. {
  195. flags: [][2]string{{"health-retries", "10"}},
  196. initial: &container.HealthConfig{Test: []string{"NONE"}},
  197. expected: &container.HealthConfig{Retries: 10},
  198. },
  199. {
  200. flags: [][2]string{{"health-retries", "10"}},
  201. initial: &container.HealthConfig{Test: []string{"CMD", "cmd1"}},
  202. expected: &container.HealthConfig{Test: []string{"CMD", "cmd1"}, Retries: 10},
  203. },
  204. {
  205. flags: [][2]string{{"health-interval", "1m"}},
  206. initial: &container.HealthConfig{Test: []string{"CMD", "cmd1"}},
  207. expected: &container.HealthConfig{Test: []string{"CMD", "cmd1"}, Interval: time.Minute},
  208. },
  209. {
  210. flags: [][2]string{{"health-cmd", ""}},
  211. initial: &container.HealthConfig{Test: []string{"CMD", "cmd1"}, Retries: 10},
  212. expected: &container.HealthConfig{Retries: 10},
  213. },
  214. {
  215. flags: [][2]string{{"health-retries", "0"}},
  216. initial: &container.HealthConfig{Test: []string{"CMD", "cmd1"}, Retries: 10},
  217. expected: &container.HealthConfig{Test: []string{"CMD", "cmd1"}},
  218. },
  219. {
  220. flags: [][2]string{{"health-cmd", "cmd1"}, {"no-healthcheck", "true"}},
  221. err: "--no-healthcheck conflicts with --health-* options",
  222. },
  223. {
  224. flags: [][2]string{{"health-interval", "10m"}, {"no-healthcheck", "true"}},
  225. err: "--no-healthcheck conflicts with --health-* options",
  226. },
  227. {
  228. flags: [][2]string{{"health-timeout", "1m"}, {"no-healthcheck", "true"}},
  229. err: "--no-healthcheck conflicts with --health-* options",
  230. },
  231. }
  232. for i, c := range testCases {
  233. flags := newUpdateCommand(nil).Flags()
  234. for _, flag := range c.flags {
  235. flags.Set(flag[0], flag[1])
  236. }
  237. cspec := &swarm.ContainerSpec{
  238. Healthcheck: c.initial,
  239. }
  240. err := updateHealthcheck(flags, cspec)
  241. if c.err != "" {
  242. assert.Error(t, err, c.err)
  243. } else {
  244. assert.NilError(t, err)
  245. if !reflect.DeepEqual(cspec.Healthcheck, c.expected) {
  246. t.Errorf("incorrect result for test %d, expected health config:\n\t%#v\ngot:\n\t%#v", i, c.expected, cspec.Healthcheck)
  247. }
  248. }
  249. }
  250. }