github.com/a4a881d4/docker@v1.9.0-rc2/api/client/ps/formatter_test.go (about) 1 package ps 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/docker/docker/api/types" 8 ) 9 10 func TestFormat(t *testing.T) { 11 contexts := []struct { 12 context Context 13 expected string 14 }{ 15 // Errors 16 { 17 Context{ 18 Format: "{{InvalidFunction}}", 19 }, 20 `Template parsing error: template: :1: function "InvalidFunction" not defined 21 `, 22 }, 23 { 24 Context{ 25 Format: "{{nil}}", 26 }, 27 `Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command 28 `, 29 }, 30 // Table Format 31 { 32 Context{ 33 Format: "table", 34 }, 35 `CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 36 containerID1 ubuntu "" 45 years ago foobar_baz 37 containerID2 ubuntu "" 45 years ago foobar_bar 38 `, 39 }, 40 { 41 Context{ 42 Format: "table {{.Image}}", 43 }, 44 "IMAGE\nubuntu\nubuntu\n", 45 }, 46 { 47 Context{ 48 Format: "table {{.Image}}", 49 Size: true, 50 }, 51 "IMAGE SIZE\nubuntu 0 B\nubuntu 0 B\n", 52 }, 53 { 54 Context{ 55 Format: "table {{.Image}}", 56 Quiet: true, 57 }, 58 "IMAGE\nubuntu\nubuntu\n", 59 }, 60 { 61 Context{ 62 Format: "table", 63 Quiet: true, 64 }, 65 "containerID1\ncontainerID2\n", 66 }, 67 // Raw Format 68 { 69 Context{ 70 Format: "raw", 71 }, 72 `container_id: containerID1 73 image: ubuntu 74 command: "" 75 created_at: 1970-01-01 00:00:00 +0000 UTC 76 status: 77 names: foobar_baz 78 labels: 79 ports: 80 81 container_id: containerID2 82 image: ubuntu 83 command: "" 84 created_at: 1970-01-01 00:00:00 +0000 UTC 85 status: 86 names: foobar_bar 87 labels: 88 ports: 89 90 `, 91 }, 92 { 93 Context{ 94 Format: "raw", 95 Size: true, 96 }, 97 `container_id: containerID1 98 image: ubuntu 99 command: "" 100 created_at: 1970-01-01 00:00:00 +0000 UTC 101 status: 102 names: foobar_baz 103 labels: 104 ports: 105 size: 0 B 106 107 container_id: containerID2 108 image: ubuntu 109 command: "" 110 created_at: 1970-01-01 00:00:00 +0000 UTC 111 status: 112 names: foobar_bar 113 labels: 114 ports: 115 size: 0 B 116 117 `, 118 }, 119 { 120 Context{ 121 Format: "raw", 122 Quiet: true, 123 }, 124 "container_id: containerID1\ncontainer_id: containerID2\n", 125 }, 126 // Custom Format 127 { 128 Context{ 129 Format: "{{.Image}}", 130 }, 131 "ubuntu\nubuntu\n", 132 }, 133 { 134 Context{ 135 Format: "{{.Image}}", 136 Size: true, 137 }, 138 "ubuntu\nubuntu\n", 139 }, 140 } 141 142 for _, context := range contexts { 143 containers := []types.Container{ 144 {ID: "containerID1", Names: []string{"/foobar_baz"}, Image: "ubuntu"}, 145 {ID: "containerID2", Names: []string{"/foobar_bar"}, Image: "ubuntu"}, 146 } 147 out := bytes.NewBufferString("") 148 context.context.Output = out 149 Format(context.context, containers) 150 actual := out.String() 151 if actual != context.expected { 152 t.Fatalf("Expected \n%s, got \n%s", context.expected, actual) 153 } 154 // Clean buffer 155 out.Reset() 156 } 157 } 158 159 func TestCustomFormatNoContainers(t *testing.T) { 160 out := bytes.NewBufferString("") 161 containers := []types.Container{} 162 163 contexts := []struct { 164 context Context 165 expected string 166 }{ 167 { 168 Context{ 169 Format: "{{.Image}}", 170 Output: out, 171 }, 172 "", 173 }, 174 { 175 Context{ 176 Format: "table {{.Image}}", 177 Output: out, 178 }, 179 "IMAGE\n", 180 }, 181 { 182 Context{ 183 Format: "{{.Image}}", 184 Output: out, 185 Size: true, 186 }, 187 "", 188 }, 189 { 190 Context{ 191 Format: "table {{.Image}}", 192 Output: out, 193 Size: true, 194 }, 195 "IMAGE SIZE\n", 196 }, 197 } 198 199 for _, context := range contexts { 200 customFormat(context.context, containers) 201 actual := out.String() 202 if actual != context.expected { 203 t.Fatalf("Expected \n%s, got \n%s", context.expected, actual) 204 } 205 // Clean buffer 206 out.Reset() 207 } 208 }