add unit tests for tunnel and subnet

This commit is contained in:
Mike Kinney
2021-12-30 20:04:32 -08:00
parent 50523ec1b1
commit 3f307880f9
2 changed files with 43 additions and 3 deletions

View File

@@ -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")

View File

@@ -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)