volume_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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/pkg/stringid"
  9. "github.com/docker/docker/pkg/testutil/assert"
  10. )
  11. func TestVolumeContext(t *testing.T) {
  12. volumeName := stringid.GenerateRandomID()
  13. var ctx volumeContext
  14. cases := []struct {
  15. volumeCtx volumeContext
  16. expValue string
  17. expHeader string
  18. call func() string
  19. }{
  20. {volumeContext{
  21. v: types.Volume{Name: volumeName},
  22. }, volumeName, volumeNameHeader, ctx.Name},
  23. {volumeContext{
  24. v: types.Volume{Driver: "driver_name"},
  25. }, "driver_name", driverHeader, ctx.Driver},
  26. {volumeContext{
  27. v: types.Volume{Scope: "local"},
  28. }, "local", scopeHeader, ctx.Scope},
  29. {volumeContext{
  30. v: types.Volume{Mountpoint: "mountpoint"},
  31. }, "mountpoint", mountpointHeader, ctx.Mountpoint},
  32. {volumeContext{
  33. v: types.Volume{},
  34. }, "", labelsHeader, ctx.Labels},
  35. {volumeContext{
  36. v: types.Volume{Labels: map[string]string{"label1": "value1", "label2": "value2"}},
  37. }, "label1=value1,label2=value2", labelsHeader, ctx.Labels},
  38. }
  39. for _, c := range cases {
  40. ctx = c.volumeCtx
  41. v := c.call()
  42. if strings.Contains(v, ",") {
  43. compareMultipleValues(t, v, c.expValue)
  44. } else if v != c.expValue {
  45. t.Fatalf("Expected %s, was %s\n", c.expValue, v)
  46. }
  47. h := ctx.FullHeader()
  48. if h != c.expHeader {
  49. t.Fatalf("Expected %s, was %s\n", c.expHeader, h)
  50. }
  51. }
  52. }
  53. func TestVolumeContextWrite(t *testing.T) {
  54. cases := []struct {
  55. context Context
  56. expected string
  57. }{
  58. // Errors
  59. {
  60. Context{Format: "{{InvalidFunction}}"},
  61. `Template parsing error: template: :1: function "InvalidFunction" not defined
  62. `,
  63. },
  64. {
  65. Context{Format: "{{nil}}"},
  66. `Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
  67. `,
  68. },
  69. // Table format
  70. {
  71. Context{Format: NewVolumeFormat("table", false)},
  72. `DRIVER VOLUME NAME
  73. foo foobar_baz
  74. bar foobar_bar
  75. `,
  76. },
  77. {
  78. Context{Format: NewVolumeFormat("table", true)},
  79. `foobar_baz
  80. foobar_bar
  81. `,
  82. },
  83. {
  84. Context{Format: NewVolumeFormat("table {{.Name}}", false)},
  85. `VOLUME NAME
  86. foobar_baz
  87. foobar_bar
  88. `,
  89. },
  90. {
  91. Context{Format: NewVolumeFormat("table {{.Name}}", true)},
  92. `VOLUME NAME
  93. foobar_baz
  94. foobar_bar
  95. `,
  96. },
  97. // Raw Format
  98. {
  99. Context{Format: NewVolumeFormat("raw", false)},
  100. `name: foobar_baz
  101. driver: foo
  102. name: foobar_bar
  103. driver: bar
  104. `,
  105. },
  106. {
  107. Context{Format: NewVolumeFormat("raw", true)},
  108. `name: foobar_baz
  109. name: foobar_bar
  110. `,
  111. },
  112. // Custom Format
  113. {
  114. Context{Format: NewVolumeFormat("{{.Name}}", false)},
  115. `foobar_baz
  116. foobar_bar
  117. `,
  118. },
  119. }
  120. for _, testcase := range cases {
  121. volumes := []*types.Volume{
  122. {Name: "foobar_baz", Driver: "foo"},
  123. {Name: "foobar_bar", Driver: "bar"},
  124. }
  125. out := bytes.NewBufferString("")
  126. testcase.context.Output = out
  127. err := VolumeWrite(testcase.context, volumes)
  128. if err != nil {
  129. assert.Error(t, err, testcase.expected)
  130. } else {
  131. assert.Equal(t, out.String(), testcase.expected)
  132. }
  133. }
  134. }
  135. func TestVolumeContextWriteJSON(t *testing.T) {
  136. volumes := []*types.Volume{
  137. {Driver: "foo", Name: "foobar_baz"},
  138. {Driver: "bar", Name: "foobar_bar"},
  139. }
  140. expectedJSONs := []map[string]interface{}{
  141. {"Driver": "foo", "Labels": "", "Links": "N/A", "Mountpoint": "", "Name": "foobar_baz", "Scope": "", "Size": "N/A"},
  142. {"Driver": "bar", "Labels": "", "Links": "N/A", "Mountpoint": "", "Name": "foobar_bar", "Scope": "", "Size": "N/A"},
  143. }
  144. out := bytes.NewBufferString("")
  145. err := VolumeWrite(Context{Format: "{{json .}}", Output: out}, volumes)
  146. if err != nil {
  147. t.Fatal(err)
  148. }
  149. for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
  150. t.Logf("Output: line %d: %s", i, line)
  151. var m map[string]interface{}
  152. if err := json.Unmarshal([]byte(line), &m); err != nil {
  153. t.Fatal(err)
  154. }
  155. assert.DeepEqual(t, m, expectedJSONs[i])
  156. }
  157. }
  158. func TestVolumeContextWriteJSONField(t *testing.T) {
  159. volumes := []*types.Volume{
  160. {Driver: "foo", Name: "foobar_baz"},
  161. {Driver: "bar", Name: "foobar_bar"},
  162. }
  163. out := bytes.NewBufferString("")
  164. err := VolumeWrite(Context{Format: "{{json .Name}}", Output: out}, volumes)
  165. if err != nil {
  166. t.Fatal(err)
  167. }
  168. for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
  169. t.Logf("Output: line %d: %s", i, line)
  170. var s string
  171. if err := json.Unmarshal([]byte(line), &s); err != nil {
  172. t.Fatal(err)
  173. }
  174. assert.Equal(t, s, volumes[i].Name)
  175. }
  176. }