Wait for container stop after kill

pull/1/head
Brian DeHamer 9 years ago
parent 13ec7ac94e
commit 1f460997cb

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"log" "log"
"strings" "strings"
"time"
"github.com/samalba/dockerclient" "github.com/samalba/dockerclient"
) )
@ -104,6 +105,22 @@ func (client DockerClient) Stop(c Container) error {
return err return err
} }
// Wait for container to exit, but proceed anyway after 10 seconds
timeout := time.After(10 * time.Second)
PollLoop:
for {
select {
case <-timeout:
break PollLoop
default:
ci, err := client.api.InspectContainer(c.containerInfo.Id)
if err != nil || !ci.State.Running {
break PollLoop
}
time.Sleep(1 * time.Second)
}
}
return client.api.RemoveContainer(c.containerInfo.Id, true, false) return client.api.RemoveContainer(c.containerInfo.Id, true, false)
} }

@ -164,8 +164,15 @@ func TestStop_DefaultSuccess(t *testing.T) {
}, },
} }
ci := &dockerclient.ContainerInfo{
State: &dockerclient.State{
Running: false,
},
}
api := mockclient.NewMockClient() api := mockclient.NewMockClient()
api.On("KillContainer", "abc123", "SIGTERM").Return(nil) api.On("KillContainer", "abc123", "SIGTERM").Return(nil)
api.On("InspectContainer", "abc123").Return(ci, nil)
api.On("RemoveContainer", "abc123", true, false).Return(nil) api.On("RemoveContainer", "abc123", true, false).Return(nil)
client := DockerClient{api: api} client := DockerClient{api: api}
@ -185,8 +192,15 @@ func TestStop_CustomSignalSuccess(t *testing.T) {
}, },
} }
ci := &dockerclient.ContainerInfo{
State: &dockerclient.State{
Running: false,
},
}
api := mockclient.NewMockClient() api := mockclient.NewMockClient()
api.On("KillContainer", "abc123", "SIGUSR1").Return(nil) api.On("KillContainer", "abc123", "SIGUSR1").Return(nil)
api.On("InspectContainer", "abc123").Return(ci, nil)
api.On("RemoveContainer", "abc123", true, false).Return(nil) api.On("RemoveContainer", "abc123", true, false).Return(nil)
client := DockerClient{api: api} client := DockerClient{api: api}
@ -227,6 +241,7 @@ func TestStop_RemoveContainerError(t *testing.T) {
api := mockclient.NewMockClient() api := mockclient.NewMockClient()
api.On("KillContainer", "abc123", "SIGTERM").Return(nil) api.On("KillContainer", "abc123", "SIGTERM").Return(nil)
api.On("InspectContainer", "abc123").Return(&dockerclient.ContainerInfo{}, errors.New("dangit"))
api.On("RemoveContainer", "abc123", true, false).Return(errors.New("whoops")) api.On("RemoveContainer", "abc123", true, false).Return(errors.New("whoops"))
client := DockerClient{api: api} client := DockerClient{api: api}

Loading…
Cancel
Save