update_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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,target=/toadd")
  100. flags.Set("mount-rm", "/toremove")
  101. mounts := []mounttypes.Mount{
  102. {Target: "/toremove", Type: mounttypes.TypeBind},
  103. {Target: "/tokeep", Type: mounttypes.TypeBind},
  104. }
  105. updateMounts(flags, &mounts)
  106. assert.Equal(t, len(mounts), 2)
  107. assert.Equal(t, mounts[0].Target, "/tokeep")
  108. assert.Equal(t, mounts[1].Target, "/toadd")
  109. }
  110. func TestUpdatePorts(t *testing.T) {
  111. flags := newUpdateCommand(nil).Flags()
  112. flags.Set("publish-add", "1000:1000")
  113. flags.Set("publish-rm", "333/udp")
  114. portConfigs := []swarm.PortConfig{
  115. {TargetPort: 333, Protocol: swarm.PortConfigProtocolUDP},
  116. {TargetPort: 555},
  117. }
  118. err := updatePorts(flags, &portConfigs)
  119. assert.Equal(t, err, nil)
  120. assert.Equal(t, len(portConfigs), 2)
  121. // Do a sort to have the order (might have changed by map)
  122. targetPorts := []int{int(portConfigs[0].TargetPort), int(portConfigs[1].TargetPort)}
  123. sort.Ints(targetPorts)
  124. assert.Equal(t, targetPorts[0], 555)
  125. assert.Equal(t, targetPorts[1], 1000)
  126. }
  127. func TestUpdatePortsDuplicateEntries(t *testing.T) {
  128. // Test case for #25375
  129. flags := newUpdateCommand(nil).Flags()
  130. flags.Set("publish-add", "80:80")
  131. portConfigs := []swarm.PortConfig{
  132. {TargetPort: 80, PublishedPort: 80},
  133. }
  134. err := updatePorts(flags, &portConfigs)
  135. assert.Equal(t, err, nil)
  136. assert.Equal(t, len(portConfigs), 1)
  137. assert.Equal(t, portConfigs[0].TargetPort, uint32(80))
  138. }
  139. func TestUpdatePortsDuplicateKeys(t *testing.T) {
  140. // Test case for #25375
  141. flags := newUpdateCommand(nil).Flags()
  142. flags.Set("publish-add", "80:20")
  143. portConfigs := []swarm.PortConfig{
  144. {TargetPort: 80, PublishedPort: 80},
  145. }
  146. err := updatePorts(flags, &portConfigs)
  147. assert.Equal(t, err, nil)
  148. assert.Equal(t, len(portConfigs), 1)
  149. assert.Equal(t, portConfigs[0].TargetPort, uint32(20))
  150. }
  151. func TestUpdatePortsConflictingFlags(t *testing.T) {
  152. // Test case for #25375
  153. flags := newUpdateCommand(nil).Flags()
  154. flags.Set("publish-add", "80:80")
  155. flags.Set("publish-add", "80:20")
  156. portConfigs := []swarm.PortConfig{
  157. {TargetPort: 80, PublishedPort: 80},
  158. }
  159. err := updatePorts(flags, &portConfigs)
  160. assert.Error(t, err, "conflicting port mapping")
  161. }
  162. func TestUpdateHealthcheckTable(t *testing.T) {
  163. type test struct {
  164. flags [][2]string
  165. initial *container.HealthConfig
  166. expected *container.HealthConfig
  167. err string
  168. }
  169. testCases := []test{
  170. {
  171. flags: [][2]string{{"no-healthcheck", "true"}},
  172. initial: &container.HealthConfig{Test: []string{"CMD-SHELL", "cmd1"}, Retries: 10},
  173. expected: &container.HealthConfig{Test: []string{"NONE"}},
  174. },
  175. {
  176. flags: [][2]string{{"health-cmd", "cmd1"}},
  177. initial: &container.HealthConfig{Test: []string{"NONE"}},
  178. expected: &container.HealthConfig{Test: []string{"CMD-SHELL", "cmd1"}},
  179. },
  180. {
  181. flags: [][2]string{{"health-retries", "10"}},
  182. initial: &container.HealthConfig{Test: []string{"NONE"}},
  183. expected: &container.HealthConfig{Retries: 10},
  184. },
  185. {
  186. flags: [][2]string{{"health-retries", "10"}},
  187. initial: &container.HealthConfig{Test: []string{"CMD", "cmd1"}},
  188. expected: &container.HealthConfig{Test: []string{"CMD", "cmd1"}, Retries: 10},
  189. },
  190. {
  191. flags: [][2]string{{"health-interval", "1m"}},
  192. initial: &container.HealthConfig{Test: []string{"CMD", "cmd1"}},
  193. expected: &container.HealthConfig{Test: []string{"CMD", "cmd1"}, Interval: time.Minute},
  194. },
  195. {
  196. flags: [][2]string{{"health-cmd", ""}},
  197. initial: &container.HealthConfig{Test: []string{"CMD", "cmd1"}, Retries: 10},
  198. expected: &container.HealthConfig{Retries: 10},
  199. },
  200. {
  201. flags: [][2]string{{"health-retries", "0"}},
  202. initial: &container.HealthConfig{Test: []string{"CMD", "cmd1"}, Retries: 10},
  203. expected: &container.HealthConfig{Test: []string{"CMD", "cmd1"}},
  204. },
  205. {
  206. flags: [][2]string{{"health-cmd", "cmd1"}, {"no-healthcheck", "true"}},
  207. err: "--no-healthcheck conflicts with --health-* options",
  208. },
  209. {
  210. flags: [][2]string{{"health-interval", "10m"}, {"no-healthcheck", "true"}},
  211. err: "--no-healthcheck conflicts with --health-* options",
  212. },
  213. {
  214. flags: [][2]string{{"health-timeout", "1m"}, {"no-healthcheck", "true"}},
  215. err: "--no-healthcheck conflicts with --health-* options",
  216. },
  217. }
  218. for i, c := range testCases {
  219. flags := newUpdateCommand(nil).Flags()
  220. for _, flag := range c.flags {
  221. flags.Set(flag[0], flag[1])
  222. }
  223. cspec := &swarm.ContainerSpec{
  224. Healthcheck: c.initial,
  225. }
  226. err := updateHealthcheck(flags, cspec)
  227. if c.err != "" {
  228. assert.Error(t, err, c.err)
  229. } else {
  230. assert.NilError(t, err)
  231. if !reflect.DeepEqual(cspec.Healthcheck, c.expected) {
  232. t.Errorf("incorrect result for test %d, expected health config:\n\t%#v\ngot:\n\t%#v", i, c.expected, cspec.Healthcheck)
  233. }
  234. }
  235. }
  236. }