service_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 PORTS
  30. id_baz baz global 2/4 *:80->8080/tcp
  31. id_bar bar replicated 2/4 *:80->8080/tcp
  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. ports: *:80->8080/tcp
  63. id: id_bar
  64. name: bar
  65. mode: replicated
  66. replicas: 2/4
  67. image:
  68. ports: *:80->8080/tcp
  69. `,
  70. },
  71. {
  72. Context{Format: NewServiceListFormat("raw", true)},
  73. `id: id_baz
  74. id: id_bar
  75. `,
  76. },
  77. // Custom Format
  78. {
  79. Context{Format: NewServiceListFormat("{{.Name}}", false)},
  80. `baz
  81. bar
  82. `,
  83. },
  84. }
  85. for _, testcase := range cases {
  86. services := []swarm.Service{
  87. {
  88. ID: "id_baz",
  89. Spec: swarm.ServiceSpec{
  90. Annotations: swarm.Annotations{Name: "baz"},
  91. EndpointSpec: &swarm.EndpointSpec{
  92. Ports: []swarm.PortConfig{
  93. {
  94. PublishMode: "ingress",
  95. PublishedPort: 80,
  96. TargetPort: 8080,
  97. Protocol: "tcp",
  98. },
  99. },
  100. },
  101. },
  102. },
  103. {
  104. ID: "id_bar",
  105. Spec: swarm.ServiceSpec{
  106. Annotations: swarm.Annotations{Name: "bar"},
  107. EndpointSpec: &swarm.EndpointSpec{
  108. Ports: []swarm.PortConfig{
  109. {
  110. PublishMode: "ingress",
  111. PublishedPort: 80,
  112. TargetPort: 8080,
  113. Protocol: "tcp",
  114. },
  115. },
  116. },
  117. },
  118. },
  119. }
  120. info := map[string]ServiceListInfo{
  121. "id_baz": {
  122. Mode: "global",
  123. Replicas: "2/4",
  124. },
  125. "id_bar": {
  126. Mode: "replicated",
  127. Replicas: "2/4",
  128. },
  129. }
  130. out := bytes.NewBufferString("")
  131. testcase.context.Output = out
  132. err := ServiceListWrite(testcase.context, services, info)
  133. if err != nil {
  134. assert.Error(t, err, testcase.expected)
  135. } else {
  136. assert.Equal(t, out.String(), testcase.expected)
  137. }
  138. }
  139. }
  140. func TestServiceContextWriteJSON(t *testing.T) {
  141. services := []swarm.Service{
  142. {
  143. ID: "id_baz",
  144. Spec: swarm.ServiceSpec{
  145. Annotations: swarm.Annotations{Name: "baz"},
  146. EndpointSpec: &swarm.EndpointSpec{
  147. Ports: []swarm.PortConfig{
  148. {
  149. PublishMode: "ingress",
  150. PublishedPort: 80,
  151. TargetPort: 8080,
  152. Protocol: "tcp",
  153. },
  154. },
  155. },
  156. },
  157. },
  158. {
  159. ID: "id_bar",
  160. Spec: swarm.ServiceSpec{
  161. Annotations: swarm.Annotations{Name: "bar"},
  162. EndpointSpec: &swarm.EndpointSpec{
  163. Ports: []swarm.PortConfig{
  164. {
  165. PublishMode: "ingress",
  166. PublishedPort: 80,
  167. TargetPort: 8080,
  168. Protocol: "tcp",
  169. },
  170. },
  171. },
  172. },
  173. },
  174. }
  175. info := map[string]ServiceListInfo{
  176. "id_baz": {
  177. Mode: "global",
  178. Replicas: "2/4",
  179. },
  180. "id_bar": {
  181. Mode: "replicated",
  182. Replicas: "2/4",
  183. },
  184. }
  185. expectedJSONs := []map[string]interface{}{
  186. {"ID": "id_baz", "Name": "baz", "Mode": "global", "Replicas": "2/4", "Image": "", "Ports": "*:80->8080/tcp"},
  187. {"ID": "id_bar", "Name": "bar", "Mode": "replicated", "Replicas": "2/4", "Image": "", "Ports": "*:80->8080/tcp"},
  188. }
  189. out := bytes.NewBufferString("")
  190. err := ServiceListWrite(Context{Format: "{{json .}}", Output: out}, services, info)
  191. if err != nil {
  192. t.Fatal(err)
  193. }
  194. for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
  195. t.Logf("Output: line %d: %s", i, line)
  196. var m map[string]interface{}
  197. if err := json.Unmarshal([]byte(line), &m); err != nil {
  198. t.Fatal(err)
  199. }
  200. assert.DeepEqual(t, m, expectedJSONs[i])
  201. }
  202. }
  203. func TestServiceContextWriteJSONField(t *testing.T) {
  204. services := []swarm.Service{
  205. {ID: "id_baz", Spec: swarm.ServiceSpec{Annotations: swarm.Annotations{Name: "baz"}}},
  206. {ID: "id_bar", Spec: swarm.ServiceSpec{Annotations: swarm.Annotations{Name: "bar"}}},
  207. }
  208. info := map[string]ServiceListInfo{
  209. "id_baz": {
  210. Mode: "global",
  211. Replicas: "2/4",
  212. },
  213. "id_bar": {
  214. Mode: "replicated",
  215. Replicas: "2/4",
  216. },
  217. }
  218. out := bytes.NewBufferString("")
  219. err := ServiceListWrite(Context{Format: "{{json .Name}}", Output: out}, services, info)
  220. if err != nil {
  221. t.Fatal(err)
  222. }
  223. for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
  224. t.Logf("Output: line %d: %s", i, line)
  225. var s string
  226. if err := json.Unmarshal([]byte(line), &s); err != nil {
  227. t.Fatal(err)
  228. }
  229. assert.Equal(t, s, services[i].Spec.Name)
  230. }
  231. }