From 3f307880f9df8f7cdc8e792dcea245402b2d47c5 Mon Sep 17 00:00:00 2001 From: Mike Kinney Date: Thu, 30 Dec 2021 20:04:32 -0800 Subject: [PATCH] add unit tests for tunnel and subnet --- meshtastic/__main__.py | 6 +++--- meshtastic/tests/test_main.py | 40 +++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index b574bad..23584c3 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -18,9 +18,6 @@ from . import portnums_pb2, channel_pb2, radioconfig_pb2 from .globals import Globals -have_tunnel = platform.system() == 'Linux' -"""We only import the tunnel code if we are on a platform that can run it. """ - def onReceive(packet, interface): """Callback invoked when a packet arrives""" our_globals = Globals.getInstance() @@ -495,6 +492,7 @@ def onConnected(interface): qr = pyqrcode.create(url) print(qr.terminal()) + have_tunnel = platform.system() == 'Linux' if have_tunnel and args.tunnel: # pylint: disable=C0415 from . import tunnel @@ -640,6 +638,7 @@ def common(): #if logfile: #logfile.close() + have_tunnel = platform.system() == 'Linux' if args.noproto or args.reply or (have_tunnel and args.tunnel): # loop until someone presses ctrlc while True: time.sleep(1000) @@ -805,6 +804,7 @@ def initParser(): parser.add_argument('--unset-router', dest='deprecated', action='store_false', help='Deprecated, use "--set is_router false" instead') + have_tunnel = platform.system() == 'Linux' if have_tunnel: parser.add_argument('--tunnel', action='store_true', help="Create a TUN tunnel device for forwarding IP packets over the mesh") diff --git a/meshtastic/tests/test_main.py b/meshtastic/tests/test_main.py index 3c5b423..831474c 100644 --- a/meshtastic/tests/test_main.py +++ b/meshtastic/tests/test_main.py @@ -5,6 +5,7 @@ import sys import os import re import logging +import platform from unittest.mock import patch, MagicMock import pytest @@ -1692,3 +1693,42 @@ def test_tunnel_no_args(capsys, reset_globals): assert pytest_wrapped_e.value.code == 1 _, err = capsys.readouterr() assert re.search(r'usage: ', err, re.MULTILINE) + + +@pytest.mark.unit +@patch('platform.system') +def test_tunnel_tunnel_arg(mock_platform_system, capsys, reset_globals): + """Test tunnel with tunnel arg (act like we are on a linux system)""" + a_mock = MagicMock() + a_mock.return_value = 'Linux' + mock_platform_system.side_effect = a_mock + sys.argv = ['', '--tunnel'] + Globals.getInstance().set_args(sys.argv) + print(f'platform.system():{platform.system()}') + with pytest.raises(SystemExit) as pytest_wrapped_e: + tunnelMain() + mock_platform_system.assert_called() + assert pytest_wrapped_e.type == SystemExit + assert pytest_wrapped_e.value.code == 1 + _, err = capsys.readouterr() + assert not re.search(r'usage: ', err, re.MULTILINE) + + +@pytest.mark.unit +@patch('platform.system') +def test_tunnel_subnet_arg(mock_platform_system, capsys, reset_globals): + """Test tunnel with subnet arg (act like we are on a linux system)""" + a_mock = MagicMock() + a_mock.return_value = 'Linux' + mock_platform_system.side_effect = a_mock + sys.argv = ['', '--subnet', 'foo'] + Globals.getInstance().set_args(sys.argv) + print(f'platform.system():{platform.system()}') + with pytest.raises(SystemExit) as pytest_wrapped_e: + tunnelMain() + mock_platform_system.assert_called() + assert pytest_wrapped_e.type == SystemExit + assert pytest_wrapped_e.value.code == 1 + out, err = capsys.readouterr() + assert not re.search(r'usage: ', err, re.MULTILINE) + assert re.search(r'Warning: No Meshtastic devices detected', out, re.MULTILINE)