From 82ae2c8e4309272be1807a64a3bfe9541d855d01 Mon Sep 17 00:00:00 2001 From: Mike Kinney Date: Fri, 3 Dec 2021 09:50:48 -0800 Subject: [PATCH 1/3] added smoke tests on one device --- README.md | 13 ++- meshtastic/node.py | 3 + meshtastic/test/test_smoke1.py | 197 +++++++++++++++++++++++++++++++++ pytest.ini | 3 + 4 files changed, 211 insertions(+), 5 deletions(-) create mode 100644 meshtastic/test/test_smoke1.py diff --git a/README.md b/README.md index ccde356..9a5efbb 100644 --- a/README.md +++ b/README.md @@ -178,8 +178,11 @@ pip3 install . pytest ``` Possible options for testing: -* for more verbosity, add "-v" or even "-vv" like this: pytest -vv -* to run just unit tests: pytest -munit -* to run just integration tests: pytest -mint -* if you want to add another classification of tests, then look in pytest.ini -* if you want to see the unit test code coverage: pytest --cov=meshtastic +* For more verbosity, add "-v" or even "-vv" like this: pytest -vv +* To run just unit tests: pytest -munit +* To run just integration tests: pytest -mint +* To run a smoke test with only one device connected serially: pytest -msmoke1 + CAUTION: Running smoke1 will reset values on the device. +* To run a specific test: pytest -msmoke1 meshtastic/test/test_smoke1.py::test_smoke1_info +* To add another classification of tests, then look in pytest.ini +* To see the unit test code coverage: pytest --cov=meshtastic diff --git a/meshtastic/node.py b/meshtastic/node.py index acafce7..fed469b 100644 --- a/meshtastic/node.py +++ b/meshtastic/node.py @@ -259,6 +259,9 @@ class Node: channelSet = apponly_pb2.ChannelSet() channelSet.ParseFromString(decodedURL) + if len(channelSet.settings) == 0: + raise Exception("There were no settings.") + i = 0 for chs in channelSet.settings: ch = channel_pb2.Channel() diff --git a/meshtastic/test/test_smoke1.py b/meshtastic/test/test_smoke1.py new file mode 100644 index 0000000..289b4f8 --- /dev/null +++ b/meshtastic/test/test_smoke1.py @@ -0,0 +1,197 @@ +"""Meshtastic smoke tests with a single device""" +import re +import subprocess +import platform +import time + +# Do not like using hard coded sleeps, but it probably makes +# sense to pause for the radio at apprpriate times +import pytest + + +@pytest.mark.smoke1 +def test_smoke1_reboot(): + """Test reboot""" + return_value, out = subprocess.getstatusoutput('meshtastic --reboot') + assert return_value == 0 + # pause for the radio to reset (10 seconds for the pause, and a few more seconds to be back up) + time.sleep(18) + + +@pytest.mark.smoke1 +def test_smoke1_info(): + """Test --info""" + return_value, out = subprocess.getstatusoutput('meshtastic --info') + assert re.match(r'Connected to radio', out) + assert re.search(r'^Owner', out, re.MULTILINE) + assert re.search(r'^My info', out, re.MULTILINE) + assert re.search(r'^Nodes in mesh', out, re.MULTILINE) + assert re.search(r'^Preferences', out, re.MULTILINE) + assert re.search(r'^Channels', out, re.MULTILINE) + assert re.search(r'^ PRIMARY', out, re.MULTILINE) + assert re.search(r'^Primary channel URL', out, re.MULTILINE) + assert return_value == 0 + + +@pytest.mark.smoke1 +def test_smoke1_send_hello(): + """Test --sendtext hello""" + return_value, out = subprocess.getstatusoutput('meshtastic --sendtext hello') + assert re.match(r'Connected to radio', out) + assert re.search(r'^Sending text message hello to \^all', out, re.MULTILINE) + assert return_value == 0 + + +@pytest.mark.smoke1 +def test_smoke1_set_is_router_true(): + """Test --set is_router true""" + return_value, out = subprocess.getstatusoutput('meshtastic --set is_router true') + assert re.match(r'Connected to radio', out) + assert re.search(r'^Set is_router to true', out, re.MULTILINE) + assert return_value == 0 + # pause for the radio + time.sleep(1) + return_value, out = subprocess.getstatusoutput('meshtastic --get is_router') + assert re.search(r'^is_router: True', out, re.MULTILINE) + assert return_value == 0 + + +@pytest.mark.smoke1 +def test_smoke1_set_location_info(): + """Test --setlat, --setlon and --setalt """ + return_value, out = subprocess.getstatusoutput('meshtastic --setlat 32.7767 --setlon -96.7970 --setalt 1337') + assert re.match(r'Connected to radio', out) + assert re.search(r'^Fixing altitude', out, re.MULTILINE) + assert re.search(r'^Fixing latitude', out, re.MULTILINE) + assert re.search(r'^Fixing longitude', out, re.MULTILINE) + assert return_value == 0 + # pause for the radio + time.sleep(1) + return_value, out2 = subprocess.getstatusoutput('meshtastic --info') + assert re.search(r'1337', out2, re.MULTILINE) + assert re.search(r'32.7767', out2, re.MULTILINE) + assert re.search(r'-96.797', out2, re.MULTILINE) + assert return_value == 0 + + +@pytest.mark.smoke1 +def test_smoke1_set_is_router_false(): + """Test --set is_router false""" + return_value, out = subprocess.getstatusoutput('meshtastic --set is_router false') + assert re.match(r'Connected to radio', out) + assert re.search(r'^Set is_router to false', out, re.MULTILINE) + assert return_value == 0 + # pause for the radio + time.sleep(1) + return_value, out = subprocess.getstatusoutput('meshtastic --get is_router') + assert re.search(r'^is_router: False', out, re.MULTILINE) + assert return_value == 0 + + +@pytest.mark.smoke1 +def test_smoke1_set_owner(): + """Test --set-owner name""" + # make sure the owner is not Joe + return_value, out = subprocess.getstatusoutput('meshtastic --set-owner Bob') + assert re.match(r'Connected to radio', out) + assert re.search(r'^Setting device owner to Bob', out, re.MULTILINE) + assert return_value == 0 + # pause for the radio + time.sleep(1) + return_value, out = subprocess.getstatusoutput('meshtastic --info') + assert not re.search(r'Owner: Joe', out, re.MULTILINE) + assert return_value == 0 + # pause for the radio + time.sleep(1) + return_value, out = subprocess.getstatusoutput('meshtastic --set-owner Joe') + assert re.match(r'Connected to radio', out) + assert re.search(r'^Setting device owner to Joe', out, re.MULTILINE) + assert return_value == 0 + # pause for the radio + time.sleep(1) + return_value, out = subprocess.getstatusoutput('meshtastic --info') + assert re.search(r'Owner: Joe', out, re.MULTILINE) + assert return_value == 0 + + +@pytest.mark.smoke1 +def test_smoke1_ch_set_name(): + """Test --ch-set name""" + return_value, out = subprocess.getstatusoutput('meshtastic --info') + assert not re.search(r'MyChannel', out, re.MULTILINE) + assert return_value == 0 + # pause for the radio + time.sleep(1) + return_value, out = subprocess.getstatusoutput('meshtastic --ch-set name MyChannel') + assert re.match(r'Connected to radio', out) + assert re.search(r'^Set name to MyChannel', out, re.MULTILINE) + assert return_value == 0 + # pause for the radio + time.sleep(1) + return_value, out = subprocess.getstatusoutput('meshtastic --info') + assert re.search(r'MyChannel', out, re.MULTILINE) + assert return_value == 0 + + +@pytest.mark.smoke1 +def test_smoke1_ch_set_modem_config(): + """Test --ch-set modem_config""" + return_value, out = subprocess.getstatusoutput('meshtastic --info') + assert not re.search(r'Bw31_25Cr48Sf512', out, re.MULTILINE) + assert return_value == 0 + # pause for the radio + time.sleep(1) + return_value, out = subprocess.getstatusoutput('meshtastic --ch-set modem_config Bw31_25Cr48Sf512') + assert re.match(r'Connected to radio', out) + assert re.search(r'^Set modem_config to Bw31_25Cr48Sf512', out, re.MULTILINE) + assert return_value == 0 + # pause for the radio + time.sleep(1) + return_value, out = subprocess.getstatusoutput('meshtastic --info') + assert re.search(r'Bw31_25Cr48Sf512', out, re.MULTILINE) + assert return_value == 0 + + +@pytest.mark.smoke1 +def test_smoke1_seturl_default(): + """Test --seturl with default value""" + # set some channel value so we no longer have a default channel + return_value, out = subprocess.getstatusoutput('meshtastic --ch-set name foo') + assert return_value == 0 + # pause for the radio + time.sleep(1) + # ensure we no longer have a default primary channel + return_value, out = subprocess.getstatusoutput('meshtastic --info') + assert not re.search('CgUYAyIBAQ', out, re.MULTILINE) + assert return_value == 0 + url = "https://www.meshtastic.org/d/#CgUYAyIBAQ" + return_value, out = subprocess.getstatusoutput(f"meshtastic --seturl {url}") + assert re.match(r'Connected to radio', out) + assert return_value == 0 + # pause for the radio + time.sleep(1) + return_value, out = subprocess.getstatusoutput('meshtastic --info') + assert re.search('CgUYAyIBAQ', out, re.MULTILINE) + assert return_value == 0 + + +@pytest.mark.smoke1 +def test_smoke1_seturl_invalid_url(): + """Test --seturl with invalid url""" + # Note: This url is no longer a valid url. + url = "https://www.meshtastic.org/c/#GAMiENTxuzogKQdZ8Lz_q89Oab8qB0RlZmF1bHQ=" + return_value, out = subprocess.getstatusoutput(f"meshtastic --seturl {url}") + assert re.match(r'Connected to radio', out) + assert re.search('Aborting', out, re.MULTILINE) + + +@pytest.mark.smoke1 +def test_smoke1_factory_reset(): + """Test factory reset""" + return_value, out = subprocess.getstatusoutput('meshtastic --set factory_reset true') + assert re.match(r'Connected to radio', out) + assert re.search(r'^Set factory_reset to true', out, re.MULTILINE) + assert re.search(r'^Writing modified preferences to device', out, re.MULTILINE) + assert return_value == 0 + # NOTE: The radio may not be responsive after this, may need to do a manual reboot + # by pressing the button diff --git a/pytest.ini b/pytest.ini index 9c7d72e..54ebba1 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,5 +1,8 @@ [pytest] +addopts = -m "not smoke1" + markers = unit: marks tests as unit tests int: marks tests as integration tests + smoke1: runs smoke test on one device From 7dd67bb4b48376b9f8024ec580c7928192deeabd Mon Sep 17 00:00:00 2001 From: Mike Kinney Date: Fri, 3 Dec 2021 10:34:00 -0800 Subject: [PATCH 2/3] add qr, port, nodes and seriallog testing --- meshtastic/test/test_smoke1.py | 54 ++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/meshtastic/test/test_smoke1.py b/meshtastic/test/test_smoke1.py index 289b4f8..3d2eea8 100644 --- a/meshtastic/test/test_smoke1.py +++ b/meshtastic/test/test_smoke1.py @@ -3,11 +3,14 @@ import re import subprocess import platform import time +import os # Do not like using hard coded sleeps, but it probably makes # sense to pause for the radio at apprpriate times import pytest +import meshtastic + @pytest.mark.smoke1 def test_smoke1_reboot(): @@ -33,6 +36,43 @@ def test_smoke1_info(): assert return_value == 0 +@pytest.mark.smoke1 +def test_smoke1_seriallog_to_file(): + """Test --seriallog to a file creates a file""" + filename = 'tmpoutput.txt' + if os.path.exists(f"{filename}"): + os.remove(f"{filename}") + return_value, out = subprocess.getstatusoutput(f'meshtastic --info --seriallog {filename}') + assert os.path.exists(f"{filename}") + assert return_value == 0 + os.remove(f"{filename}") + + +@pytest.mark.smoke1 +def test_smoke1_qr(): + """Test --qr""" + filename = 'tmpqr' + if os.path.exists(f"{filename}"): + os.remove(f"{filename}") + return_value, out = subprocess.getstatusoutput(f'meshtastic --qr > {filename}') + assert os.path.exists(f"{filename}") + # not really testing that a valid qr code is created, just that the file size + # is reasonably big enough for a qr code + assert os.stat(f"{filename}").st_size > 20000 + assert return_value == 0 + os.remove(f"{filename}") + + +@pytest.mark.smoke1 +def test_smoke1_nodes(): + """Test --nodes""" + return_value, out = subprocess.getstatusoutput('meshtastic --nodes') + assert re.match(r'Connected to radio', out) + assert re.search(r'^│ N │ User', out, re.MULTILINE) + assert re.search(r'^│ 1 │', out, re.MULTILINE) + assert return_value == 0 + + @pytest.mark.smoke1 def test_smoke1_send_hello(): """Test --sendtext hello""" @@ -42,6 +82,20 @@ def test_smoke1_send_hello(): assert return_value == 0 +@pytest.mark.smoke1 +def test_smoke1_port(): + """Test --port""" + # first, get the ports + ports = meshtastic.util.findPorts() + # hopefully there is just one + assert len(ports) == 1 + port = ports[0] + return_value, out = subprocess.getstatusoutput(f'meshtastic --port {port} --info') + assert re.match(r'Connected to radio', out) + assert re.search(r'^Owner', out, re.MULTILINE) + assert return_value == 0 + + @pytest.mark.smoke1 def test_smoke1_set_is_router_true(): """Test --set is_router true""" From f8ab4ffcfef2ba4daf4dbbf785be8e7ac0226121 Mon Sep 17 00:00:00 2001 From: Mike Kinney Date: Fri, 3 Dec 2021 11:38:50 -0800 Subject: [PATCH 3/3] add more smoke1 tests: debug, team, ch-longslow, ch-shortfast, ch-add, ch-del, ch-index --- README.md | 1 + bin/prerelease-tests.sh | 3 ++ meshtastic/__main__.py | 2 +- meshtastic/test/test_smoke1.py | 82 ++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9a5efbb..452995b 100644 --- a/README.md +++ b/README.md @@ -183,6 +183,7 @@ Possible options for testing: * To run just integration tests: pytest -mint * To run a smoke test with only one device connected serially: pytest -msmoke1 CAUTION: Running smoke1 will reset values on the device. + Be sure to hit the button on the device to reset after the test is run. * To run a specific test: pytest -msmoke1 meshtastic/test/test_smoke1.py::test_smoke1_info * To add another classification of tests, then look in pytest.ini * To see the unit test code coverage: pytest --cov=meshtastic diff --git a/bin/prerelease-tests.sh b/bin/prerelease-tests.sh index 6127792..27fe0a6 100755 --- a/bin/prerelease-tests.sh +++ b/bin/prerelease-tests.sh @@ -1,5 +1,7 @@ set -e +# You may consider running: "pytest -m smoke1" instead of this test. + echo "Running (crude) prerelease tests to verify sanity" echo running hello python3 tests/hello_world.py @@ -7,6 +9,7 @@ python3 tests/hello_world.py echo toggling router bin/run.sh --set is_router true bin/run.sh --set is_router false +# TODO: This does not seem to work. echo setting channel bin/run.sh --seturl "https://www.meshtastic.org/c/#GAMiENTxuzogKQdZ8Lz_q89Oab8qB0RlZmF1bHQ=" echo setting time diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index 702c828..6247a32 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -628,7 +628,7 @@ def initParser(): "--set-owner", help="Set device owner name", action="store") parser.add_argument( - "--set-team", help="Set team affiliation (? for options)", action="store") + "--set-team", help="Set team affiliation (an invalid team will list valid values)", action="store") parser.add_argument( "--set-ham", help="Set licensed HAM ID and turn off encryption", action="store") diff --git a/meshtastic/test/test_smoke1.py b/meshtastic/test/test_smoke1.py index 3d2eea8..72290db 100644 --- a/meshtastic/test/test_smoke1.py +++ b/meshtastic/test/test_smoke1.py @@ -36,6 +36,15 @@ def test_smoke1_info(): assert return_value == 0 +@pytest.mark.smoke1 +def test_smoke1_debug(): + """Test --debug""" + return_value, out = subprocess.getstatusoutput('meshtastic --info --debug') + assert re.search(r'^Owner', out, re.MULTILINE) + assert re.search(r'^DEBUG:root', out, re.MULTILINE) + assert return_value == 0 + + @pytest.mark.smoke1 def test_smoke1_seriallog_to_file(): """Test --seriallog to a file creates a file""" @@ -168,6 +177,51 @@ def test_smoke1_set_owner(): assert return_value == 0 +@pytest.mark.smoke1 +def test_smoke1_set_team(): + """Test --set-team """ + # unset the team + return_value, out = subprocess.getstatusoutput('meshtastic --set-team CLEAR') + assert re.match(r'Connected to radio', out) + assert re.search(r'^Setting team to CLEAR', out, re.MULTILINE) + assert return_value == 0 + # pause for the radio + time.sleep(1) + return_value, out = subprocess.getstatusoutput('meshtastic --set-team CYAN') + assert re.search(r'Setting team to CYAN', out, re.MULTILINE) + assert return_value == 0 + # pause for the radio + time.sleep(1) + return_value, out = subprocess.getstatusoutput('meshtastic --info') + assert re.search(r'CYAN', out, re.MULTILINE) + assert return_value == 0 + + +@pytest.mark.smoke1 +def test_smoke1_ch_longslow_and_ch_shortfast(): + """Test --ch-longslow and --ch-shortfast""" + # unset the team + return_value, out = subprocess.getstatusoutput('meshtastic --ch-longslow') + assert re.match(r'Connected to radio', out) + assert re.search(r'Writing modified channels to device', out, re.MULTILINE) + assert return_value == 0 + # pause for the radio (might reboot) + time.sleep(5) + return_value, out = subprocess.getstatusoutput('meshtastic --info') + assert re.search(r'Bw125Cr48Sf4096', out, re.MULTILINE) + assert return_value == 0 + # pause for the radio + time.sleep(1) + return_value, out = subprocess.getstatusoutput('meshtastic --ch-shortfast') + assert re.search(r'Writing modified channels to device', out, re.MULTILINE) + assert return_value == 0 + # pause for the radio (might reboot) + time.sleep(5) + return_value, out = subprocess.getstatusoutput('meshtastic --info') + assert re.search(r'Bw500Cr45Sf128', out, re.MULTILINE) + assert return_value == 0 + + @pytest.mark.smoke1 def test_smoke1_ch_set_name(): """Test --ch-set name""" @@ -187,6 +241,34 @@ def test_smoke1_ch_set_name(): assert return_value == 0 +@pytest.mark.smoke1 +def test_smoke1_ch_add_and_ch_del(): + """Test --ch-add""" + return_value, out = subprocess.getstatusoutput('meshtastic --ch-add testing') + assert re.search(r'Writing modified channels to device', out, re.MULTILINE) + assert return_value == 0 + # pause for the radio + time.sleep(1) + return_value, out = subprocess.getstatusoutput('meshtastic --info') + assert re.match(r'Connected to radio', out) + assert re.search(r'SECONDARY', out, re.MULTILINE) + assert re.search(r'testing', out, re.MULTILINE) + assert return_value == 0 + # pause for the radio + time.sleep(1) + return_value, out = subprocess.getstatusoutput('meshtastic --ch-index 1 --ch-del') + assert re.search(r'Deleting channel 1', out, re.MULTILINE) + assert return_value == 0 + # pause for the radio + time.sleep(4) + # make sure the secondar channel is not there + return_value, out = subprocess.getstatusoutput('meshtastic --info') + assert re.match(r'Connected to radio', out) + assert not re.search(r'SECONDARY', out, re.MULTILINE) + assert not re.search(r'testing', out, re.MULTILINE) + assert return_value == 0 + + @pytest.mark.smoke1 def test_smoke1_ch_set_modem_config(): """Test --ch-set modem_config"""