From 697be6aaa009cd2bea5f07ae0b0780703e6565e1 Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Fri, 29 Nov 2013 09:57:59 -0800 Subject: [PATCH] Create helper function for tests --- integration/commands_test.go | 66 ++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 22 deletions(-) diff --git a/integration/commands_test.go b/integration/commands_test.go index 75d09facb4..fa8f25836c 100644 --- a/integration/commands_test.go +++ b/integration/commands_test.go @@ -32,6 +32,47 @@ func closeWrap(args ...io.Closer) error { return nil } +func setRaw(t *testing.T, c *docker.Container) *term.State { + pty, err := c.GetPtyMaster() + if err != nil { + t.Fatal(err) + } + state, err := term.MakeRaw(pty.Fd()) + if err != nil { + t.Fatal(err) + } + return state +} + +func unsetRaw(t *testing.T, c *docker.Container, state *term.State) { + pty, err := c.GetPtyMaster() + if err != nil { + t.Fatal(err) + } + term.RestoreTerminal(pty.Fd(), state) +} + +func waitContainerStart(t *testing.T, timeout time.Duration) *docker.Container { + var container *docker.Container + + setTimeout(t, "Waiting for the container to be started timed out", timeout, func() { + for { + l := globalRuntime.List() + if len(l) == 1 && l[0].State.IsRunning() { + container = l[0] + break + } + time.Sleep(10 * time.Millisecond) + } + }) + + if container == nil { + t.Fatal("An error occured while waiting for the container to start") + } + + return container +} + func setTimeout(t *testing.T, msg string, d time.Duration, f func()) { c := make(chan bool) @@ -480,18 +521,7 @@ func TestAttachDetach(t *testing.T) { } }() - var container *docker.Container - - setTimeout(t, "Waiting for the container to be started timed out", 10*time.Second, func() { - for { - l := globalRuntime.List() - if len(l) == 1 && l[0].State.IsRunning() { - container = l[0] - break - } - time.Sleep(10 * time.Millisecond) - } - }) + container := waitContainerStart(t, 10*time.Second) setTimeout(t, "Reading container's id timed out", 10*time.Second, func() { buf := make([]byte, 1024) @@ -508,16 +538,8 @@ func TestAttachDetach(t *testing.T) { <-ch }) - pty, err := container.GetPtyMaster() - if err != nil { - t.Fatal(err) - } - - state, err := term.MakeRaw(pty.Fd()) - if err != nil { - t.Fatal(err) - } - defer term.RestoreTerminal(pty.Fd(), state) + state := setRaw(t, container) + defer unsetRaw(t, container, state) stdin, stdinPipe = io.Pipe() stdout, stdoutPipe = io.Pipe()