Browse code

Improve test for container list `ps`

Docker-DCO-1.1-Signed-off-by: Fabio Falci <fabiofalci@gmail.com> (github: fabiofalci)

Fabio Falci authored on 2014/01/27 08:10:34
Showing 1 changed files
... ...
@@ -519,6 +519,120 @@ func TestImageInsert(t *testing.T) {
519 519
 	}
520 520
 }
521 521
 
522
+func TestListContainers(t *testing.T) {
523
+	eng := NewTestEngine(t)
524
+	srv := mkServerFromEngine(eng, t)
525
+	defer mkRuntimeFromEngine(eng, t).Nuke()
526
+
527
+	config := docker.Config{
528
+		Image:     unitTestImageID,
529
+		Cmd:       []string{"/bin/sh", "-c", "cat"},
530
+		OpenStdin: true,
531
+	}
532
+
533
+	firstID := createTestContainer(eng, &config, t)
534
+	secondID := createTestContainer(eng, &config, t)
535
+	thirdID := createTestContainer(eng, &config, t)
536
+	fourthID := createTestContainer(eng, &config, t)
537
+	defer func() {
538
+		containerKill(eng, firstID, t)
539
+		containerKill(eng, secondID, t)
540
+		containerKill(eng, fourthID, t)
541
+		containerWait(eng, firstID, t)
542
+		containerWait(eng, secondID, t)
543
+		containerWait(eng, fourthID, t)
544
+	}()
545
+
546
+	startContainer(eng, firstID, t)
547
+	startContainer(eng, secondID, t)
548
+	startContainer(eng, fourthID, t)
549
+
550
+	// all
551
+	if !assertContainerList(srv, true, -1, "", "", []string{fourthID, thirdID, secondID, firstID}) {
552
+		t.Error("Container list is not in the correct order")
553
+	}
554
+
555
+	// running
556
+	if !assertContainerList(srv, false, -1, "", "", []string{fourthID, secondID, firstID}) {
557
+		t.Error("Container list is not in the correct order")
558
+	}
559
+
560
+	// from here 'all' flag is ignored
561
+
562
+	// limit
563
+	expected := []string{fourthID, thirdID}
564
+	if !assertContainerList(srv, true, 2, "", "", expected) ||
565
+		!assertContainerList(srv, false, 2, "", "", expected) {
566
+		t.Error("Container list is not in the correct order")
567
+	}
568
+
569
+	// since
570
+	expected = []string{fourthID, thirdID, secondID}
571
+	if !assertContainerList(srv, true, -1, firstID, "", expected) ||
572
+		!assertContainerList(srv, false, -1, firstID, "", expected) {
573
+		t.Error("Container list is not in the correct order")
574
+	}
575
+
576
+	// before
577
+	expected = []string{secondID, firstID}
578
+	if !assertContainerList(srv, true, -1, "", thirdID, expected) ||
579
+		!assertContainerList(srv, false, -1, "", thirdID, expected) {
580
+		t.Error("Container list is not in the correct order")
581
+	}
582
+
583
+	// since & before
584
+	expected = []string{thirdID, secondID}
585
+	if !assertContainerList(srv, true, -1, firstID, fourthID, expected) ||
586
+		!assertContainerList(srv, false, -1, firstID, fourthID, expected) {
587
+		t.Error("Container list is not in the correct order")
588
+	}
589
+
590
+	// since & limit
591
+	expected = []string{fourthID, thirdID}
592
+	if !assertContainerList(srv, true, 2, firstID, "", expected) ||
593
+		!assertContainerList(srv, false, 2, firstID, "", expected) {
594
+		t.Error("Container list is not in the correct order")
595
+	}
596
+
597
+	// before & limit
598
+	expected = []string{thirdID}
599
+	if !assertContainerList(srv, true, 1, "", fourthID, expected) ||
600
+		!assertContainerList(srv, false, 1, "", fourthID, expected) {
601
+		t.Error("Container list is not in the correct order")
602
+	}
603
+
604
+	// since & before & limit
605
+	expected = []string{thirdID}
606
+	if !assertContainerList(srv, true, 1, firstID, fourthID, expected) ||
607
+		!assertContainerList(srv, false, 1, firstID, fourthID, expected) {
608
+		t.Error("Container list is not in the correct order")
609
+	}
610
+}
611
+
612
+func assertContainerList(srv *docker.Server, all bool, limit int, since, before string, expected []string) bool {
613
+	job := srv.Eng.Job("containers")
614
+	job.SetenvBool("all", all)
615
+	job.SetenvInt("limit", limit)
616
+	job.Setenv("since", since)
617
+	job.Setenv("before", before)
618
+	outs, err := job.Stdout.AddListTable()
619
+	if err != nil {
620
+		return false
621
+	}
622
+	if err := job.Run(); err != nil {
623
+		return false
624
+	}
625
+	if len(outs.Data) != len(expected) {
626
+		return false
627
+	}
628
+	for i := 0; i < len(outs.Data); i++ {
629
+		if outs.Data[i].Get("ID") != expected[i] {
630
+			return false
631
+		}
632
+	}
633
+	return true
634
+}
635
+
522 636
 // Regression test for being able to untag an image with an existing
523 637
 // container
524 638
 func TestDeleteTagWithExistingContainers(t *testing.T) {