1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
16 "golang.org/x/net/context"
20 func busyboxDockerImage(c *C) string {
21 fnm := "busybox_uclibc.tar"
23 cachefile := cachedir + "/" + fnm
24 if _, err := os.Stat(cachefile); err == nil {
28 f, err := ioutil.TempFile(cachedir, "")
31 defer os.Remove(f.Name())
33 resp, err := http.Get("https://cache.arvados.org/" + fnm)
35 defer resp.Body.Close()
36 _, err = io.Copy(f, resp.Body)
40 err = os.Rename(f.Name(), cachefile)
46 type nopWriteCloser struct{ io.Writer }
48 func (nopWriteCloser) Close() error { return nil }
50 // embedded by dockerSuite and singularitySuite so they can share
52 type executorSuite struct {
53 newExecutor func(*C) // embedding struct's SetUpSuite method must set this
54 executor containerExecutor
60 func (s *executorSuite) SetUpTest(c *C) {
62 s.stdout = bytes.Buffer{}
63 s.stderr = bytes.Buffer{}
64 s.spec = containerSpec{
65 Image: "busybox:uclibc",
68 Env: map[string]string{"PATH": "/bin:/usr/bin"},
69 NetworkMode: "default",
70 Stdout: nopWriteCloser{&s.stdout},
71 Stderr: nopWriteCloser{&s.stderr},
73 err := s.executor.LoadImage(busyboxDockerImage(c))
77 func (s *executorSuite) TearDownTest(c *C) {
81 func (s *executorSuite) TestExecTrivialContainer(c *C) {
82 s.spec.Command = []string{"echo", "ok"}
84 c.Check(s.stdout.String(), Equals, "ok\n")
85 c.Check(s.stderr.String(), Equals, "")
88 func (s *executorSuite) TestExecStop(c *C) {
89 s.spec.Command = []string{"sh", "-c", "sleep 10; echo ok"}
90 err := s.executor.Create(s.spec)
92 err = s.executor.Start()
95 time.Sleep(time.Second / 10)
98 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(10*time.Second))
100 code, err := s.executor.Wait(ctx)
101 c.Check(code, Not(Equals), 0)
103 c.Check(s.stdout.String(), Equals, "")
104 c.Check(s.stderr.String(), Equals, "")
107 func (s *executorSuite) TestExecCleanEnv(c *C) {
108 s.spec.Command = []string{"env"}
110 c.Check(s.stderr.String(), Equals, "")
111 got := map[string]string{}
112 for _, kv := range strings.Split(s.stdout.String(), "\n") {
116 kv := strings.SplitN(kv, "=", 2)
118 case "HOSTNAME", "HOME":
119 // docker sets these by itself
120 case "LD_LIBRARY_PATH", "SINGULARITY_NAME", "PWD", "LANG", "SHLVL", "SINGULARITY_INIT", "SINGULARITY_CONTAINER":
121 // singularity sets these by itself (cf. https://sylabs.io/guides/3.5/user-guide/environment_and_metadata.html)
122 case "PROMPT_COMMAND", "PS1", "SINGULARITY_APPNAME":
123 // singularity also sets these by itself (as of v3.5.2)
128 c.Check(got, DeepEquals, s.spec.Env)
130 func (s *executorSuite) TestExecEnableNetwork(c *C) {
131 for _, enable := range []bool{false, true} {
133 s.spec.Command = []string{"ip", "route"}
134 s.spec.EnableNetwork = enable
137 c.Check(s.stdout.String(), Matches, "(?ms).*default via.*")
139 c.Check(s.stdout.String(), Equals, "")
144 func (s *executorSuite) TestExecWorkingDir(c *C) {
145 s.spec.WorkingDir = "/tmp"
146 s.spec.Command = []string{"sh", "-c", "pwd"}
148 c.Check(s.stdout.String(), Equals, "/tmp\n")
151 func (s *executorSuite) TestExecStdoutStderr(c *C) {
152 s.spec.Command = []string{"sh", "-c", "echo foo; echo -n bar >&2; echo baz; echo waz >&2"}
154 c.Check(s.stdout.String(), Equals, "foo\nbaz\n")
155 c.Check(s.stderr.String(), Equals, "barwaz\n")
158 func (s *executorSuite) checkRun(c *C, expectCode int) {
159 c.Assert(s.executor.Create(s.spec), IsNil)
160 c.Assert(s.executor.Start(), IsNil)
161 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(10*time.Second))
163 code, err := s.executor.Wait(ctx)
165 c.Check(code, Equals, expectCode)