inspect_test.go 3.1 KB

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