1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
21 "git.arvados.org/arvados.git/lib/diagnostics"
22 "git.arvados.org/arvados.git/sdk/go/arvados"
23 "git.arvados.org/arvados.git/sdk/go/arvadostest"
27 type nopWriteCloser struct{ io.Writer }
29 func (nopWriteCloser) Close() error { return nil }
31 // embedded by dockerSuite and singularitySuite so they can share
33 type executorSuite struct {
34 newExecutor func(*C) // embedding struct's SetUpSuite method must set this
35 executor containerExecutor
41 func (s *executorSuite) SetUpTest(c *C) {
43 s.stdout = bytes.Buffer{}
44 s.stderr = bytes.Buffer{}
45 s.spec = containerSpec{
46 Image: "busybox:uclibc",
49 Env: map[string]string{"PATH": "/bin:/usr/bin"},
50 NetworkMode: "default",
51 Stdout: nopWriteCloser{&s.stdout},
52 Stderr: nopWriteCloser{&s.stderr},
54 err := s.executor.LoadImage("", arvadostest.BusyboxDockerImage(c), arvados.Container{}, "", nil)
58 func (s *executorSuite) TearDownTest(c *C) {
62 func (s *executorSuite) TestExecTrivialContainer(c *C) {
63 c.Logf("Using container runtime: %s", s.executor.Runtime())
64 s.spec.Command = []string{"echo", "ok"}
66 c.Check(s.stdout.String(), Equals, "ok\n")
67 c.Check(s.stderr.String(), Equals, "")
70 func (s *executorSuite) TestDiagnosticsImage(c *C) {
71 imagefile := c.MkDir() + "/hello-world.tar"
72 err := ioutil.WriteFile(imagefile, diagnostics.HelloWorldDockerImage, 0777)
74 err = s.executor.LoadImage("", imagefile, arvados.Container{}, "", nil)
77 c.Logf("Using container runtime: %s", s.executor.Runtime())
78 s.spec.Image = "hello-world"
79 s.spec.Command = []string{"/hello"}
81 c.Check(s.stdout.String(), Matches, `(?ms)\nHello from Docker!\n.*`)
84 func (s *executorSuite) TestExitStatus(c *C) {
85 s.spec.Command = []string{"false"}
89 func (s *executorSuite) TestSignalExitStatus(c *C) {
90 if _, isdocker := s.executor.(*dockerExecutor); isdocker {
91 // It's not quite this easy to make busybox kill
92 // itself in docker where it's pid 1.
93 c.Skip("kill -9 $$ doesn't work on busybox with pid=1 in docker")
96 s.spec.Command = []string{"sh", "-c", "kill -9 $$"}
100 func (s *executorSuite) TestExecStop(c *C) {
101 s.spec.Command = []string{"sh", "-c", "sleep 10; echo ok"}
102 err := s.executor.Create(s.spec)
104 err = s.executor.Start()
107 time.Sleep(time.Second / 10)
110 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(10*time.Second))
112 code, err := s.executor.Wait(ctx)
113 c.Check(code, Not(Equals), 0)
115 c.Check(s.stdout.String(), Equals, "")
116 c.Check(s.stderr.String(), Equals, "")
119 func (s *executorSuite) TestExecCleanEnv(c *C) {
120 s.spec.Command = []string{"env"}
122 c.Check(s.stderr.String(), Equals, "")
123 got := map[string]string{}
124 for _, kv := range strings.Split(s.stdout.String(), "\n") {
128 kv := strings.SplitN(kv, "=", 2)
130 case "HOSTNAME", "HOME":
131 // docker sets these by itself
132 case "LD_LIBRARY_PATH", "SINGULARITY_NAME", "PWD", "LANG", "SHLVL", "SINGULARITY_INIT", "SINGULARITY_CONTAINER":
133 // singularity sets these by itself (cf. https://sylabs.io/guides/3.5/user-guide/environment_and_metadata.html)
134 case "SINGULARITY_APPNAME":
135 // singularity also sets this by itself (v3.5.2, but not v3.7.4)
136 case "PROMPT_COMMAND", "PS1", "SINGULARITY_BIND", "SINGULARITY_COMMAND", "SINGULARITY_ENVIRONMENT":
137 // singularity also sets these by itself (v3.7.4)
138 case "SINGULARITY_NO_EVAL":
139 // our singularity driver sets this to control
140 // singularity behavior, and it gets passed
141 // through to the container
146 c.Check(got, DeepEquals, s.spec.Env)
148 func (s *executorSuite) TestExecEnableNetwork(c *C) {
149 for _, enable := range []bool{false, true} {
151 s.spec.Command = []string{"ip", "route"}
152 s.spec.EnableNetwork = enable
155 c.Check(s.stdout.String(), Matches, "(?ms).*default via.*")
157 c.Check(s.stdout.String(), Equals, "")
162 func (s *executorSuite) TestExecWorkingDir(c *C) {
163 s.spec.WorkingDir = "/tmp"
164 s.spec.Command = []string{"sh", "-c", "pwd"}
166 c.Check(s.stdout.String(), Equals, "/tmp\n")
169 func (s *executorSuite) TestExecStdoutStderr(c *C) {
170 s.spec.Command = []string{"sh", "-c", "echo foo; echo -n bar >&2; echo baz; echo waz >&2"}
172 c.Check(s.stdout.String(), Equals, "foo\nbaz\n")
173 c.Check(s.stderr.String(), Equals, "barwaz\n")
176 func (s *executorSuite) TestEnableNetwork_Listen(c *C) {
177 // Listen on an available port on the host.
178 ln, err := net.Listen("tcp", net.JoinHostPort("0.0.0.0", "0"))
181 _, port, err := net.SplitHostPort(ln.Addr().String())
184 // Start a container that listens on the same port number that
185 // is already in use on the host.
186 s.spec.Command = []string{"nc", "-l", "-p", port, "-e", "printf", `HTTP/1.1 418 I'm a teapot\r\n\r\n`}
187 s.spec.EnableNetwork = true
188 c.Assert(s.executor.Create(s.spec), IsNil)
189 c.Assert(s.executor.Start(), IsNil)
190 starttime := time.Now()
192 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(10*time.Second))
196 time.Sleep(time.Second / 10)
197 if ctx.Err() != nil {
202 ip, err := s.executor.IPAddress()
204 c.Logf("s.executor.IPAddress: %s", err)
207 c.Assert(ip, Not(Equals), "")
209 // When we connect to the port using
210 // s.executor.IPAddress(), we should reach the nc
211 // process running inside the container, not the
212 // net.Listen() running outside the container, even
213 // though both listen on the same port.
214 ctx, cancel := context.WithDeadline(ctx, time.Now().Add(time.Second))
216 req, err := http.NewRequestWithContext(ctx, "BREW", "http://"+net.JoinHostPort(ip, port), nil)
218 resp, err := http.DefaultClient.Do(req)
220 c.Logf("%s (retrying...)", err)
223 c.Check(resp.StatusCode, Equals, http.StatusTeapot)
224 c.Logf("%s %q: %s", req.Method, req.URL, resp.Status)
229 code, _ := s.executor.Wait(ctx)
230 c.Logf("container ran for %v", time.Now().Sub(starttime))
231 c.Check(code, Equals, -1)
233 c.Logf("stdout:\n%s\n\n", s.stdout.String())
234 c.Logf("stderr:\n%s\n\n", s.stderr.String())
237 func (s *executorSuite) TestEnableNetwork_IPAddress(c *C) {
238 s.spec.Command = []string{"ip", "ad"}
239 s.spec.EnableNetwork = true
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))
244 code, _ := s.executor.Wait(ctx)
245 c.Check(code, Equals, 0)
246 c.Logf("stdout:\n%s\n\n", s.stdout.String())
247 c.Logf("stderr:\n%s\n\n", s.stderr.String())
250 for _, m := range regexp.MustCompile(` inet (.+?)/`).FindAllStringSubmatch(s.stdout.String(), -1) {
251 if addr, err := netip.ParseAddr(m[1]); err == nil && !addr.IsLoopback() {
253 c.Logf("found non-loopback IP address %q", m[1])
257 c.Check(found, Equals, true, Commentf("container does not appear to have a non-loopback IP address"))
260 func (s *executorSuite) TestInject(c *C) {
262 c.Assert(os.WriteFile(hostdir+"/testfile", []byte("first tube"), 0777), IsNil)
263 mountdir := fmt.Sprintf("/injecttest-%d", os.Getpid())
264 s.spec.Command = []string{"sleep", "10"}
265 s.spec.BindMounts = map[string]bindmount{mountdir: {HostPath: hostdir, ReadOnly: true}}
266 c.Assert(s.executor.Create(s.spec), IsNil)
267 c.Assert(s.executor.Start(), IsNil)
268 starttime := time.Now()
270 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(2*time.Second))
273 // Allow InjectCommand to fail a few times while the container
275 for ctx.Err() == nil {
276 _, err := s.executor.InjectCommand(ctx, "", "root", false, []string{"true"})
280 time.Sleep(time.Second / 10)
283 injectcmd := []string{"cat", mountdir + "/testfile"}
284 cmd, err := s.executor.InjectCommand(ctx, "", "root", false, injectcmd)
286 out, err := cmd.CombinedOutput()
287 c.Logf("inject %s => %q", injectcmd, out)
289 c.Check(string(out), Equals, "first tube")
292 code, _ := s.executor.Wait(ctx)
293 c.Logf("container ran for %v", time.Now().Sub(starttime))
294 c.Check(code, Equals, -1)
297 func (s *executorSuite) checkRun(c *C, expectCode int) {
298 c.Assert(s.executor.Create(s.spec), IsNil)
299 c.Assert(s.executor.Start(), IsNil)
300 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(10*time.Second))
302 code, err := s.executor.Wait(ctx)
304 c.Check(code, Equals, expectCode)