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