2014-02-25 11:17:48 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-08-31 08:53:05 -04:00
|
|
|
func TestTopMultipleArgs(t *testing.T) {
|
2015-04-08 19:32:45 -04:00
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-i", "-d", "busybox", "top")
|
2014-08-31 08:53:05 -04:00
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
2014-10-14 16:59:44 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to start the container: %s, %v", out, err)
|
|
|
|
}
|
2014-08-31 08:53:05 -04:00
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-08-31 08:53:05 -04:00
|
|
|
defer deleteContainer(cleanedContainerID)
|
|
|
|
|
|
|
|
topCmd := exec.Command(dockerBinary, "top", cleanedContainerID, "-o", "pid")
|
|
|
|
out, _, err = runCommandWithOutput(topCmd)
|
2014-10-14 16:59:44 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to run top: %s, %v", out, err)
|
|
|
|
}
|
2014-08-31 08:53:05 -04:00
|
|
|
|
|
|
|
if !strings.Contains(out, "PID") {
|
2014-10-14 16:59:44 -04:00
|
|
|
t.Fatalf("did not see PID after top -o pid: %s", out)
|
2014-08-31 08:53:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
logDone("top - multiple arguments")
|
|
|
|
}
|
|
|
|
|
2014-04-09 07:43:19 -04:00
|
|
|
func TestTopNonPrivileged(t *testing.T) {
|
2015-04-08 19:32:45 -04:00
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-i", "-d", "busybox", "top")
|
2014-02-25 11:17:48 -05:00
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
2014-10-14 16:59:44 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to start the container: %s, %v", out, err)
|
|
|
|
}
|
2014-02-25 11:17:48 -05:00
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-02-25 11:17:48 -05:00
|
|
|
|
|
|
|
topCmd := exec.Command(dockerBinary, "top", cleanedContainerID)
|
2014-10-14 16:59:44 -04:00
|
|
|
out1, _, err := runCommandWithOutput(topCmd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to run top: %s, %v", out1, err)
|
|
|
|
}
|
2014-02-25 11:17:48 -05:00
|
|
|
|
2014-04-16 20:38:08 -04:00
|
|
|
topCmd = exec.Command(dockerBinary, "top", cleanedContainerID)
|
2014-10-14 16:59:44 -04:00
|
|
|
out2, _, err := runCommandWithOutput(topCmd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to run top: %s, %v", out2, err)
|
|
|
|
}
|
2014-04-16 20:38:08 -04:00
|
|
|
|
2014-02-25 11:17:48 -05:00
|
|
|
killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID)
|
2014-10-14 16:59:44 -04:00
|
|
|
if out, _, err = runCommandWithOutput(killCmd); err != nil {
|
|
|
|
t.Fatalf("failed to kill container: %s, %v", out, err)
|
|
|
|
}
|
2014-02-25 11:17:48 -05:00
|
|
|
|
2014-04-03 20:22:32 -04:00
|
|
|
deleteContainer(cleanedContainerID)
|
2014-02-25 11:17:48 -05:00
|
|
|
|
2015-04-08 19:32:45 -04:00
|
|
|
if !strings.Contains(out1, "top") && !strings.Contains(out2, "top") {
|
|
|
|
t.Fatal("top should've listed `top` in the process list, but failed twice")
|
|
|
|
} else if !strings.Contains(out1, "top") {
|
|
|
|
t.Fatal("top should've listed `top` in the process list, but failed the first time")
|
|
|
|
} else if !strings.Contains(out2, "top") {
|
|
|
|
t.Fatal("top should've listed `top` in the process list, but failed the second itime")
|
2014-02-25 11:17:48 -05:00
|
|
|
}
|
|
|
|
|
2015-04-08 19:32:45 -04:00
|
|
|
logDone("top - top process should be listed in non privileged mode")
|
2014-04-09 07:43:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTopPrivileged(t *testing.T) {
|
2015-04-08 19:32:45 -04:00
|
|
|
runCmd := exec.Command(dockerBinary, "run", "--privileged", "-i", "-d", "busybox", "top")
|
2014-04-09 07:43:19 -04:00
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
2014-10-14 16:59:44 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to start the container: %s, %v", out, err)
|
|
|
|
}
|
2014-04-09 07:43:19 -04:00
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-04-09 07:43:19 -04:00
|
|
|
|
|
|
|
topCmd := exec.Command(dockerBinary, "top", cleanedContainerID)
|
2014-10-14 16:59:44 -04:00
|
|
|
out1, _, err := runCommandWithOutput(topCmd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to run top: %s, %v", out1, err)
|
|
|
|
}
|
2014-04-09 07:43:19 -04:00
|
|
|
|
2014-04-16 20:38:08 -04:00
|
|
|
topCmd = exec.Command(dockerBinary, "top", cleanedContainerID)
|
2014-10-14 16:59:44 -04:00
|
|
|
out2, _, err := runCommandWithOutput(topCmd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to run top: %s, %v", out2, err)
|
|
|
|
}
|
2014-04-16 20:38:08 -04:00
|
|
|
|
2014-04-09 07:43:19 -04:00
|
|
|
killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID)
|
2014-10-14 16:59:44 -04:00
|
|
|
if out, _, err = runCommandWithOutput(killCmd); err != nil {
|
|
|
|
t.Fatalf("failed to kill container: %s, %v", out, err)
|
|
|
|
}
|
2014-04-09 07:43:19 -04:00
|
|
|
|
|
|
|
deleteContainer(cleanedContainerID)
|
|
|
|
|
2015-04-08 19:32:45 -04:00
|
|
|
if !strings.Contains(out1, "top") && !strings.Contains(out2, "top") {
|
|
|
|
t.Fatal("top should've listed `top` in the process list, but failed twice")
|
|
|
|
} else if !strings.Contains(out1, "top") {
|
|
|
|
t.Fatal("top should've listed `top` in the process list, but failed the first time")
|
|
|
|
} else if !strings.Contains(out2, "top") {
|
|
|
|
t.Fatal("top should've listed `top` in the process list, but failed the second itime")
|
2014-04-09 07:43:19 -04:00
|
|
|
}
|
|
|
|
|
2015-04-08 19:32:45 -04:00
|
|
|
logDone("top - top process should be listed in privileged mode")
|
2014-02-25 11:17:48 -05:00
|
|
|
}
|