Merge branch '18947-githttpd'
[arvados.git] / lib / crunchrun / integration_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package crunchrun
6
7 import (
8         "bytes"
9         "encoding/json"
10         "fmt"
11         "io"
12         "io/ioutil"
13         "os"
14         "os/exec"
15         "strings"
16
17         "git.arvados.org/arvados.git/lib/config"
18         "git.arvados.org/arvados.git/sdk/go/arvados"
19         "git.arvados.org/arvados.git/sdk/go/arvadosclient"
20         "git.arvados.org/arvados.git/sdk/go/arvadostest"
21         "git.arvados.org/arvados.git/sdk/go/ctxlog"
22         "git.arvados.org/arvados.git/sdk/go/keepclient"
23         . "gopkg.in/check.v1"
24 )
25
26 var _ = Suite(&integrationSuite{})
27
28 type integrationSuite struct {
29         engine string
30         image  arvados.Collection
31         input  arvados.Collection
32         stdin  bytes.Buffer
33         stdout bytes.Buffer
34         stderr bytes.Buffer
35         args   []string
36         cr     arvados.ContainerRequest
37         client *arvados.Client
38         ac     *arvadosclient.ArvadosClient
39         kc     *keepclient.KeepClient
40
41         logCollection    arvados.Collection
42         outputCollection arvados.Collection
43         logFiles         map[string]string // filename => contents
44 }
45
46 func (s *integrationSuite) SetUpSuite(c *C) {
47         _, err := exec.LookPath("docker")
48         if err != nil {
49                 c.Skip("looks like docker is not installed")
50         }
51
52         arvadostest.StartKeep(2, true)
53
54         out, err := exec.Command("docker", "load", "--input", busyboxDockerImage(c)).CombinedOutput()
55         c.Log(string(out))
56         c.Assert(err, IsNil)
57         out, err = exec.Command("arv-keepdocker", "--no-resume", "busybox:uclibc").Output()
58         imageUUID := strings.TrimSpace(string(out))
59         c.Logf("image uuid %s", imageUUID)
60         if !c.Check(err, IsNil) {
61                 if err, ok := err.(*exec.ExitError); ok {
62                         c.Logf("%s", err.Stderr)
63                 }
64                 c.Fail()
65         }
66         err = arvados.NewClientFromEnv().RequestAndDecode(&s.image, "GET", "arvados/v1/collections/"+imageUUID, nil, nil)
67         c.Assert(err, IsNil)
68         c.Logf("image pdh %s", s.image.PortableDataHash)
69
70         s.client = arvados.NewClientFromEnv()
71         s.ac, err = arvadosclient.New(s.client)
72         c.Assert(err, IsNil)
73         s.kc = keepclient.New(s.ac)
74         fs, err := s.input.FileSystem(s.client, s.kc)
75         c.Assert(err, IsNil)
76         f, err := fs.OpenFile("inputfile", os.O_CREATE|os.O_WRONLY, 0755)
77         c.Assert(err, IsNil)
78         _, err = f.Write([]byte("inputdata"))
79         c.Assert(err, IsNil)
80         err = f.Close()
81         c.Assert(err, IsNil)
82         s.input.ManifestText, err = fs.MarshalManifest(".")
83         c.Assert(err, IsNil)
84         err = s.client.RequestAndDecode(&s.input, "POST", "arvados/v1/collections", nil, map[string]interface{}{
85                 "ensure_unique_name": true,
86                 "collection": map[string]interface{}{
87                         "manifest_text": s.input.ManifestText,
88                 },
89         })
90         c.Assert(err, IsNil)
91         c.Logf("input pdh %s", s.input.PortableDataHash)
92 }
93
94 func (s *integrationSuite) TearDownSuite(c *C) {
95         os.Unsetenv("ARVADOS_KEEP_SERVICES")
96         if s.client == nil {
97                 // didn't set up
98                 return
99         }
100         err := s.client.RequestAndDecode(nil, "POST", "database/reset", nil, nil)
101         c.Check(err, IsNil)
102 }
103
104 func (s *integrationSuite) SetUpTest(c *C) {
105         os.Unsetenv("ARVADOS_KEEP_SERVICES")
106         s.engine = "docker"
107         s.args = nil
108         s.stdin = bytes.Buffer{}
109         s.stdout = bytes.Buffer{}
110         s.stderr = bytes.Buffer{}
111         s.logCollection = arvados.Collection{}
112         s.outputCollection = arvados.Collection{}
113         s.logFiles = map[string]string{}
114         s.cr = arvados.ContainerRequest{
115                 Priority:       1,
116                 State:          "Committed",
117                 OutputPath:     "/mnt/out",
118                 ContainerImage: s.image.PortableDataHash,
119                 Mounts: map[string]arvados.Mount{
120                         "/mnt/json": {
121                                 Kind: "json",
122                                 Content: []interface{}{
123                                         "foo",
124                                         map[string]string{"foo": "bar"},
125                                         nil,
126                                 },
127                         },
128                         "/mnt/in": {
129                                 Kind:             "collection",
130                                 PortableDataHash: s.input.PortableDataHash,
131                         },
132                         "/mnt/out": {
133                                 Kind:     "tmp",
134                                 Capacity: 1000,
135                         },
136                 },
137                 RuntimeConstraints: arvados.RuntimeConstraints{
138                         RAM:   128000000,
139                         VCPUs: 1,
140                         API:   true,
141                 },
142         }
143 }
144
145 func (s *integrationSuite) setup(c *C) {
146         err := s.client.RequestAndDecode(&s.cr, "POST", "arvados/v1/container_requests", nil, map[string]interface{}{"container_request": map[string]interface{}{
147                 "priority":            s.cr.Priority,
148                 "state":               s.cr.State,
149                 "command":             s.cr.Command,
150                 "output_path":         s.cr.OutputPath,
151                 "container_image":     s.cr.ContainerImage,
152                 "mounts":              s.cr.Mounts,
153                 "runtime_constraints": s.cr.RuntimeConstraints,
154                 "use_existing":        false,
155         }})
156         c.Assert(err, IsNil)
157         c.Assert(s.cr.ContainerUUID, Not(Equals), "")
158         err = s.client.RequestAndDecode(nil, "POST", "arvados/v1/containers/"+s.cr.ContainerUUID+"/lock", nil, nil)
159         c.Assert(err, IsNil)
160 }
161
162 func (s *integrationSuite) TestRunTrivialContainerWithDocker(c *C) {
163         s.engine = "docker"
164         s.testRunTrivialContainer(c)
165 }
166
167 func (s *integrationSuite) TestRunTrivialContainerWithSingularity(c *C) {
168         s.engine = "singularity"
169         s.testRunTrivialContainer(c)
170 }
171
172 func (s *integrationSuite) TestRunTrivialContainerWithLocalKeepstore(c *C) {
173         for _, trial := range []struct {
174                 logConfig           string
175                 matchGetReq         Checker
176                 matchPutReq         Checker
177                 matchStartupMessage Checker
178         }{
179                 {"none", Not(Matches), Not(Matches), Not(Matches)},
180                 {"all", Matches, Matches, Matches},
181                 {"errors", Not(Matches), Not(Matches), Matches},
182         } {
183                 c.Logf("=== testing with Containers.LocalKeepLogsToContainerLog: %q", trial.logConfig)
184                 s.SetUpTest(c)
185
186                 cfg, err := config.NewLoader(nil, ctxlog.TestLogger(c)).Load()
187                 c.Assert(err, IsNil)
188                 cluster, err := cfg.GetCluster("")
189                 c.Assert(err, IsNil)
190                 for uuid, volume := range cluster.Volumes {
191                         volume.AccessViaHosts = nil
192                         volume.Replication = 2
193                         cluster.Volumes[uuid] = volume
194                 }
195                 cluster.Containers.LocalKeepLogsToContainerLog = trial.logConfig
196
197                 s.stdin.Reset()
198                 err = json.NewEncoder(&s.stdin).Encode(ConfigData{
199                         Env:         nil,
200                         KeepBuffers: 1,
201                         Cluster:     cluster,
202                 })
203                 c.Assert(err, IsNil)
204
205                 s.engine = "docker"
206                 s.testRunTrivialContainer(c)
207
208                 log, logExists := s.logFiles["keepstore.txt"]
209                 if trial.logConfig == "none" {
210                         c.Check(logExists, Equals, false)
211                 } else {
212                         c.Check(log, trial.matchGetReq, `(?ms).*"reqMethod":"GET".*`)
213                         c.Check(log, trial.matchPutReq, `(?ms).*"reqMethod":"PUT".*,"reqPath":"0e3bcff26d51c895a60ea0d4585e134d".*`)
214                 }
215         }
216
217         // Check that (1) config is loaded from $ARVADOS_CONFIG when
218         // not provided on stdin and (2) if a local keepstore is not
219         // started, crunch-run.txt explains why not.
220         s.SetUpTest(c)
221         s.stdin.Reset()
222         s.testRunTrivialContainer(c)
223         c.Check(s.logFiles["crunch-run.txt"], Matches, `(?ms).*not starting a local keepstore process because a volume \(zzzzz-nyw5e-000000000000000\) uses AccessViaHosts\n.*`)
224
225         // Check that config read errors are logged
226         s.SetUpTest(c)
227         s.args = []string{"-config", c.MkDir() + "/config-error.yaml"}
228         s.stdin.Reset()
229         s.testRunTrivialContainer(c)
230         c.Check(s.logFiles["crunch-run.txt"], Matches, `(?ms).*could not load config file \Q`+s.args[1]+`\E:.* no such file or directory\n.*`)
231
232         s.SetUpTest(c)
233         s.args = []string{"-config", c.MkDir() + "/config-unreadable.yaml"}
234         s.stdin.Reset()
235         err := ioutil.WriteFile(s.args[1], []byte{}, 0)
236         c.Check(err, IsNil)
237         s.testRunTrivialContainer(c)
238         c.Check(s.logFiles["crunch-run.txt"], Matches, `(?ms).*could not load config file \Q`+s.args[1]+`\E:.* permission denied\n.*`)
239
240         s.SetUpTest(c)
241         s.stdin.Reset()
242         s.testRunTrivialContainer(c)
243         c.Check(s.logFiles["crunch-run.txt"], Matches, `(?ms).*loaded config file \Q`+os.Getenv("ARVADOS_CONFIG")+`\E\n.*`)
244 }
245
246 func (s *integrationSuite) testRunTrivialContainer(c *C) {
247         if err := exec.Command("which", s.engine).Run(); err != nil {
248                 c.Skip(fmt.Sprintf("%s: %s", s.engine, err))
249         }
250         s.cr.Command = []string{"sh", "-c", "cat /mnt/in/inputfile >/mnt/out/inputfile && cat /mnt/json >/mnt/out/json && ! touch /mnt/in/shouldbereadonly && mkdir /mnt/out/emptydir"}
251         s.setup(c)
252
253         args := []string{
254                 "-runtime-engine=" + s.engine,
255                 "-enable-memory-limit=false",
256         }
257         if s.stdin.Len() > 0 {
258                 args = append(args, "-stdin-config=true")
259         }
260         args = append(args, s.args...)
261         args = append(args, s.cr.ContainerUUID)
262         code := command{}.RunCommand("crunch-run", args, &s.stdin, io.MultiWriter(&s.stdout, os.Stderr), io.MultiWriter(&s.stderr, os.Stderr))
263         c.Logf("\n===== stdout =====\n%s", s.stdout.String())
264         c.Logf("\n===== stderr =====\n%s", s.stderr.String())
265         c.Check(code, Equals, 0)
266         err := s.client.RequestAndDecode(&s.cr, "GET", "arvados/v1/container_requests/"+s.cr.UUID, nil, nil)
267         c.Assert(err, IsNil)
268         c.Logf("Finished container request: %#v", s.cr)
269
270         var log arvados.Collection
271         err = s.client.RequestAndDecode(&log, "GET", "arvados/v1/collections/"+s.cr.LogUUID, nil, nil)
272         c.Assert(err, IsNil)
273         fs, err := log.FileSystem(s.client, s.kc)
274         c.Assert(err, IsNil)
275         if d, err := fs.Open("/"); c.Check(err, IsNil) {
276                 fis, err := d.Readdir(-1)
277                 c.Assert(err, IsNil)
278                 for _, fi := range fis {
279                         if fi.IsDir() {
280                                 continue
281                         }
282                         f, err := fs.Open(fi.Name())
283                         c.Assert(err, IsNil)
284                         buf, err := ioutil.ReadAll(f)
285                         c.Assert(err, IsNil)
286                         c.Logf("\n===== %s =====\n%s", fi.Name(), buf)
287                         s.logFiles[fi.Name()] = string(buf)
288                 }
289         }
290         s.logCollection = log
291
292         var output arvados.Collection
293         err = s.client.RequestAndDecode(&output, "GET", "arvados/v1/collections/"+s.cr.OutputUUID, nil, nil)
294         c.Assert(err, IsNil)
295         fs, err = output.FileSystem(s.client, s.kc)
296         c.Assert(err, IsNil)
297         if f, err := fs.Open("inputfile"); c.Check(err, IsNil) {
298                 defer f.Close()
299                 buf, err := ioutil.ReadAll(f)
300                 c.Check(err, IsNil)
301                 c.Check(string(buf), Equals, "inputdata")
302         }
303         if f, err := fs.Open("json"); c.Check(err, IsNil) {
304                 defer f.Close()
305                 buf, err := ioutil.ReadAll(f)
306                 c.Check(err, IsNil)
307                 c.Check(string(buf), Equals, `["foo",{"foo":"bar"},null]`)
308         }
309         if fi, err := fs.Stat("emptydir"); c.Check(err, IsNil) {
310                 c.Check(fi.IsDir(), Equals, true)
311         }
312         if d, err := fs.Open("emptydir"); c.Check(err, IsNil) {
313                 defer d.Close()
314                 fis, err := d.Readdir(-1)
315                 c.Assert(err, IsNil)
316                 // crunch-run still saves a ".keep" file to preserve
317                 // empty dirs even though that shouldn't be
318                 // necessary. Ideally we would do:
319                 // c.Check(fis, HasLen, 0)
320                 for _, fi := range fis {
321                         c.Check(fi.Name(), Equals, ".keep")
322                 }
323         }
324         s.outputCollection = output
325 }