github.com/feiyang21687/docker@v1.5.0/integration-cli/docker_cli_top_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os/exec"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestTopMultipleArgs(t *testing.T) {
    10  	runCmd := exec.Command(dockerBinary, "run", "-i", "-d", "busybox", "sleep", "20")
    11  	out, _, err := runCommandWithOutput(runCmd)
    12  	if err != nil {
    13  		t.Fatalf("failed to start the container: %s, %v", out, err)
    14  	}
    15  
    16  	cleanedContainerID := stripTrailingCharacters(out)
    17  	defer deleteContainer(cleanedContainerID)
    18  
    19  	topCmd := exec.Command(dockerBinary, "top", cleanedContainerID, "-o", "pid")
    20  	out, _, err = runCommandWithOutput(topCmd)
    21  	if err != nil {
    22  		t.Fatalf("failed to run top: %s, %v", out, err)
    23  	}
    24  
    25  	if !strings.Contains(out, "PID") {
    26  		t.Fatalf("did not see PID after top -o pid: %s", out)
    27  	}
    28  
    29  	logDone("top - multiple arguments")
    30  }
    31  
    32  func TestTopNonPrivileged(t *testing.T) {
    33  	runCmd := exec.Command(dockerBinary, "run", "-i", "-d", "busybox", "sleep", "20")
    34  	out, _, err := runCommandWithOutput(runCmd)
    35  	if err != nil {
    36  		t.Fatalf("failed to start the container: %s, %v", out, err)
    37  	}
    38  
    39  	cleanedContainerID := stripTrailingCharacters(out)
    40  
    41  	topCmd := exec.Command(dockerBinary, "top", cleanedContainerID)
    42  	out1, _, err := runCommandWithOutput(topCmd)
    43  	if err != nil {
    44  		t.Fatalf("failed to run top: %s, %v", out1, err)
    45  	}
    46  
    47  	topCmd = exec.Command(dockerBinary, "top", cleanedContainerID)
    48  	out2, _, err := runCommandWithOutput(topCmd)
    49  	if err != nil {
    50  		t.Fatalf("failed to run top: %s, %v", out2, err)
    51  	}
    52  
    53  	killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID)
    54  	if out, _, err = runCommandWithOutput(killCmd); err != nil {
    55  		t.Fatalf("failed to kill container: %s, %v", out, err)
    56  	}
    57  
    58  	deleteContainer(cleanedContainerID)
    59  
    60  	if !strings.Contains(out1, "sleep 20") && !strings.Contains(out2, "sleep 20") {
    61  		t.Fatal("top should've listed `sleep 20` in the process list, but failed twice")
    62  	} else if !strings.Contains(out1, "sleep 20") {
    63  		t.Fatal("top should've listed `sleep 20` in the process list, but failed the first time")
    64  	} else if !strings.Contains(out2, "sleep 20") {
    65  		t.Fatal("top should've listed `sleep 20` in the process list, but failed the second itime")
    66  	}
    67  
    68  	logDone("top - sleep process should be listed in non privileged mode")
    69  }
    70  
    71  func TestTopPrivileged(t *testing.T) {
    72  	runCmd := exec.Command(dockerBinary, "run", "--privileged", "-i", "-d", "busybox", "sleep", "20")
    73  	out, _, err := runCommandWithOutput(runCmd)
    74  	if err != nil {
    75  		t.Fatalf("failed to start the container: %s, %v", out, err)
    76  	}
    77  
    78  	cleanedContainerID := stripTrailingCharacters(out)
    79  
    80  	topCmd := exec.Command(dockerBinary, "top", cleanedContainerID)
    81  	out1, _, err := runCommandWithOutput(topCmd)
    82  	if err != nil {
    83  		t.Fatalf("failed to run top: %s, %v", out1, err)
    84  	}
    85  
    86  	topCmd = exec.Command(dockerBinary, "top", cleanedContainerID)
    87  	out2, _, err := runCommandWithOutput(topCmd)
    88  	if err != nil {
    89  		t.Fatalf("failed to run top: %s, %v", out2, err)
    90  	}
    91  
    92  	killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID)
    93  	if out, _, err = runCommandWithOutput(killCmd); err != nil {
    94  		t.Fatalf("failed to kill container: %s, %v", out, err)
    95  	}
    96  
    97  	deleteContainer(cleanedContainerID)
    98  
    99  	if !strings.Contains(out1, "sleep 20") && !strings.Contains(out2, "sleep 20") {
   100  		t.Fatal("top should've listed `sleep 20` in the process list, but failed twice")
   101  	} else if !strings.Contains(out1, "sleep 20") {
   102  		t.Fatal("top should've listed `sleep 20` in the process list, but failed the first time")
   103  	} else if !strings.Contains(out2, "sleep 20") {
   104  		t.Fatal("top should've listed `sleep 20` in the process list, but failed the second itime")
   105  	}
   106  
   107  	logDone("top - sleep process should be listed in privileged mode")
   108  }