| ... | ... |
@@ -5,6 +5,7 @@ import ( |
| 5 | 5 |
"os/exec" |
| 6 | 6 |
"regexp" |
| 7 | 7 |
"strings" |
| 8 |
+ "sync" |
|
| 8 | 9 |
"testing" |
| 9 | 10 |
) |
| 10 | 11 |
|
| ... | ... |
@@ -436,3 +437,81 @@ func TestExitCode(t *testing.T) {
|
| 436 | 436 |
|
| 437 | 437 |
logDone("run - correct exit code")
|
| 438 | 438 |
} |
| 439 |
+ |
|
| 440 |
+func TestUserDefaultsToRoot(t *testing.T) {
|
|
| 441 |
+ cmd := exec.Command(dockerBinary, "run", "busybox", "id") |
|
| 442 |
+ |
|
| 443 |
+ out, _, err := runCommandWithOutput(cmd) |
|
| 444 |
+ if err != nil {
|
|
| 445 |
+ t.Fatal(err, out) |
|
| 446 |
+ } |
|
| 447 |
+ if !strings.Contains(out, "uid=0(root) gid=0(root)") {
|
|
| 448 |
+ t.Fatalf("expected root user got %s", out)
|
|
| 449 |
+ } |
|
| 450 |
+ deleteAllContainers() |
|
| 451 |
+ |
|
| 452 |
+ logDone("run - default user")
|
|
| 453 |
+} |
|
| 454 |
+ |
|
| 455 |
+func TestUserByName(t *testing.T) {
|
|
| 456 |
+ cmd := exec.Command(dockerBinary, "run", "-u", "root", "busybox", "id") |
|
| 457 |
+ |
|
| 458 |
+ out, _, err := runCommandWithOutput(cmd) |
|
| 459 |
+ if err != nil {
|
|
| 460 |
+ t.Fatal(err, out) |
|
| 461 |
+ } |
|
| 462 |
+ if !strings.Contains(out, "uid=0(root) gid=0(root)") {
|
|
| 463 |
+ t.Fatalf("expected root user got %s", out)
|
|
| 464 |
+ } |
|
| 465 |
+ deleteAllContainers() |
|
| 466 |
+ |
|
| 467 |
+ logDone("run - user by name")
|
|
| 468 |
+} |
|
| 469 |
+ |
|
| 470 |
+func TestUserByID(t *testing.T) {
|
|
| 471 |
+ cmd := exec.Command(dockerBinary, "run", "-u", "1", "busybox", "id") |
|
| 472 |
+ |
|
| 473 |
+ out, _, err := runCommandWithOutput(cmd) |
|
| 474 |
+ if err != nil {
|
|
| 475 |
+ t.Fatal(err, out) |
|
| 476 |
+ } |
|
| 477 |
+ if !strings.Contains(out, "uid=1(daemon) gid=1(daemon)") {
|
|
| 478 |
+ t.Fatalf("expected daemon user got %s", out)
|
|
| 479 |
+ } |
|
| 480 |
+ deleteAllContainers() |
|
| 481 |
+ |
|
| 482 |
+ logDone("run - user by id")
|
|
| 483 |
+} |
|
| 484 |
+ |
|
| 485 |
+func TestUserNotFound(t *testing.T) {
|
|
| 486 |
+ cmd := exec.Command(dockerBinary, "run", "-u", "notme", "busybox", "id") |
|
| 487 |
+ |
|
| 488 |
+ _, err := runCommand(cmd) |
|
| 489 |
+ if err == nil {
|
|
| 490 |
+ t.Fatal("unknown user should cause container to fail")
|
|
| 491 |
+ } |
|
| 492 |
+ deleteAllContainers() |
|
| 493 |
+ |
|
| 494 |
+ logDone("run - user not found")
|
|
| 495 |
+} |
|
| 496 |
+ |
|
| 497 |
+func TestRunTwoConcurrentContainers(t *testing.T) {
|
|
| 498 |
+ group := sync.WaitGroup{}
|
|
| 499 |
+ group.Add(2) |
|
| 500 |
+ |
|
| 501 |
+ for i := 0; i < 2; i++ {
|
|
| 502 |
+ go func() {
|
|
| 503 |
+ defer group.Done() |
|
| 504 |
+ cmd := exec.Command(dockerBinary, "run", "busybox", "sleep", "2") |
|
| 505 |
+ if _, err := runCommand(cmd); err != nil {
|
|
| 506 |
+ t.Fatal(err) |
|
| 507 |
+ } |
|
| 508 |
+ }() |
|
| 509 |
+ } |
|
| 510 |
+ |
|
| 511 |
+ group.Wait() |
|
| 512 |
+ |
|
| 513 |
+ deleteAllContainers() |
|
| 514 |
+ |
|
| 515 |
+ logDone("run - two concurrent containers")
|
|
| 516 |
+} |
| ... | ... |
@@ -216,190 +216,6 @@ func TestRestartStdin(t *testing.T) {
|
| 216 | 216 |
} |
| 217 | 217 |
} |
| 218 | 218 |
|
| 219 |
-func TestUser(t *testing.T) {
|
|
| 220 |
- daemon := mkDaemon(t) |
|
| 221 |
- defer nuke(daemon) |
|
| 222 |
- |
|
| 223 |
- // Default user must be root |
|
| 224 |
- container, _, err := daemon.Create(&runconfig.Config{
|
|
| 225 |
- Image: GetTestImage(daemon).ID, |
|
| 226 |
- Cmd: []string{"id"},
|
|
| 227 |
- }, |
|
| 228 |
- "", |
|
| 229 |
- ) |
|
| 230 |
- if err != nil {
|
|
| 231 |
- t.Fatal(err) |
|
| 232 |
- } |
|
| 233 |
- defer daemon.Destroy(container) |
|
| 234 |
- output, err := container.Output() |
|
| 235 |
- if err != nil {
|
|
| 236 |
- t.Fatal(err) |
|
| 237 |
- } |
|
| 238 |
- if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
|
|
| 239 |
- t.Error(string(output)) |
|
| 240 |
- } |
|
| 241 |
- |
|
| 242 |
- // Set a username |
|
| 243 |
- container, _, err = daemon.Create(&runconfig.Config{
|
|
| 244 |
- Image: GetTestImage(daemon).ID, |
|
| 245 |
- Cmd: []string{"id"},
|
|
| 246 |
- |
|
| 247 |
- User: "root", |
|
| 248 |
- }, |
|
| 249 |
- "", |
|
| 250 |
- ) |
|
| 251 |
- if err != nil {
|
|
| 252 |
- t.Fatal(err) |
|
| 253 |
- } |
|
| 254 |
- defer daemon.Destroy(container) |
|
| 255 |
- output, err = container.Output() |
|
| 256 |
- if code := container.State.GetExitCode(); err != nil || code != 0 {
|
|
| 257 |
- t.Fatal(err) |
|
| 258 |
- } |
|
| 259 |
- if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
|
|
| 260 |
- t.Error(string(output)) |
|
| 261 |
- } |
|
| 262 |
- |
|
| 263 |
- // Set a UID |
|
| 264 |
- container, _, err = daemon.Create(&runconfig.Config{
|
|
| 265 |
- Image: GetTestImage(daemon).ID, |
|
| 266 |
- Cmd: []string{"id"},
|
|
| 267 |
- |
|
| 268 |
- User: "0", |
|
| 269 |
- }, |
|
| 270 |
- "", |
|
| 271 |
- ) |
|
| 272 |
- if code := container.State.GetExitCode(); err != nil || code != 0 {
|
|
| 273 |
- t.Fatal(err) |
|
| 274 |
- } |
|
| 275 |
- defer daemon.Destroy(container) |
|
| 276 |
- output, err = container.Output() |
|
| 277 |
- if code := container.State.GetExitCode(); err != nil || code != 0 {
|
|
| 278 |
- t.Fatal(err) |
|
| 279 |
- } |
|
| 280 |
- if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
|
|
| 281 |
- t.Error(string(output)) |
|
| 282 |
- } |
|
| 283 |
- |
|
| 284 |
- // Set a different user by uid |
|
| 285 |
- container, _, err = daemon.Create(&runconfig.Config{
|
|
| 286 |
- Image: GetTestImage(daemon).ID, |
|
| 287 |
- Cmd: []string{"id"},
|
|
| 288 |
- |
|
| 289 |
- User: "1", |
|
| 290 |
- }, |
|
| 291 |
- "", |
|
| 292 |
- ) |
|
| 293 |
- if err != nil {
|
|
| 294 |
- t.Fatal(err) |
|
| 295 |
- } |
|
| 296 |
- defer daemon.Destroy(container) |
|
| 297 |
- output, err = container.Output() |
|
| 298 |
- if err != nil {
|
|
| 299 |
- t.Fatal(err) |
|
| 300 |
- } else if code := container.State.GetExitCode(); code != 0 {
|
|
| 301 |
- t.Fatalf("Container exit code is invalid: %d\nOutput:\n%s\n", code, output)
|
|
| 302 |
- } |
|
| 303 |
- if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
|
|
| 304 |
- t.Error(string(output)) |
|
| 305 |
- } |
|
| 306 |
- |
|
| 307 |
- // Set a different user by username |
|
| 308 |
- container, _, err = daemon.Create(&runconfig.Config{
|
|
| 309 |
- Image: GetTestImage(daemon).ID, |
|
| 310 |
- Cmd: []string{"id"},
|
|
| 311 |
- |
|
| 312 |
- User: "daemon", |
|
| 313 |
- }, |
|
| 314 |
- "", |
|
| 315 |
- ) |
|
| 316 |
- if err != nil {
|
|
| 317 |
- t.Fatal(err) |
|
| 318 |
- } |
|
| 319 |
- defer daemon.Destroy(container) |
|
| 320 |
- output, err = container.Output() |
|
| 321 |
- if code := container.State.GetExitCode(); err != nil || code != 0 {
|
|
| 322 |
- t.Fatal(err) |
|
| 323 |
- } |
|
| 324 |
- if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
|
|
| 325 |
- t.Error(string(output)) |
|
| 326 |
- } |
|
| 327 |
- |
|
| 328 |
- // Test an wrong username |
|
| 329 |
- container, _, err = daemon.Create(&runconfig.Config{
|
|
| 330 |
- Image: GetTestImage(daemon).ID, |
|
| 331 |
- Cmd: []string{"id"},
|
|
| 332 |
- |
|
| 333 |
- User: "unknownuser", |
|
| 334 |
- }, |
|
| 335 |
- "", |
|
| 336 |
- ) |
|
| 337 |
- if err != nil {
|
|
| 338 |
- t.Fatal(err) |
|
| 339 |
- } |
|
| 340 |
- defer daemon.Destroy(container) |
|
| 341 |
- output, err = container.Output() |
|
| 342 |
- if container.State.GetExitCode() == 0 {
|
|
| 343 |
- t.Fatal("Starting container with wrong uid should fail but it passed.")
|
|
| 344 |
- } |
|
| 345 |
-} |
|
| 346 |
- |
|
| 347 |
-func TestMultipleContainers(t *testing.T) {
|
|
| 348 |
- daemon := mkDaemon(t) |
|
| 349 |
- defer nuke(daemon) |
|
| 350 |
- |
|
| 351 |
- container1, _, err := daemon.Create(&runconfig.Config{
|
|
| 352 |
- Image: GetTestImage(daemon).ID, |
|
| 353 |
- Cmd: []string{"sleep", "2"},
|
|
| 354 |
- }, |
|
| 355 |
- "", |
|
| 356 |
- ) |
|
| 357 |
- if err != nil {
|
|
| 358 |
- t.Fatal(err) |
|
| 359 |
- } |
|
| 360 |
- defer daemon.Destroy(container1) |
|
| 361 |
- |
|
| 362 |
- container2, _, err := daemon.Create(&runconfig.Config{
|
|
| 363 |
- Image: GetTestImage(daemon).ID, |
|
| 364 |
- Cmd: []string{"sleep", "2"},
|
|
| 365 |
- }, |
|
| 366 |
- "", |
|
| 367 |
- ) |
|
| 368 |
- if err != nil {
|
|
| 369 |
- t.Fatal(err) |
|
| 370 |
- } |
|
| 371 |
- defer daemon.Destroy(container2) |
|
| 372 |
- |
|
| 373 |
- // Start both containers |
|
| 374 |
- if err := container1.Start(); err != nil {
|
|
| 375 |
- t.Fatal(err) |
|
| 376 |
- } |
|
| 377 |
- if err := container2.Start(); err != nil {
|
|
| 378 |
- t.Fatal(err) |
|
| 379 |
- } |
|
| 380 |
- |
|
| 381 |
- // Make sure they are running before trying to kill them |
|
| 382 |
- container1.WaitTimeout(250 * time.Millisecond) |
|
| 383 |
- container2.WaitTimeout(250 * time.Millisecond) |
|
| 384 |
- |
|
| 385 |
- // If we are here, both containers should be running |
|
| 386 |
- if !container1.State.IsRunning() {
|
|
| 387 |
- t.Fatal("Container not running")
|
|
| 388 |
- } |
|
| 389 |
- if !container2.State.IsRunning() {
|
|
| 390 |
- t.Fatal("Container not running")
|
|
| 391 |
- } |
|
| 392 |
- |
|
| 393 |
- // Kill them |
|
| 394 |
- if err := container1.Kill(); err != nil {
|
|
| 395 |
- t.Fatal(err) |
|
| 396 |
- } |
|
| 397 |
- |
|
| 398 |
- if err := container2.Kill(); err != nil {
|
|
| 399 |
- t.Fatal(err) |
|
| 400 |
- } |
|
| 401 |
-} |
|
| 402 |
- |
|
| 403 | 219 |
func TestStdin(t *testing.T) {
|
| 404 | 220 |
daemon := mkDaemon(t) |
| 405 | 221 |
defer nuke(daemon) |