Skip to content

Commit 53a2bf7

Browse files
committed
skip ping if node has no ip
1 parent 600878b commit 53a2bf7

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

internal/clusters/ping4.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,18 @@ func ping4(logger zerolog.Logger, conn *icmp.PacketConn, id, seq int, dst *net.U
124124
// Ping pings a single IPv4 address with the requested amount of retries.
125125
// An error is returned when more then count/2 packets are lost.
126126
func Ping(logger zerolog.Logger, count int, dst string) error {
127+
dstAddr := &net.UDPAddr{IP: net.ParseIP(dst)}
128+
if dstAddr.IP == nil {
129+
logger.Warn().Msgf("Received invalid IP address to ping %q, skipping unreachability check", dst)
130+
return nil
131+
}
132+
127133
conn, err := icmp.ListenPacket("udp4", "0.0.0.0")
128134
if err != nil {
129135
return fmt.Errorf("failed to listen for icmp packets: %w", err)
130136
}
131137
defer conn.Close()
132138

133-
dstAddr := &net.UDPAddr{IP: net.ParseIP(dst)}
134139
id := os.Getpid() & 0xffff // 16bit id
135140
lost := 0
136141

internal/clusters/ping4_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,53 @@ func TestPingAll(t *testing.T) {
250250
})
251251
}
252252
}
253+
254+
func TestPing(t *testing.T) {
255+
logger := zerolog.New(os.Stdout)
256+
type args struct {
257+
count int
258+
dst string
259+
}
260+
261+
tests := []struct {
262+
name string
263+
args args
264+
wantErr bool
265+
}{
266+
{
267+
name: "ok-empty",
268+
args: args{
269+
count: 5,
270+
dst: "",
271+
},
272+
wantErr: false,
273+
},
274+
{
275+
name: "ok-localhost",
276+
args: args{
277+
count: 5,
278+
dst: "127.0.0.1",
279+
},
280+
wantErr: false,
281+
},
282+
{
283+
name: "fail-invalid",
284+
args: args{
285+
count: 5,
286+
dst: "0.0.0.0",
287+
},
288+
wantErr: true,
289+
},
290+
}
291+
292+
for _, tt := range tests {
293+
t.Run(tt.name, func(t *testing.T) {
294+
t.Parallel()
295+
296+
err := Ping(logger, tt.args.count, tt.args.dst)
297+
if (err != nil) != tt.wantErr {
298+
t.Fatalf("Ping() = %v want %v", err, tt.wantErr)
299+
}
300+
})
301+
}
302+
}

0 commit comments

Comments
 (0)