Signed-off-by: Brian Goff <cpuguy83@gmail.com>
| ... | ... |
@@ -3,6 +3,7 @@ |
| 3 | 3 |
package main |
| 4 | 4 |
|
| 5 | 5 |
import ( |
| 6 |
+ "bufio" |
|
| 6 | 7 |
"fmt" |
| 7 | 8 |
"io/ioutil" |
| 8 | 9 |
"os" |
| ... | ... |
@@ -201,3 +202,73 @@ func TestRunDeviceDirectory(t *testing.T) {
|
| 201 | 201 |
|
| 202 | 202 |
logDone("run - test --device directory mounts all internal devices")
|
| 203 | 203 |
} |
| 204 |
+ |
|
| 205 |
+// TestRunDetach checks attaching and detaching with the escape sequence. |
|
| 206 |
+func TestRunAttachDetach(t *testing.T) {
|
|
| 207 |
+ defer deleteAllContainers() |
|
| 208 |
+ name := "attach-detach" |
|
| 209 |
+ cmd := exec.Command(dockerBinary, "run", "--name", name, "-it", "busybox", "cat") |
|
| 210 |
+ stdout, err := cmd.StdoutPipe() |
|
| 211 |
+ if err != nil {
|
|
| 212 |
+ t.Fatal(err) |
|
| 213 |
+ } |
|
| 214 |
+ cpty, tty, err := pty.Open() |
|
| 215 |
+ if err != nil {
|
|
| 216 |
+ t.Fatal(err) |
|
| 217 |
+ } |
|
| 218 |
+ defer cpty.Close() |
|
| 219 |
+ cmd.Stdin = tty |
|
| 220 |
+ if err := cmd.Start(); err != nil {
|
|
| 221 |
+ t.Fatal(err) |
|
| 222 |
+ } |
|
| 223 |
+ if err := waitRun(name); err != nil {
|
|
| 224 |
+ t.Fatal(err) |
|
| 225 |
+ } |
|
| 226 |
+ |
|
| 227 |
+ if _, err := cpty.Write([]byte("hello\n")); err != nil {
|
|
| 228 |
+ t.Fatal(err) |
|
| 229 |
+ } |
|
| 230 |
+ |
|
| 231 |
+ out, err := bufio.NewReader(stdout).ReadString('\n')
|
|
| 232 |
+ if err != nil {
|
|
| 233 |
+ t.Fatal(err) |
|
| 234 |
+ } |
|
| 235 |
+ if strings.TrimSpace(out) != "hello" {
|
|
| 236 |
+ t.Fatalf("exepected 'hello', got %q", out)
|
|
| 237 |
+ } |
|
| 238 |
+ |
|
| 239 |
+ // escape sequence |
|
| 240 |
+ if _, err := cpty.Write([]byte{16}); err != nil {
|
|
| 241 |
+ t.Fatal(err) |
|
| 242 |
+ } |
|
| 243 |
+ time.Sleep(100 * time.Millisecond) |
|
| 244 |
+ if _, err := cpty.Write([]byte{17}); err != nil {
|
|
| 245 |
+ t.Fatal(err) |
|
| 246 |
+ } |
|
| 247 |
+ |
|
| 248 |
+ ch := make(chan struct{})
|
|
| 249 |
+ go func() {
|
|
| 250 |
+ cmd.Wait() |
|
| 251 |
+ ch <- struct{}{}
|
|
| 252 |
+ }() |
|
| 253 |
+ |
|
| 254 |
+ running, err := inspectField(name, "State.Running") |
|
| 255 |
+ if err != nil {
|
|
| 256 |
+ t.Fatal(err) |
|
| 257 |
+ } |
|
| 258 |
+ if running != "true" {
|
|
| 259 |
+ t.Fatal("exepected container to still be running")
|
|
| 260 |
+ } |
|
| 261 |
+ |
|
| 262 |
+ go func() {
|
|
| 263 |
+ dockerCmd(t, "kill", name) |
|
| 264 |
+ }() |
|
| 265 |
+ |
|
| 266 |
+ select {
|
|
| 267 |
+ case <-ch: |
|
| 268 |
+ case <-time.After(10 * time.Millisecond): |
|
| 269 |
+ t.Fatal("timed out waiting for container to exit")
|
|
| 270 |
+ } |
|
| 271 |
+ |
|
| 272 |
+ logDone("run - attach detach")
|
|
| 273 |
+} |
| ... | ... |
@@ -213,7 +213,16 @@ func waitInspect(name, expr, expected string, timeout int) error {
|
| 213 | 213 |
cmd := exec.Command(dockerBinary, "inspect", "-f", expr, name) |
| 214 | 214 |
out, _, err := runCommandWithOutput(cmd) |
| 215 | 215 |
if err != nil {
|
| 216 |
- return fmt.Errorf("error executing docker inspect: %v", err)
|
|
| 216 |
+ if !strings.Contains(out, "No such") {
|
|
| 217 |
+ return fmt.Errorf("error executing docker inspect: %v\n%s", err, out)
|
|
| 218 |
+ } |
|
| 219 |
+ select {
|
|
| 220 |
+ case <-after: |
|
| 221 |
+ return err |
|
| 222 |
+ default: |
|
| 223 |
+ time.Sleep(10 * time.Millisecond) |
|
| 224 |
+ continue |
|
| 225 |
+ } |
|
| 217 | 226 |
} |
| 218 | 227 |
|
| 219 | 228 |
out = strings.TrimSpace(out) |
| ... | ... |
@@ -113,56 +113,6 @@ func assertPipe(input, output string, r io.Reader, w io.Writer, count int) error |
| 113 | 113 |
return nil |
| 114 | 114 |
} |
| 115 | 115 |
|
| 116 |
-// TestRunDetach checks attaching and detaching with the escape sequence. |
|
| 117 |
-func TestRunDetach(t *testing.T) {
|
|
| 118 |
- stdout, stdoutPipe := io.Pipe() |
|
| 119 |
- cpty, tty, err := pty.Open() |
|
| 120 |
- if err != nil {
|
|
| 121 |
- t.Fatal(err) |
|
| 122 |
- } |
|
| 123 |
- |
|
| 124 |
- cli := client.NewDockerCli(tty, stdoutPipe, ioutil.Discard, "", testDaemonProto, testDaemonAddr, nil) |
|
| 125 |
- defer cleanup(globalEngine, t) |
|
| 126 |
- |
|
| 127 |
- ch := make(chan struct{})
|
|
| 128 |
- go func() {
|
|
| 129 |
- defer close(ch) |
|
| 130 |
- cli.CmdRun("-i", "-t", unitTestImageID, "cat")
|
|
| 131 |
- }() |
|
| 132 |
- |
|
| 133 |
- container := waitContainerStart(t, 10*time.Second) |
|
| 134 |
- |
|
| 135 |
- state := setRaw(t, container) |
|
| 136 |
- defer unsetRaw(t, container, state) |
|
| 137 |
- |
|
| 138 |
- setTimeout(t, "First read/write assertion timed out", 2*time.Second, func() {
|
|
| 139 |
- if err := assertPipe("hello\n", "hello", stdout, cpty, 150); err != nil {
|
|
| 140 |
- t.Fatal(err) |
|
| 141 |
- } |
|
| 142 |
- }) |
|
| 143 |
- |
|
| 144 |
- setTimeout(t, "Escape sequence timeout", 5*time.Second, func() {
|
|
| 145 |
- cpty.Write([]byte{16})
|
|
| 146 |
- time.Sleep(100 * time.Millisecond) |
|
| 147 |
- cpty.Write([]byte{17})
|
|
| 148 |
- }) |
|
| 149 |
- |
|
| 150 |
- // wait for CmdRun to return |
|
| 151 |
- setTimeout(t, "Waiting for CmdRun timed out", 15*time.Second, func() {
|
|
| 152 |
- <-ch |
|
| 153 |
- }) |
|
| 154 |
- closeWrap(cpty, stdout, stdoutPipe) |
|
| 155 |
- |
|
| 156 |
- time.Sleep(500 * time.Millisecond) |
|
| 157 |
- if !container.IsRunning() {
|
|
| 158 |
- t.Fatal("The detached container should be still running")
|
|
| 159 |
- } |
|
| 160 |
- |
|
| 161 |
- setTimeout(t, "Waiting for container to die timed out", 20*time.Second, func() {
|
|
| 162 |
- container.Kill() |
|
| 163 |
- }) |
|
| 164 |
-} |
|
| 165 |
- |
|
| 166 | 116 |
// TestAttachDetach checks that attach in tty mode can be detached using the long container ID |
| 167 | 117 |
func TestAttachDetach(t *testing.T) {
|
| 168 | 118 |
stdout, stdoutPipe := io.Pipe() |