1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
16 "git.arvados.org/arvados.git/sdk/go/arvados"
17 "golang.org/x/net/context"
21 func busyboxDockerImage(c *C) string {
22 fnm := "busybox_uclibc.tar"
24 cachefile := cachedir + "/" + fnm
25 if _, err := os.Stat(cachefile); err == nil {
29 f, err := ioutil.TempFile(cachedir, "")
32 defer os.Remove(f.Name())
34 resp, err := http.Get("https://cache.arvados.org/" + fnm)
36 defer resp.Body.Close()
37 _, err = io.Copy(f, resp.Body)
41 err = os.Rename(f.Name(), cachefile)
47 type nopWriteCloser struct{ io.Writer }
49 func (nopWriteCloser) Close() error { return nil }
51 // embedded by dockerSuite and singularitySuite so they can share
53 type executorSuite struct {
54 newExecutor func(*C) // embedding struct's SetUpSuite method must set this
55 executor containerExecutor
61 func (s *executorSuite) SetUpTest(c *C) {
63 s.stdout = bytes.Buffer{}
64 s.stderr = bytes.Buffer{}
65 s.spec = containerSpec{
66 Image: "busybox:uclibc",
69 Env: map[string]string{"PATH": "/bin:/usr/bin"},
70 NetworkMode: "default",
71 Stdout: nopWriteCloser{&s.stdout},
72 Stderr: nopWriteCloser{&s.stderr},
74 err := s.executor.LoadImage("", busyboxDockerImage(c), arvados.Container{}, "", nil)
78 func (s *executorSuite) TearDownTest(c *C) {
82 func (s *executorSuite) TestExecTrivialContainer(c *C) {
83 s.spec.Command = []string{"echo", "ok"}
85 c.Check(s.stdout.String(), Equals, "ok\n")
86 c.Check(s.stderr.String(), Equals, "")
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)
93 err = s.executor.Start()
96 time.Sleep(time.Second / 10)
99 ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(10*time.Second))
101 code, err := s.executor.Wait(ctx)
102 c.Check(code, Not(Equals), 0)
104 c.Check(s.stdout.String(), Equals, "")
105 c.Check(s.stderr.String(), Equals, "")
108 func (s *executorSuite) TestExecCleanEnv(c *C) {
109 s.spec.Command = []string{"env"}
111 c.Check(s.stderr.String(), Equals, "")
112 got := map[string]string{}
113 for _, kv := range strings.Split(s.stdout.String(), "\n") {
117 kv := strings.SplitN(kv, "=", 2)
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)
131 c.Check(got, DeepEquals, s.spec.Env)
133 func (s *executorSuite) TestExecEnableNetwork(c *C) {
134 for _, enable := range []bool{false, true} {
136 s.spec.Command = []string{"ip", "route"}
137 s.spec.EnableNetwork = enable
140 c.Check(s.stdout.String(), Matches, "(?ms).*default via.*")
142 c.Check(s.stdout.String(), Equals, "")
147 func (s *executorSuite) TestExecWorkingDir(c *C) {
148 s.spec.WorkingDir = "/tmp"
149 s.spec.Command = []string{"sh", "-c", "pwd"}
151 c.Check(s.stdout.String(), Equals, "/tmp\n")
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"}
157 c.Check(s.stdout.String(), Equals, "foo\nbaz\n")
158 c.Check(s.stderr.String(), Equals, "barwaz\n")
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))
166 code, err := s.executor.Wait(ctx)
168 c.Check(code, Equals, expectCode)