volume_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package formatter
  2. import (
  3. "bytes"
  4. "strings"
  5. "testing"
  6. "github.com/docker/docker/api/types"
  7. "github.com/docker/docker/pkg/stringid"
  8. "github.com/docker/docker/pkg/testutil/assert"
  9. )
  10. func TestVolumeContext(t *testing.T) {
  11. volumeName := stringid.GenerateRandomID()
  12. var ctx volumeContext
  13. cases := []struct {
  14. volumeCtx volumeContext
  15. expValue string
  16. expHeader string
  17. call func() string
  18. }{
  19. {volumeContext{
  20. v: types.Volume{Name: volumeName},
  21. }, volumeName, nameHeader, ctx.Name},
  22. {volumeContext{
  23. v: types.Volume{Driver: "driver_name"},
  24. }, "driver_name", driverHeader, ctx.Driver},
  25. {volumeContext{
  26. v: types.Volume{Scope: "local"},
  27. }, "local", scopeHeader, ctx.Scope},
  28. {volumeContext{
  29. v: types.Volume{Mountpoint: "mountpoint"},
  30. }, "mountpoint", mountpointHeader, ctx.Mountpoint},
  31. {volumeContext{
  32. v: types.Volume{},
  33. }, "", labelsHeader, ctx.Labels},
  34. {volumeContext{
  35. v: types.Volume{Labels: map[string]string{"label1": "value1", "label2": "value2"}},
  36. }, "label1=value1,label2=value2", labelsHeader, ctx.Labels},
  37. }
  38. for _, c := range cases {
  39. ctx = c.volumeCtx
  40. v := c.call()
  41. if strings.Contains(v, ",") {
  42. compareMultipleValues(t, v, c.expValue)
  43. } else if v != c.expValue {
  44. t.Fatalf("Expected %s, was %s\n", c.expValue, v)
  45. }
  46. h := ctx.FullHeader()
  47. if h != c.expHeader {
  48. t.Fatalf("Expected %s, was %s\n", c.expHeader, h)
  49. }
  50. }
  51. }
  52. func TestVolumeContextWrite(t *testing.T) {
  53. cases := []struct {
  54. context Context
  55. expected string
  56. }{
  57. // Errors
  58. {
  59. Context{Format: "{{InvalidFunction}}"},
  60. `Template parsing error: template: :1: function "InvalidFunction" not defined
  61. `,
  62. },
  63. {
  64. Context{Format: "{{nil}}"},
  65. `Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
  66. `,
  67. },
  68. // Table format
  69. {
  70. Context{Format: NewVolumeFormat("table", false)},
  71. `DRIVER NAME
  72. foo foobar_baz
  73. bar foobar_bar
  74. `,
  75. },
  76. {
  77. Context{Format: NewVolumeFormat("table", true)},
  78. `foobar_baz
  79. foobar_bar
  80. `,
  81. },
  82. {
  83. Context{Format: NewVolumeFormat("table {{.Name}}", false)},
  84. `NAME
  85. foobar_baz
  86. foobar_bar
  87. `,
  88. },
  89. {
  90. Context{Format: NewVolumeFormat("table {{.Name}}", true)},
  91. `NAME
  92. foobar_baz
  93. foobar_bar
  94. `,
  95. },
  96. // Raw Format
  97. {
  98. Context{Format: NewVolumeFormat("raw", false)},
  99. `name: foobar_baz
  100. driver: foo
  101. name: foobar_bar
  102. driver: bar
  103. `,
  104. },
  105. {
  106. Context{Format: NewVolumeFormat("raw", true)},
  107. `name: foobar_baz
  108. name: foobar_bar
  109. `,
  110. },
  111. // Custom Format
  112. {
  113. Context{Format: NewVolumeFormat("{{.Name}}", false)},
  114. `foobar_baz
  115. foobar_bar
  116. `,
  117. },
  118. }
  119. for _, testcase := range cases {
  120. volumes := []*types.Volume{
  121. {Name: "foobar_baz", Driver: "foo"},
  122. {Name: "foobar_bar", Driver: "bar"},
  123. }
  124. out := bytes.NewBufferString("")
  125. testcase.context.Output = out
  126. err := VolumeWrite(testcase.context, volumes)
  127. if err != nil {
  128. assert.Error(t, err, testcase.expected)
  129. } else {
  130. assert.Equal(t, out.String(), testcase.expected)
  131. }
  132. }
  133. }