17813: Singularity image caching wip
[arvados.git] / lib / crunchrun / executor_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         "io"
10         "io/ioutil"
11         "net/http"
12         "os"
13         "strings"
14         "time"
15
16         "golang.org/x/net/context"
17         . "gopkg.in/check.v1"
18 )
19
20 func busyboxDockerImage(c *C) string {
21         fnm := "busybox_uclibc.tar"
22         cachedir := c.MkDir()
23         cachefile := cachedir + "/" + fnm
24         if _, err := os.Stat(cachefile); err == nil {
25                 return cachefile
26         }
27
28         f, err := ioutil.TempFile(cachedir, "")
29         c.Assert(err, IsNil)
30         defer f.Close()
31         defer os.Remove(f.Name())
32
33         resp, err := http.Get("https://cache.arvados.org/" + fnm)
34         c.Assert(err, IsNil)
35         defer resp.Body.Close()
36         _, err = io.Copy(f, resp.Body)
37         c.Assert(err, IsNil)
38         err = f.Close()
39         c.Assert(err, IsNil)
40         err = os.Rename(f.Name(), cachefile)
41         c.Assert(err, IsNil)
42
43         return cachefile
44 }
45
46 type nopWriteCloser struct{ io.Writer }
47
48 func (nopWriteCloser) Close() error { return nil }
49
50 // embedded by dockerSuite and singularitySuite so they can share
51 // tests.
52 type executorSuite struct {
53         newExecutor func(*C) // embedding struct's SetUpSuite method must set this
54         executor    containerExecutor
55         spec        containerSpec
56         stdout      bytes.Buffer
57         stderr      bytes.Buffer
58 }
59
60 func (s *executorSuite) SetUpTest(c *C) {
61         s.newExecutor(c)
62         s.stdout = bytes.Buffer{}
63         s.stderr = bytes.Buffer{}
64         s.spec = containerSpec{
65                 Image:       "busybox:uclibc",
66                 VCPUs:       1,
67                 WorkingDir:  "",
68                 Env:         map[string]string{"PATH": "/bin:/usr/bin"},
69                 NetworkMode: "default",
70                 Stdout:      nopWriteCloser{&s.stdout},
71                 Stderr:      nopWriteCloser{&s.stderr},
72         }
73         err := s.executor.LoadImage(busyboxDockerImage(c))
74         c.Assert(err, IsNil)
75 }
76
77 func (s *executorSuite) TearDownTest(c *C) {
78         s.executor.Close()
79 }
80
81 func (s *executorSuite) TestExecTrivialContainer(c *C) {
82         s.spec.Command = []string{"echo", "ok"}
83         s.checkRun(c, 0)
84         c.Check(s.stdout.String(), Equals, "ok\n")
85         c.Check(s.stderr.String(), Equals, "")
86 }
87
88 func (s *executorSuite) TestExecStop(c *C) {
89         s.spec.Command = []string{"sh", "-c", "sleep 10; echo ok"}
90         err := s.executor.Create(s.spec)
91         c.Assert(err, IsNil)
92         err = s.executor.Start()
93         c.Assert(err, IsNil)
94         go func() {
95                 time.Sleep(time.Second / 10)
96                 s.executor.Stop()
97         }()
98         ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(10*time.Second))
99         defer cancel()
100         code, err := s.executor.Wait(ctx)
101         c.Check(code, Not(Equals), 0)
102         c.Check(err, IsNil)
103         c.Check(s.stdout.String(), Equals, "")
104         c.Check(s.stderr.String(), Equals, "")
105 }
106
107 func (s *executorSuite) TestExecCleanEnv(c *C) {
108         s.spec.Command = []string{"env"}
109         s.checkRun(c, 0)
110         c.Check(s.stderr.String(), Equals, "")
111         got := map[string]string{}
112         for _, kv := range strings.Split(s.stdout.String(), "\n") {
113                 if kv == "" {
114                         continue
115                 }
116                 kv := strings.SplitN(kv, "=", 2)
117                 switch kv[0] {
118                 case "HOSTNAME", "HOME":
119                         // docker sets these by itself
120                 case "LD_LIBRARY_PATH", "SINGULARITY_NAME", "PWD", "LANG", "SHLVL", "SINGULARITY_INIT", "SINGULARITY_CONTAINER":
121                         // singularity sets these by itself (cf. https://sylabs.io/guides/3.5/user-guide/environment_and_metadata.html)
122                 case "PROMPT_COMMAND", "PS1", "SINGULARITY_APPNAME":
123                         // singularity also sets these by itself (as of v3.5.2)
124                 default:
125                         got[kv[0]] = kv[1]
126                 }
127         }
128         c.Check(got, DeepEquals, s.spec.Env)
129 }
130 func (s *executorSuite) TestExecEnableNetwork(c *C) {
131         for _, enable := range []bool{false, true} {
132                 s.SetUpTest(c)
133                 s.spec.Command = []string{"ip", "route"}
134                 s.spec.EnableNetwork = enable
135                 s.checkRun(c, 0)
136                 if enable {
137                         c.Check(s.stdout.String(), Matches, "(?ms).*default via.*")
138                 } else {
139                         c.Check(s.stdout.String(), Equals, "")
140                 }
141         }
142 }
143
144 func (s *executorSuite) TestExecWorkingDir(c *C) {
145         s.spec.WorkingDir = "/tmp"
146         s.spec.Command = []string{"sh", "-c", "pwd"}
147         s.checkRun(c, 0)
148         c.Check(s.stdout.String(), Equals, "/tmp\n")
149 }
150
151 func (s *executorSuite) TestExecStdoutStderr(c *C) {
152         s.spec.Command = []string{"sh", "-c", "echo foo; echo -n bar >&2; echo baz; echo waz >&2"}
153         s.checkRun(c, 0)
154         c.Check(s.stdout.String(), Equals, "foo\nbaz\n")
155         c.Check(s.stderr.String(), Equals, "barwaz\n")
156 }
157
158 func (s *executorSuite) checkRun(c *C, expectCode int) {
159         c.Assert(s.executor.Create(s.spec), IsNil)
160         c.Assert(s.executor.Start(), IsNil)
161         ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(10*time.Second))
162         defer cancel()
163         code, err := s.executor.Wait(ctx)
164         c.Assert(err, IsNil)
165         c.Check(code, Equals, expectCode)
166 }