Signed-off-by: Brian Goff <cpuguy83@gmail.com>
| ... | ... |
@@ -9,6 +9,7 @@ import ( |
| 9 | 9 |
"testing" |
| 10 | 10 |
"time" |
| 11 | 11 |
|
| 12 |
+ "github.com/docker/docker/pkg/stringid" |
|
| 12 | 13 |
"github.com/kr/pty" |
| 13 | 14 |
) |
| 14 | 15 |
|
| ... | ... |
@@ -213,3 +214,75 @@ func TestAttachDetach(t *testing.T) {
|
| 213 | 213 |
|
| 214 | 214 |
logDone("attach - detach")
|
| 215 | 215 |
} |
| 216 |
+ |
|
| 217 |
+// TestAttachDetachTruncatedID checks that attach in tty mode can be detached |
|
| 218 |
+func TestAttachDetachTruncatedID(t *testing.T) {
|
|
| 219 |
+ out, _, _ := dockerCmd(t, "run", "-itd", "busybox", "cat") |
|
| 220 |
+ id := stringid.TruncateID(strings.TrimSpace(out)) |
|
| 221 |
+ if err := waitRun(id); err != nil {
|
|
| 222 |
+ t.Fatal(err) |
|
| 223 |
+ } |
|
| 224 |
+ |
|
| 225 |
+ cpty, tty, err := pty.Open() |
|
| 226 |
+ if err != nil {
|
|
| 227 |
+ t.Fatal(err) |
|
| 228 |
+ } |
|
| 229 |
+ defer cpty.Close() |
|
| 230 |
+ |
|
| 231 |
+ cmd := exec.Command(dockerBinary, "attach", id) |
|
| 232 |
+ cmd.Stdin = tty |
|
| 233 |
+ stdout, err := cmd.StdoutPipe() |
|
| 234 |
+ if err != nil {
|
|
| 235 |
+ t.Fatal(err) |
|
| 236 |
+ } |
|
| 237 |
+ defer stdout.Close() |
|
| 238 |
+ if err := cmd.Start(); err != nil {
|
|
| 239 |
+ t.Fatal(err) |
|
| 240 |
+ } |
|
| 241 |
+ |
|
| 242 |
+ if _, err := cpty.Write([]byte("hello\n")); err != nil {
|
|
| 243 |
+ t.Fatal(err) |
|
| 244 |
+ } |
|
| 245 |
+ out, err = bufio.NewReader(stdout).ReadString('\n')
|
|
| 246 |
+ if err != nil {
|
|
| 247 |
+ t.Fatal(err) |
|
| 248 |
+ } |
|
| 249 |
+ if strings.TrimSpace(out) != "hello" {
|
|
| 250 |
+ t.Fatalf("exepected 'hello', got %q", out)
|
|
| 251 |
+ } |
|
| 252 |
+ |
|
| 253 |
+ // escape sequence |
|
| 254 |
+ if _, err := cpty.Write([]byte{16}); err != nil {
|
|
| 255 |
+ t.Fatal(err) |
|
| 256 |
+ } |
|
| 257 |
+ time.Sleep(100 * time.Millisecond) |
|
| 258 |
+ if _, err := cpty.Write([]byte{17}); err != nil {
|
|
| 259 |
+ t.Fatal(err) |
|
| 260 |
+ } |
|
| 261 |
+ |
|
| 262 |
+ ch := make(chan struct{})
|
|
| 263 |
+ go func() {
|
|
| 264 |
+ cmd.Wait() |
|
| 265 |
+ ch <- struct{}{}
|
|
| 266 |
+ }() |
|
| 267 |
+ |
|
| 268 |
+ running, err := inspectField(id, "State.Running") |
|
| 269 |
+ if err != nil {
|
|
| 270 |
+ t.Fatal(err) |
|
| 271 |
+ } |
|
| 272 |
+ if running != "true" {
|
|
| 273 |
+ t.Fatal("exepected container to still be running")
|
|
| 274 |
+ } |
|
| 275 |
+ |
|
| 276 |
+ go func() {
|
|
| 277 |
+ dockerCmd(t, "kill", id) |
|
| 278 |
+ }() |
|
| 279 |
+ |
|
| 280 |
+ select {
|
|
| 281 |
+ case <-ch: |
|
| 282 |
+ case <-time.After(10 * time.Millisecond): |
|
| 283 |
+ t.Fatal("timed out waiting for container to exit")
|
|
| 284 |
+ } |
|
| 285 |
+ |
|
| 286 |
+ logDone("attach - detach truncated ID")
|
|
| 287 |
+} |
| ... | ... |
@@ -12,7 +12,6 @@ import ( |
| 12 | 12 |
"github.com/Sirupsen/logrus" |
| 13 | 13 |
"github.com/docker/docker/api/client" |
| 14 | 14 |
"github.com/docker/docker/daemon" |
| 15 |
- "github.com/docker/docker/pkg/stringid" |
|
| 16 | 15 |
"github.com/docker/docker/pkg/term" |
| 17 | 16 |
"github.com/kr/pty" |
| 18 | 17 |
) |
| ... | ... |
@@ -113,78 +112,6 @@ func assertPipe(input, output string, r io.Reader, w io.Writer, count int) error |
| 113 | 113 |
return nil |
| 114 | 114 |
} |
| 115 | 115 |
|
| 116 |
-// TestAttachDetachTruncatedID checks that attach in tty mode can be detached |
|
| 117 |
-func TestAttachDetachTruncatedID(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 |
- // Discard the CmdRun output |
|
| 128 |
- go stdout.Read(make([]byte, 1024)) |
|
| 129 |
- setTimeout(t, "Starting container timed out", 2*time.Second, func() {
|
|
| 130 |
- if err := cli.CmdRun("-i", "-t", "-d", unitTestImageID, "cat"); err != nil {
|
|
| 131 |
- t.Fatal(err) |
|
| 132 |
- } |
|
| 133 |
- }) |
|
| 134 |
- |
|
| 135 |
- container := waitContainerStart(t, 10*time.Second) |
|
| 136 |
- |
|
| 137 |
- state := setRaw(t, container) |
|
| 138 |
- defer unsetRaw(t, container, state) |
|
| 139 |
- |
|
| 140 |
- stdout, stdoutPipe = io.Pipe() |
|
| 141 |
- cpty, tty, err = pty.Open() |
|
| 142 |
- if err != nil {
|
|
| 143 |
- t.Fatal(err) |
|
| 144 |
- } |
|
| 145 |
- |
|
| 146 |
- cli = client.NewDockerCli(tty, stdoutPipe, ioutil.Discard, "", testDaemonProto, testDaemonAddr, nil) |
|
| 147 |
- |
|
| 148 |
- ch := make(chan struct{})
|
|
| 149 |
- go func() {
|
|
| 150 |
- defer close(ch) |
|
| 151 |
- if err := cli.CmdAttach(stringid.TruncateID(container.ID)); err != nil {
|
|
| 152 |
- if err != io.ErrClosedPipe {
|
|
| 153 |
- t.Fatal(err) |
|
| 154 |
- } |
|
| 155 |
- } |
|
| 156 |
- }() |
|
| 157 |
- |
|
| 158 |
- setTimeout(t, "First read/write assertion timed out", 2*time.Second, func() {
|
|
| 159 |
- if err := assertPipe("hello\n", "hello", stdout, cpty, 150); err != nil {
|
|
| 160 |
- if err != io.ErrClosedPipe {
|
|
| 161 |
- t.Fatal(err) |
|
| 162 |
- } |
|
| 163 |
- } |
|
| 164 |
- }) |
|
| 165 |
- |
|
| 166 |
- setTimeout(t, "Escape sequence timeout", 5*time.Second, func() {
|
|
| 167 |
- cpty.Write([]byte{16})
|
|
| 168 |
- time.Sleep(100 * time.Millisecond) |
|
| 169 |
- cpty.Write([]byte{17})
|
|
| 170 |
- }) |
|
| 171 |
- |
|
| 172 |
- // wait for CmdRun to return |
|
| 173 |
- setTimeout(t, "Waiting for CmdAttach timed out", 15*time.Second, func() {
|
|
| 174 |
- <-ch |
|
| 175 |
- }) |
|
| 176 |
- closeWrap(cpty, stdout, stdoutPipe) |
|
| 177 |
- |
|
| 178 |
- time.Sleep(500 * time.Millisecond) |
|
| 179 |
- if !container.IsRunning() {
|
|
| 180 |
- t.Fatal("The detached container should be still running")
|
|
| 181 |
- } |
|
| 182 |
- |
|
| 183 |
- setTimeout(t, "Waiting for container to die timedout", 5*time.Second, func() {
|
|
| 184 |
- container.Kill() |
|
| 185 |
- }) |
|
| 186 |
-} |
|
| 187 |
- |
|
| 188 | 116 |
// Expected behaviour, the process stays alive when the client disconnects |
| 189 | 117 |
func TestAttachDisconnect(t *testing.T) {
|
| 190 | 118 |
stdout, stdoutPipe := io.Pipe() |