|
@@ -2,15 +2,17 @@ package service
|
|
|
|
|
|
import (
|
|
import (
|
|
"bytes"
|
|
"bytes"
|
|
|
|
+ "encoding/json"
|
|
"strings"
|
|
"strings"
|
|
"testing"
|
|
"testing"
|
|
"time"
|
|
"time"
|
|
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
"github.com/docker/docker/api/types/swarm"
|
|
"github.com/docker/docker/cli/command/formatter"
|
|
"github.com/docker/docker/cli/command/formatter"
|
|
|
|
+ "github.com/docker/docker/pkg/testutil/assert"
|
|
)
|
|
)
|
|
|
|
|
|
-func TestPrettyPrintWithNoUpdateConfig(t *testing.T) {
|
|
|
|
|
|
+func formatServiceInspect(t *testing.T, format formatter.Format, now time.Time) string {
|
|
b := new(bytes.Buffer)
|
|
b := new(bytes.Buffer)
|
|
|
|
|
|
endpointSpec := &swarm.EndpointSpec{
|
|
endpointSpec := &swarm.EndpointSpec{
|
|
@@ -29,8 +31,8 @@ func TestPrettyPrintWithNoUpdateConfig(t *testing.T) {
|
|
ID: "de179gar9d0o7ltdybungplod",
|
|
ID: "de179gar9d0o7ltdybungplod",
|
|
Meta: swarm.Meta{
|
|
Meta: swarm.Meta{
|
|
Version: swarm.Version{Index: 315},
|
|
Version: swarm.Version{Index: 315},
|
|
- CreatedAt: time.Now(),
|
|
|
|
- UpdatedAt: time.Now(),
|
|
|
|
|
|
+ CreatedAt: now,
|
|
|
|
+ UpdatedAt: now,
|
|
},
|
|
},
|
|
Spec: swarm.ServiceSpec{
|
|
Spec: swarm.ServiceSpec{
|
|
Annotations: swarm.Annotations{
|
|
Annotations: swarm.Annotations{
|
|
@@ -73,14 +75,14 @@ func TestPrettyPrintWithNoUpdateConfig(t *testing.T) {
|
|
},
|
|
},
|
|
},
|
|
},
|
|
UpdateStatus: swarm.UpdateStatus{
|
|
UpdateStatus: swarm.UpdateStatus{
|
|
- StartedAt: time.Now(),
|
|
|
|
- CompletedAt: time.Now(),
|
|
|
|
|
|
+ StartedAt: now,
|
|
|
|
+ CompletedAt: now,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
|
|
ctx := formatter.Context{
|
|
ctx := formatter.Context{
|
|
Output: b,
|
|
Output: b,
|
|
- Format: formatter.NewServiceFormat("pretty"),
|
|
|
|
|
|
+ Format: format,
|
|
}
|
|
}
|
|
|
|
|
|
err := formatter.ServiceInspectWrite(ctx, []string{"de179gar9d0o7ltdybungplod"}, func(ref string) (interface{}, []byte, error) {
|
|
err := formatter.ServiceInspectWrite(ctx, []string{"de179gar9d0o7ltdybungplod"}, func(ref string) (interface{}, []byte, error) {
|
|
@@ -89,8 +91,39 @@ func TestPrettyPrintWithNoUpdateConfig(t *testing.T) {
|
|
if err != nil {
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
+ return b.String()
|
|
|
|
+}
|
|
|
|
|
|
- if strings.Contains(b.String(), "UpdateStatus") {
|
|
|
|
|
|
+func TestPrettyPrintWithNoUpdateConfig(t *testing.T) {
|
|
|
|
+ s := formatServiceInspect(t, formatter.NewServiceFormat("pretty"), time.Now())
|
|
|
|
+ if strings.Contains(s, "UpdateStatus") {
|
|
t.Fatal("Pretty print failed before parsing UpdateStatus")
|
|
t.Fatal("Pretty print failed before parsing UpdateStatus")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+func TestJSONFormatWithNoUpdateConfig(t *testing.T) {
|
|
|
|
+ now := time.Now()
|
|
|
|
+ // s1: [{"ID":..}]
|
|
|
|
+ // s2: {"ID":..}
|
|
|
|
+ s1 := formatServiceInspect(t, formatter.NewServiceFormat(""), now)
|
|
|
|
+ t.Log("// s1")
|
|
|
|
+ t.Logf("%s", s1)
|
|
|
|
+ s2 := formatServiceInspect(t, formatter.NewServiceFormat("{{json .}}"), now)
|
|
|
|
+ t.Log("// s2")
|
|
|
|
+ t.Logf("%s", s2)
|
|
|
|
+ var m1Wrap []map[string]interface{}
|
|
|
|
+ if err := json.Unmarshal([]byte(s1), &m1Wrap); err != nil {
|
|
|
|
+ t.Fatal(err)
|
|
|
|
+ }
|
|
|
|
+ if len(m1Wrap) != 1 {
|
|
|
|
+ t.Fatalf("strange s1=%s", s1)
|
|
|
|
+ }
|
|
|
|
+ m1 := m1Wrap[0]
|
|
|
|
+ t.Logf("m1=%+v", m1)
|
|
|
|
+ var m2 map[string]interface{}
|
|
|
|
+ if err := json.Unmarshal([]byte(s2), &m2); err != nil {
|
|
|
|
+ t.Fatal(err)
|
|
|
|
+ }
|
|
|
|
+ t.Logf("m2=%+v", m2)
|
|
|
|
+ assert.DeepEqual(t, m2, m1)
|
|
|
|
+}
|