nixos/test-driver: add optional address arg to wait_for_{open,closed}_port

This is useful for testing servers configured to listen on specific
addresses.
This commit is contained in:
Bjørn Forsman 2022-12-29 13:33:25 +01:00
parent 904f07f595
commit dba49a43a0
3 changed files with 16 additions and 12 deletions

View file

@ -273,12 +273,13 @@ The following methods are available on machine objects:
`wait_for_open_port` `wait_for_open_port`
: Wait until a process is listening on the given TCP port (on : Wait until a process is listening on the given TCP port and IP address
`localhost`, at least). (default `localhost`).
`wait_for_closed_port` `wait_for_closed_port`
: Wait until nobody is listening on the given TCP port. : Wait until nobody is listening on the given TCP port and IP address
(default `localhost`).
`wait_for_x` `wait_for_x`

View file

@ -483,8 +483,8 @@ start_all()
</term> </term>
<listitem> <listitem>
<para> <para>
Wait until a process is listening on the given TCP port (on Wait until a process is listening on the given TCP port and
<literal>localhost</literal>, at least). IP address (default <literal>localhost</literal>).
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
@ -494,7 +494,8 @@ start_all()
</term> </term>
<listitem> <listitem>
<para> <para>
Wait until nobody is listening on the given TCP port. Wait until nobody is listening on the given TCP port and IP
address (default <literal>localhost</literal>).
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>

View file

@ -699,20 +699,22 @@ class Machine:
with self.nested("waiting for file {}".format(filename)): with self.nested("waiting for file {}".format(filename)):
retry(check_file) retry(check_file)
def wait_for_open_port(self, port: int) -> None: def wait_for_open_port(self, port: int, addr: str = "localhost") -> None:
def port_is_open(_: Any) -> bool: def port_is_open(_: Any) -> bool:
status, _ = self.execute("nc -z localhost {}".format(port)) status, _ = self.execute("nc -z {} {}".format(addr, port))
return status == 0 return status == 0
with self.nested("waiting for TCP port {}".format(port)): with self.nested("waiting for TCP port {} on {}".format(port, addr)):
retry(port_is_open) retry(port_is_open)
def wait_for_closed_port(self, port: int) -> None: def wait_for_closed_port(self, port: int, addr: str = "localhost") -> None:
def port_is_closed(_: Any) -> bool: def port_is_closed(_: Any) -> bool:
status, _ = self.execute("nc -z localhost {}".format(port)) status, _ = self.execute("nc -z {} {}".format(addr, port))
return status != 0 return status != 0
with self.nested("waiting for TCP port {} to be closed".format(port)): with self.nested(
"waiting for TCP port {} on {} to be closed".format(port, addr)
):
retry(port_is_closed) retry(port_is_closed)
def start_job(self, jobname: str, user: Optional[str] = None) -> Tuple[int, str]: def start_job(self, jobname: str, user: Optional[str] = None) -> Tuple[int, str]: