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