github.com/jandre/docker@v1.7.0/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 if !strings.Contains(out1, "top") && !strings.Contains(out2, "top") { 58 c.Fatal("top should've listed `top` in the process list, but failed twice") 59 } else if !strings.Contains(out1, "top") { 60 c.Fatal("top should've listed `top` in the process list, but failed the first time") 61 } else if !strings.Contains(out2, "top") { 62 c.Fatal("top should've listed `top` in the process list, but failed the second itime") 63 } 64 65 } 66 67 func (s *DockerSuite) TestTopPrivileged(c *check.C) { 68 runCmd := exec.Command(dockerBinary, "run", "--privileged", "-i", "-d", "busybox", "top") 69 out, _, err := runCommandWithOutput(runCmd) 70 if err != nil { 71 c.Fatalf("failed to start the container: %s, %v", out, err) 72 } 73 74 cleanedContainerID := strings.TrimSpace(out) 75 76 topCmd := exec.Command(dockerBinary, "top", cleanedContainerID) 77 out1, _, err := runCommandWithOutput(topCmd) 78 if err != nil { 79 c.Fatalf("failed to run top: %s, %v", out1, err) 80 } 81 82 topCmd = exec.Command(dockerBinary, "top", cleanedContainerID) 83 out2, _, err := runCommandWithOutput(topCmd) 84 if err != nil { 85 c.Fatalf("failed to run top: %s, %v", out2, err) 86 } 87 88 killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID) 89 if out, _, err = runCommandWithOutput(killCmd); err != nil { 90 c.Fatalf("failed to kill container: %s, %v", out, err) 91 } 92 93 if !strings.Contains(out1, "top") && !strings.Contains(out2, "top") { 94 c.Fatal("top should've listed `top` in the process list, but failed twice") 95 } else if !strings.Contains(out1, "top") { 96 c.Fatal("top should've listed `top` in the process list, but failed the first time") 97 } else if !strings.Contains(out2, "top") { 98 c.Fatal("top should've listed `top` in the process list, but failed the second itime") 99 } 100 101 }