Skip to content

Commit 07b59ef

Browse files
committed
Fix service update of Args
add a unit test Signed-off-by: Daniel Nephin <dnephin@docker.com>
1 parent 3d231c7 commit 07b59ef

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

api/client/service/update.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func runUpdate(dockerCli *client.DockerCli, flags *pflag.FlagSet, serviceID stri
4545
return err
4646
}
4747

48-
err = updateService(&service.Spec, flags)
48+
err = updateService(flags, &service.Spec)
4949
if err != nil {
5050
return err
5151
}
@@ -58,7 +58,7 @@ func runUpdate(dockerCli *client.DockerCli, flags *pflag.FlagSet, serviceID stri
5858
return nil
5959
}
6060

61-
func updateService(spec *swarm.ServiceSpec, flags *pflag.FlagSet) error {
61+
func updateService(flags *pflag.FlagSet, spec *swarm.ServiceSpec) error {
6262

6363
updateString := func(flag string, field *string) {
6464
if flags.Changed(flag) {
@@ -123,7 +123,7 @@ func updateService(spec *swarm.ServiceSpec, flags *pflag.FlagSet) error {
123123
updateLabels(flags, &spec.Labels)
124124
updateString("image", &cspec.Image)
125125
updateSlice("command", &cspec.Command)
126-
updateSlice("arg", &cspec.Command)
126+
updateSlice("arg", &cspec.Args)
127127
updateListOpts("env", &cspec.Env)
128128
updateString("workdir", &cspec.Dir)
129129
updateString(flagUser, &cspec.User)

api/client/service/update_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 TestUpdateServiceCommandAndArgs(t *testing.T) {
11+
flags := newUpdateCommand(nil).Flags()
12+
flags.Set("command", "the")
13+
flags.Set("command", "new")
14+
flags.Set("command", "command")
15+
flags.Set("arg", "the")
16+
flags.Set("arg", "new args")
17+
18+
spec := &swarm.ServiceSpec{}
19+
cspec := &spec.TaskTemplate.ContainerSpec
20+
cspec.Command = []string{"old", "command"}
21+
cspec.Args = []string{"old", "args"}
22+
23+
updateService(flags, spec)
24+
assert.EqualStringSlice(t, cspec.Command, []string{"the", "new", "command"})
25+
assert.EqualStringSlice(t, cspec.Args, []string{"the", "new args"})
26+
}

pkg/testutil/assert/assert.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ func Equal(t TestingT, actual, expected interface{}) {
1919
}
2020
}
2121

22+
//EqualStringSlice compares two slices and fails the test if they do not contain
23+
// the same items.
24+
func EqualStringSlice(t TestingT, actual, expected []string) {
25+
if len(actual) != len(expected) {
26+
t.Fatalf("Expected (length %d): %q\nActual (length %d): %q",
27+
len(expected), expected, len(actual), actual)
28+
}
29+
for i, item := range actual {
30+
if item != expected[i] {
31+
t.Fatalf("Slices differ at element %d, expected %q got %q",
32+
i, expected[i], item)
33+
}
34+
}
35+
}
36+
2237
// NilError asserts that the error is nil, otherwise it fails the test.
2338
func NilError(t TestingT, err error) {
2439
if err != nil {

0 commit comments

Comments
 (0)