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