17296: Log when/why gateway server is disabled.
[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         "fmt"
10         "io"
11         "io/ioutil"
12         "os"
13         "os/exec"
14         "strings"
15
16         "git.arvados.org/arvados.git/sdk/go/arvados"
17         "git.arvados.org/arvados.git/sdk/go/arvadosclient"
18         "git.arvados.org/arvados.git/sdk/go/arvadostest"
19         "git.arvados.org/arvados.git/sdk/go/keepclient"
20         . "gopkg.in/check.v1"
21 )
22
23 var _ = Suite(&integrationSuite{})
24
25 type integrationSuite struct {
26         engine string
27         image  arvados.Collection
28         input  arvados.Collection
29         stdin  bytes.Buffer
30         stdout bytes.Buffer
31         stderr bytes.Buffer
32         cr     arvados.ContainerRequest
33         client *arvados.Client
34         ac     *arvadosclient.ArvadosClient
35         kc     *keepclient.KeepClient
36 }
37
38 func (s *integrationSuite) SetUpSuite(c *C) {
39         arvadostest.StartKeep(2, true)
40
41         out, err := exec.Command("docker", "load", "--input", busyboxDockerImage(c)).CombinedOutput()
42         c.Log(string(out))
43         c.Assert(err, IsNil)
44         out, err = exec.Command("arv-keepdocker", "--no-resume", "busybox:uclibc").Output()
45         imageUUID := strings.TrimSpace(string(out))
46         c.Logf("image uuid %s", imageUUID)
47         c.Assert(err, IsNil)
48         err = arvados.NewClientFromEnv().RequestAndDecode(&s.image, "GET", "arvados/v1/collections/"+imageUUID, nil, nil)
49         c.Assert(err, IsNil)
50         c.Logf("image pdh %s", s.image.PortableDataHash)
51
52         s.client = arvados.NewClientFromEnv()
53         s.ac, err = arvadosclient.New(s.client)
54         c.Assert(err, IsNil)
55         s.kc = keepclient.New(s.ac)
56         fs, err := s.input.FileSystem(s.client, s.kc)
57         c.Assert(err, IsNil)
58         f, err := fs.OpenFile("inputfile", os.O_CREATE|os.O_WRONLY, 0755)
59         c.Assert(err, IsNil)
60         _, err = f.Write([]byte("inputdata"))
61         c.Assert(err, IsNil)
62         err = f.Close()
63         c.Assert(err, IsNil)
64         s.input.ManifestText, err = fs.MarshalManifest(".")
65         c.Assert(err, IsNil)
66         err = s.client.RequestAndDecode(&s.input, "POST", "arvados/v1/collections", nil, map[string]interface{}{
67                 "ensure_unique_name": true,
68                 "collection": map[string]interface{}{
69                         "manifest_text": s.input.ManifestText,
70                 },
71         })
72         c.Assert(err, IsNil)
73         c.Logf("input pdh %s", s.input.PortableDataHash)
74 }
75
76 func (s *integrationSuite) TearDownSuite(c *C) {
77         err := s.client.RequestAndDecode(nil, "POST", "database/reset", nil, nil)
78         c.Check(err, IsNil)
79 }
80
81 func (s *integrationSuite) SetUpTest(c *C) {
82         s.engine = "docker"
83         s.stdin = bytes.Buffer{}
84         s.stdout = bytes.Buffer{}
85         s.stderr = bytes.Buffer{}
86         s.cr = arvados.ContainerRequest{
87                 Priority:       1,
88                 State:          "Committed",
89                 OutputPath:     "/mnt/out",
90                 ContainerImage: s.image.PortableDataHash,
91                 Mounts: map[string]arvados.Mount{
92                         "/mnt/json": {
93                                 Kind: "json",
94                                 Content: []interface{}{
95                                         "foo",
96                                         map[string]string{"foo": "bar"},
97                                         nil,
98                                 },
99                         },
100                         "/mnt/in": {
101                                 Kind:             "collection",
102                                 PortableDataHash: s.input.PortableDataHash,
103                         },
104                         "/mnt/out": {
105                                 Kind:     "tmp",
106                                 Capacity: 1000,
107                         },
108                 },
109                 RuntimeConstraints: arvados.RuntimeConstraints{
110                         RAM:   128000000,
111                         VCPUs: 1,
112                         API:   true,
113                 },
114         }
115 }
116
117 func (s *integrationSuite) setup(c *C) {
118         err := s.client.RequestAndDecode(&s.cr, "POST", "arvados/v1/container_requests", nil, map[string]interface{}{"container_request": map[string]interface{}{
119                 "priority":            s.cr.Priority,
120                 "state":               s.cr.State,
121                 "command":             s.cr.Command,
122                 "output_path":         s.cr.OutputPath,
123                 "container_image":     s.cr.ContainerImage,
124                 "mounts":              s.cr.Mounts,
125                 "runtime_constraints": s.cr.RuntimeConstraints,
126                 "use_existing":        false,
127         }})
128         c.Assert(err, IsNil)
129         c.Assert(s.cr.ContainerUUID, Not(Equals), "")
130         err = s.client.RequestAndDecode(nil, "POST", "arvados/v1/containers/"+s.cr.ContainerUUID+"/lock", nil, nil)
131         c.Assert(err, IsNil)
132 }
133
134 func (s *integrationSuite) TestRunTrivialContainerWithDocker(c *C) {
135         s.engine = "docker"
136         s.testRunTrivialContainer(c)
137 }
138
139 func (s *integrationSuite) TestRunTrivialContainerWithSingularity(c *C) {
140         s.engine = "singularity"
141         s.testRunTrivialContainer(c)
142 }
143
144 func (s *integrationSuite) testRunTrivialContainer(c *C) {
145         if err := exec.Command("which", s.engine).Run(); err != nil {
146                 c.Skip(fmt.Sprintf("%s: %s", s.engine, err))
147         }
148         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"}
149         s.setup(c)
150         code := command{}.RunCommand("crunch-run", []string{
151                 "-runtime-engine=" + s.engine,
152                 "-enable-memory-limit=false",
153                 s.cr.ContainerUUID,
154         }, &s.stdin, io.MultiWriter(&s.stdout, os.Stderr), io.MultiWriter(&s.stderr, os.Stderr))
155         c.Check(code, Equals, 0)
156         err := s.client.RequestAndDecode(&s.cr, "GET", "arvados/v1/container_requests/"+s.cr.UUID, nil, nil)
157         c.Assert(err, IsNil)
158         c.Logf("Finished container request: %#v", s.cr)
159
160         var log arvados.Collection
161         err = s.client.RequestAndDecode(&log, "GET", "arvados/v1/collections/"+s.cr.LogUUID, nil, nil)
162         c.Assert(err, IsNil)
163         fs, err := log.FileSystem(s.client, s.kc)
164         c.Assert(err, IsNil)
165         if d, err := fs.Open("/"); c.Check(err, IsNil) {
166                 fis, err := d.Readdir(-1)
167                 c.Assert(err, IsNil)
168                 for _, fi := range fis {
169                         if fi.IsDir() {
170                                 continue
171                         }
172                         f, err := fs.Open(fi.Name())
173                         c.Assert(err, IsNil)
174                         buf, err := ioutil.ReadAll(f)
175                         c.Assert(err, IsNil)
176                         c.Logf("\n===== %s =====\n%s", fi.Name(), buf)
177                 }
178         }
179
180         var output arvados.Collection
181         err = s.client.RequestAndDecode(&output, "GET", "arvados/v1/collections/"+s.cr.OutputUUID, nil, nil)
182         c.Assert(err, IsNil)
183         fs, err = output.FileSystem(s.client, s.kc)
184         c.Assert(err, IsNil)
185         if f, err := fs.Open("inputfile"); c.Check(err, IsNil) {
186                 defer f.Close()
187                 buf, err := ioutil.ReadAll(f)
188                 c.Check(err, IsNil)
189                 c.Check(string(buf), Equals, "inputdata")
190         }
191         if f, err := fs.Open("json"); c.Check(err, IsNil) {
192                 defer f.Close()
193                 buf, err := ioutil.ReadAll(f)
194                 c.Check(err, IsNil)
195                 c.Check(string(buf), Equals, `["foo",{"foo":"bar"},null]`)
196         }
197         if fi, err := fs.Stat("emptydir"); c.Check(err, IsNil) {
198                 c.Check(fi.IsDir(), Equals, true)
199         }
200         if d, err := fs.Open("emptydir"); c.Check(err, IsNil) {
201                 defer d.Close()
202                 fis, err := d.Readdir(-1)
203                 c.Assert(err, IsNil)
204                 // crunch-run still saves a ".keep" file to preserve
205                 // empty dirs even though that shouldn't be
206                 // necessary. Ideally we would do:
207                 // c.Check(fis, HasLen, 0)
208                 for _, fi := range fis {
209                         c.Check(fi.Name(), Equals, ".keep")
210                 }
211         }
212 }