1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
17 "git.arvados.org/arvados.git/sdk/go/arvados"
18 "git.arvados.org/arvados.git/sdk/go/arvadostest"
19 "golang.org/x/net/context"
23 type nopWriteCloser struct{ io.Writer }
25 func (nopWriteCloser) Close() error { return nil }
27 // embedded by dockerSuite and singularitySuite so they can share
29 type executorSuite struct {
30 newExecutor func(*C) // embedding struct's SetUpSuite method must set this
31 executor containerExecutor
37 func (s *executorSuite) SetUpTest(c *C) {
39 s.stdout = bytes.Buffer{}
40 s.stderr = bytes.Buffer{}
41 s.spec = containerSpec{
42 Image: "busybox:uclibc",
45 Env: map[string]string{"PATH": "/bin:/usr/bin"},
46 NetworkMode: "default",
47 Stdout: nopWriteCloser{&s.stdout},
48 Stderr: nopWriteCloser{&s.stderr},
50 err := s.executor.LoadImage("", arvadostest.BusyboxDockerImage(c), arvados.Container{}, "", nil)
54 func (s *executorSuite) TearDownTest(c *C) {
58 func (s *executorSuite) TestExecTrivialContainer(c *C) {
59 c.Logf("Using container runtime: %s", s.executor.Runtime())
60 s.spec.Command = []string{"echo", "ok"}
62 c.Check(s.stdout.String(), Equals, "ok\n")
63 c.Check(s.stderr.String(), Equals, "")
66 func (s *executorSuite) TestExitStatus(c *C) {
67 s.spec.Command = []string{"false"}
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")
78 s.spec.Command = []string{"sh", "-c", "kill -9 $$"}
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)
86 err = s.executor.Start()
89 time.Sleep(time.Second / 10)
92 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(10*time.Second))
94 code, err := s.executor.Wait(ctx)
95 c.Check(code, Not(Equals), 0)
97 c.Check(s.stdout.String(), Equals, "")
98 c.Check(s.stderr.String(), Equals, "")
101 func (s *executorSuite) TestExecCleanEnv(c *C) {
102 s.spec.Command = []string{"env"}
104 c.Check(s.stderr.String(), Equals, "")
105 got := map[string]string{}
106 for _, kv := range strings.Split(s.stdout.String(), "\n") {
110 kv := strings.SplitN(kv, "=", 2)
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)
124 c.Check(got, DeepEquals, s.spec.Env)
126 func (s *executorSuite) TestExecEnableNetwork(c *C) {
127 for _, enable := range []bool{false, true} {
129 s.spec.Command = []string{"ip", "route"}
130 s.spec.EnableNetwork = enable
133 c.Check(s.stdout.String(), Matches, "(?ms).*default via.*")
135 c.Check(s.stdout.String(), Equals, "")
140 func (s *executorSuite) TestExecWorkingDir(c *C) {
141 s.spec.WorkingDir = "/tmp"
142 s.spec.Command = []string{"sh", "-c", "pwd"}
144 c.Check(s.stdout.String(), Equals, "/tmp\n")
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"}
150 c.Check(s.stdout.String(), Equals, "foo\nbaz\n")
151 c.Check(s.stderr.String(), Equals, "barwaz\n")
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"))
159 _, port, err := net.SplitHostPort(ln.Addr().String())
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()
170 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(10*time.Second))
173 for ctx.Err() == nil {
174 time.Sleep(time.Second / 10)
175 _, err := s.executor.IPAddress()
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)
188 resp, err := http.DefaultClient.Do(req)
190 c.Check(resp.StatusCode, Equals, http.StatusTeapot)
194 code, _ := s.executor.Wait(ctx)
195 c.Logf("container ran for %v", time.Now().Sub(starttime))
196 c.Check(code, Equals, -1)
198 c.Logf("stdout:\n%s\n\n", s.stdout.String())
199 c.Logf("stderr:\n%s\n\n", s.stderr.String())
202 func (s *executorSuite) TestInject(c *C) {
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()
212 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(2*time.Second))
215 // Allow InjectCommand to fail a few times while the container
217 for ctx.Err() == nil {
218 _, err := s.executor.InjectCommand(ctx, "", "root", false, []string{"true"})
222 time.Sleep(time.Second / 10)
225 injectcmd := []string{"cat", mountdir + "/testfile"}
226 cmd, err := s.executor.InjectCommand(ctx, "", "root", false, injectcmd)
228 out, err := cmd.CombinedOutput()
229 c.Logf("inject %s => %q", injectcmd, out)
231 c.Check(string(out), Equals, "first tube")
234 code, _ := s.executor.Wait(ctx)
235 c.Logf("container ran for %v", time.Now().Sub(starttime))
236 c.Check(code, Equals, -1)
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))
244 code, err := s.executor.Wait(ctx)
246 c.Check(code, Equals, expectCode)