1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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"
25 type nopWriteCloser struct{ io.Writer }
27 func (nopWriteCloser) Close() error { return nil }
29 // embedded by dockerSuite and singularitySuite so they can share
31 type executorSuite struct {
32 newExecutor func(*C) // embedding struct's SetUpSuite method must set this
33 executor containerExecutor
39 func (s *executorSuite) SetUpTest(c *C) {
41 s.stdout = bytes.Buffer{}
42 s.stderr = bytes.Buffer{}
43 s.spec = containerSpec{
44 Image: "busybox:uclibc",
47 Env: map[string]string{"PATH": "/bin:/usr/bin"},
48 NetworkMode: "default",
49 Stdout: nopWriteCloser{&s.stdout},
50 Stderr: nopWriteCloser{&s.stderr},
52 err := s.executor.LoadImage("", arvadostest.BusyboxDockerImage(c), arvados.Container{}, "", nil)
56 func (s *executorSuite) TearDownTest(c *C) {
60 func (s *executorSuite) TestExecTrivialContainer(c *C) {
61 c.Logf("Using container runtime: %s", s.executor.Runtime())
62 s.spec.Command = []string{"echo", "ok"}
64 c.Check(s.stdout.String(), Equals, "ok\n")
65 c.Check(s.stderr.String(), Equals, "")
68 func (s *executorSuite) TestDiagnosticsImage(c *C) {
70 imagefile := c.MkDir() + "/hello-world.tar"
71 err := ioutil.WriteFile(imagefile, diagnostics.HelloWorldDockerImage, 0777)
73 err = s.executor.LoadImage("", imagefile, arvados.Container{}, "", nil)
76 c.Logf("Using container runtime: %s", s.executor.Runtime())
77 s.spec.Image = "hello-world"
78 s.spec.Command = []string{"/hello"}
80 c.Check(s.stdout.String(), Matches, `(?ms)\nHello from Docker!\n.*`)
83 func (s *executorSuite) TestExitStatus(c *C) {
84 s.spec.Command = []string{"false"}
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")
95 s.spec.Command = []string{"sh", "-c", "kill -9 $$"}
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)
103 err = s.executor.Start()
106 time.Sleep(time.Second / 10)
109 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(10*time.Second))
111 code, err := s.executor.Wait(ctx)
112 c.Check(code, Not(Equals), 0)
114 c.Check(s.stdout.String(), Equals, "")
115 c.Check(s.stderr.String(), Equals, "")
118 func (s *executorSuite) TestExecCleanEnv(c *C) {
119 s.spec.Command = []string{"env"}
121 c.Check(s.stderr.String(), Equals, "")
122 got := map[string]string{}
123 for _, kv := range strings.Split(s.stdout.String(), "\n") {
127 kv := strings.SplitN(kv, "=", 2)
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
145 c.Check(got, DeepEquals, s.spec.Env)
147 func (s *executorSuite) TestExecEnableNetwork(c *C) {
148 for _, enable := range []bool{false, true} {
150 s.spec.Command = []string{"ip", "route"}
151 s.spec.EnableNetwork = enable
154 c.Check(s.stdout.String(), Matches, "(?ms).*default via.*")
156 c.Check(s.stdout.String(), Equals, "")
161 func (s *executorSuite) TestExecWorkingDir(c *C) {
162 s.spec.WorkingDir = "/tmp"
163 s.spec.Command = []string{"sh", "-c", "pwd"}
165 c.Check(s.stdout.String(), Equals, "/tmp\n")
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"}
171 c.Check(s.stdout.String(), Equals, "foo\nbaz\n")
172 c.Check(s.stderr.String(), Equals, "barwaz\n")
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"))
180 _, port, err := net.SplitHostPort(ln.Addr().String())
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()
191 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(10*time.Second))
194 for ctx.Err() == nil {
195 time.Sleep(time.Second / 10)
196 _, err := s.executor.IPAddress()
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)
209 resp, err := http.DefaultClient.Do(req)
211 c.Check(resp.StatusCode, Equals, http.StatusTeapot)
215 code, _ := s.executor.Wait(ctx)
216 c.Logf("container ran for %v", time.Now().Sub(starttime))
217 c.Check(code, Equals, -1)
219 c.Logf("stdout:\n%s\n\n", s.stdout.String())
220 c.Logf("stderr:\n%s\n\n", s.stderr.String())
223 func (s *executorSuite) TestInject(c *C) {
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()
233 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(2*time.Second))
236 // Allow InjectCommand to fail a few times while the container
238 for ctx.Err() == nil {
239 _, err := s.executor.InjectCommand(ctx, "", "root", false, []string{"true"})
243 time.Sleep(time.Second / 10)
246 injectcmd := []string{"cat", mountdir + "/testfile"}
247 cmd, err := s.executor.InjectCommand(ctx, "", "root", false, injectcmd)
249 out, err := cmd.CombinedOutput()
250 c.Logf("inject %s => %q", injectcmd, out)
252 c.Check(string(out), Equals, "first tube")
255 code, _ := s.executor.Wait(ctx)
256 c.Logf("container ran for %v", time.Now().Sub(starttime))
257 c.Check(code, Equals, -1)
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))
265 code, err := s.executor.Wait(ctx)
267 c.Check(code, Equals, expectCode)