github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/api/common_test.go (about) 1 package api 2 3 import ( 4 "io/ioutil" 5 "path/filepath" 6 "testing" 7 8 "os" 9 10 "github.com/docker/engine-api/types" 11 ) 12 13 type ports struct { 14 ports []types.Port 15 expected string 16 } 17 18 // DisplayablePorts 19 func TestDisplayablePorts(t *testing.T) { 20 cases := []ports{ 21 { 22 []types.Port{ 23 { 24 PrivatePort: 9988, 25 Type: "tcp", 26 }, 27 }, 28 "9988/tcp"}, 29 { 30 []types.Port{ 31 { 32 PrivatePort: 9988, 33 Type: "udp", 34 }, 35 }, 36 "9988/udp", 37 }, 38 { 39 []types.Port{ 40 { 41 IP: "0.0.0.0", 42 PrivatePort: 9988, 43 Type: "tcp", 44 }, 45 }, 46 "0.0.0.0:0->9988/tcp", 47 }, 48 { 49 []types.Port{ 50 { 51 PrivatePort: 9988, 52 PublicPort: 8899, 53 Type: "tcp", 54 }, 55 }, 56 "9988/tcp", 57 }, 58 { 59 []types.Port{ 60 { 61 IP: "4.3.2.1", 62 PrivatePort: 9988, 63 PublicPort: 8899, 64 Type: "tcp", 65 }, 66 }, 67 "4.3.2.1:8899->9988/tcp", 68 }, 69 { 70 []types.Port{ 71 { 72 IP: "4.3.2.1", 73 PrivatePort: 9988, 74 PublicPort: 9988, 75 Type: "tcp", 76 }, 77 }, 78 "4.3.2.1:9988->9988/tcp", 79 }, 80 { 81 []types.Port{ 82 { 83 PrivatePort: 9988, 84 Type: "udp", 85 }, { 86 PrivatePort: 9988, 87 Type: "udp", 88 }, 89 }, 90 "9988/udp, 9988/udp", 91 }, 92 { 93 []types.Port{ 94 { 95 IP: "1.2.3.4", 96 PublicPort: 9998, 97 PrivatePort: 9998, 98 Type: "udp", 99 }, { 100 IP: "1.2.3.4", 101 PublicPort: 9999, 102 PrivatePort: 9999, 103 Type: "udp", 104 }, 105 }, 106 "1.2.3.4:9998-9999->9998-9999/udp", 107 }, 108 { 109 []types.Port{ 110 { 111 IP: "1.2.3.4", 112 PublicPort: 8887, 113 PrivatePort: 9998, 114 Type: "udp", 115 }, { 116 IP: "1.2.3.4", 117 PublicPort: 8888, 118 PrivatePort: 9999, 119 Type: "udp", 120 }, 121 }, 122 "1.2.3.4:8887->9998/udp, 1.2.3.4:8888->9999/udp", 123 }, 124 { 125 []types.Port{ 126 { 127 PrivatePort: 9998, 128 Type: "udp", 129 }, { 130 PrivatePort: 9999, 131 Type: "udp", 132 }, 133 }, 134 "9998-9999/udp", 135 }, 136 { 137 []types.Port{ 138 { 139 IP: "1.2.3.4", 140 PrivatePort: 6677, 141 PublicPort: 7766, 142 Type: "tcp", 143 }, { 144 PrivatePort: 9988, 145 PublicPort: 8899, 146 Type: "udp", 147 }, 148 }, 149 "9988/udp, 1.2.3.4:7766->6677/tcp", 150 }, 151 { 152 []types.Port{ 153 { 154 IP: "1.2.3.4", 155 PrivatePort: 9988, 156 PublicPort: 8899, 157 Type: "udp", 158 }, { 159 IP: "1.2.3.4", 160 PrivatePort: 9988, 161 PublicPort: 8899, 162 Type: "tcp", 163 }, { 164 IP: "4.3.2.1", 165 PrivatePort: 2233, 166 PublicPort: 3322, 167 Type: "tcp", 168 }, 169 }, 170 "4.3.2.1:3322->2233/tcp, 1.2.3.4:8899->9988/tcp, 1.2.3.4:8899->9988/udp", 171 }, 172 { 173 []types.Port{ 174 { 175 PrivatePort: 9988, 176 PublicPort: 8899, 177 Type: "udp", 178 }, { 179 IP: "1.2.3.4", 180 PrivatePort: 6677, 181 PublicPort: 7766, 182 Type: "tcp", 183 }, { 184 IP: "4.3.2.1", 185 PrivatePort: 2233, 186 PublicPort: 3322, 187 Type: "tcp", 188 }, 189 }, 190 "9988/udp, 4.3.2.1:3322->2233/tcp, 1.2.3.4:7766->6677/tcp", 191 }, 192 { 193 []types.Port{ 194 { 195 PrivatePort: 80, 196 Type: "tcp", 197 }, { 198 PrivatePort: 1024, 199 Type: "tcp", 200 }, { 201 PrivatePort: 80, 202 Type: "udp", 203 }, { 204 PrivatePort: 1024, 205 Type: "udp", 206 }, { 207 IP: "1.1.1.1", 208 PublicPort: 80, 209 PrivatePort: 1024, 210 Type: "tcp", 211 }, { 212 IP: "1.1.1.1", 213 PublicPort: 80, 214 PrivatePort: 1024, 215 Type: "udp", 216 }, { 217 IP: "1.1.1.1", 218 PublicPort: 1024, 219 PrivatePort: 80, 220 Type: "tcp", 221 }, { 222 IP: "1.1.1.1", 223 PublicPort: 1024, 224 PrivatePort: 80, 225 Type: "udp", 226 }, { 227 IP: "2.1.1.1", 228 PublicPort: 80, 229 PrivatePort: 1024, 230 Type: "tcp", 231 }, { 232 IP: "2.1.1.1", 233 PublicPort: 80, 234 PrivatePort: 1024, 235 Type: "udp", 236 }, { 237 IP: "2.1.1.1", 238 PublicPort: 1024, 239 PrivatePort: 80, 240 Type: "tcp", 241 }, { 242 IP: "2.1.1.1", 243 PublicPort: 1024, 244 PrivatePort: 80, 245 Type: "udp", 246 }, 247 }, 248 "80/tcp, 80/udp, 1024/tcp, 1024/udp, 1.1.1.1:1024->80/tcp, 1.1.1.1:1024->80/udp, 2.1.1.1:1024->80/tcp, 2.1.1.1:1024->80/udp, 1.1.1.1:80->1024/tcp, 1.1.1.1:80->1024/udp, 2.1.1.1:80->1024/tcp, 2.1.1.1:80->1024/udp", 249 }, 250 } 251 252 for _, port := range cases { 253 actual := DisplayablePorts(port.ports) 254 if port.expected != actual { 255 t.Fatalf("Expected %s, got %s.", port.expected, actual) 256 } 257 } 258 } 259 260 // MatchesContentType 261 func TestJsonContentType(t *testing.T) { 262 if !MatchesContentType("application/json", "application/json") { 263 t.Fail() 264 } 265 266 if !MatchesContentType("application/json; charset=utf-8", "application/json") { 267 t.Fail() 268 } 269 270 if MatchesContentType("dockerapplication/json", "application/json") { 271 t.Fail() 272 } 273 } 274 275 // LoadOrCreateTrustKey 276 func TestLoadOrCreateTrustKeyInvalidKeyFile(t *testing.T) { 277 tmpKeyFolderPath, err := ioutil.TempDir("", "api-trustkey-test") 278 if err != nil { 279 t.Fatal(err) 280 } 281 defer os.RemoveAll(tmpKeyFolderPath) 282 283 tmpKeyFile, err := ioutil.TempFile(tmpKeyFolderPath, "keyfile") 284 if err != nil { 285 t.Fatal(err) 286 } 287 288 if _, err := LoadOrCreateTrustKey(tmpKeyFile.Name()); err == nil { 289 t.Fatalf("expected an error, got nothing.") 290 } 291 292 } 293 294 func TestLoadOrCreateTrustKeyCreateKey(t *testing.T) { 295 tmpKeyFolderPath, err := ioutil.TempDir("", "api-trustkey-test") 296 if err != nil { 297 t.Fatal(err) 298 } 299 defer os.RemoveAll(tmpKeyFolderPath) 300 301 // Without the need to create the folder hierarchy 302 tmpKeyFile := filepath.Join(tmpKeyFolderPath, "keyfile") 303 304 if key, err := LoadOrCreateTrustKey(tmpKeyFile); err != nil || key == nil { 305 t.Fatalf("expected a new key file, got : %v and %v", err, key) 306 } 307 308 if _, err := os.Stat(tmpKeyFile); err != nil { 309 t.Fatalf("Expected to find a file %s, got %v", tmpKeyFile, err) 310 } 311 312 // With the need to create the folder hierarchy as tmpKeyFie is in a path 313 // where some folders do not exist. 314 tmpKeyFile = filepath.Join(tmpKeyFolderPath, "folder/hierarchy/keyfile") 315 316 if key, err := LoadOrCreateTrustKey(tmpKeyFile); err != nil || key == nil { 317 t.Fatalf("expected a new key file, got : %v and %v", err, key) 318 } 319 320 if _, err := os.Stat(tmpKeyFile); err != nil { 321 t.Fatalf("Expected to find a file %s, got %v", tmpKeyFile, err) 322 } 323 324 // With no path at all 325 defer os.Remove("keyfile") 326 if key, err := LoadOrCreateTrustKey("keyfile"); err != nil || key == nil { 327 t.Fatalf("expected a new key file, got : %v and %v", err, key) 328 } 329 330 if _, err := os.Stat("keyfile"); err != nil { 331 t.Fatalf("Expected to find a file keyfile, got %v", err) 332 } 333 } 334 335 func TestLoadOrCreateTrustKeyLoadValidKey(t *testing.T) { 336 tmpKeyFile := filepath.Join("fixtures", "keyfile") 337 338 if key, err := LoadOrCreateTrustKey(tmpKeyFile); err != nil || key == nil { 339 t.Fatalf("expected a key file, got : %v and %v", err, key) 340 } 341 }