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