Browse code

Fix unmarshalling of Command and Entrypoint

Signed-off-by: Brian Goff <cpuguy83@gmail.com>

Brian Goff authored on 2015/06/26 14:15:57
Showing 3 changed files
... ...
@@ -1636,3 +1636,48 @@ func (s *DockerSuite) TestPostContainerStop(c *check.C) {
1636 1636
 		c.Fatal(err)
1637 1637
 	}
1638 1638
 }
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
+}
... ...
@@ -32,7 +32,11 @@ func (e *Entrypoint) UnmarshalJSON(b []byte) error {
32 32
 
33 33
 	p := make([]string, 0, 1)
34 34
 	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)
36 40
 	}
37 41
 	e.parts = p
38 42
 	return nil
... ...
@@ -79,7 +83,11 @@ func (e *Command) UnmarshalJSON(b []byte) error {
79 79
 
80 80
 	p := make([]string, 0, 1)
81 81
 	if err := json.Unmarshal(b, &p); err != nil {
82
-		p = append(p, string(b))
82
+		var s string
83
+		if err := json.Unmarshal(b, &s); err != nil {
84
+			return err
85
+		}
86
+		p = append(p, s)
83 87
 	}
84 88
 	e.parts = p
85 89
 	return nil
... ...
@@ -2,6 +2,7 @@ package runconfig
2 2
 
3 3
 import (
4 4
 	"bytes"
5
+	"encoding/json"
5 6
 	"fmt"
6 7
 	"io/ioutil"
7 8
 	"strings"
... ...
@@ -310,3 +311,83 @@ func TestDecodeContainerConfig(t *testing.T) {
310 310
 		}
311 311
 	}
312 312
 }
313
+
314
+func TestEntrypointUnmarshalString(t *testing.T) {
315
+	var e *Entrypoint
316
+	echo, err := json.Marshal("echo")
317
+	if err != nil {
318
+		t.Fatal(err)
319
+	}
320
+	if err := json.Unmarshal(echo, &e); err != nil {
321
+		t.Fatal(err)
322
+	}
323
+
324
+	slice := e.Slice()
325
+	if len(slice) != 1 {
326
+		t.Fatalf("expected 1 element after unmarshal: %q", slice)
327
+	}
328
+
329
+	if slice[0] != "echo" {
330
+		t.Fatalf("expected `echo`, got: %q", slice[0])
331
+	}
332
+}
333
+
334
+func TestEntrypointUnmarshalSlice(t *testing.T) {
335
+	var e *Entrypoint
336
+	echo, err := json.Marshal([]string{"echo"})
337
+	if err != nil {
338
+		t.Fatal(err)
339
+	}
340
+	if err := json.Unmarshal(echo, &e); err != nil {
341
+		t.Fatal(err)
342
+	}
343
+
344
+	slice := e.Slice()
345
+	if len(slice) != 1 {
346
+		t.Fatalf("expected 1 element after unmarshal: %q", slice)
347
+	}
348
+
349
+	if slice[0] != "echo" {
350
+		t.Fatalf("expected `echo`, got: %q", slice[0])
351
+	}
352
+}
353
+
354
+func TestCommandUnmarshalSlice(t *testing.T) {
355
+	var e *Command
356
+	echo, err := json.Marshal([]string{"echo"})
357
+	if err != nil {
358
+		t.Fatal(err)
359
+	}
360
+	if err := json.Unmarshal(echo, &e); err != nil {
361
+		t.Fatal(err)
362
+	}
363
+
364
+	slice := e.Slice()
365
+	if len(slice) != 1 {
366
+		t.Fatalf("expected 1 element after unmarshal: %q", slice)
367
+	}
368
+
369
+	if slice[0] != "echo" {
370
+		t.Fatalf("expected `echo`, got: %q", slice[0])
371
+	}
372
+}
373
+
374
+func TestCommandUnmarshalString(t *testing.T) {
375
+	var e *Command
376
+	echo, err := json.Marshal("echo")
377
+	if err != nil {
378
+		t.Fatal(err)
379
+	}
380
+	if err := json.Unmarshal(echo, &e); err != nil {
381
+		t.Fatal(err)
382
+	}
383
+
384
+	slice := e.Slice()
385
+	if len(slice) != 1 {
386
+		t.Fatalf("expected 1 element after unmarshal: %q", slice)
387
+	}
388
+
389
+	if slice[0] != "echo" {
390
+		t.Fatalf("expected `echo`, got: %q", slice[0])
391
+	}
392
+}