inspect_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package service
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "strings"
  6. "testing"
  7. "time"
  8. "github.com/docker/docker/api/types/swarm"
  9. "github.com/docker/docker/cli/command/formatter"
  10. "github.com/docker/docker/pkg/testutil/assert"
  11. )
  12. func formatServiceInspect(t *testing.T, format formatter.Format, now time.Time) string {
  13. b := new(bytes.Buffer)
  14. endpointSpec := &swarm.EndpointSpec{
  15. Mode: "vip",
  16. Ports: []swarm.PortConfig{
  17. {
  18. Protocol: swarm.PortConfigProtocolTCP,
  19. TargetPort: 5000,
  20. },
  21. },
  22. }
  23. two := uint64(2)
  24. s := swarm.Service{
  25. ID: "de179gar9d0o7ltdybungplod",
  26. Meta: swarm.Meta{
  27. Version: swarm.Version{Index: 315},
  28. CreatedAt: now,
  29. UpdatedAt: now,
  30. },
  31. Spec: swarm.ServiceSpec{
  32. Annotations: swarm.Annotations{
  33. Name: "my_service",
  34. Labels: map[string]string{"com.label": "foo"},
  35. },
  36. TaskTemplate: swarm.TaskSpec{
  37. ContainerSpec: swarm.ContainerSpec{
  38. Image: "foo/bar@sha256:this_is_a_test",
  39. },
  40. },
  41. Mode: swarm.ServiceMode{
  42. Replicated: &swarm.ReplicatedService{
  43. Replicas: &two,
  44. },
  45. },
  46. Networks: []swarm.NetworkAttachmentConfig{
  47. {
  48. Target: "5vpyomhb6ievnk0i0o60gcnei",
  49. Aliases: []string{"web"},
  50. },
  51. },
  52. EndpointSpec: endpointSpec,
  53. },
  54. Endpoint: swarm.Endpoint{
  55. Spec: *endpointSpec,
  56. Ports: []swarm.PortConfig{
  57. {
  58. Protocol: swarm.PortConfigProtocolTCP,
  59. TargetPort: 5000,
  60. PublishedPort: 30000,
  61. },
  62. },
  63. VirtualIPs: []swarm.EndpointVirtualIP{
  64. {
  65. NetworkID: "6o4107cj2jx9tihgb0jyts6pj",
  66. Addr: "10.255.0.4/16",
  67. },
  68. },
  69. },
  70. UpdateStatus: &swarm.UpdateStatus{
  71. StartedAt: &now,
  72. CompletedAt: &now,
  73. },
  74. }
  75. ctx := formatter.Context{
  76. Output: b,
  77. Format: format,
  78. }
  79. err := formatter.ServiceInspectWrite(ctx, []string{"de179gar9d0o7ltdybungplod"}, func(ref string) (interface{}, []byte, error) {
  80. return s, nil, nil
  81. })
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. return b.String()
  86. }
  87. func TestPrettyPrintWithNoUpdateConfig(t *testing.T) {
  88. s := formatServiceInspect(t, formatter.NewServiceFormat("pretty"), time.Now())
  89. if strings.Contains(s, "UpdateStatus") {
  90. t.Fatal("Pretty print failed before parsing UpdateStatus")
  91. }
  92. }
  93. func TestJSONFormatWithNoUpdateConfig(t *testing.T) {
  94. now := time.Now()
  95. // s1: [{"ID":..}]
  96. // s2: {"ID":..}
  97. s1 := formatServiceInspect(t, formatter.NewServiceFormat(""), now)
  98. t.Log("// s1")
  99. t.Logf("%s", s1)
  100. s2 := formatServiceInspect(t, formatter.NewServiceFormat("{{json .}}"), now)
  101. t.Log("// s2")
  102. t.Logf("%s", s2)
  103. var m1Wrap []map[string]interface{}
  104. if err := json.Unmarshal([]byte(s1), &m1Wrap); err != nil {
  105. t.Fatal(err)
  106. }
  107. if len(m1Wrap) != 1 {
  108. t.Fatalf("strange s1=%s", s1)
  109. }
  110. m1 := m1Wrap[0]
  111. t.Logf("m1=%+v", m1)
  112. var m2 map[string]interface{}
  113. if err := json.Unmarshal([]byte(s2), &m2); err != nil {
  114. t.Fatal(err)
  115. }
  116. t.Logf("m2=%+v", m2)
  117. assert.DeepEqual(t, m2, m1)
  118. }