Update controller-supplied status messages to new format.
[arvados.git] / lib / crunchrun / executor_test.go
index 7356f3b56afe6d84fffb192e7d7ae82122327637..134ca560ce46978b35103aa7c9580b35af310cd3 100644 (file)
@@ -12,7 +12,9 @@ import (
        "io/ioutil"
        "net"
        "net/http"
+       "net/netip"
        "os"
+       "regexp"
        "strings"
        "time"
 
@@ -172,7 +174,7 @@ func (s *executorSuite) TestExecStdoutStderr(c *C) {
        c.Check(s.stderr.String(), Equals, "barwaz\n")
 }
 
-func (s *executorSuite) TestIPAddress(c *C) {
+func (s *executorSuite) TestEnableNetwork_Listen(c *C) {
        // Listen on an available port on the host.
        ln, err := net.Listen("tcp", net.JoinHostPort("0.0.0.0", "0"))
        c.Assert(err, IsNil)
@@ -192,13 +194,15 @@ func (s *executorSuite) TestIPAddress(c *C) {
        defer cancel()
 
        for {
+               time.Sleep(time.Second / 10)
                if ctx.Err() != nil {
                        c.Error("timed out")
                        break
                }
-               time.Sleep(time.Second / 10)
+
                ip, err := s.executor.IPAddress()
                if err != nil {
+                       c.Logf("s.executor.IPAddress: %s", err)
                        continue
                }
                c.Assert(ip, Not(Equals), "")
@@ -208,7 +212,9 @@ func (s *executorSuite) TestIPAddress(c *C) {
                // process running inside the container, not the
                // net.Listen() running outside the container, even
                // though both listen on the same port.
-               req, err := http.NewRequest("BREW", "http://"+net.JoinHostPort(ip, port), nil)
+               ctx, cancel := context.WithDeadline(ctx, time.Now().Add(time.Second))
+               defer cancel()
+               req, err := http.NewRequestWithContext(ctx, "BREW", "http://"+net.JoinHostPort(ip, port), nil)
                c.Assert(err, IsNil)
                resp, err := http.DefaultClient.Do(req)
                if err != nil {
@@ -229,6 +235,29 @@ func (s *executorSuite) TestIPAddress(c *C) {
        c.Logf("stderr:\n%s\n\n", s.stderr.String())
 }
 
+func (s *executorSuite) TestEnableNetwork_IPAddress(c *C) {
+       s.spec.Command = []string{"ip", "ad"}
+       s.spec.EnableNetwork = true
+       c.Assert(s.executor.Create(s.spec), IsNil)
+       c.Assert(s.executor.Start(), IsNil)
+       ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(10*time.Second))
+       defer cancel()
+       code, _ := s.executor.Wait(ctx)
+       c.Check(code, Equals, 0)
+       c.Logf("stdout:\n%s\n\n", s.stdout.String())
+       c.Logf("stderr:\n%s\n\n", s.stderr.String())
+
+       found := false
+       for _, m := range regexp.MustCompile(` inet (.+?)/`).FindAllStringSubmatch(s.stdout.String(), -1) {
+               if addr, err := netip.ParseAddr(m[1]); err == nil && !addr.IsLoopback() {
+                       found = true
+                       c.Logf("found non-loopback IP address %q", m[1])
+                       break
+               }
+       }
+       c.Check(found, Equals, true, Commentf("container does not appear to have a non-loopback IP address"))
+}
+
 func (s *executorSuite) TestInject(c *C) {
        hostdir := c.MkDir()
        c.Assert(os.WriteFile(hostdir+"/testfile", []byte("first tube"), 0777), IsNil)