Browse code

add TestContainerApiPause case

Signed-off-by: Yuan Sun <sunyuan3@huawei.com>

Yuan Sun authored on 2015/04/10 10:14:01
Showing 2 changed files
... ...
@@ -3,14 +3,14 @@ package main
3 3
 import (
4 4
 	"bytes"
5 5
 	"encoding/json"
6
+	"github.com/docker/docker/api/types"
7
+	"github.com/docker/docker/pkg/stringid"
8
+	"github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
6 9
 	"io"
7 10
 	"os/exec"
8 11
 	"strings"
9 12
 	"testing"
10 13
 	"time"
11
-
12
-	"github.com/docker/docker/api/types"
13
-	"github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
14 14
 )
15 15
 
16 16
 func TestContainerApiGetAll(t *testing.T) {
... ...
@@ -553,3 +553,45 @@ func TestPostContainerBindNormalVolume(t *testing.T) {
553 553
 
554 554
 	logDone("container REST API - can use path from normal volume as bind-mount to overwrite another volume")
555 555
 }
556
+
557
+func TestContainerApiPause(t *testing.T) {
558
+	defer deleteAllContainers()
559
+	defer unpauseAllContainers()
560
+	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sleep", "30")
561
+	out, _, err := runCommandWithOutput(runCmd)
562
+
563
+	if err != nil {
564
+		t.Fatalf("failed to create a container: %s, %v", out, err)
565
+	}
566
+	ContainerID := strings.TrimSpace(out)
567
+
568
+	if _, err = sockRequest("POST", "/containers/"+ContainerID+"/pause", nil); err != nil && !strings.Contains(err.Error(), "204 No Content") {
569
+		t.Fatalf("POST a container pause: sockRequest failed: %v", err)
570
+	}
571
+
572
+	pausedContainers, err := getSliceOfPausedContainers()
573
+
574
+	if err != nil {
575
+		t.Fatalf("error thrown while checking if containers were paused: %v", err)
576
+	}
577
+
578
+	if len(pausedContainers) != 1 || stringid.TruncateID(ContainerID) != pausedContainers[0] {
579
+		t.Fatalf("there should be one paused container and not %d", len(pausedContainers))
580
+	}
581
+
582
+	if _, err = sockRequest("POST", "/containers/"+ContainerID+"/unpause", nil); err != nil && !strings.Contains(err.Error(), "204 No Content") {
583
+		t.Fatalf("POST a container pause: sockRequest failed: %v", err)
584
+	}
585
+
586
+	pausedContainers, err = getSliceOfPausedContainers()
587
+
588
+	if err != nil {
589
+		t.Fatalf("error thrown while checking if containers were paused: %v", err)
590
+	}
591
+
592
+	if pausedContainers != nil {
593
+		t.Fatalf("There should be no paused container.")
594
+	}
595
+
596
+	logDone("container REST API - check POST containers/pause nad unpause")
597
+}
... ...
@@ -389,6 +389,9 @@ func getPausedContainers() (string, error) {
389 389
 func getSliceOfPausedContainers() ([]string, error) {
390 390
 	out, err := getPausedContainers()
391 391
 	if err == nil {
392
+		if len(out) == 0 {
393
+			return nil, err
394
+		}
392 395
 		slice := strings.Split(strings.TrimSpace(out), "\n")
393 396
 		return slice, err
394 397
 	}