21128: Merge commit 'adbbc9e3c7a36d39b30f403555ee5889e32adcc0' into 21128-toolbar...
[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         "context"
10         "fmt"
11         "io"
12         "io/ioutil"
13         "net"
14         "net/http"
15         "os"
16         "strings"
17         "time"
18
19         "git.arvados.org/arvados.git/lib/diagnostics"
20         "git.arvados.org/arvados.git/sdk/go/arvados"
21         "git.arvados.org/arvados.git/sdk/go/arvadostest"
22         . "gopkg.in/check.v1"
23 )
24
25 type nopWriteCloser struct{ io.Writer }
26
27 func (nopWriteCloser) Close() error { return nil }
28
29 // embedded by dockerSuite and singularitySuite so they can share
30 // tests.
31 type executorSuite struct {
32         newExecutor func(*C) // embedding struct's SetUpSuite method must set this
33         executor    containerExecutor
34         spec        containerSpec
35         stdout      bytes.Buffer
36         stderr      bytes.Buffer
37 }
38
39 func (s *executorSuite) SetUpTest(c *C) {
40         s.newExecutor(c)
41         s.stdout = bytes.Buffer{}
42         s.stderr = bytes.Buffer{}
43         s.spec = containerSpec{
44                 Image:       "busybox:uclibc",
45                 VCPUs:       1,
46                 WorkingDir:  "",
47                 Env:         map[string]string{"PATH": "/bin:/usr/bin"},
48                 NetworkMode: "default",
49                 Stdout:      nopWriteCloser{&s.stdout},
50                 Stderr:      nopWriteCloser{&s.stderr},
51         }
52         err := s.executor.LoadImage("", arvadostest.BusyboxDockerImage(c), arvados.Container{}, "", nil)
53         c.Assert(err, IsNil)
54 }
55
56 func (s *executorSuite) TearDownTest(c *C) {
57         s.executor.Close()
58 }
59
60 func (s *executorSuite) TestExecTrivialContainer(c *C) {
61         c.Logf("Using container runtime: %s", s.executor.Runtime())
62         s.spec.Command = []string{"echo", "ok"}
63         s.checkRun(c, 0)
64         c.Check(s.stdout.String(), Equals, "ok\n")
65         c.Check(s.stderr.String(), Equals, "")
66 }
67
68 func (s *executorSuite) TestDiagnosticsImage(c *C) {
69         s.newExecutor(c)
70         imagefile := c.MkDir() + "/hello-world.tar"
71         err := ioutil.WriteFile(imagefile, diagnostics.HelloWorldDockerImage, 0777)
72         c.Assert(err, IsNil)
73         err = s.executor.LoadImage("", imagefile, arvados.Container{}, "", nil)
74         c.Assert(err, IsNil)
75
76         c.Logf("Using container runtime: %s", s.executor.Runtime())
77         s.spec.Image = "hello-world"
78         s.spec.Command = []string{"/hello"}
79         s.checkRun(c, 0)
80         c.Check(s.stdout.String(), Matches, `(?ms)\nHello from Docker!\n.*`)
81 }
82
83 func (s *executorSuite) TestExitStatus(c *C) {
84         s.spec.Command = []string{"false"}
85         s.checkRun(c, 1)
86 }
87
88 func (s *executorSuite) TestSignalExitStatus(c *C) {
89         if _, isdocker := s.executor.(*dockerExecutor); isdocker {
90                 // It's not quite this easy to make busybox kill
91                 // itself in docker where it's pid 1.
92                 c.Skip("kill -9 $$ doesn't work on busybox with pid=1 in docker")
93                 return
94         }
95         s.spec.Command = []string{"sh", "-c", "kill -9 $$"}
96         s.checkRun(c, 0x80+9)
97 }
98
99 func (s *executorSuite) TestExecStop(c *C) {
100         s.spec.Command = []string{"sh", "-c", "sleep 10; echo ok"}
101         err := s.executor.Create(s.spec)
102         c.Assert(err, IsNil)
103         err = s.executor.Start()
104         c.Assert(err, IsNil)
105         go func() {
106                 time.Sleep(time.Second / 10)
107                 s.executor.Stop()
108         }()
109         ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(10*time.Second))
110         defer cancel()
111         code, err := s.executor.Wait(ctx)
112         c.Check(code, Not(Equals), 0)
113         c.Check(err, IsNil)
114         c.Check(s.stdout.String(), Equals, "")
115         c.Check(s.stderr.String(), Equals, "")
116 }
117
118 func (s *executorSuite) TestExecCleanEnv(c *C) {
119         s.spec.Command = []string{"env"}
120         s.checkRun(c, 0)
121         c.Check(s.stderr.String(), Equals, "")
122         got := map[string]string{}
123         for _, kv := range strings.Split(s.stdout.String(), "\n") {
124                 if kv == "" {
125                         continue
126                 }
127                 kv := strings.SplitN(kv, "=", 2)
128                 switch kv[0] {
129                 case "HOSTNAME", "HOME":
130                         // docker sets these by itself
131                 case "LD_LIBRARY_PATH", "SINGULARITY_NAME", "PWD", "LANG", "SHLVL", "SINGULARITY_INIT", "SINGULARITY_CONTAINER":
132                         // singularity sets these by itself (cf. https://sylabs.io/guides/3.5/user-guide/environment_and_metadata.html)
133                 case "SINGULARITY_APPNAME":
134                         // singularity also sets this by itself (v3.5.2, but not v3.7.4)
135                 case "PROMPT_COMMAND", "PS1", "SINGULARITY_BIND", "SINGULARITY_COMMAND", "SINGULARITY_ENVIRONMENT":
136                         // singularity also sets these by itself (v3.7.4)
137                 case "SINGULARITY_NO_EVAL":
138                         // our singularity driver sets this to control
139                         // singularity behavior, and it gets passed
140                         // through to the container
141                 default:
142                         got[kv[0]] = kv[1]
143                 }
144         }
145         c.Check(got, DeepEquals, s.spec.Env)
146 }
147 func (s *executorSuite) TestExecEnableNetwork(c *C) {
148         for _, enable := range []bool{false, true} {
149                 s.SetUpTest(c)
150                 s.spec.Command = []string{"ip", "route"}
151                 s.spec.EnableNetwork = enable
152                 s.checkRun(c, 0)
153                 if enable {
154                         c.Check(s.stdout.String(), Matches, "(?ms).*default via.*")
155                 } else {
156                         c.Check(s.stdout.String(), Equals, "")
157                 }
158         }
159 }
160
161 func (s *executorSuite) TestExecWorkingDir(c *C) {
162         s.spec.WorkingDir = "/tmp"
163         s.spec.Command = []string{"sh", "-c", "pwd"}
164         s.checkRun(c, 0)
165         c.Check(s.stdout.String(), Equals, "/tmp\n")
166 }
167
168 func (s *executorSuite) TestExecStdoutStderr(c *C) {
169         s.spec.Command = []string{"sh", "-c", "echo foo; echo -n bar >&2; echo baz; echo waz >&2"}
170         s.checkRun(c, 0)
171         c.Check(s.stdout.String(), Equals, "foo\nbaz\n")
172         c.Check(s.stderr.String(), Equals, "barwaz\n")
173 }
174
175 func (s *executorSuite) TestIPAddress(c *C) {
176         // Listen on an available port on the host.
177         ln, err := net.Listen("tcp", net.JoinHostPort("0.0.0.0", "0"))
178         c.Assert(err, IsNil)
179         defer ln.Close()
180         _, port, err := net.SplitHostPort(ln.Addr().String())
181         c.Assert(err, IsNil)
182
183         // Start a container that listens on the same port number that
184         // is already in use on the host.
185         s.spec.Command = []string{"nc", "-l", "-p", port, "-e", "printf", `HTTP/1.1 418 I'm a teapot\r\n\r\n`}
186         s.spec.EnableNetwork = true
187         c.Assert(s.executor.Create(s.spec), IsNil)
188         c.Assert(s.executor.Start(), IsNil)
189         starttime := time.Now()
190
191         ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(10*time.Second))
192         defer cancel()
193
194         for ctx.Err() == nil {
195                 time.Sleep(time.Second / 10)
196                 _, err := s.executor.IPAddress()
197                 if err == nil {
198                         break
199                 }
200         }
201         // When we connect to the port using s.executor.IPAddress(),
202         // we should reach the nc process running inside the
203         // container, not the net.Listen() running outside the
204         // container, even though both listen on the same port.
205         ip, err := s.executor.IPAddress()
206         if c.Check(err, IsNil) && c.Check(ip, Not(Equals), "") {
207                 req, err := http.NewRequest("BREW", "http://"+net.JoinHostPort(ip, port), nil)
208                 c.Assert(err, IsNil)
209                 resp, err := http.DefaultClient.Do(req)
210                 c.Assert(err, IsNil)
211                 c.Check(resp.StatusCode, Equals, http.StatusTeapot)
212         }
213
214         s.executor.Stop()
215         code, _ := s.executor.Wait(ctx)
216         c.Logf("container ran for %v", time.Now().Sub(starttime))
217         c.Check(code, Equals, -1)
218
219         c.Logf("stdout:\n%s\n\n", s.stdout.String())
220         c.Logf("stderr:\n%s\n\n", s.stderr.String())
221 }
222
223 func (s *executorSuite) TestInject(c *C) {
224         hostdir := c.MkDir()
225         c.Assert(os.WriteFile(hostdir+"/testfile", []byte("first tube"), 0777), IsNil)
226         mountdir := fmt.Sprintf("/injecttest-%d", os.Getpid())
227         s.spec.Command = []string{"sleep", "10"}
228         s.spec.BindMounts = map[string]bindmount{mountdir: {HostPath: hostdir, ReadOnly: true}}
229         c.Assert(s.executor.Create(s.spec), IsNil)
230         c.Assert(s.executor.Start(), IsNil)
231         starttime := time.Now()
232
233         ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(2*time.Second))
234         defer cancel()
235
236         // Allow InjectCommand to fail a few times while the container
237         // is starting
238         for ctx.Err() == nil {
239                 _, err := s.executor.InjectCommand(ctx, "", "root", false, []string{"true"})
240                 if err == nil {
241                         break
242                 }
243                 time.Sleep(time.Second / 10)
244         }
245
246         injectcmd := []string{"cat", mountdir + "/testfile"}
247         cmd, err := s.executor.InjectCommand(ctx, "", "root", false, injectcmd)
248         c.Assert(err, IsNil)
249         out, err := cmd.CombinedOutput()
250         c.Logf("inject %s => %q", injectcmd, out)
251         c.Check(err, IsNil)
252         c.Check(string(out), Equals, "first tube")
253
254         s.executor.Stop()
255         code, _ := s.executor.Wait(ctx)
256         c.Logf("container ran for %v", time.Now().Sub(starttime))
257         c.Check(code, Equals, -1)
258 }
259
260 func (s *executorSuite) checkRun(c *C, expectCode int) {
261         c.Assert(s.executor.Create(s.spec), IsNil)
262         c.Assert(s.executor.Start(), IsNil)
263         ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(10*time.Second))
264         defer cancel()
265         code, err := s.executor.Wait(ctx)
266         c.Assert(err, IsNil)
267         c.Check(code, Equals, expectCode)
268 }