service_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package formatter
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "strings"
  6. "testing"
  7. "github.com/docker/docker/api/types/swarm"
  8. "github.com/docker/docker/pkg/testutil/assert"
  9. )
  10. func TestServiceContextWrite(t *testing.T) {
  11. cases := []struct {
  12. context Context
  13. expected string
  14. }{
  15. // Errors
  16. {
  17. Context{Format: "{{InvalidFunction}}"},
  18. `Template parsing error: template: :1: function "InvalidFunction" not defined
  19. `,
  20. },
  21. {
  22. Context{Format: "{{nil}}"},
  23. `Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
  24. `,
  25. },
  26. // Table format
  27. {
  28. Context{Format: NewServiceListFormat("table", false)},
  29. `ID NAME MODE REPLICAS IMAGE
  30. id_baz baz global 2/4
  31. id_bar bar replicated 2/4
  32. `,
  33. },
  34. {
  35. Context{Format: NewServiceListFormat("table", true)},
  36. `id_baz
  37. id_bar
  38. `,
  39. },
  40. {
  41. Context{Format: NewServiceListFormat("table {{.Name}}", false)},
  42. `NAME
  43. baz
  44. bar
  45. `,
  46. },
  47. {
  48. Context{Format: NewServiceListFormat("table {{.Name}}", true)},
  49. `NAME
  50. baz
  51. bar
  52. `,
  53. },
  54. // Raw Format
  55. {
  56. Context{Format: NewServiceListFormat("raw", false)},
  57. `id: id_baz
  58. name: baz
  59. mode: global
  60. replicas: 2/4
  61. image:
  62. id: id_bar
  63. name: bar
  64. mode: replicated
  65. replicas: 2/4
  66. image:
  67. `,
  68. },
  69. {
  70. Context{Format: NewServiceListFormat("raw", true)},
  71. `id: id_baz
  72. id: id_bar
  73. `,
  74. },
  75. // Custom Format
  76. {
  77. Context{Format: NewServiceListFormat("{{.Name}}", false)},
  78. `baz
  79. bar
  80. `,
  81. },
  82. }
  83. for _, testcase := range cases {
  84. services := []swarm.Service{
  85. {ID: "id_baz", Spec: swarm.ServiceSpec{Annotations: swarm.Annotations{Name: "baz"}}},
  86. {ID: "id_bar", Spec: swarm.ServiceSpec{Annotations: swarm.Annotations{Name: "bar"}}},
  87. }
  88. info := map[string]ServiceListInfo{
  89. "id_baz": {
  90. Mode: "global",
  91. Replicas: "2/4",
  92. },
  93. "id_bar": {
  94. Mode: "replicated",
  95. Replicas: "2/4",
  96. },
  97. }
  98. out := bytes.NewBufferString("")
  99. testcase.context.Output = out
  100. err := ServiceListWrite(testcase.context, services, info)
  101. if err != nil {
  102. assert.Error(t, err, testcase.expected)
  103. } else {
  104. assert.Equal(t, out.String(), testcase.expected)
  105. }
  106. }
  107. }
  108. func TestServiceContextWriteJSON(t *testing.T) {
  109. services := []swarm.Service{
  110. {ID: "id_baz", Spec: swarm.ServiceSpec{Annotations: swarm.Annotations{Name: "baz"}}},
  111. {ID: "id_bar", Spec: swarm.ServiceSpec{Annotations: swarm.Annotations{Name: "bar"}}},
  112. }
  113. info := map[string]ServiceListInfo{
  114. "id_baz": {
  115. Mode: "global",
  116. Replicas: "2/4",
  117. },
  118. "id_bar": {
  119. Mode: "replicated",
  120. Replicas: "2/4",
  121. },
  122. }
  123. expectedJSONs := []map[string]interface{}{
  124. {"ID": "id_baz", "Name": "baz", "Mode": "global", "Replicas": "2/4", "Image": ""},
  125. {"ID": "id_bar", "Name": "bar", "Mode": "replicated", "Replicas": "2/4", "Image": ""},
  126. }
  127. out := bytes.NewBufferString("")
  128. err := ServiceListWrite(Context{Format: "{{json .}}", Output: out}, services, info)
  129. if err != nil {
  130. t.Fatal(err)
  131. }
  132. for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
  133. t.Logf("Output: line %d: %s", i, line)
  134. var m map[string]interface{}
  135. if err := json.Unmarshal([]byte(line), &m); err != nil {
  136. t.Fatal(err)
  137. }
  138. assert.DeepEqual(t, m, expectedJSONs[i])
  139. }
  140. }
  141. func TestServiceContextWriteJSONField(t *testing.T) {
  142. services := []swarm.Service{
  143. {ID: "id_baz", Spec: swarm.ServiceSpec{Annotations: swarm.Annotations{Name: "baz"}}},
  144. {ID: "id_bar", Spec: swarm.ServiceSpec{Annotations: swarm.Annotations{Name: "bar"}}},
  145. }
  146. info := map[string]ServiceListInfo{
  147. "id_baz": {
  148. Mode: "global",
  149. Replicas: "2/4",
  150. },
  151. "id_bar": {
  152. Mode: "replicated",
  153. Replicas: "2/4",
  154. },
  155. }
  156. out := bytes.NewBufferString("")
  157. err := ServiceListWrite(Context{Format: "{{json .Name}}", Output: out}, services, info)
  158. if err != nil {
  159. t.Fatal(err)
  160. }
  161. for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
  162. t.Logf("Output: line %d: %s", i, line)
  163. var s string
  164. if err := json.Unmarshal([]byte(line), &s); err != nil {
  165. t.Fatal(err)
  166. }
  167. assert.Equal(t, s, services[i].Spec.Name)
  168. }
  169. }