"io/ioutil"
"net"
"net/http"
+ "net/netip"
"os"
+ "regexp"
"strings"
"time"
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)
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), "")
// 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 {
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)