github.com/brahmaroutu/docker@v1.2.1-0.20160809185609-eb28dde01f16/api/client/formatter/image_test.go (about) 1 package formatter 2 3 import ( 4 "bytes" 5 "fmt" 6 "strings" 7 "testing" 8 "time" 9 10 "github.com/docker/docker/pkg/stringid" 11 "github.com/docker/engine-api/types" 12 ) 13 14 func TestImageContext(t *testing.T) { 15 imageID := stringid.GenerateRandomID() 16 unix := time.Now().Unix() 17 18 var ctx imageContext 19 cases := []struct { 20 imageCtx imageContext 21 expValue string 22 expHeader string 23 call func() string 24 }{ 25 {imageContext{ 26 i: types.Image{ID: imageID}, 27 trunc: true, 28 }, stringid.TruncateID(imageID), imageIDHeader, ctx.ID}, 29 {imageContext{ 30 i: types.Image{ID: imageID}, 31 trunc: false, 32 }, imageID, imageIDHeader, ctx.ID}, 33 {imageContext{ 34 i: types.Image{Size: 10}, 35 trunc: true, 36 }, "10 B", sizeHeader, ctx.Size}, 37 {imageContext{ 38 i: types.Image{Created: unix}, 39 trunc: true, 40 }, time.Unix(unix, 0).String(), createdAtHeader, ctx.CreatedAt}, 41 // FIXME 42 // {imageContext{ 43 // i: types.Image{Created: unix}, 44 // trunc: true, 45 // }, units.HumanDuration(time.Unix(unix, 0)), createdSinceHeader, ctx.CreatedSince}, 46 {imageContext{ 47 i: types.Image{}, 48 repo: "busybox", 49 }, "busybox", repositoryHeader, ctx.Repository}, 50 {imageContext{ 51 i: types.Image{}, 52 tag: "latest", 53 }, "latest", tagHeader, ctx.Tag}, 54 {imageContext{ 55 i: types.Image{}, 56 digest: "sha256:d149ab53f8718e987c3a3024bb8aa0e2caadf6c0328f1d9d850b2a2a67f2819a", 57 }, "sha256:d149ab53f8718e987c3a3024bb8aa0e2caadf6c0328f1d9d850b2a2a67f2819a", digestHeader, ctx.Digest}, 58 } 59 60 for _, c := range cases { 61 ctx = c.imageCtx 62 v := c.call() 63 if strings.Contains(v, ",") { 64 compareMultipleValues(t, v, c.expValue) 65 } else if v != c.expValue { 66 t.Fatalf("Expected %s, was %s\n", c.expValue, v) 67 } 68 69 h := ctx.fullHeader() 70 if h != c.expHeader { 71 t.Fatalf("Expected %s, was %s\n", c.expHeader, h) 72 } 73 } 74 } 75 76 func TestImageContextWrite(t *testing.T) { 77 unixTime := time.Now().AddDate(0, 0, -1).Unix() 78 expectedTime := time.Unix(unixTime, 0).String() 79 80 contexts := []struct { 81 context ImageContext 82 expected string 83 }{ 84 // Errors 85 { 86 ImageContext{ 87 Context: Context{ 88 Format: "{{InvalidFunction}}", 89 }, 90 }, 91 `Template parsing error: template: :1: function "InvalidFunction" not defined 92 `, 93 }, 94 { 95 ImageContext{ 96 Context: Context{ 97 Format: "{{nil}}", 98 }, 99 }, 100 `Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command 101 `, 102 }, 103 // Table Format 104 { 105 ImageContext{ 106 Context: Context{ 107 Format: "table", 108 }, 109 }, 110 `REPOSITORY TAG IMAGE ID CREATED SIZE 111 image tag1 imageID1 24 hours ago 0 B 112 image tag2 imageID2 24 hours ago 0 B 113 <none> <none> imageID3 24 hours ago 0 B 114 `, 115 }, 116 { 117 ImageContext{ 118 Context: Context{ 119 Format: "table {{.Repository}}", 120 }, 121 }, 122 "REPOSITORY\nimage\nimage\n<none>\n", 123 }, 124 { 125 ImageContext{ 126 Context: Context{ 127 Format: "table {{.Repository}}", 128 }, 129 Digest: true, 130 }, 131 `REPOSITORY DIGEST 132 image sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf 133 image <none> 134 <none> <none> 135 `, 136 }, 137 { 138 ImageContext{ 139 Context: Context{ 140 Format: "table {{.Repository}}", 141 Quiet: true, 142 }, 143 }, 144 "REPOSITORY\nimage\nimage\n<none>\n", 145 }, 146 { 147 ImageContext{ 148 Context: Context{ 149 Format: "table", 150 Quiet: true, 151 }, 152 }, 153 "imageID1\nimageID2\nimageID3\n", 154 }, 155 { 156 ImageContext{ 157 Context: Context{ 158 Format: "table", 159 Quiet: false, 160 }, 161 Digest: true, 162 }, 163 `REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE 164 image tag1 sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf imageID1 24 hours ago 0 B 165 image tag2 <none> imageID2 24 hours ago 0 B 166 <none> <none> <none> imageID3 24 hours ago 0 B 167 `, 168 }, 169 { 170 ImageContext{ 171 Context: Context{ 172 Format: "table", 173 Quiet: true, 174 }, 175 Digest: true, 176 }, 177 "imageID1\nimageID2\nimageID3\n", 178 }, 179 // Raw Format 180 { 181 ImageContext{ 182 Context: Context{ 183 Format: "raw", 184 }, 185 }, 186 fmt.Sprintf(`repository: image 187 tag: tag1 188 image_id: imageID1 189 created_at: %s 190 virtual_size: 0 B 191 192 repository: image 193 tag: tag2 194 image_id: imageID2 195 created_at: %s 196 virtual_size: 0 B 197 198 repository: <none> 199 tag: <none> 200 image_id: imageID3 201 created_at: %s 202 virtual_size: 0 B 203 204 `, expectedTime, expectedTime, expectedTime), 205 }, 206 { 207 ImageContext{ 208 Context: Context{ 209 Format: "raw", 210 }, 211 Digest: true, 212 }, 213 fmt.Sprintf(`repository: image 214 tag: tag1 215 digest: sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf 216 image_id: imageID1 217 created_at: %s 218 virtual_size: 0 B 219 220 repository: image 221 tag: tag2 222 digest: <none> 223 image_id: imageID2 224 created_at: %s 225 virtual_size: 0 B 226 227 repository: <none> 228 tag: <none> 229 digest: <none> 230 image_id: imageID3 231 created_at: %s 232 virtual_size: 0 B 233 234 `, expectedTime, expectedTime, expectedTime), 235 }, 236 { 237 ImageContext{ 238 Context: Context{ 239 Format: "raw", 240 Quiet: true, 241 }, 242 }, 243 `image_id: imageID1 244 image_id: imageID2 245 image_id: imageID3 246 `, 247 }, 248 // Custom Format 249 { 250 ImageContext{ 251 Context: Context{ 252 Format: "{{.Repository}}", 253 }, 254 }, 255 "image\nimage\n<none>\n", 256 }, 257 { 258 ImageContext{ 259 Context: Context{ 260 Format: "{{.Repository}}", 261 }, 262 Digest: true, 263 }, 264 "image\nimage\n<none>\n", 265 }, 266 } 267 268 for _, context := range contexts { 269 images := []types.Image{ 270 {ID: "imageID1", RepoTags: []string{"image:tag1"}, RepoDigests: []string{"image@sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf"}, Created: unixTime}, 271 {ID: "imageID2", RepoTags: []string{"image:tag2"}, Created: unixTime}, 272 {ID: "imageID3", RepoTags: []string{"<none>:<none>"}, RepoDigests: []string{"<none>@<none>"}, Created: unixTime}, 273 } 274 out := bytes.NewBufferString("") 275 context.context.Output = out 276 context.context.Images = images 277 context.context.Write() 278 actual := out.String() 279 if actual != context.expected { 280 t.Fatalf("Expected \n%s, got \n%s", context.expected, actual) 281 } 282 // Clean buffer 283 out.Reset() 284 } 285 } 286 287 func TestImageContextWriteWithNoImage(t *testing.T) { 288 out := bytes.NewBufferString("") 289 images := []types.Image{} 290 291 contexts := []struct { 292 context ImageContext 293 expected string 294 }{ 295 { 296 ImageContext{ 297 Context: Context{ 298 Format: "{{.Repository}}", 299 Output: out, 300 }, 301 }, 302 "", 303 }, 304 { 305 ImageContext{ 306 Context: Context{ 307 Format: "table {{.Repository}}", 308 Output: out, 309 }, 310 }, 311 "REPOSITORY\n", 312 }, 313 { 314 ImageContext{ 315 Context: Context{ 316 Format: "{{.Repository}}", 317 Output: out, 318 }, 319 Digest: true, 320 }, 321 "", 322 }, 323 { 324 ImageContext{ 325 Context: Context{ 326 Format: "table {{.Repository}}", 327 Output: out, 328 }, 329 Digest: true, 330 }, 331 "REPOSITORY DIGEST\n", 332 }, 333 } 334 335 for _, context := range contexts { 336 context.context.Images = images 337 context.context.Write() 338 actual := out.String() 339 if actual != context.expected { 340 t.Fatalf("Expected \n%s, got \n%s", context.expected, actual) 341 } 342 // Clean buffer 343 out.Reset() 344 } 345 }