node_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package formatter
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "strings"
  6. "testing"
  7. "github.com/docker/docker/api/types"
  8. "github.com/docker/docker/api/types/swarm"
  9. "github.com/docker/docker/pkg/stringid"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestNodeContext(t *testing.T) {
  13. nodeID := stringid.GenerateRandomID()
  14. var ctx nodeContext
  15. cases := []struct {
  16. nodeCtx nodeContext
  17. expValue string
  18. call func() string
  19. }{
  20. {nodeContext{
  21. n: swarm.Node{ID: nodeID},
  22. }, nodeID, ctx.ID},
  23. {nodeContext{
  24. n: swarm.Node{Description: swarm.NodeDescription{Hostname: "node_hostname"}},
  25. }, "node_hostname", ctx.Hostname},
  26. {nodeContext{
  27. n: swarm.Node{Status: swarm.NodeStatus{State: swarm.NodeState("foo")}},
  28. }, "Foo", ctx.Status},
  29. {nodeContext{
  30. n: swarm.Node{Spec: swarm.NodeSpec{Availability: swarm.NodeAvailability("drain")}},
  31. }, "Drain", ctx.Availability},
  32. {nodeContext{
  33. n: swarm.Node{ManagerStatus: &swarm.ManagerStatus{Leader: true}},
  34. }, "Leader", ctx.ManagerStatus},
  35. }
  36. for _, c := range cases {
  37. ctx = c.nodeCtx
  38. v := c.call()
  39. if strings.Contains(v, ",") {
  40. compareMultipleValues(t, v, c.expValue)
  41. } else if v != c.expValue {
  42. t.Fatalf("Expected %s, was %s\n", c.expValue, v)
  43. }
  44. }
  45. }
  46. func TestNodeContextWrite(t *testing.T) {
  47. cases := []struct {
  48. context Context
  49. expected string
  50. }{
  51. // Errors
  52. {
  53. Context{Format: "{{InvalidFunction}}"},
  54. `Template parsing error: template: :1: function "InvalidFunction" not defined
  55. `,
  56. },
  57. {
  58. Context{Format: "{{nil}}"},
  59. `Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
  60. `,
  61. },
  62. // Table format
  63. {
  64. Context{Format: NewNodeFormat("table", false)},
  65. `ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
  66. nodeID1 foobar_baz Foo Drain Leader
  67. nodeID2 foobar_bar Bar Active Reachable
  68. `,
  69. },
  70. {
  71. Context{Format: NewNodeFormat("table", true)},
  72. `nodeID1
  73. nodeID2
  74. `,
  75. },
  76. {
  77. Context{Format: NewNodeFormat("table {{.Hostname}}", false)},
  78. `HOSTNAME
  79. foobar_baz
  80. foobar_bar
  81. `,
  82. },
  83. {
  84. Context{Format: NewNodeFormat("table {{.Hostname}}", true)},
  85. `HOSTNAME
  86. foobar_baz
  87. foobar_bar
  88. `,
  89. },
  90. // Raw Format
  91. {
  92. Context{Format: NewNodeFormat("raw", false)},
  93. `node_id: nodeID1
  94. hostname: foobar_baz
  95. status: Foo
  96. availability: Drain
  97. manager_status: Leader
  98. node_id: nodeID2
  99. hostname: foobar_bar
  100. status: Bar
  101. availability: Active
  102. manager_status: Reachable
  103. `,
  104. },
  105. {
  106. Context{Format: NewNodeFormat("raw", true)},
  107. `node_id: nodeID1
  108. node_id: nodeID2
  109. `,
  110. },
  111. // Custom Format
  112. {
  113. Context{Format: NewNodeFormat("{{.Hostname}}", false)},
  114. `foobar_baz
  115. foobar_bar
  116. `,
  117. },
  118. }
  119. for _, testcase := range cases {
  120. nodes := []swarm.Node{
  121. {ID: "nodeID1", Description: swarm.NodeDescription{Hostname: "foobar_baz"}, Status: swarm.NodeStatus{State: swarm.NodeState("foo")}, Spec: swarm.NodeSpec{Availability: swarm.NodeAvailability("drain")}, ManagerStatus: &swarm.ManagerStatus{Leader: true}},
  122. {ID: "nodeID2", Description: swarm.NodeDescription{Hostname: "foobar_bar"}, Status: swarm.NodeStatus{State: swarm.NodeState("bar")}, Spec: swarm.NodeSpec{Availability: swarm.NodeAvailability("active")}, ManagerStatus: &swarm.ManagerStatus{Leader: false, Reachability: swarm.Reachability("Reachable")}},
  123. }
  124. out := bytes.NewBufferString("")
  125. testcase.context.Output = out
  126. err := NodeWrite(testcase.context, nodes, types.Info{})
  127. if err != nil {
  128. assert.EqualError(t, err, testcase.expected)
  129. } else {
  130. assert.Equal(t, testcase.expected, out.String())
  131. }
  132. }
  133. }
  134. func TestNodeContextWriteJSON(t *testing.T) {
  135. nodes := []swarm.Node{
  136. {ID: "nodeID1", Description: swarm.NodeDescription{Hostname: "foobar_baz"}},
  137. {ID: "nodeID2", Description: swarm.NodeDescription{Hostname: "foobar_bar"}},
  138. }
  139. expectedJSONs := []map[string]interface{}{
  140. {"Availability": "", "Hostname": "foobar_baz", "ID": "nodeID1", "ManagerStatus": "", "Status": "", "Self": false},
  141. {"Availability": "", "Hostname": "foobar_bar", "ID": "nodeID2", "ManagerStatus": "", "Status": "", "Self": false},
  142. }
  143. out := bytes.NewBufferString("")
  144. err := NodeWrite(Context{Format: "{{json .}}", Output: out}, nodes, types.Info{})
  145. if err != nil {
  146. t.Fatal(err)
  147. }
  148. for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
  149. t.Logf("Output: line %d: %s", i, line)
  150. var m map[string]interface{}
  151. if err := json.Unmarshal([]byte(line), &m); err != nil {
  152. t.Fatal(err)
  153. }
  154. assert.Equal(t, expectedJSONs[i], m)
  155. }
  156. }
  157. func TestNodeContextWriteJSONField(t *testing.T) {
  158. nodes := []swarm.Node{
  159. {ID: "nodeID1", Description: swarm.NodeDescription{Hostname: "foobar_baz"}},
  160. {ID: "nodeID2", Description: swarm.NodeDescription{Hostname: "foobar_bar"}},
  161. }
  162. out := bytes.NewBufferString("")
  163. err := NodeWrite(Context{Format: "{{json .ID}}", Output: out}, nodes, types.Info{})
  164. if err != nil {
  165. t.Fatal(err)
  166. }
  167. for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
  168. t.Logf("Output: line %d: %s", i, line)
  169. var s string
  170. if err := json.Unmarshal([]byte(line), &s); err != nil {
  171. t.Fatal(err)
  172. }
  173. assert.Equal(t, nodes[i].ID, s)
  174. }
  175. }