Skip to content

Commit 4cc30ae

Browse files
committed
tests/ci: Make e2e more CI friendly
Chose a random *free* port for I/O --- still not enough to make it run reliably on the CI.
1 parent c82a75f commit 4cc30ae

File tree

1 file changed

+83
-51
lines changed

1 file changed

+83
-51
lines changed

tests/test_tls.py

Lines changed: 83 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
from mbedtls.x509 import CRT, CSR, BasicConstraints
1717

1818

19+
@pytest.fixture(scope="module")
20+
def rootpath():
21+
return Path(__file__).parent.parent
22+
23+
1924
class TestPickle:
2025
@pytest.mark.parametrize(
2126
"obj",
@@ -623,73 +628,100 @@ def test_dtls(self):
623628

624629

625630
@pytest.mark.local
626-
class TestPrograms:
631+
class TestProgramsTLS:
627632
@pytest.fixture
628-
def rootpath(self):
629-
return Path(__file__).parent.parent
633+
def port(self):
634+
"""Return a free port
630635
631-
@pytest.fixture
632-
def tls_server(self, rootpath):
633-
proc = subprocess.Popen(
634-
[
635-
rootpath / "programs" / "server.py",
636-
"--tls",
637-
"--psk-store",
638-
"cli=secret",
639-
]
640-
)
641-
yield proc
642-
proc.kill()
643-
proc.wait(1.0)
636+
Note:
637+
Not 100% race condition free.
638+
639+
"""
640+
port = 0
641+
with socket.socket() as sock:
642+
sock.bind(("", port))
643+
port = sock.getsockname()[1]
644+
return port
644645

645646
@pytest.fixture
646-
def dtls_server(self, rootpath):
647-
proc = subprocess.Popen(
648-
[
649-
rootpath / "programs" / "server.py",
650-
"--dtls",
651-
"--psk-store",
652-
"cli=secret",
653-
]
654-
)
647+
def server(self, rootpath, port):
648+
args = [
649+
rootpath / "programs" / "server.py",
650+
"--port",
651+
f"{port}",
652+
"--tls",
653+
"--psk-store",
654+
"cli=secret",
655+
]
656+
proc = subprocess.Popen(args)
655657
yield proc
656658
proc.kill()
657659
proc.wait(1.0)
658660

659-
@pytest.mark.usefixtures("tls_server")
661+
@pytest.mark.usefixtures("server")
660662
@pytest.mark.timeout(10)
661-
def test_e2e_tls(self, rootpath):
663+
def test_communicate(self, rootpath, port):
662664
secret = b"a very secret message"
663-
665+
args = [
666+
rootpath / "programs" / "client.py",
667+
"--port",
668+
f"{port}",
669+
"--tls",
670+
"--psk",
671+
"cli=secret",
672+
secret,
673+
]
664674
for _ in range(3):
665-
with subprocess.Popen(
666-
[
667-
rootpath / "programs" / "client.py",
668-
"--tls",
669-
"--psk",
670-
"cli=secret",
671-
secret,
672-
],
673-
stdout=subprocess.PIPE,
674-
) as client:
675+
with subprocess.Popen(args, stdout=subprocess.PIPE) as client:
675676
out, err = client.communicate()
676677
assert out == secret + b"\n"
677678

678-
@pytest.mark.usefixtures("dtls_server")
679+
680+
@pytest.mark.local
681+
class TestProgramsDTLS:
682+
@pytest.fixture
683+
def port(self):
684+
"""Return a free port
685+
686+
Note:
687+
Not 100% race condition free.
688+
689+
"""
690+
port = 0
691+
with socket.socket() as sock:
692+
sock.bind(("", port))
693+
port = sock.getsockname()[1]
694+
return port
695+
696+
@pytest.fixture
697+
def server(self, rootpath, port):
698+
args = [
699+
rootpath / "programs" / "server.py",
700+
"--port",
701+
f"{port}",
702+
"--dtls",
703+
"--psk-store",
704+
"cli=secret",
705+
]
706+
proc = subprocess.Popen(args)
707+
yield proc
708+
proc.kill()
709+
proc.wait(1.0)
710+
711+
@pytest.mark.usefixtures("server")
679712
@pytest.mark.timeout(10)
680-
def test_e2e_dtls(self, rootpath):
713+
def test_communication(self, rootpath, port):
681714
secret = b"a very secret message"
682-
715+
args = [
716+
rootpath / "programs" / "client.py",
717+
"--port",
718+
f"{port}",
719+
"--dtls",
720+
"--psk",
721+
"cli=secret",
722+
secret,
723+
]
683724
for _ in range(3):
684-
with subprocess.Popen(
685-
[
686-
rootpath / "programs" / "client.py",
687-
"--dtls",
688-
"--psk",
689-
"cli=secret",
690-
secret,
691-
],
692-
stdout=subprocess.PIPE,
693-
) as client:
725+
with subprocess.Popen(args, stdout=subprocess.PIPE) as client:
694726
out, err = client.communicate()
695727
assert out == secret + b"\n"

0 commit comments

Comments
 (0)