X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/d3841f6869a8c6ae667df0288a5c88da6d830a6e..7ebe828a435dcaa1b5668b72adbaad495059f211:/lib/crunchrun/executor_test.go diff --git a/lib/crunchrun/executor_test.go b/lib/crunchrun/executor_test.go index 08c7140add..1c963f9211 100644 --- a/lib/crunchrun/executor_test.go +++ b/lib/crunchrun/executor_test.go @@ -8,6 +8,7 @@ import ( "bytes" "io" "io/ioutil" + "net" "net/http" "os" "strings" @@ -86,6 +87,22 @@ func (s *executorSuite) TestExecTrivialContainer(c *C) { c.Check(s.stderr.String(), Equals, "") } +func (s *executorSuite) TestExitStatus(c *C) { + s.spec.Command = []string{"false"} + s.checkRun(c, 1) +} + +func (s *executorSuite) TestSignalExitStatus(c *C) { + if _, isdocker := s.executor.(*dockerExecutor); isdocker { + // It's not quite this easy to make busybox kill + // itself in docker where it's pid 1. + c.Skip("kill -9 $$ doesn't work on busybox with pid=1 in docker") + return + } + s.spec.Command = []string{"sh", "-c", "kill -9 $$"} + s.checkRun(c, 0x80+9) +} + func (s *executorSuite) TestExecStop(c *C) { s.spec.Command = []string{"sh", "-c", "sleep 10; echo ok"} err := s.executor.Create(s.spec) @@ -158,6 +175,61 @@ func (s *executorSuite) TestExecStdoutStderr(c *C) { c.Check(s.stderr.String(), Equals, "barwaz\n") } +func (s *executorSuite) TestIPAddress(c *C) { + s.spec.Command = []string{"nc", "-l", "-p", "1951", "-e", "printf", `HTTP/1.1 418 I'm a teapot\r\n\r\n`} + s.spec.EnableNetwork = true + c.Assert(s.executor.Create(s.spec), IsNil) + c.Assert(s.executor.Start(), IsNil) + starttime := time.Now() + + ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(2*time.Second)) + defer cancel() + + for ctx.Err() == nil { + time.Sleep(time.Second / 10) + _, err := s.executor.IPAddress() + if err == nil { + break + } + } + ip, err := s.executor.IPAddress() + if c.Check(err, IsNil) && c.Check(ip, Not(Equals), "") { + req, err := http.NewRequest("BREW", "http://"+net.JoinHostPort(ip, "1951"), nil) + c.Assert(err, IsNil) + resp, err := http.DefaultClient.Do(req) + c.Assert(err, IsNil) + c.Check(resp.StatusCode, Equals, http.StatusTeapot) + } + + s.executor.Stop() + code, _ := s.executor.Wait(ctx) + c.Logf("container ran for %v", time.Now().Sub(starttime)) + c.Check(code, Equals, -1) +} + +func (s *executorSuite) TestInject(c *C) { + s.spec.Command = []string{"sleep", "10"} + c.Assert(s.executor.Create(s.spec), IsNil) + c.Assert(s.executor.Start(), IsNil) + starttime := time.Now() + + ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(2*time.Second)) + defer cancel() + + injectcmd := []string{"cat", "/proc/1/cmdline"} + cmd, err := s.executor.InjectCommand(ctx, "", "root", false, injectcmd) + c.Assert(err, IsNil) + out, err := cmd.CombinedOutput() + c.Logf("inject %s => %q", injectcmd, out) + c.Check(err, IsNil) + c.Check(string(out), Equals, "sleep\00010\000") + + s.executor.Stop() + code, _ := s.executor.Wait(ctx) + c.Logf("container ran for %v", time.Now().Sub(starttime)) + c.Check(code, Equals, -1) +} + func (s *executorSuite) checkRun(c *C, expectCode int) { c.Assert(s.executor.Create(s.spec), IsNil) c.Assert(s.executor.Start(), IsNil)