1
0
mirror of https://github.com/golang/go synced 2024-11-17 14:14:56 -07:00

net: skip tests if creating a socket is disallowed

In a container environment, creating a socket may be disallowed. Try to
detect these cases and skip the tests instead of failing them.

Fixes #58114

Change-Id: I681d19107e946d2508e2d1704956360f13c7335b
Reviewed-on: https://go-review.googlesource.com/c/go/+/476217
Reviewed-by: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
This commit is contained in:
Tobias Klauser 2023-03-15 15:17:11 +01:00 committed by Gopher Robot
parent fbe3136646
commit 1d06667bc4
2 changed files with 19 additions and 10 deletions

View File

@ -7,6 +7,7 @@
package net package net
import ( import (
"internal/testenv"
"reflect" "reflect"
"testing" "testing"
) )
@ -94,7 +95,11 @@ func TestIPConnLocalName(t *testing.T) {
continue continue
} }
c, err := ListenIP(tt.net, tt.laddr) c, err := ListenIP(tt.net, tt.laddr)
if err != nil { if testenv.SyscallIsNotSupported(err) {
// May be inside a container that disallows creating a socket.
t.Logf("skipping %s test: %v", tt.net, err)
continue
} else if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer c.Close() defer c.Close()
@ -105,13 +110,17 @@ func TestIPConnLocalName(t *testing.T) {
} }
func TestIPConnRemoteName(t *testing.T) { func TestIPConnRemoteName(t *testing.T) {
if !testableNetwork("ip:tcp") { network := "ip:tcp"
t.Skip("ip:tcp test") if !testableNetwork(network) {
t.Skipf("skipping %s test", network)
} }
raddr := &IPAddr{IP: IPv4(127, 0, 0, 1).To4()} raddr := &IPAddr{IP: IPv4(127, 0, 0, 1).To4()}
c, err := DialIP("ip:tcp", &IPAddr{IP: IPv4(127, 0, 0, 1)}, raddr) c, err := DialIP(network, &IPAddr{IP: IPv4(127, 0, 0, 1)}, raddr)
if err != nil { if testenv.SyscallIsNotSupported(err) {
// May be inside a container that disallows creating a socket.
t.Skipf("skipping %s test: %v", network, err)
} else if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer c.Close() defer c.Close()

View File

@ -10,6 +10,7 @@
package net package net
import ( import (
"internal/testenv"
"os" "os"
"runtime" "runtime"
"testing" "testing"
@ -161,16 +162,15 @@ func TestUDPConnSpecificMethods(t *testing.T) {
} }
func TestIPConnSpecificMethods(t *testing.T) { func TestIPConnSpecificMethods(t *testing.T) {
if os.Getuid() != 0 {
t.Skip("must be root")
}
la, err := ResolveIPAddr("ip4", "127.0.0.1") la, err := ResolveIPAddr("ip4", "127.0.0.1")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
c, err := ListenIP("ip4:icmp", la) c, err := ListenIP("ip4:icmp", la)
if err != nil { if testenv.SyscallIsNotSupported(err) {
// May be inside a container that disallows creating a socket.
t.Skipf("skipping: %v", err)
} else if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer c.Close() defer c.Close()