volume_test.go 3.3 KB

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