Skip to content

Commit 1d06667

Browse files
tklausergopherbot
authored andcommitted
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 <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Tobias Klauser <[email protected]> Auto-Submit: Tobias Klauser <[email protected]> Reviewed-by: Bryan Mills <[email protected]>
1 parent fbe3136 commit 1d06667

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/net/iprawsock_test.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package net
88

99
import (
10+
"internal/testenv"
1011
"reflect"
1112
"testing"
1213
)
@@ -94,7 +95,11 @@ func TestIPConnLocalName(t *testing.T) {
9495
continue
9596
}
9697
c, err := ListenIP(tt.net, tt.laddr)
97-
if err != nil {
98+
if testenv.SyscallIsNotSupported(err) {
99+
// May be inside a container that disallows creating a socket.
100+
t.Logf("skipping %s test: %v", tt.net, err)
101+
continue
102+
} else if err != nil {
98103
t.Fatal(err)
99104
}
100105
defer c.Close()
@@ -105,13 +110,17 @@ func TestIPConnLocalName(t *testing.T) {
105110
}
106111

107112
func TestIPConnRemoteName(t *testing.T) {
108-
if !testableNetwork("ip:tcp") {
109-
t.Skip("ip:tcp test")
113+
network := "ip:tcp"
114+
if !testableNetwork(network) {
115+
t.Skipf("skipping %s test", network)
110116
}
111117

112118
raddr := &IPAddr{IP: IPv4(127, 0, 0, 1).To4()}
113-
c, err := DialIP("ip:tcp", &IPAddr{IP: IPv4(127, 0, 0, 1)}, raddr)
114-
if err != nil {
119+
c, err := DialIP(network, &IPAddr{IP: IPv4(127, 0, 0, 1)}, raddr)
120+
if testenv.SyscallIsNotSupported(err) {
121+
// May be inside a container that disallows creating a socket.
122+
t.Skipf("skipping %s test: %v", network, err)
123+
} else if err != nil {
115124
t.Fatal(err)
116125
}
117126
defer c.Close()

src/net/protoconn_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package net
1111

1212
import (
13+
"internal/testenv"
1314
"os"
1415
"runtime"
1516
"testing"
@@ -161,16 +162,15 @@ func TestUDPConnSpecificMethods(t *testing.T) {
161162
}
162163

163164
func TestIPConnSpecificMethods(t *testing.T) {
164-
if os.Getuid() != 0 {
165-
t.Skip("must be root")
166-
}
167-
168165
la, err := ResolveIPAddr("ip4", "127.0.0.1")
169166
if err != nil {
170167
t.Fatal(err)
171168
}
172169
c, err := ListenIP("ip4:icmp", la)
173-
if err != nil {
170+
if testenv.SyscallIsNotSupported(err) {
171+
// May be inside a container that disallows creating a socket.
172+
t.Skipf("skipping: %v", err)
173+
} else if err != nil {
174174
t.Fatal(err)
175175
}
176176
defer c.Close()

0 commit comments

Comments
 (0)