1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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 "git.arvados.org/arvados.git/services/keepstore"
27 var _ = Suite(&integrationSuite{})
29 type integrationSuite struct {
31 image arvados.Collection
32 input arvados.Collection
37 cr arvados.ContainerRequest
38 client *arvados.Client
39 ac *arvadosclient.ArvadosClient
40 kc *keepclient.KeepClient
42 logCollection arvados.Collection
43 outputCollection arvados.Collection
44 logFiles map[string]string // filename => contents
47 func (s *integrationSuite) SetUpSuite(c *C) {
48 _, err := exec.LookPath("docker")
50 c.Skip("looks like docker is not installed")
53 arvadostest.StartKeep(2, true)
55 out, err := exec.Command("docker", "load", "--input", arvadostest.BusyboxDockerImage(c)).CombinedOutput()
58 out, err = exec.Command("arv-keepdocker", "--no-resume", "busybox:uclibc").Output()
59 imageUUID := strings.TrimSpace(string(out))
60 c.Logf("image uuid %s", imageUUID)
61 if !c.Check(err, IsNil) {
62 if err, ok := err.(*exec.ExitError); ok {
63 c.Logf("%s", err.Stderr)
67 err = arvados.NewClientFromEnv().RequestAndDecode(&s.image, "GET", "arvados/v1/collections/"+imageUUID, nil, nil)
69 c.Logf("image pdh %s", s.image.PortableDataHash)
71 s.client = arvados.NewClientFromEnv()
72 s.ac, err = arvadosclient.New(s.client)
74 s.kc = keepclient.New(s.ac)
75 fs, err := s.input.FileSystem(s.client, s.kc)
77 f, err := fs.OpenFile("inputfile", os.O_CREATE|os.O_WRONLY, 0755)
79 _, err = f.Write([]byte("inputdata"))
83 s.input.ManifestText, err = fs.MarshalManifest(".")
85 err = s.client.RequestAndDecode(&s.input, "POST", "arvados/v1/collections", nil, map[string]interface{}{
86 "ensure_unique_name": true,
87 "collection": map[string]interface{}{
88 "manifest_text": s.input.ManifestText,
92 c.Logf("input pdh %s", s.input.PortableDataHash)
95 func (s *integrationSuite) TearDownSuite(c *C) {
96 os.Unsetenv("ARVADOS_KEEP_SERVICES")
101 err := s.client.RequestAndDecode(nil, "POST", "database/reset", nil, nil)
105 func (s *integrationSuite) SetUpTest(c *C) {
106 os.Unsetenv("ARVADOS_KEEP_SERVICES")
109 s.stdin = bytes.Buffer{}
110 s.stdout = bytes.Buffer{}
111 s.stderr = bytes.Buffer{}
112 s.logCollection = arvados.Collection{}
113 s.outputCollection = arvados.Collection{}
114 s.logFiles = map[string]string{}
115 s.cr = arvados.ContainerRequest{
118 OutputPath: "/mnt/out",
119 ContainerImage: s.image.PortableDataHash,
120 Mounts: map[string]arvados.Mount{
123 Content: []interface{}{
125 map[string]string{"foo": "bar"},
131 PortableDataHash: s.input.PortableDataHash,
138 RuntimeConstraints: arvados.RuntimeConstraints{
146 func (s *integrationSuite) setup(c *C) {
147 err := s.client.RequestAndDecode(&s.cr, "POST", "arvados/v1/container_requests", nil, map[string]interface{}{"container_request": map[string]interface{}{
148 "priority": s.cr.Priority,
150 "command": s.cr.Command,
151 "output_path": s.cr.OutputPath,
152 "container_image": s.cr.ContainerImage,
153 "mounts": s.cr.Mounts,
154 "runtime_constraints": s.cr.RuntimeConstraints,
155 "use_existing": false,
158 c.Assert(s.cr.ContainerUUID, Not(Equals), "")
159 err = s.client.RequestAndDecode(nil, "POST", "arvados/v1/containers/"+s.cr.ContainerUUID+"/lock", nil, nil)
163 func (s *integrationSuite) TestRunTrivialContainerWithDocker(c *C) {
165 s.testRunTrivialContainer(c)
166 c.Check(s.logFiles["crunch-run.txt"], Matches, `(?ms).*Using container runtime: docker Engine \d+\.\d+.*`)
169 func (s *integrationSuite) TestRunTrivialContainerWithSingularity(c *C) {
170 s.engine = "singularity"
171 s.testRunTrivialContainer(c)
172 c.Check(s.logFiles["crunch-run.txt"], Matches, `(?ms).*Using container runtime: singularity.* version 3\.\d+.*`)
175 func (s *integrationSuite) TestRunTrivialContainerWithLocalKeepstore(c *C) {
176 for _, trial := range []struct {
180 matchStartupMessage Checker
182 {"none", Not(Matches), Not(Matches), Not(Matches)},
183 {"all", Matches, Matches, Matches},
184 {"errors", Not(Matches), Not(Matches), Matches},
186 c.Logf("=== testing with Containers.LocalKeepLogsToContainerLog: %q", trial.logConfig)
189 cfg, err := config.NewLoader(nil, ctxlog.TestLogger(c)).Load()
191 cluster, err := cfg.GetCluster("")
193 for uuid, volume := range cluster.Volumes {
194 volume.AccessViaHosts = nil
195 volume.Replication = 2
196 cluster.Volumes[uuid] = volume
198 var v keepstore.UnixVolume
199 err = json.Unmarshal(volume.DriverParameters, &v)
201 err = os.Mkdir(v.Root, 0777)
202 if !os.IsExist(err) {
206 cluster.Containers.LocalKeepLogsToContainerLog = trial.logConfig
209 err = json.NewEncoder(&s.stdin).Encode(ConfigData{
217 s.testRunTrivialContainer(c)
219 log, logExists := s.logFiles["keepstore.txt"]
220 if trial.logConfig == "none" {
221 c.Check(logExists, Equals, false)
223 c.Check(log, trial.matchGetReq, `(?ms).*"reqMethod":"GET".*`)
224 c.Check(log, trial.matchPutReq, `(?ms).*"reqMethod":"PUT".*,"reqPath":"0e3bcff26d51c895a60ea0d4585e134d".*`)
227 c.Check(s.logFiles["crunch-run.txt"], Matches, `(?ms).*using local keepstore process .* at http://[\d\.]{7,}:\d+.*`)
228 c.Check(s.logFiles["crunch-run.txt"], Not(Matches), `(?ms).* at http://127\..*`)
229 c.Check(s.logFiles["crunch-run.txt"], Not(Matches), `(?ms).* at http://169\.254\..*`)
230 c.Check(s.logFiles["stderr.txt"], Matches, `(?ms).*ARVADOS_KEEP_SERVICES=http://[\d\.]{7,}:\d+\n.*`)
234 func (s *integrationSuite) TestRunTrivialContainerWithNoLocalKeepstore(c *C) {
235 // Check that (1) config is loaded from $ARVADOS_CONFIG when
236 // not provided on stdin and (2) if a local keepstore is not
237 // started, crunch-run.txt explains why not.
240 s.testRunTrivialContainer(c)
241 c.Check(s.logFiles["crunch-run.txt"], Matches, `(?ms).*not starting a local keepstore process because KeepBuffers=0 in config\n.*`)
244 s.args = []string{"-config", c.MkDir() + "/config.yaml"}
246 buf, err := ioutil.ReadFile(os.Getenv("ARVADOS_CONFIG"))
248 err = ioutil.WriteFile(s.args[1], bytes.Replace(buf, []byte("LocalKeepBlobBuffersPerVCPU: 0"), []byte("LocalKeepBlobBuffersPerVCPU: 1"), -1), 0666)
250 s.testRunTrivialContainer(c)
251 c.Check(s.logFiles["crunch-run.txt"], Matches, `(?ms).*not starting a local keepstore process because a volume \(zzzzz-nyw5e-00000000000000\d\) uses AccessViaHosts\n.*`)
253 // Check that config read errors are logged
255 s.args = []string{"-config", c.MkDir() + "/config-error.yaml"}
257 s.testRunTrivialContainer(c)
258 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.*`)
261 s.args = []string{"-config", c.MkDir() + "/config-unreadable.yaml"}
263 err = ioutil.WriteFile(s.args[1], []byte{}, 0)
265 s.testRunTrivialContainer(c)
266 c.Check(s.logFiles["crunch-run.txt"], Matches, `(?ms).*could not load config file \Q`+s.args[1]+`\E:.* permission denied\n.*`)
270 s.testRunTrivialContainer(c)
271 c.Check(s.logFiles["crunch-run.txt"], Matches, `(?ms).*loaded config file \Q`+os.Getenv("ARVADOS_CONFIG")+`\E\n.*`)
274 func (s *integrationSuite) testRunTrivialContainer(c *C) {
275 if err := exec.Command("which", s.engine).Run(); err != nil {
276 c.Skip(fmt.Sprintf("%s: %s", s.engine, err))
278 s.cr.Command = []string{"sh", "-c", "env >&2 && cat /mnt/in/inputfile >/mnt/out/inputfile && cat /mnt/json >/mnt/out/json && ! touch /mnt/in/shouldbereadonly && mkdir /mnt/out/emptydir"}
282 "-runtime-engine=" + s.engine,
283 "-enable-memory-limit=false",
285 if s.stdin.Len() > 0 {
286 args = append(args, "-stdin-config=true")
288 args = append(args, s.args...)
289 args = append(args, s.cr.ContainerUUID)
290 code := command{}.RunCommand("crunch-run", args, &s.stdin, io.MultiWriter(&s.stdout, os.Stderr), io.MultiWriter(&s.stderr, os.Stderr))
291 c.Logf("\n===== stdout =====\n%s", s.stdout.String())
292 c.Logf("\n===== stderr =====\n%s", s.stderr.String())
293 c.Check(code, Equals, 0)
294 err := s.client.RequestAndDecode(&s.cr, "GET", "arvados/v1/container_requests/"+s.cr.UUID, nil, nil)
296 c.Logf("Finished container request: %#v", s.cr)
298 var log arvados.Collection
299 err = s.client.RequestAndDecode(&log, "GET", "arvados/v1/collections/"+s.cr.LogUUID, nil, nil)
301 fs, err := log.FileSystem(s.client, s.kc)
303 if d, err := fs.Open("/"); c.Check(err, IsNil) {
304 fis, err := d.Readdir(-1)
306 for _, fi := range fis {
310 f, err := fs.Open(fi.Name())
312 buf, err := ioutil.ReadAll(f)
314 c.Logf("\n===== %s =====\n%s", fi.Name(), buf)
315 s.logFiles[fi.Name()] = string(buf)
318 s.logCollection = log
320 var output arvados.Collection
321 err = s.client.RequestAndDecode(&output, "GET", "arvados/v1/collections/"+s.cr.OutputUUID, nil, nil)
323 fs, err = output.FileSystem(s.client, s.kc)
325 if f, err := fs.Open("inputfile"); c.Check(err, IsNil) {
327 buf, err := ioutil.ReadAll(f)
329 c.Check(string(buf), Equals, "inputdata")
331 if f, err := fs.Open("json"); c.Check(err, IsNil) {
333 buf, err := ioutil.ReadAll(f)
335 c.Check(string(buf), Equals, `["foo",{"foo":"bar"},null]`)
337 if fi, err := fs.Stat("emptydir"); c.Check(err, IsNil) {
338 c.Check(fi.IsDir(), Equals, true)
340 if d, err := fs.Open("emptydir"); c.Check(err, IsNil) {
342 fis, err := d.Readdir(-1)
344 // crunch-run still saves a ".keep" file to preserve
345 // empty dirs even though that shouldn't be
346 // necessary. Ideally we would do:
347 // c.Check(fis, HasLen, 0)
348 for _, fi := range fis {
349 c.Check(fi.Name(), Equals, ".keep")
352 s.outputCollection = output