Skip to content

Remove usage of not standard nc in remote_ops.py is_port_free #284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions testgres/operations/remote_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,22 +681,31 @@ def get_process_children(self, pid):
def is_port_free(self, number: int) -> bool:
assert type(number) == int # noqa: E721

cmd = ["nc", "-w", "5", "-z", "-v", "localhost", str(number)]
# grep -q returns 0 if a listening socket on that port is found
port_hex = format(number, '04X')

exit_status, output, error = self.exec_command(cmd=cmd, encoding=get_default_encoding(), ignore_errors=True, verbose=True)
# Search /proc/net/tcp and tcp6 for any entry with this port
cmd = ['/bin/bash', '-lc',
f"grep -q ':{port_hex} ' /proc/net/tcp /proc/net/tcp6"]

assert type(output) == str # noqa: E721
assert type(error) == str # noqa: E721
exit_status, output, error = self.exec_command(
cmd=cmd,
encoding=get_default_encoding(),
ignore_errors=True,
verbose=True
)

# grep exit 0 -> port is busy
if exit_status == 0:
return __class__._is_port_free__process_0(error)
return False

# grep exit 1 -> port is free
if exit_status == 1:
return __class__._is_port_free__process_1(error)

errMsg = "nc returns an unknown result code: {0}".format(exit_status)
return True

RaiseError.CommandExecutionError(
# any other code is an unexpected error
errMsg = f"grep returned unexpected exit code: {exit_status}"
raise RaiseError.CommandExecutionError(
cmd=cmd,
exit_code=exit_status,
message=errMsg,
Expand Down Expand Up @@ -746,12 +755,7 @@ def _is_port_free__process_0(error: str) -> bool:
@staticmethod
def _is_port_free__process_1(error: str) -> bool:
assert type(error) == str # noqa: E721
#
# Example of error text:
# "nc: connect to localhost (127.0.0.1) port 1024 (tcp) failed: Connection refused\n"
#
# May be here is needed to check error message?
#
return True

@staticmethod
Expand Down