diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 05:38:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 05:38:43 +0000 |
commit | 1dd702b721f7d1836af27c8a114f1386ff9f57bd (patch) | |
tree | 622a84466e38b4480dbbbdfa64263da19d736e2f /tests | |
parent | Adding upstream version 0.7.1. (diff) | |
download | port-for-upstream/0.7.2.tar.xz port-for-upstream/0.7.2.zip |
Adding upstream version 0.7.2.upstream/0.7.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_cases.py | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/tests/test_cases.py b/tests/test_cases.py index 02772a8..8f5f3f1 100644 --- a/tests/test_cases.py +++ b/tests/test_cases.py @@ -1,10 +1,10 @@ -# -*- coding: utf-8 -*- -import unittest +"""Test cases.""" +import os +import socket import tempfile +import unittest +from typing import List, Set, Tuple, Union from unittest import mock -import socket -import os -from typing import Union, List, Set, Tuple import pytest @@ -14,11 +14,13 @@ from port_for.utils import ranges_to_set def test_common_ports() -> None: + """Check common ports (not available).""" assert not port_for.is_available(80) assert not port_for.is_available(11211) def test_good_port_ranges() -> None: + """Select good ranges of ports aout of provided.""" ranges = [ (10, 15), # too short (100, 200), # good @@ -33,16 +35,18 @@ def test_good_port_ranges() -> None: def test_something_works() -> None: + """Test default behaviour of good_port_ranges and available_good_ports.""" assert len(port_for.good_port_ranges()) > 10 assert len(port_for.available_good_ports()) > 1000 def test_binding() -> None: - # low ports are not available + """Low ports are not available.""" assert port_for.port_is_used(10) def test_binding_high() -> None: + """Test ports that are not used.""" s = socket.socket() s.bind(("", 0)) port = s.getsockname()[1] @@ -115,13 +119,17 @@ def test_port_mix() -> None: class SelectPortTest(unittest.TestCase): + """Port selecting tests.""" + @mock.patch("port_for.api.port_is_used") def test_all_used(self, port_is_used: mock.MagicMock) -> None: + """Check behaviour if there are no ports to use.""" port_is_used.return_value = True self.assertRaises(port_for.PortForException, port_for.select_random) @mock.patch("port_for.api.port_is_used") def test_random_port(self, port_is_used: mock.MagicMock) -> None: + """Test random ports.""" ports = set([1, 2, 3]) used = {1: True, 2: False, 3: True} port_is_used.side_effect = lambda port: used[port] @@ -131,14 +139,19 @@ class SelectPortTest(unittest.TestCase): class StoreTest(unittest.TestCase): + """Port Store test suite.""" + def setUp(self) -> None: + """Set up tests.""" fd, self.fname = tempfile.mkstemp() self.store = port_for.PortStore(self.fname) def tearDown(self) -> None: + """Tear down tests.""" os.remove(self.fname) def test_store(self) -> None: + """Test port store.""" assert self.store.bound_ports() == [] port = self.store.bind_port("foo") @@ -156,14 +169,14 @@ class StoreTest(unittest.TestCase): self.assertEqual(self.store.bound_ports(), [("foo", port)]) def test_rebind(self) -> None: - # try to rebind an used port for an another app + """Try to rebind an used port for an another app.""" port = self.store.bind_port("foo") self.assertRaises( port_for.PortForException, self.store.bind_port, "baz", port ) def test_change_port(self) -> None: - # changing app ports is not supported. + """Changing app ports is not supported.""" port = self.store.bind_port("foo") another_port = port_for.select_random() assert port != another_port @@ -172,13 +185,13 @@ class StoreTest(unittest.TestCase): ) def test_bind_unavailable(self) -> None: - # it is possible to explicitly bind currently unavailable port + """It is possible to explicitly bind currently unavailable port.""" port = self.store.bind_port("foo", 80) self.assertEqual(port, 80) self.assertEqual(self.store.bound_ports(), [("foo", 80)]) def test_bind_non_auto(self) -> None: - # it is possible to pass a port + """It is possible to pass a port.""" port = port_for.select_random() res_port = self.store.bind_port("foo", port) self.assertEqual(res_port, port) |