Skip to content

Commit

Permalink
Move bad port tests to non-integration, use mocker instead of relying on
Browse files Browse the repository at this point in the history
port number to fail the test
  • Loading branch information
julianneswinoga committed Jan 1, 2024
1 parent c3809d1 commit 8c5c9f2
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 21 deletions.
17 changes: 17 additions & 0 deletions tests/test_ctrls_connection.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
from flightgear_python.fg_if import CtrlsConnection
from flightgear_python.fg_util import FGConnectionError
from testing_common import supported_ctrls_versions

import pytest


def test_ctrls_wrong_version_on_create():
with pytest.raises(NotImplementedError):
CtrlsConnection(ctrls_version=1)


@pytest.mark.parametrize('ctrls_version', supported_ctrls_versions)
def test_ctrls_bad_port(mocker, ctrls_version):
def mock_bind(addr):
# Binding to port 1 should usually fail with Should fail with `[Errno 13] Permission denied`
# but this is more reliable
raise PermissionError('Mock permission fail')

mocker.patch('socket.socket.bind', mock_bind)

ctrls_c = CtrlsConnection(ctrls_version)
with pytest.raises(FGConnectionError):
# Should fail with [Errno 13] Permission denied
ctrls_c.connect_rx('localhost', 1, lambda data, pipe: data)
15 changes: 15 additions & 0 deletions tests/test_fdm_connection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from flightgear_python.fg_if import FDMConnection
from flightgear_python.fg_util import FGConnectionError
from testing_common import supported_fdm_versions

import pytest
Expand Down Expand Up @@ -95,3 +96,17 @@ def rx_cb(fdm_data, event_pipe):
def test_fdm_wrong_version_on_create():
with pytest.raises(NotImplementedError):
FDMConnection(fdm_version=1)


@pytest.mark.parametrize('fdm_version', supported_fdm_versions)
def test_fdm_bad_port(mocker, fdm_version):
def mock_bind(addr):
# Binding to port 1 should usually fail with Should fail with `[Errno 13] Permission denied`
# but this is more reliable
raise PermissionError('Mock permission fail')

mocker.patch('socket.socket.bind', mock_bind)

fdm_c = FDMConnection(fdm_version)
with pytest.raises(FGConnectionError):
fdm_c.connect_rx('localhost', 1, lambda data, pipe: data)
17 changes: 17 additions & 0 deletions tests/test_gui_connection.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
from flightgear_python.fg_if import GuiConnection
from flightgear_python.fg_util import FGConnectionError
from testing_common import supported_gui_versions

import pytest


def test_gui_wrong_version_on_create():
with pytest.raises(NotImplementedError):
GuiConnection(gui_version=1)


@pytest.mark.parametrize('gui_version', supported_gui_versions)
def test_gui_bad_port(mocker, gui_version):
def mock_bind(addr):
# Binding to port 1 should usually fail with Should fail with `[Errno 13] Permission denied`
# but this is more reliable
raise PermissionError('Mock permission fail')

mocker.patch('socket.socket.bind', mock_bind)

gui_c = GuiConnection(gui_version)
with pytest.raises(FGConnectionError):
# Should fail with [Errno 13] Permission denied
gui_c.connect_rx('localhost', 1, lambda data, pipe: data)
7 changes: 0 additions & 7 deletions tests/test_integration_FG_Ctrls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest

from flightgear_python.fg_if import CtrlsConnection
from flightgear_python.fg_util import FGConnectionError


pytestmark = pytest.mark.fg_integration
Expand Down Expand Up @@ -35,9 +34,3 @@ def rx_cb(ctrls_data, event_pipe):
# Prevent 'ResourceWarning: unclosed' warning
ctrls_c.fg_rx_sock.close()
ctrls_c.fg_tx_sock.close()


def test_ctrls_wrong_port():
ctrls_c = CtrlsConnection(ctrls_version)
# with pytest.raises(FGConnectionError):
ctrls_c.connect_rx('localhost', 123, lambda data, pipe: data)
8 changes: 1 addition & 7 deletions tests/test_integration_FG_FDM.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from flightgear_python.fg_if import FDMConnection
from flightgear_python.fg_util import FGConnectionError, FGCommunicationError
from flightgear_python.fg_util import FGCommunicationError

from testing_common import supported_fdm_versions

Expand Down Expand Up @@ -39,12 +39,6 @@ def rx_cb(fdm_data, event_pipe):
fdm_c.fg_tx_sock.close()


def test_fdm_wrong_port():
fdm_c = FDMConnection(fdm_version)
# with pytest.raises(FGConnectionError):
fdm_c.connect_rx('localhost', 123, lambda data, pipe: data)


def test_fdm_wrong_version_in_stream():
fdm_version_supported_but_not_downloaded = fdm_version + 1
assert fdm_version_supported_but_not_downloaded in supported_fdm_versions
Expand Down
7 changes: 0 additions & 7 deletions tests/test_integration_FG_Gui.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest

from flightgear_python.fg_if import GuiConnection
from flightgear_python.fg_util import FGConnectionError


pytestmark = pytest.mark.fg_integration
Expand Down Expand Up @@ -35,9 +34,3 @@ def rx_cb(gui_data, event_pipe):
# Prevent 'ResourceWarning: unclosed' warning
gui_c.fg_rx_sock.close()
gui_c.fg_tx_sock.close()


def test_gui_wrong_port():
gui_c = GuiConnection(gui_version)
# with pytest.raises(FGConnectionError):
gui_c.connect_rx('localhost', 123, lambda data, pipe: data)

0 comments on commit 8c5c9f2

Please sign in to comment.