github.com/cspotcode/docker-cli@v20.10.0-rc1.0.20201201121459-3faad7acc5b8+incompatible/cli/command/network/formatter_test.go (about) 1 package network 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "strings" 8 "testing" 9 "time" 10 11 "github.com/docker/cli/cli/command/formatter" 12 "github.com/docker/cli/internal/test" 13 "github.com/docker/docker/api/types" 14 "github.com/docker/docker/pkg/stringid" 15 "gotest.tools/v3/assert" 16 is "gotest.tools/v3/assert/cmp" 17 ) 18 19 func TestNetworkContext(t *testing.T) { 20 networkID := stringid.GenerateRandomID() 21 22 var ctx networkContext 23 cases := []struct { 24 networkCtx networkContext 25 expValue string 26 call func() string 27 }{ 28 {networkContext{ 29 n: types.NetworkResource{ID: networkID}, 30 trunc: false, 31 }, networkID, ctx.ID}, 32 {networkContext{ 33 n: types.NetworkResource{ID: networkID}, 34 trunc: true, 35 }, stringid.TruncateID(networkID), ctx.ID}, 36 {networkContext{ 37 n: types.NetworkResource{Name: "network_name"}, 38 }, "network_name", ctx.Name}, 39 {networkContext{ 40 n: types.NetworkResource{Driver: "driver_name"}, 41 }, "driver_name", ctx.Driver}, 42 {networkContext{ 43 n: types.NetworkResource{EnableIPv6: true}, 44 }, "true", ctx.IPv6}, 45 {networkContext{ 46 n: types.NetworkResource{EnableIPv6: false}, 47 }, "false", ctx.IPv6}, 48 {networkContext{ 49 n: types.NetworkResource{Internal: true}, 50 }, "true", ctx.Internal}, 51 {networkContext{ 52 n: types.NetworkResource{Internal: false}, 53 }, "false", ctx.Internal}, 54 {networkContext{ 55 n: types.NetworkResource{}, 56 }, "", ctx.Labels}, 57 {networkContext{ 58 n: types.NetworkResource{Labels: map[string]string{"label1": "value1", "label2": "value2"}}, 59 }, "label1=value1,label2=value2", ctx.Labels}, 60 } 61 62 for _, c := range cases { 63 ctx = c.networkCtx 64 v := c.call() 65 if strings.Contains(v, ",") { 66 test.CompareMultipleValues(t, v, c.expValue) 67 } else if v != c.expValue { 68 t.Fatalf("Expected %s, was %s\n", c.expValue, v) 69 } 70 } 71 } 72 73 func TestNetworkContextWrite(t *testing.T) { 74 cases := []struct { 75 context formatter.Context 76 expected string 77 }{ 78 79 // Errors 80 { 81 formatter.Context{Format: "{{InvalidFunction}}"}, 82 `Template parsing error: template: :1: function "InvalidFunction" not defined 83 `, 84 }, 85 { 86 formatter.Context{Format: "{{nil}}"}, 87 `Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command 88 `, 89 }, 90 // Table format 91 { 92 formatter.Context{Format: NewFormat("table", false)}, 93 `NETWORK ID NAME DRIVER SCOPE 94 networkID1 foobar_baz foo local 95 networkID2 foobar_bar bar local 96 `, 97 }, 98 { 99 formatter.Context{Format: NewFormat("table", true)}, 100 `networkID1 101 networkID2 102 `, 103 }, 104 { 105 formatter.Context{Format: NewFormat("table {{.Name}}", false)}, 106 `NAME 107 foobar_baz 108 foobar_bar 109 `, 110 }, 111 { 112 formatter.Context{Format: NewFormat("table {{.Name}}", true)}, 113 `NAME 114 foobar_baz 115 foobar_bar 116 `, 117 }, 118 // Raw Format 119 { 120 formatter.Context{Format: NewFormat("raw", false)}, 121 `network_id: networkID1 122 name: foobar_baz 123 driver: foo 124 scope: local 125 126 network_id: networkID2 127 name: foobar_bar 128 driver: bar 129 scope: local 130 131 `, 132 }, 133 { 134 formatter.Context{Format: NewFormat("raw", true)}, 135 `network_id: networkID1 136 network_id: networkID2 137 `, 138 }, 139 // Custom Format 140 { 141 formatter.Context{Format: NewFormat("{{.Name}}", false)}, 142 `foobar_baz 143 foobar_bar 144 `, 145 }, 146 // Custom Format with CreatedAt 147 { 148 formatter.Context{Format: NewFormat("{{.Name}} {{.CreatedAt}}", false)}, 149 `foobar_baz 2016-01-01 00:00:00 +0000 UTC 150 foobar_bar 2017-01-01 00:00:00 +0000 UTC 151 `, 152 }, 153 } 154 155 timestamp1, _ := time.Parse("2006-01-02", "2016-01-01") 156 timestamp2, _ := time.Parse("2006-01-02", "2017-01-01") 157 158 networks := []types.NetworkResource{ 159 {ID: "networkID1", Name: "foobar_baz", Driver: "foo", Scope: "local", Created: timestamp1}, 160 {ID: "networkID2", Name: "foobar_bar", Driver: "bar", Scope: "local", Created: timestamp2}, 161 } 162 163 for _, tc := range cases { 164 tc := tc 165 t.Run(string(tc.context.Format), func(t *testing.T) { 166 var out bytes.Buffer 167 tc.context.Output = &out 168 err := FormatWrite(tc.context, networks) 169 if err != nil { 170 assert.Error(t, err, tc.expected) 171 } else { 172 assert.Equal(t, out.String(), tc.expected) 173 } 174 }) 175 } 176 } 177 178 func TestNetworkContextWriteJSON(t *testing.T) { 179 networks := []types.NetworkResource{ 180 {ID: "networkID1", Name: "foobar_baz"}, 181 {ID: "networkID2", Name: "foobar_bar"}, 182 } 183 expectedJSONs := []map[string]interface{}{ 184 {"Driver": "", "ID": "networkID1", "IPv6": "false", "Internal": "false", "Labels": "", "Name": "foobar_baz", "Scope": "", "CreatedAt": "0001-01-01 00:00:00 +0000 UTC"}, 185 {"Driver": "", "ID": "networkID2", "IPv6": "false", "Internal": "false", "Labels": "", "Name": "foobar_bar", "Scope": "", "CreatedAt": "0001-01-01 00:00:00 +0000 UTC"}, 186 } 187 188 out := bytes.NewBufferString("") 189 err := FormatWrite(formatter.Context{Format: "{{json .}}", Output: out}, networks) 190 if err != nil { 191 t.Fatal(err) 192 } 193 for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") { 194 msg := fmt.Sprintf("Output: line %d: %s", i, line) 195 var m map[string]interface{} 196 err := json.Unmarshal([]byte(line), &m) 197 assert.NilError(t, err, msg) 198 assert.Check(t, is.DeepEqual(expectedJSONs[i], m), msg) 199 } 200 } 201 202 func TestNetworkContextWriteJSONField(t *testing.T) { 203 networks := []types.NetworkResource{ 204 {ID: "networkID1", Name: "foobar_baz"}, 205 {ID: "networkID2", Name: "foobar_bar"}, 206 } 207 out := bytes.NewBufferString("") 208 err := FormatWrite(formatter.Context{Format: "{{json .ID}}", Output: out}, networks) 209 if err != nil { 210 t.Fatal(err) 211 } 212 for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") { 213 msg := fmt.Sprintf("Output: line %d: %s", i, line) 214 var s string 215 err := json.Unmarshal([]byte(line), &s) 216 assert.NilError(t, err, msg) 217 assert.Check(t, is.Equal(networks[i].ID, s), msg) 218 } 219 }