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