github.com/brahmaroutu/docker@v1.2.1-0.20160809185609-eb28dde01f16/api/client/service/update_test.go (about) 1 package service 2 3 import ( 4 "testing" 5 6 "github.com/docker/docker/pkg/testutil/assert" 7 "github.com/docker/engine-api/types/swarm" 8 ) 9 10 func TestUpdateServiceArgs(t *testing.T) { 11 flags := newUpdateCommand(nil).Flags() 12 flags.Set("args", "the \"new args\"") 13 14 spec := &swarm.ServiceSpec{} 15 cspec := &spec.TaskTemplate.ContainerSpec 16 cspec.Args = []string{"old", "args"} 17 18 updateService(flags, spec) 19 assert.EqualStringSlice(t, cspec.Args, []string{"the", "new args"}) 20 } 21 22 func TestUpdateLabels(t *testing.T) { 23 flags := newUpdateCommand(nil).Flags() 24 flags.Set("label-add", "toadd=newlabel") 25 flags.Set("label-rm", "toremove") 26 27 labels := map[string]string{ 28 "toremove": "thelabeltoremove", 29 "tokeep": "value", 30 } 31 32 updateLabels(flags, &labels) 33 assert.Equal(t, len(labels), 2) 34 assert.Equal(t, labels["tokeep"], "value") 35 assert.Equal(t, labels["toadd"], "newlabel") 36 } 37 38 func TestUpdateLabelsRemoveALabelThatDoesNotExist(t *testing.T) { 39 flags := newUpdateCommand(nil).Flags() 40 flags.Set("label-rm", "dne") 41 42 labels := map[string]string{"foo": "theoldlabel"} 43 updateLabels(flags, &labels) 44 assert.Equal(t, len(labels), 1) 45 } 46 47 func TestUpdatePlacement(t *testing.T) { 48 flags := newUpdateCommand(nil).Flags() 49 flags.Set("constraint-add", "node=toadd") 50 flags.Set("constraint-rm", "node!=toremove") 51 52 placement := &swarm.Placement{ 53 Constraints: []string{"node!=toremove", "container=tokeep"}, 54 } 55 56 updatePlacement(flags, placement) 57 assert.Equal(t, len(placement.Constraints), 2) 58 assert.Equal(t, placement.Constraints[0], "container=tokeep") 59 assert.Equal(t, placement.Constraints[1], "node=toadd") 60 } 61 62 func TestUpdateEnvironment(t *testing.T) { 63 flags := newUpdateCommand(nil).Flags() 64 flags.Set("env-add", "toadd=newenv") 65 flags.Set("env-rm", "toremove") 66 67 envs := []string{"toremove=theenvtoremove", "tokeep=value"} 68 69 updateEnvironment(flags, &envs) 70 assert.Equal(t, len(envs), 2) 71 assert.Equal(t, envs[0], "tokeep=value") 72 assert.Equal(t, envs[1], "toadd=newenv") 73 } 74 75 func TestUpdateEnvironmentWithDuplicateValues(t *testing.T) { 76 flags := newUpdateCommand(nil).Flags() 77 flags.Set("env-add", "foo=newenv") 78 flags.Set("env-add", "foo=dupe") 79 flags.Set("env-rm", "foo") 80 81 envs := []string{"foo=value"} 82 83 updateEnvironment(flags, &envs) 84 assert.Equal(t, len(envs), 0) 85 } 86 87 func TestUpdateMounts(t *testing.T) { 88 flags := newUpdateCommand(nil).Flags() 89 flags.Set("mount-add", "type=volume,target=/toadd") 90 flags.Set("mount-rm", "/toremove") 91 92 mounts := []swarm.Mount{ 93 {Target: "/toremove", Type: swarm.MountTypeBind}, 94 {Target: "/tokeep", Type: swarm.MountTypeBind}, 95 } 96 97 updateMounts(flags, &mounts) 98 assert.Equal(t, len(mounts), 2) 99 assert.Equal(t, mounts[0].Target, "/tokeep") 100 assert.Equal(t, mounts[1].Target, "/toadd") 101 } 102 103 func TestUpdateNetworks(t *testing.T) { 104 flags := newUpdateCommand(nil).Flags() 105 flags.Set("network-add", "toadd") 106 flags.Set("network-rm", "toremove") 107 108 attachments := []swarm.NetworkAttachmentConfig{ 109 {Target: "toremove", Aliases: []string{"foo"}}, 110 {Target: "tokeep"}, 111 } 112 113 updateNetworks(flags, &attachments) 114 assert.Equal(t, len(attachments), 2) 115 assert.Equal(t, attachments[0].Target, "tokeep") 116 assert.Equal(t, attachments[1].Target, "toadd") 117 } 118 119 func TestUpdatePorts(t *testing.T) { 120 flags := newUpdateCommand(nil).Flags() 121 flags.Set("publish-add", "1000:1000") 122 flags.Set("publish-rm", "333/udp") 123 124 portConfigs := []swarm.PortConfig{ 125 {TargetPort: 333, Protocol: swarm.PortConfigProtocolUDP}, 126 {TargetPort: 555}, 127 } 128 129 updatePorts(flags, &portConfigs) 130 assert.Equal(t, len(portConfigs), 2) 131 assert.Equal(t, portConfigs[0].TargetPort, uint32(555)) 132 assert.Equal(t, portConfigs[1].TargetPort, uint32(1000)) 133 }