Merge branch 'master' into 10231-keep-cache-runtime-constraints
[arvados.git] / services / crunch-run / crunchrun_test.go
index 9880230ce8785a3d61d4c836b52f1091b65274dc..2fbbb4db97da2be13eeb5f4f37bc31ed2fb61c09 100644 (file)
@@ -14,9 +14,9 @@ import (
        . "gopkg.in/check.v1"
        "io"
        "io/ioutil"
-       "log"
        "os"
        "os/exec"
+       "path/filepath"
        "sort"
        "strings"
        "sync"
@@ -69,6 +69,7 @@ type TestDockerClient struct {
        stop        chan bool
        cwd         string
        env         []string
+       api         *ArvTestClient
 }
 
 func NewTestDockerClient() *TestDockerClient {
@@ -139,7 +140,7 @@ func (client *ArvTestClient) Create(resourceType string,
        client.Mutex.Lock()
        defer client.Mutex.Unlock()
 
-       client.Calls += 1
+       client.Calls++
        client.Content = append(client.Content, parameters)
 
        if resourceType == "logs" {
@@ -192,7 +193,7 @@ func (client *ArvTestClient) Get(resourceType string, uuid string, parameters ar
 func (client *ArvTestClient) Update(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) (err error) {
        client.Mutex.Lock()
        defer client.Mutex.Unlock()
-       client.Calls += 1
+       client.Calls++
        client.Content = append(client.Content, parameters)
        if resourceType == "containers" {
                if parameters["container"].(arvadosclient.Dict)["state"] == "Running" {
@@ -202,11 +203,17 @@ func (client *ArvTestClient) Update(resourceType string, uuid string, parameters
        return nil
 }
 
+var discoveryMap = map[string]interface{}{"defaultTrashLifetime": float64(1209600)}
+
+func (client *ArvTestClient) Discovery(key string) (interface{}, error) {
+       return discoveryMap[key], nil
+}
+
 // CalledWith returns the parameters from the first API call whose
 // parameters match jpath/string. E.g., CalledWith(c, "foo.bar",
 // "baz") returns parameters with parameters["foo"]["bar"]=="baz". If
 // no call matches, it returns nil.
-func (client *ArvTestClient) CalledWith(jpath, expect string) arvadosclient.Dict {
+func (client *ArvTestClient) CalledWith(jpath string, expect interface{}) arvadosclient.Dict {
 call:
        for _, content := range client.Content {
                var v interface{} = content
@@ -217,7 +224,7 @@ call:
                                v = dict[k]
                        }
                }
-               if v, ok := v.(string); ok && v == expect {
+               if v == expect {
                        return content
                }
        }
@@ -307,6 +314,10 @@ func (ArvErrorTestClient) Update(resourceType string, uuid string, parameters ar
        return nil
 }
 
+func (ArvErrorTestClient) Discovery(key string) (interface{}, error) {
+       return discoveryMap[key], nil
+}
+
 type KeepErrorTestClient struct{}
 
 func (KeepErrorTestClient) PutHB(hash string, buf []byte) (string, int, error) {
@@ -517,7 +528,9 @@ func FullRunHelper(c *C, record string, fn func(t *TestDockerClient)) (api *ArvT
        docker.RemoveImage(hwImageId, true)
 
        api = &ArvTestClient{Container: rec}
+       docker.api = api
        cr = NewContainerRunner(api, &KeepTestClient{}, docker, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
+       cr.statInterval = 100 * time.Millisecond
        am := &ArvMountCmdLine{}
        cr.RunArvMount = am.ArvMountTest
 
@@ -553,14 +566,45 @@ func (s *TestSuite) TestFullRunHello(c *C) {
                t.finish <- dockerclient.WaitResult{}
        })
 
-       c.Check(api.Calls, Equals, 7)
-       c.Check(api.Content[6]["container"].(arvadosclient.Dict)["exit_code"], Equals, 0)
-       c.Check(api.Content[6]["container"].(arvadosclient.Dict)["state"], Equals, "Complete")
-
+       c.Check(api.CalledWith("container.exit_code", 0), NotNil)
+       c.Check(api.CalledWith("container.state", "Complete"), NotNil)
        c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "hello world\n"), Equals, true)
 
 }
 
+func (s *TestSuite) TestCrunchstat(c *C) {
+       api, _ := FullRunHelper(c, `{
+               "command": ["sleep", "1"],
+               "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
+               "cwd": ".",
+               "environment": {},
+               "mounts": {"/tmp": {"kind": "tmp"} },
+               "output_path": "/tmp",
+               "priority": 1,
+               "runtime_constraints": {}
+       }`, func(t *TestDockerClient) {
+               time.Sleep(time.Second)
+               t.logWriter.Close()
+               t.finish <- dockerclient.WaitResult{}
+       })
+
+       c.Check(api.CalledWith("container.exit_code", 0), NotNil)
+       c.Check(api.CalledWith("container.state", "Complete"), NotNil)
+
+       // We didn't actually start a container, so crunchstat didn't
+       // find accounting files and therefore didn't log any stats.
+       // It should have logged a "can't find accounting files"
+       // message after one poll interval, though, so we can confirm
+       // it's alive:
+       c.Assert(api.Logs["crunchstat"], NotNil)
+       c.Check(api.Logs["crunchstat"].String(), Matches, `(?ms).*cgroup stats files have not appeared after 100ms.*`)
+
+       // The "files never appeared" log assures us that we called
+       // (*crunchstat.Reporter)Stop(), and that we set it up with
+       // the correct container ID "abcde":
+       c.Check(api.Logs["crunchstat"].String(), Matches, `(?ms).*cgroup stats files never appeared for abcde\n`)
+}
+
 func (s *TestSuite) TestFullRunStderr(c *C) {
        api, _ := FullRunHelper(c, `{
     "command": ["/bin/sh", "-c", "echo hello ; echo world 1>&2 ; exit 1"],
@@ -578,10 +622,10 @@ func (s *TestSuite) TestFullRunStderr(c *C) {
                t.finish <- dockerclient.WaitResult{ExitCode: 1}
        })
 
-       c.Assert(api.Calls, Equals, 8)
-       c.Check(api.Content[7]["container"].(arvadosclient.Dict)["log"], NotNil)
-       c.Check(api.Content[7]["container"].(arvadosclient.Dict)["exit_code"], Equals, 1)
-       c.Check(api.Content[7]["container"].(arvadosclient.Dict)["state"], Equals, "Complete")
+       final := api.CalledWith("container.state", "Complete")
+       c.Assert(final, NotNil)
+       c.Check(final["container"].(arvadosclient.Dict)["exit_code"], Equals, 1)
+       c.Check(final["container"].(arvadosclient.Dict)["log"], NotNil)
 
        c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "hello\n"), Equals, true)
        c.Check(strings.HasSuffix(api.Logs["stderr"].String(), "world\n"), Equals, true)
@@ -603,12 +647,9 @@ func (s *TestSuite) TestFullRunDefaultCwd(c *C) {
                t.finish <- dockerclient.WaitResult{ExitCode: 0}
        })
 
-       c.Check(api.Calls, Equals, 7)
-       c.Check(api.Content[6]["container"].(arvadosclient.Dict)["exit_code"], Equals, 0)
-       c.Check(api.Content[6]["container"].(arvadosclient.Dict)["state"], Equals, "Complete")
-
-       log.Print(api.Logs["stdout"].String())
-
+       c.Check(api.CalledWith("container.exit_code", 0), NotNil)
+       c.Check(api.CalledWith("container.state", "Complete"), NotNil)
+       c.Log(api.Logs["stdout"])
        c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "/\n"), Equals, true)
 }
 
@@ -628,10 +669,8 @@ func (s *TestSuite) TestFullRunSetCwd(c *C) {
                t.finish <- dockerclient.WaitResult{ExitCode: 0}
        })
 
-       c.Check(api.Calls, Equals, 7)
-       c.Check(api.Content[6]["container"].(arvadosclient.Dict)["exit_code"], Equals, 0)
-       c.Check(api.Content[6]["container"].(arvadosclient.Dict)["state"], Equals, "Complete")
-
+       c.Check(api.CalledWith("container.exit_code", 0), NotNil)
+       c.Check(api.CalledWith("container.state", "Complete"), NotNil)
        c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "/bin\n"), Equals, true)
 }
 
@@ -682,9 +721,8 @@ func (s *TestSuite) TestCancel(c *C) {
                }
        }
 
-       c.Assert(api.Calls, Equals, 6)
-       c.Check(api.Content[5]["container"].(arvadosclient.Dict)["log"], IsNil)
-       c.Check(api.Content[5]["container"].(arvadosclient.Dict)["state"], Equals, "Cancelled")
+       c.Check(api.CalledWith("container.log", nil), NotNil)
+       c.Check(api.CalledWith("container.state", "Cancelled"), NotNil)
        c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "foo\n"), Equals, true)
 
 }
@@ -705,10 +743,8 @@ func (s *TestSuite) TestFullRunSetEnv(c *C) {
                t.finish <- dockerclient.WaitResult{ExitCode: 0}
        })
 
-       c.Check(api.Calls, Equals, 7)
-       c.Check(api.Content[6]["container"].(arvadosclient.Dict)["exit_code"], Equals, 0)
-       c.Check(api.Content[6]["container"].(arvadosclient.Dict)["state"], Equals, "Complete")
-
+       c.Check(api.CalledWith("container.exit_code", 0), NotNil)
+       c.Check(api.CalledWith("container.state", "Complete"), NotNil)
        c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "bilbo\n"), Equals, true)
 }
 
@@ -730,73 +766,139 @@ func (s *TestSuite) TestSetupMounts(c *C) {
        am := &ArvMountCmdLine{}
        cr.RunArvMount = am.ArvMountTest
 
+       realTemp, err := ioutil.TempDir("", "crunchrun_test-")
+       c.Assert(err, IsNil)
+       defer os.RemoveAll(realTemp)
+
        i := 0
-       cr.MkTempDir = func(string, string) (string, error) {
-               i += 1
-               d := fmt.Sprintf("/tmp/mktmpdir%d", i)
-               os.Mkdir(d, os.ModePerm)
-               return d, nil
+       cr.MkTempDir = func(_ string, prefix string) (string, error) {
+               i++
+               d := fmt.Sprintf("%s/%s%d", realTemp, prefix, i)
+               err := os.Mkdir(d, os.ModePerm)
+               if err != nil && strings.Contains(err.Error(), ": file exists") {
+                       // Test case must have pre-populated the tempdir
+                       err = nil
+               }
+               return d, err
+       }
+
+       checkEmpty := func() {
+               filepath.Walk(realTemp, func(path string, _ os.FileInfo, err error) error {
+                       c.Check(path, Equals, realTemp)
+                       c.Check(err, IsNil)
+                       return nil
+               })
        }
 
        {
+               i = 0
                cr.Container.Mounts = make(map[string]arvados.Mount)
                cr.Container.Mounts["/tmp"] = arvados.Mount{Kind: "tmp"}
                cr.OutputPath = "/tmp"
 
                err := cr.SetupMounts()
                c.Check(err, IsNil)
-               c.Check(am.Cmd, DeepEquals, []string{"--foreground", "--allow-other", "--read-write", "--mount-by-pdh", "by_id", "/tmp/mktmpdir1"})
-               c.Check(cr.Binds, DeepEquals, []string{"/tmp/mktmpdir2:/tmp"})
+               c.Check(am.Cmd, DeepEquals, []string{"--foreground", "--allow-other", "--read-write", "--mount-by-pdh", "by_id", realTemp + "/keep1"})
+               c.Check(cr.Binds, DeepEquals, []string{realTemp + "/2:/tmp"})
                cr.CleanupDirs()
+               checkEmpty()
        }
 
        {
                i = 0
-               cr.Container.Mounts = make(map[string]arvados.Mount)
-               cr.Container.Mounts["/keeptmp"] = arvados.Mount{Kind: "collection", Writable: true}
+               cr.Container.Mounts = map[string]arvados.Mount{
+                       "/keeptmp": {Kind: "collection", Writable: true},
+               }
                cr.OutputPath = "/keeptmp"
 
-               os.MkdirAll("/tmp/mktmpdir1/tmp0", os.ModePerm)
+               os.MkdirAll(realTemp+"/keep1/tmp0", os.ModePerm)
 
                err := cr.SetupMounts()
                c.Check(err, IsNil)
-               c.Check(am.Cmd, DeepEquals, []string{"--foreground", "--allow-other", "--read-write", "--mount-tmp", "tmp0", "--mount-by-pdh", "by_id", "/tmp/mktmpdir1"})
-               c.Check(cr.Binds, DeepEquals, []string{"/tmp/mktmpdir1/tmp0:/keeptmp"})
+               c.Check(am.Cmd, DeepEquals, []string{"--foreground", "--allow-other", "--read-write", "--mount-tmp", "tmp0", "--mount-by-pdh", "by_id", realTemp + "/keep1"})
+               c.Check(cr.Binds, DeepEquals, []string{realTemp + "/keep1/tmp0:/keeptmp"})
                cr.CleanupDirs()
+               checkEmpty()
        }
 
        {
                i = 0
-               cr.Container.Mounts = make(map[string]arvados.Mount)
-               cr.Container.Mounts["/keepinp"] = arvados.Mount{Kind: "collection", PortableDataHash: "59389a8f9ee9d399be35462a0f92541c+53"}
-               cr.Container.Mounts["/keepout"] = arvados.Mount{Kind: "collection", Writable: true}
+               cr.Container.Mounts = map[string]arvados.Mount{
+                       "/keepinp": {Kind: "collection", PortableDataHash: "59389a8f9ee9d399be35462a0f92541c+53"},
+                       "/keepout": {Kind: "collection", Writable: true},
+               }
                cr.OutputPath = "/keepout"
 
-               os.MkdirAll("/tmp/mktmpdir1/by_id/59389a8f9ee9d399be35462a0f92541c+53", os.ModePerm)
-               os.MkdirAll("/tmp/mktmpdir1/tmp0", os.ModePerm)
+               os.MkdirAll(realTemp+"/keep1/by_id/59389a8f9ee9d399be35462a0f92541c+53", os.ModePerm)
+               os.MkdirAll(realTemp+"/keep1/tmp0", os.ModePerm)
 
                err := cr.SetupMounts()
                c.Check(err, IsNil)
-               c.Check(am.Cmd, DeepEquals, []string{"--foreground", "--allow-other", "--read-write", "--mount-tmp", "tmp0", "--mount-by-pdh", "by_id", "/tmp/mktmpdir1"})
-               var ss sort.StringSlice = cr.Binds
-               ss.Sort()
-               c.Check(cr.Binds, DeepEquals, []string{"/tmp/mktmpdir1/by_id/59389a8f9ee9d399be35462a0f92541c+53:/keepinp:ro",
-                       "/tmp/mktmpdir1/tmp0:/keepout"})
+               c.Check(am.Cmd, DeepEquals, []string{"--foreground", "--allow-other", "--read-write", "--mount-tmp", "tmp0", "--mount-by-pdh", "by_id", realTemp + "/keep1"})
+               sort.StringSlice(cr.Binds).Sort()
+               c.Check(cr.Binds, DeepEquals, []string{realTemp + "/keep1/by_id/59389a8f9ee9d399be35462a0f92541c+53:/keepinp:ro",
+                       realTemp + "/keep1/tmp0:/keepout"})
                cr.CleanupDirs()
+               checkEmpty()
+       }
+
+       {
+               i = 0
+               cr.Container.RuntimeConstraints.KeepCacheRAM = 512
+               cr.Container.Mounts = map[string]arvados.Mount{
+                       "/keepinp": {Kind: "collection", PortableDataHash: "59389a8f9ee9d399be35462a0f92541c+53"},
+                       "/keepout": {Kind: "collection", Writable: true},
+               }
+               cr.OutputPath = "/keepout"
+
+               os.MkdirAll(realTemp+"/keep1/by_id/59389a8f9ee9d399be35462a0f92541c+53", os.ModePerm)
+               os.MkdirAll(realTemp+"/keep1/tmp0", os.ModePerm)
+
+               err := cr.SetupMounts()
+               c.Check(err, IsNil)
+               c.Check(am.Cmd, DeepEquals, []string{"--foreground", "--allow-other", "--read-write", "--mount-tmp", "tmp0", "--mount-by-pdh", "by_id", realTemp + "/keep1", "--file-cache", "512"})
+               sort.StringSlice(cr.Binds).Sort()
+               c.Check(cr.Binds, DeepEquals, []string{realTemp + "/keep1/by_id/59389a8f9ee9d399be35462a0f92541c+53:/keepinp:ro",
+                       realTemp + "/keep1/tmp0:/keepout"})
+               cr.CleanupDirs()
+               checkEmpty()
+       }
+
+       for _, test := range []struct {
+               in  interface{}
+               out string
+       }{
+               {in: "foo", out: `"foo"`},
+               {in: nil, out: `null`},
+               {in: map[string]int{"foo": 123}, out: `{"foo":123}`},
+       } {
+               i = 0
+               cr.Container.Mounts = map[string]arvados.Mount{
+                       "/mnt/test.json": {Kind: "json", Content: test.in},
+               }
+               err := cr.SetupMounts()
+               c.Check(err, IsNil)
+               sort.StringSlice(cr.Binds).Sort()
+               c.Check(cr.Binds, DeepEquals, []string{realTemp + "/2/mountdata.json:/mnt/test.json:ro"})
+               content, err := ioutil.ReadFile(realTemp + "/2/mountdata.json")
+               c.Check(err, IsNil)
+               c.Check(content, DeepEquals, []byte(test.out))
+               cr.CleanupDirs()
+               checkEmpty()
        }
 }
 
 func (s *TestSuite) TestStdout(c *C) {
-       helperRecord := `{`
-       helperRecord += `"command": ["/bin/sh", "-c", "echo $FROBIZ"],`
-       helperRecord += `"container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",`
-       helperRecord += `"cwd": "/bin",`
-       helperRecord += `"environment": {"FROBIZ": "bilbo"},`
-       helperRecord += `"mounts": {"/tmp": {"kind": "tmp"}, "stdout": {"kind": "file", "path": "/tmp/a/b/c.out"} },`
-       helperRecord += `"output_path": "/tmp",`
-       helperRecord += `"priority": 1,`
-       helperRecord += `"runtime_constraints": {}`
-       helperRecord += `}`
+       helperRecord := `{
+               "command": ["/bin/sh", "-c", "echo $FROBIZ"],
+               "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
+               "cwd": "/bin",
+               "environment": {"FROBIZ": "bilbo"},
+               "mounts": {"/tmp": {"kind": "tmp"}, "stdout": {"kind": "file", "path": "/tmp/a/b/c.out"} },
+               "output_path": "/tmp",
+               "priority": 1,
+               "runtime_constraints": {}
+       }`
 
        api, _ := FullRunHelper(c, helperRecord, func(t *TestDockerClient) {
                t.logWriter.Write(dockerLog(1, t.env[0][7:]+"\n"))
@@ -804,10 +906,9 @@ func (s *TestSuite) TestStdout(c *C) {
                t.finish <- dockerclient.WaitResult{ExitCode: 0}
        })
 
-       c.Assert(api.Calls, Equals, 6)
-       c.Check(api.Content[5]["container"].(arvadosclient.Dict)["exit_code"], Equals, 0)
-       c.Check(api.Content[5]["container"].(arvadosclient.Dict)["state"], Equals, "Complete")
-       c.Check(api.CalledWith("collection.manifest_text", "./a/b 307372fa8fd5c146b22ae7a45b49bc31+6 0:6:c.out\n"), Not(IsNil))
+       c.Check(api.CalledWith("container.exit_code", 0), NotNil)
+       c.Check(api.CalledWith("container.state", "Complete"), NotNil)
+       c.Check(api.CalledWith("collection.manifest_text", "./a/b 307372fa8fd5c146b22ae7a45b49bc31+6 0:6:c.out\n"), NotNil)
 }
 
 // Used by the TestStdoutWithWrongPath*()
@@ -858,3 +959,50 @@ func (s *TestSuite) TestStdoutWithWrongKindCollection(c *C) {
        c.Check(err, NotNil)
        c.Check(strings.Contains(err.Error(), "Unsupported mount kind 'collection' for stdout"), Equals, true)
 }
+
+func (s *TestSuite) TestFullRunWithAPI(c *C) {
+       os.Setenv("ARVADOS_API_HOST", "test.arvados.org")
+       defer os.Unsetenv("ARVADOS_API_HOST")
+       api, _ := FullRunHelper(c, `{
+    "command": ["/bin/sh", "-c", "echo $ARVADOS_API_HOST"],
+    "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
+    "cwd": "/bin",
+    "environment": {},
+    "mounts": {"/tmp": {"kind": "tmp"} },
+    "output_path": "/tmp",
+    "priority": 1,
+    "runtime_constraints": {"API": true}
+}`, func(t *TestDockerClient) {
+               t.logWriter.Write(dockerLog(1, t.env[1][17:]+"\n"))
+               t.logWriter.Close()
+               t.finish <- dockerclient.WaitResult{ExitCode: 0}
+       })
+
+       c.Check(api.CalledWith("container.exit_code", 0), NotNil)
+       c.Check(api.CalledWith("container.state", "Complete"), NotNil)
+       c.Check(strings.HasSuffix(api.Logs["stdout"].String(), "test.arvados.org\n"), Equals, true)
+       c.Check(api.CalledWith("container.output", "d41d8cd98f00b204e9800998ecf8427e+0"), NotNil)
+}
+
+func (s *TestSuite) TestFullRunSetOutput(c *C) {
+       os.Setenv("ARVADOS_API_HOST", "test.arvados.org")
+       defer os.Unsetenv("ARVADOS_API_HOST")
+       api, _ := FullRunHelper(c, `{
+    "command": ["/bin/sh", "-c", "echo $ARVADOS_API_HOST"],
+    "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
+    "cwd": "/bin",
+    "environment": {},
+    "mounts": {"/tmp": {"kind": "tmp"} },
+    "output_path": "/tmp",
+    "priority": 1,
+    "runtime_constraints": {"API": true}
+}`, func(t *TestDockerClient) {
+               t.api.Container.Output = "d4ab34d3d4f8a72f5c4973051ae69fab+122"
+               t.logWriter.Close()
+               t.finish <- dockerclient.WaitResult{ExitCode: 0}
+       })
+
+       c.Check(api.CalledWith("container.exit_code", 0), NotNil)
+       c.Check(api.CalledWith("container.state", "Complete"), NotNil)
+       c.Check(api.CalledWith("container.output", "d4ab34d3d4f8a72f5c4973051ae69fab+122"), NotNil)
+}