libcontainerd: fix leaking container/exec state

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi
2017-11-13 14:53:56 -08:00
parent aea31ab242
commit 6c4ce7cb6c
3 changed files with 70 additions and 6 deletions

View File

@@ -8,6 +8,8 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
"github.com/docker/docker/api/types"
@@ -198,6 +200,45 @@ func (s *DockerSuite) TestExecAPIStartInvalidCommand(c *check.C) {
c.Assert(inspectJSON.ExecIDs, checker.IsNil)
}
func (s *DockerSuite) TestExecStateCleanup(c *check.C) {
testRequires(c, DaemonIsLinux, SameHostDaemon)
// This test checks accidental regressions. Not part of stable API.
name := "exec_cleanup"
cid, _ := dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh")
cid = strings.TrimSpace(cid)
stateDir := "/var/run/docker/containerd/" + cid
checkReadDir := func(c *check.C) (interface{}, check.CommentInterface) {
fi, err := ioutil.ReadDir(stateDir)
c.Assert(err, checker.IsNil)
return len(fi), nil
}
fi, err := ioutil.ReadDir(stateDir)
c.Assert(err, checker.IsNil)
c.Assert(len(fi), checker.GreaterThan, 1)
id := createExecCmd(c, name, "ls")
startExec(c, id, http.StatusOK)
waitForExec(c, id)
waitAndAssert(c, 5*time.Second, checkReadDir, checker.Equals, len(fi))
id = createExecCmd(c, name, "invalid")
startExec(c, id, http.StatusBadRequest)
waitForExec(c, id)
waitAndAssert(c, 5*time.Second, checkReadDir, checker.Equals, len(fi))
dockerCmd(c, "stop", name)
_, err = os.Stat(stateDir)
c.Assert(err, checker.NotNil)
c.Assert(os.IsNotExist(err), checker.True)
}
func createExec(c *check.C, name string) string {
return createExecCmd(c, name, "true")
}