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