Skip to content

Commit a6f19ef

Browse files
committed
FIX docker-archive-public#3141 Refactor netstat command and tests
Updated `netstat` command to minimize its output with `-t` and `-l` flags. Refactored regex on `libmachine/provision/utils.go`:`checkDaemonUp` to avoid `matchNetstatOut` returning `true` when remote machine has a similar port to dockerPort listening (e.g. dockerPort := 2376 and remote machine has port 23760 listening). Refactored `utils_test.go` with more accurate `netstat` output. Signed-off-by: Tiago Pires <tandrepires@gmail.com>
1 parent 0d58079 commit a6f19ef

File tree

2 files changed

+10
-27
lines changed

2 files changed

+10
-27
lines changed

libmachine/provision/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,10 @@ func getFilesystemType(p Provisioner, directory string) (string, error) {
250250
}
251251

252252
func checkDaemonUp(p Provisioner, dockerPort int) func() bool {
253-
reDaemonListening := fmt.Sprintf(":%d.*LISTEN", dockerPort)
253+
reDaemonListening := fmt.Sprintf(":%d\\s+.*:.*", dockerPort)
254254
return func() bool {
255255
// HACK: Check netstat's output to see if anyone's listening on the Docker API port.
256-
netstatOut, err := p.SSHCommand("netstat -an")
256+
netstatOut, err := p.SSHCommand("netstat -tln")
257257
if err != nil {
258258
log.Warnf("Error running SSH command: %s", err)
259259
return false

libmachine/provision/utils_test.go

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,16 @@ import (
1717
)
1818

1919
var (
20-
reDaemonListening = ":2376.*LISTEN"
20+
reDaemonListening = ":2376\\s+.*:.*"
2121
)
2222

2323
func TestMatchNetstatOutMissing(t *testing.T) {
2424
nsOut := `Active Internet connections (servers and established)
2525
Proto Recv-Q Send-Q Local Address Foreign Address State
26-
tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN
27-
tcp 0 72 192.168.25.141:ssh 192.168.25.1:63213 ESTABLISHED
28-
tcp 0 0 :::ssh :::* LISTEN
29-
Active UNIX domain sockets (servers and established)
30-
Proto RefCnt Flags Type State I-Node Path
31-
unix 2 [ ACC ] STREAM LISTENING 17990 /var/run/acpid.socket
32-
unix 2 [ ACC ] SEQPACKET LISTENING 14233 /run/udev/control
33-
unix 3 [ ] STREAM CONNECTED 18688
34-
unix 3 [ ] DGRAM 14243
35-
unix 3 [ ] STREAM CONNECTED 18689
36-
unix 3 [ ] DGRAM 14242`
26+
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
27+
tcp 0 0 0.0.0.0:237 0.0.0.0:* LISTEN
28+
tcp6 0 0 :::22 :::* LISTEN
29+
tcp6 0 0 :::23760 :::* LISTEN`
3730
if matchNetstatOut(reDaemonListening, nsOut) {
3831
t.Fatal("Expected not to match the netstat output as showing the daemon listening but got a match")
3932
}
@@ -42,19 +35,9 @@ unix 3 [ ] DGRAM 14242`
4235
func TestMatchNetstatOutPresent(t *testing.T) {
4336
nsOut := `Active Internet connections (servers and established)
4437
Proto Recv-Q Send-Q Local Address Foreign Address State
45-
tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN
46-
tcp 0 72 192.168.25.141:ssh 192.168.25.1:63235 ESTABLISHED
47-
tcp 0 0 :::2376 :::* LISTEN
48-
tcp 0 0 :::ssh :::* LISTEN
49-
Active UNIX domain sockets (servers and established)
50-
Proto RefCnt Flags Type State I-Node Path
51-
unix 2 [ ACC ] STREAM LISTENING 17990 /var/run/acpid.socket
52-
unix 2 [ ACC ] SEQPACKET LISTENING 14233 /run/udev/control
53-
unix 2 [ ACC ] STREAM LISTENING 19365 /var/run/docker.sock
54-
unix 3 [ ] STREAM CONNECTED 19774
55-
unix 3 [ ] STREAM CONNECTED 19775
56-
unix 3 [ ] DGRAM 14243
57-
unix 3 [ ] DGRAM 14242`
38+
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
39+
tcp6 0 0 :::2376 :::* LISTEN
40+
tcp6 0 0 :::22 :::* LISTEN`
5841
if !matchNetstatOut(reDaemonListening, nsOut) {
5942
t.Fatal("Expected to match the netstat output as showing the daemon listening but didn't")
6043
}

0 commit comments

Comments
 (0)