Skip to content

Commit 9fa1601

Browse files
AeyohanJordanYates
authored andcommitted
tests: util: argparse: add server port tests
Added tests for the `add_server_port_parser` function for registering the `--server-port` argument. Tests include checking correct single and multi-port handling and default values for each. Added test for the `ServerPort` type to ensure that it correctly parses valid and invalid port numbers. Signed-off-by: Aeyohan Furtado <aeyohan@embeint.com>
1 parent c957e62 commit 9fa1601

1 file changed

Lines changed: 52 additions & 1 deletion

File tree

tests/util/test_argparse.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@
66

77
import pytest
88

9-
from infuse_iot.util.argparse import BtLeAddress, HexString, InfuseDeviceId, ValidDir, ValidFile
9+
from infuse_iot.util.argparse import (
10+
BtLeAddress,
11+
HexString,
12+
InfuseDeviceId,
13+
ServerPort,
14+
ValidDir,
15+
ValidFile,
16+
add_server_port_parser,
17+
)
1018

1119
assert "TOXTEMPDIR" in os.environ, "you must run these tests using tox"
1220

@@ -72,3 +80,46 @@ def test_hexstring():
7280
assert HexString("AABB") == b"\xaa\xbb"
7381
assert HexString("aa00bb") == b"\xaa\x00\xbb"
7482
assert HexString("00AABB") == b"\x00\xaa\xbb"
83+
84+
def test_server_port():
85+
with pytest.raises(argparse.ArgumentTypeError):
86+
ServerPort("NotAnInt")
87+
with pytest.raises(argparse.ArgumentTypeError):
88+
ServerPort("-1")
89+
with pytest.raises(argparse.ArgumentTypeError):
90+
ServerPort("65536")
91+
with pytest.raises(argparse.ArgumentError):
92+
# Must be odd
93+
ServerPort("2")
94+
with pytest.raises(argparse.ArgumentTypeError):
95+
# Cannot be wildcard port
96+
ServerPort("0")
97+
assert ServerPort("1") == ("224.1.1.1", 1)
98+
assert ServerPort("8751") == ("224.1.1.1", 8751)
99+
assert ServerPort("65535") == ("224.1.1.1", 65535)
100+
101+
def test_server_port_parser(capsys):
102+
parser = argparse.ArgumentParser()
103+
add_server_port_parser(parser)
104+
105+
args = parser.parse_args([])
106+
assert args.server_sock == ("224.1.1.1", 8751)
107+
args = parser.parse_args(["--server-port", "8751"])
108+
assert args.server_sock == ("224.1.1.1", 8751)
109+
args = parser.parse_args(["--server-port", "8753"])
110+
assert args.server_sock == ("224.1.1.1", 8753)
111+
with pytest.raises(SystemExit):
112+
# Additional port supplied when not supported
113+
args = parser.parse_args(["--server-port", "8751", "8753"])
114+
115+
parser = argparse.ArgumentParser()
116+
add_server_port_parser(parser, multi_port=True)
117+
118+
args = parser.parse_args([])
119+
assert args.server_sock == [("224.1.1.1", 8751)]
120+
args = parser.parse_args(["--server-port", "8751"])
121+
assert args.server_sock == [("224.1.1.1", 8751)]
122+
args = parser.parse_args(["--server-port", "8753"])
123+
assert args.server_sock == [("224.1.1.1", 8753)]
124+
args = parser.parse_args(["--server-port", "8751", "8753"])
125+
assert args.server_sock == [("224.1.1.1", 8751), ("224.1.1.1", 8753)]

0 commit comments

Comments
 (0)