Docker-DCO-1.1-Signed-off-by: Vishnu Kannan <vishnuk@google.com> (github: vishh)
| ... | ... |
@@ -1916,3 +1916,78 @@ func TestRunPortInUse(t *testing.T) {
|
| 1916 | 1916 |
deleteAllContainers() |
| 1917 | 1917 |
logDone("run - fail if port already in use")
|
| 1918 | 1918 |
} |
| 1919 |
+ |
|
| 1920 |
+// "test" should be printed by docker exec |
|
| 1921 |
+func TestDockerExec(t *testing.T) {
|
|
| 1922 |
+ runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && sleep 100") |
|
| 1923 |
+ out, _, _, err := runCommandWithStdoutStderr(runCmd) |
|
| 1924 |
+ errorOut(err, t, out) |
|
| 1925 |
+ |
|
| 1926 |
+ execCmd := exec.Command(dockerBinary, "exec", "testing", "cat", "/tmp/file") |
|
| 1927 |
+ |
|
| 1928 |
+ out, _, err = runCommandWithOutput(execCmd) |
|
| 1929 |
+ errorOut(err, t, out) |
|
| 1930 |
+ |
|
| 1931 |
+ out = strings.Trim(out, "\r\n") |
|
| 1932 |
+ |
|
| 1933 |
+ if expected := "test"; out != expected {
|
|
| 1934 |
+ t.Errorf("container exec should've printed %q but printed %q", expected, out)
|
|
| 1935 |
+ } |
|
| 1936 |
+ |
|
| 1937 |
+ deleteAllContainers() |
|
| 1938 |
+ |
|
| 1939 |
+ logDone("exec - basic test")
|
|
| 1940 |
+} |
|
| 1941 |
+ |
|
| 1942 |
+// "test" should be printed by docker exec |
|
| 1943 |
+func TestDockerExecInteractive(t *testing.T) {
|
|
| 1944 |
+ runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && sleep 100") |
|
| 1945 |
+ out, _, _, err := runCommandWithStdoutStderr(runCmd) |
|
| 1946 |
+ errorOut(err, t, out) |
|
| 1947 |
+ |
|
| 1948 |
+ execCmd := exec.Command(dockerBinary, "exec", "-i", "testing", "sh") |
|
| 1949 |
+ stdin, err := execCmd.StdinPipe() |
|
| 1950 |
+ if err != nil {
|
|
| 1951 |
+ t.Fatal(err) |
|
| 1952 |
+ } |
|
| 1953 |
+ stdout, err := execCmd.StdoutPipe() |
|
| 1954 |
+ if err != nil {
|
|
| 1955 |
+ t.Fatal(err) |
|
| 1956 |
+ } |
|
| 1957 |
+ |
|
| 1958 |
+ if err := execCmd.Start(); err != nil {
|
|
| 1959 |
+ t.Fatal(err) |
|
| 1960 |
+ } |
|
| 1961 |
+ if _, err := stdin.Write([]byte("cat /tmp/file\n")); err != nil {
|
|
| 1962 |
+ t.Fatal(err) |
|
| 1963 |
+ } |
|
| 1964 |
+ |
|
| 1965 |
+ r := bufio.NewReader(stdout) |
|
| 1966 |
+ line, err := r.ReadString('\n')
|
|
| 1967 |
+ if err != nil {
|
|
| 1968 |
+ t.Fatal(err) |
|
| 1969 |
+ } |
|
| 1970 |
+ line = strings.TrimSpace(line) |
|
| 1971 |
+ if line != "test" {
|
|
| 1972 |
+ t.Fatalf("Output should be 'test', got '%q'", line)
|
|
| 1973 |
+ } |
|
| 1974 |
+ if err := stdin.Close(); err != nil {
|
|
| 1975 |
+ t.Fatal(err) |
|
| 1976 |
+ } |
|
| 1977 |
+ finish := make(chan struct{})
|
|
| 1978 |
+ go func() {
|
|
| 1979 |
+ if err := execCmd.Wait(); err != nil {
|
|
| 1980 |
+ t.Fatal(err) |
|
| 1981 |
+ } |
|
| 1982 |
+ close(finish) |
|
| 1983 |
+ }() |
|
| 1984 |
+ select {
|
|
| 1985 |
+ case <-finish: |
|
| 1986 |
+ case <-time.After(1 * time.Second): |
|
| 1987 |
+ t.Fatal("docker exec failed to exit on stdin close")
|
|
| 1988 |
+ } |
|
| 1989 |
+ |
|
| 1990 |
+ deleteAllContainers() |
|
| 1991 |
+ |
|
| 1992 |
+ logDone("exec - Interactive test")
|
|
| 1993 |
+} |