Skip to content

Commit 17d6f00

Browse files
committed
Fix unmarshalling of Command and Entrypoint
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
1 parent c7ece73 commit 17d6f00

File tree

3 files changed

+136
-2
lines changed

3 files changed

+136
-2
lines changed

integration-cli/docker_api_containers_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,3 +1636,48 @@ func (s *DockerSuite) TestPostContainerStop(c *check.C) {
16361636
c.Fatal(err)
16371637
}
16381638
}
1639+
1640+
// #14170
1641+
func (s *DockerSuite) TestPostContainersCreateWithStringOrSliceEntrypoint(c *check.C) {
1642+
config := struct {
1643+
Image string
1644+
Entrypoint string
1645+
Cmd []string
1646+
}{"busybox", "echo", []string{"hello", "world"}}
1647+
_, _, err := sockRequest("POST", "/containers/create?name=echotest", config)
1648+
c.Assert(err, check.IsNil)
1649+
out, _ := dockerCmd(c, "start", "-a", "echotest")
1650+
c.Assert(strings.TrimSpace(out), check.Equals, "hello world")
1651+
1652+
config2 := struct {
1653+
Image string
1654+
Entrypoint []string
1655+
Cmd []string
1656+
}{"busybox", []string{"echo"}, []string{"hello", "world"}}
1657+
_, _, err = sockRequest("POST", "/containers/create?name=echotest2", config2)
1658+
c.Assert(err, check.IsNil)
1659+
out, _ = dockerCmd(c, "start", "-a", "echotest2")
1660+
c.Assert(strings.TrimSpace(out), check.Equals, "hello world")
1661+
}
1662+
1663+
// #14170
1664+
func (s *DockerSuite) TestPostContainersCreateWithStringOrSliceCmd(c *check.C) {
1665+
config := struct {
1666+
Image string
1667+
Entrypoint string
1668+
Cmd string
1669+
}{"busybox", "echo", "hello world"}
1670+
_, _, err := sockRequest("POST", "/containers/create?name=echotest", config)
1671+
c.Assert(err, check.IsNil)
1672+
out, _ := dockerCmd(c, "start", "-a", "echotest")
1673+
c.Assert(strings.TrimSpace(out), check.Equals, "hello world")
1674+
1675+
config2 := struct {
1676+
Image string
1677+
Cmd []string
1678+
}{"busybox", []string{"echo", "hello", "world"}}
1679+
_, _, err = sockRequest("POST", "/containers/create?name=echotest2", config2)
1680+
c.Assert(err, check.IsNil)
1681+
out, _ = dockerCmd(c, "start", "-a", "echotest2")
1682+
c.Assert(strings.TrimSpace(out), check.Equals, "hello world")
1683+
}

runconfig/config.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ func (e *Entrypoint) UnmarshalJSON(b []byte) error {
3232

3333
p := make([]string, 0, 1)
3434
if err := json.Unmarshal(b, &p); err != nil {
35-
p = append(p, string(b))
35+
var s string
36+
if err := json.Unmarshal(b, &s); err != nil {
37+
return err
38+
}
39+
p = append(p, s)
3640
}
3741
e.parts = p
3842
return nil
@@ -79,7 +83,11 @@ func (e *Command) UnmarshalJSON(b []byte) error {
7983

8084
p := make([]string, 0, 1)
8185
if err := json.Unmarshal(b, &p); err != nil {
82-
p = append(p, string(b))
86+
var s string
87+
if err := json.Unmarshal(b, &s); err != nil {
88+
return err
89+
}
90+
p = append(p, s)
8391
}
8492
e.parts = p
8593
return nil

runconfig/config_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package runconfig
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"fmt"
67
"io/ioutil"
78
"strings"
@@ -310,3 +311,83 @@ func TestDecodeContainerConfig(t *testing.T) {
310311
}
311312
}
312313
}
314+
315+
func TestEntrypointUnmarshalString(t *testing.T) {
316+
var e *Entrypoint
317+
echo, err := json.Marshal("echo")
318+
if err != nil {
319+
t.Fatal(err)
320+
}
321+
if err := json.Unmarshal(echo, &e); err != nil {
322+
t.Fatal(err)
323+
}
324+
325+
slice := e.Slice()
326+
if len(slice) != 1 {
327+
t.Fatalf("expected 1 element after unmarshal: %q", slice)
328+
}
329+
330+
if slice[0] != "echo" {
331+
t.Fatalf("expected `echo`, got: %q", slice[0])
332+
}
333+
}
334+
335+
func TestEntrypointUnmarshalSlice(t *testing.T) {
336+
var e *Entrypoint
337+
echo, err := json.Marshal([]string{"echo"})
338+
if err != nil {
339+
t.Fatal(err)
340+
}
341+
if err := json.Unmarshal(echo, &e); err != nil {
342+
t.Fatal(err)
343+
}
344+
345+
slice := e.Slice()
346+
if len(slice) != 1 {
347+
t.Fatalf("expected 1 element after unmarshal: %q", slice)
348+
}
349+
350+
if slice[0] != "echo" {
351+
t.Fatalf("expected `echo`, got: %q", slice[0])
352+
}
353+
}
354+
355+
func TestCommandUnmarshalSlice(t *testing.T) {
356+
var e *Command
357+
echo, err := json.Marshal([]string{"echo"})
358+
if err != nil {
359+
t.Fatal(err)
360+
}
361+
if err := json.Unmarshal(echo, &e); err != nil {
362+
t.Fatal(err)
363+
}
364+
365+
slice := e.Slice()
366+
if len(slice) != 1 {
367+
t.Fatalf("expected 1 element after unmarshal: %q", slice)
368+
}
369+
370+
if slice[0] != "echo" {
371+
t.Fatalf("expected `echo`, got: %q", slice[0])
372+
}
373+
}
374+
375+
func TestCommandUnmarshalString(t *testing.T) {
376+
var e *Command
377+
echo, err := json.Marshal("echo")
378+
if err != nil {
379+
t.Fatal(err)
380+
}
381+
if err := json.Unmarshal(echo, &e); err != nil {
382+
t.Fatal(err)
383+
}
384+
385+
slice := e.Slice()
386+
if len(slice) != 1 {
387+
t.Fatalf("expected 1 element after unmarshal: %q", slice)
388+
}
389+
390+
if slice[0] != "echo" {
391+
t.Fatalf("expected `echo`, got: %q", slice[0])
392+
}
393+
}

0 commit comments

Comments
 (0)