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