add a unit test
Signed-off-by: Daniel Nephin <dnephin@docker.com>
| ... | ... |
@@ -45,7 +45,7 @@ func runUpdate(dockerCli *client.DockerCli, flags *pflag.FlagSet, serviceID stri |
| 45 | 45 |
return err |
| 46 | 46 |
} |
| 47 | 47 |
|
| 48 |
- err = updateService(&service.Spec, flags) |
|
| 48 |
+ err = updateService(flags, &service.Spec) |
|
| 49 | 49 |
if err != nil {
|
| 50 | 50 |
return err |
| 51 | 51 |
} |
| ... | ... |
@@ -58,7 +58,7 @@ func runUpdate(dockerCli *client.DockerCli, flags *pflag.FlagSet, serviceID stri |
| 58 | 58 |
return nil |
| 59 | 59 |
} |
| 60 | 60 |
|
| 61 |
-func updateService(spec *swarm.ServiceSpec, flags *pflag.FlagSet) error {
|
|
| 61 |
+func updateService(flags *pflag.FlagSet, spec *swarm.ServiceSpec) error {
|
|
| 62 | 62 |
|
| 63 | 63 |
updateString := func(flag string, field *string) {
|
| 64 | 64 |
if flags.Changed(flag) {
|
| ... | ... |
@@ -123,7 +123,7 @@ func updateService(spec *swarm.ServiceSpec, flags *pflag.FlagSet) error {
|
| 123 | 123 |
updateLabels(flags, &spec.Labels) |
| 124 | 124 |
updateString("image", &cspec.Image)
|
| 125 | 125 |
updateSlice("command", &cspec.Command)
|
| 126 |
- updateSlice("arg", &cspec.Command)
|
|
| 126 |
+ updateSlice("arg", &cspec.Args)
|
|
| 127 | 127 |
updateListOpts("env", &cspec.Env)
|
| 128 | 128 |
updateString("workdir", &cspec.Dir)
|
| 129 | 129 |
updateString(flagUser, &cspec.User) |
| 130 | 130 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,26 @@ |
| 0 |
+package service |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "testing" |
|
| 4 |
+ |
|
| 5 |
+ "github.com/docker/docker/pkg/testutil/assert" |
|
| 6 |
+ "github.com/docker/engine-api/types/swarm" |
|
| 7 |
+) |
|
| 8 |
+ |
|
| 9 |
+func TestUpdateServiceCommandAndArgs(t *testing.T) {
|
|
| 10 |
+ flags := newUpdateCommand(nil).Flags() |
|
| 11 |
+ flags.Set("command", "the")
|
|
| 12 |
+ flags.Set("command", "new")
|
|
| 13 |
+ flags.Set("command", "command")
|
|
| 14 |
+ flags.Set("arg", "the")
|
|
| 15 |
+ flags.Set("arg", "new args")
|
|
| 16 |
+ |
|
| 17 |
+ spec := &swarm.ServiceSpec{}
|
|
| 18 |
+ cspec := &spec.TaskTemplate.ContainerSpec |
|
| 19 |
+ cspec.Command = []string{"old", "command"}
|
|
| 20 |
+ cspec.Args = []string{"old", "args"}
|
|
| 21 |
+ |
|
| 22 |
+ updateService(flags, spec) |
|
| 23 |
+ assert.EqualStringSlice(t, cspec.Command, []string{"the", "new", "command"})
|
|
| 24 |
+ assert.EqualStringSlice(t, cspec.Args, []string{"the", "new args"})
|
|
| 25 |
+} |
| ... | ... |
@@ -19,6 +19,21 @@ func Equal(t TestingT, actual, expected interface{}) {
|
| 19 | 19 |
} |
| 20 | 20 |
} |
| 21 | 21 |
|
| 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 |
+ |
|
| 22 | 37 |
// NilError asserts that the error is nil, otherwise it fails the test. |
| 23 | 38 |
func NilError(t TestingT, err error) {
|
| 24 | 39 |
if err != nil {
|