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