14328: Check ContainerInspect instead of List, give up on error.
[arvados.git] / services / crunch-run / crunchrun_test.go
index 8d8e0400003a94dae160ee65a69ccd92f723c823..eb4f220e227d399b31b0669b15ba77c75449e557 100644 (file)
@@ -47,6 +47,7 @@ var _ = Suite(&TestSuite{})
 type TestSuite struct {
        client *arvados.Client
        docker *TestDockerClient
+       runner *ContainerRunner
 }
 
 func (s *TestSuite) SetUpTest(c *C) {
@@ -103,6 +104,7 @@ type TestDockerClient struct {
        api         *ArvTestClient
        realTemp    string
        calledWait  bool
+       ctrExited   bool
 }
 
 func NewTestDockerClient() *TestDockerClient {
@@ -176,6 +178,17 @@ func (t *TestDockerClient) ContainerWait(ctx context.Context, container string,
        return body, err
 }
 
+func (t *TestDockerClient) ContainerInspect(ctx context.Context, id string) (c dockertypes.ContainerJSON, err error) {
+       c.ContainerJSONBase = &dockertypes.ContainerJSONBase{}
+       c.ID = "abcde"
+       if t.ctrExited {
+               c.State = &dockertypes.ContainerState{Status: "exited", Dead: true}
+       } else {
+               c.State = &dockertypes.ContainerState{Status: "running", Pid: 1234, Running: true}
+       }
+       return
+}
+
 func (t *TestDockerClient) ImageInspectWithRaw(ctx context.Context, image string) (dockertypes.ImageInspect, []byte, error) {
        if t.exitCode == 2 {
                return dockertypes.ImageInspect{}, nil, fmt.Errorf("Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?")
@@ -230,6 +243,7 @@ func (client *ArvTestClient) Create(resourceType string,
                mt := parameters["collection"].(arvadosclient.Dict)["manifest_text"].(string)
                outmap := output.(*arvados.Collection)
                outmap.PortableDataHash = fmt.Sprintf("%x+%d", md5.Sum([]byte(mt)), len(mt))
+               outmap.UUID = fmt.Sprintf("zzzzz-4zz18-%15.15x", md5.Sum([]byte(mt)))
        }
 
        return nil
@@ -316,6 +330,10 @@ func (client *ArvTestClient) Update(resourceType string, uuid string, parameters
                if parameters["container"].(arvadosclient.Dict)["state"] == "Running" {
                        client.WasSetRunning = true
                }
+       } else if resourceType == "collections" {
+               mt := parameters["collection"].(arvadosclient.Dict)["manifest_text"].(string)
+               output.(*arvados.Collection).UUID = uuid
+               output.(*arvados.Collection).PortableDataHash = fmt.Sprintf("%x", md5.Sum([]byte(mt)))
        }
        return nil
 }
@@ -731,7 +749,9 @@ func (s *TestSuite) fullRunHelper(c *C, record string, extraMounts []string, exi
        defer kc.Close()
        cr, err = NewContainerRunner(s.client, api, kc, s.docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
        c.Assert(err, IsNil)
+       s.runner = cr
        cr.statInterval = 100 * time.Millisecond
+       cr.containerWatchdogInterval = time.Second
        am := &ArvMountCmdLine{}
        cr.RunArvMount = am.ArvMountTest
 
@@ -825,6 +845,24 @@ func (s *TestSuite) TestRunTimeExceeded(c *C) {
        c.Check(api.Logs["crunch-run"].String(), Matches, "(?ms).*maximum run time exceeded.*")
 }
 
+func (s *TestSuite) TestContainerWaitFails(c *C) {
+       api, _, _ := s.fullRunHelper(c, `{
+    "command": ["sleep", "3"],
+    "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
+    "cwd": ".",
+    "mounts": {"/tmp": {"kind": "tmp"} },
+    "output_path": "/tmp",
+    "priority": 1
+}`, nil, 0, func(t *TestDockerClient) {
+               t.ctrExited = true
+               time.Sleep(10 * time.Second)
+               t.logWriter.Close()
+       })
+
+       c.Check(api.CalledWith("container.state", "Cancelled"), NotNil)
+       c.Check(api.Logs["crunch-run"].String(), Matches, "(?ms).*Container is not running.*")
+}
+
 func (s *TestSuite) TestCrunchstat(c *C) {
        api, _, _ := s.fullRunHelper(c, `{
                "command": ["sleep", "1"],
@@ -1142,7 +1180,7 @@ func (s *TestSuite) TestSetupMounts(c *C) {
                cr.ArvMountPoint = ""
                cr.Container.Mounts = make(map[string]arvados.Mount)
                cr.Container.Mounts["/tmp"] = arvados.Mount{Kind: "tmp"}
-               cr.OutputPath = "/tmp"
+               cr.Container.OutputPath = "/tmp"
                cr.statInterval = 5 * time.Second
                err := cr.SetupMounts()
                c.Check(err, IsNil)
@@ -1161,7 +1199,7 @@ func (s *TestSuite) TestSetupMounts(c *C) {
                cr.Container.Mounts = make(map[string]arvados.Mount)
                cr.Container.Mounts["/out"] = arvados.Mount{Kind: "tmp"}
                cr.Container.Mounts["/tmp"] = arvados.Mount{Kind: "tmp"}
-               cr.OutputPath = "/out"
+               cr.Container.OutputPath = "/out"
 
                err := cr.SetupMounts()
                c.Check(err, IsNil)
@@ -1179,7 +1217,7 @@ func (s *TestSuite) TestSetupMounts(c *C) {
                cr.ArvMountPoint = ""
                cr.Container.Mounts = make(map[string]arvados.Mount)
                cr.Container.Mounts["/tmp"] = arvados.Mount{Kind: "tmp"}
-               cr.OutputPath = "/tmp"
+               cr.Container.OutputPath = "/tmp"
 
                apiflag := true
                cr.Container.RuntimeConstraints.API = &apiflag
@@ -1203,7 +1241,7 @@ func (s *TestSuite) TestSetupMounts(c *C) {
                cr.Container.Mounts = map[string]arvados.Mount{
                        "/keeptmp": {Kind: "collection", Writable: true},
                }
-               cr.OutputPath = "/keeptmp"
+               cr.Container.OutputPath = "/keeptmp"
 
                os.MkdirAll(realTemp+"/keep1/tmp0", os.ModePerm)
 
@@ -1225,7 +1263,7 @@ func (s *TestSuite) TestSetupMounts(c *C) {
                        "/keepinp": {Kind: "collection", PortableDataHash: "59389a8f9ee9d399be35462a0f92541c+53"},
                        "/keepout": {Kind: "collection", Writable: true},
                }
-               cr.OutputPath = "/keepout"
+               cr.Container.OutputPath = "/keepout"
 
                os.MkdirAll(realTemp+"/keep1/by_id/59389a8f9ee9d399be35462a0f92541c+53", os.ModePerm)
                os.MkdirAll(realTemp+"/keep1/tmp0", os.ModePerm)
@@ -1251,7 +1289,7 @@ func (s *TestSuite) TestSetupMounts(c *C) {
                        "/keepinp": {Kind: "collection", PortableDataHash: "59389a8f9ee9d399be35462a0f92541c+53"},
                        "/keepout": {Kind: "collection", Writable: true},
                }
-               cr.OutputPath = "/keepout"
+               cr.Container.OutputPath = "/keepout"
 
                os.MkdirAll(realTemp+"/keep1/by_id/59389a8f9ee9d399be35462a0f92541c+53", os.ModePerm)
                os.MkdirAll(realTemp+"/keep1/tmp0", os.ModePerm)
@@ -1332,7 +1370,7 @@ func (s *TestSuite) TestSetupMounts(c *C) {
                        "/tmp":     {Kind: "tmp"},
                        "/tmp/foo": {Kind: "collection"},
                }
-               cr.OutputPath = "/tmp"
+               cr.Container.OutputPath = "/tmp"
 
                os.MkdirAll(realTemp+"/keep1/tmp0", os.ModePerm)
 
@@ -1362,7 +1400,7 @@ func (s *TestSuite) TestSetupMounts(c *C) {
                                Path:             "baz",
                                Writable:         true},
                }
-               cr.OutputPath = "/tmp"
+               cr.Container.OutputPath = "/tmp"
 
                os.MkdirAll(realTemp+"/keep1/by_id/59389a8f9ee9d399be35462a0f92541c+53", os.ModePerm)
                os.MkdirAll(realTemp+"/keep1/by_id/59389a8f9ee9d399be35462a0f92541d+53/baz", os.ModePerm)
@@ -1391,7 +1429,7 @@ func (s *TestSuite) TestSetupMounts(c *C) {
                        "/tmp":     {Kind: "tmp"},
                        "/tmp/foo": {Kind: "tmp"},
                }
-               cr.OutputPath = "/tmp"
+               cr.Container.OutputPath = "/tmp"
 
                err := cr.SetupMounts()
                c.Check(err, NotNil)
@@ -1439,7 +1477,7 @@ func (s *TestSuite) TestSetupMounts(c *C) {
                                Path:   "/",
                        },
                }
-               cr.OutputPath = "/tmp"
+               cr.Container.OutputPath = "/tmp"
 
                err := cr.SetupMounts()
                c.Check(err, IsNil)