Files
OctoPrint/tests/util/test_comm.py
T
Gina Häußge 18e6bfc7f8 Merge branch 'maintenance' into devel
# Conflicts:
#	.github/workflows/build.yml
#	docs/conf.py
#	docs/development/environment.rst
#	docs/sphinxext/codeblockext.py
#	docs/sphinxext/onlineinclude.py
#	setup.py
#	src/octoprint/access/groups.py
#	src/octoprint/access/permissions.py
#	src/octoprint/access/users.py
#	src/octoprint/cli/__init__.py
#	src/octoprint/cli/analysis.py
#	src/octoprint/cli/client.py
#	src/octoprint/cli/config.py
#	src/octoprint/cli/plugins.py
#	src/octoprint/cli/server.py
#	src/octoprint/cli/user.py
#	src/octoprint/environment.py
#	src/octoprint/events.py
#	src/octoprint/filemanager/__init__.py
#	src/octoprint/filemanager/analysis.py
#	src/octoprint/filemanager/destinations.py
#	src/octoprint/filemanager/storage.py
#	src/octoprint/filemanager/util.py
#	src/octoprint/logging/__init__.py
#	src/octoprint/logging/handlers.py
#	src/octoprint/plugin/__init__.py
#	src/octoprint/plugin/core.py
#	src/octoprint/plugins/action_command_notification/__init__.py
#	src/octoprint/plugins/action_command_prompt/__init__.py
#	src/octoprint/plugins/appkeys/__init__.py
#	src/octoprint/plugins/backup/__init__.py
#	src/octoprint/plugins/corewizard/__init__.py
#	src/octoprint/plugins/corewizard/subwizards.py
#	src/octoprint/plugins/discovery/__init__.py
#	src/octoprint/plugins/errortracking/__init__.py
#	src/octoprint/plugins/gcodeviewer/__init__.py
#	src/octoprint/plugins/logging/__init__.py
#	src/octoprint/plugins/pi_support/__init__.py
#	src/octoprint/plugins/softwareupdate/cli.py
#	src/octoprint/plugins/softwareupdate/updaters/single_file_plugin.py
#	src/octoprint/plugins/softwareupdate/updaters/sleep_a_bit.py
#	src/octoprint/plugins/softwareupdate/version_checks/always_current.py
#	src/octoprint/plugins/softwareupdate/version_checks/bitbucket_commit.py
#	src/octoprint/plugins/softwareupdate/version_checks/github_release.py
#	src/octoprint/plugins/softwareupdate/version_checks/httpheader.py
#	src/octoprint/plugins/softwareupdate/version_checks/jsondata.py
#	src/octoprint/plugins/softwareupdate/version_checks/never_current.py
#	src/octoprint/plugins/softwareupdate/version_checks/pypi_release.py
#	src/octoprint/plugins/tracking/__init__.py
#	src/octoprint/plugins/virtual_printer/virtual.py
#	src/octoprint/printer/__init__.py
#	src/octoprint/printer/estimation.py
#	src/octoprint/printer/profile.py
#	src/octoprint/printer/standard.py
#	src/octoprint/server/__init__.py
#	src/octoprint/server/api/__init__.py
#	src/octoprint/server/api/system.py
#	src/octoprint/server/api/users.py
#	src/octoprint/server/util/flask.py
#	src/octoprint/server/util/tornado.py
#	src/octoprint/server/util/webassets.py
#	src/octoprint/settings.py
#	src/octoprint/slicing/__init__.py
#	src/octoprint/timelapse.py
#	src/octoprint/util/__init__.py
#	src/octoprint/util/comm.py
#	src/octoprint/util/commandline.py
#	src/octoprint/util/connectivity.py
#	src/octoprint/util/fixes.py
#	src/octoprint/util/gcodeInterpreter.py
#	src/octoprint/util/jinja.py
#	src/octoprint/util/json/__init__.py
#	src/octoprint/util/net.py
#	src/octoprint_client/__init__.py
#	src/octoprint_setuptools/__init__.py
#	tests/filemanager/test_filemanager.py
#	tests/filemanager/test_localstorage.py
#	tests/plugin/test_core.py
#	tests/plugin/test_settings.py
#	tests/plugin/test_types_blueprint.py
#	tests/plugin/test_types_settings.py
#	tests/server/api/test_system.py
#	tests/server/util/test_flask.py
#	tests/server/util/test_tornado.py
#	tests/slicing/test_slicingmanager.py
#	tests/test_daemon.py
#	tests/timelapse/test_timelapse_helpers.py
#	tests/util/test_comm.py
#	tests/util/test_file_helpers.py
#	tests/util/test_pip.py
#	tests/util/test_repeated_timer.py
#	tests/util/test_resettable_timer.py
#	versioneer.py
2020-10-07 21:08:44 +02:00

292 lines
9.1 KiB
Python

# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
import unittest
import unittest.mock as mock
import ddt
import octoprint.util.comm
@ddt.ddt
class TestCommErrorHandling(unittest.TestCase):
def setUp(self):
self._comm = mock.create_autospec(octoprint.util.comm.MachineCom)
# mocks
self._comm._handle_errors = (
lambda *args, **kwargs: octoprint.util.comm.MachineCom._handle_errors(
self._comm, *args, **kwargs
)
)
self._comm._trigger_error = (
lambda *args, **kwargs: octoprint.util.comm.MachineCom._trigger_error(
self._comm, *args, **kwargs
)
)
self._comm._recoverable_communication_errors = (
octoprint.util.comm.MachineCom._recoverable_communication_errors
)
self._comm._resend_request_communication_errors = (
octoprint.util.comm.MachineCom._resend_request_communication_errors
)
self._comm._sd_card_errors = octoprint.util.comm.MachineCom._sd_card_errors
self._comm._lastCommError = None
self._comm._errorValue = None
self._comm._clear_to_send = mock.Mock()
self._comm._error_message_hooks = {}
self._comm._trigger_emergency_stop = mock.Mock()
# settings
self._comm._ignore_errors = False
self._comm._disconnect_on_errors = True
self._comm._send_m112_on_error = True
self._comm.isPrinting.return_value = True
self._comm.isSdPrinting.return_value = False
self._comm.isError.return_value = False
@ddt.data(
# Marlin
"Error: Line Number is not Last Line Number+1, Last Line: 1",
# Repetier
"Error: Expected Line 1 got 2",
# !! error type for good measure
"!! expected line 1 got 2",
)
def test_lineno_mismatch(self, line):
result = self._comm._handle_errors(line)
self.assertEqual(line, result)
self.assert_resend()
@ddt.data(
# Marlin
"Error: No Line Number with checksum, Last Line: 1",
)
def test_lineno_missing(self, line):
"""Should simulate OK to force resend request"""
result = self._comm._handle_errors(line)
self.assertEqual(line, result)
self.assert_recoverable()
@ddt.data(
# Marlin
"Error: checksum mismatch",
# Repetier
"Error: Wrong checksum",
)
def test_checksum_mismatch(self, line):
"""Should prepare receiving resend request"""
result = self._comm._handle_errors(line)
self.assertEqual(line, result)
self.assert_resend()
@ddt.data(
# Marlin
"Error: No Checksum with line number, Last Line: 1",
# Repetier
"Error: Missing checksum",
)
def test_checksum_missing(self, line):
"""Should prepare receiving resend request"""
result = self._comm._handle_errors(line)
self.assertEqual(line, result)
self.assert_resend()
@ddt.data(
# Marlin
"Error: volume.init failed",
"Error: openRoot failed",
"Error: workDir open failed",
"Error: Cannot enter subdir: folder",
# Repetier
"Error: file.open failed",
# Marlin & Repetier (halleluja!)
"Error: error writing to file",
"Error: open failed, File: foo.gco",
# Legacy?
"Error: Cannot open foo.gco",
)
def test_sd_error(self, line):
"""Should pass"""
result = self._comm._handle_errors(line)
self.assertEqual(line, result)
self.assert_nop()
@ddt.data(
# Marlin
'Error: Unknown command: "ABC"',
# Repetier
"Error: Unknown command:ABC",
)
def test_unknown_command(self, line):
"""Should pass"""
result = self._comm._handle_errors(line)
self.assertEqual(line, result)
self.assert_nop()
@ddt.data("Error: This should get handled", "!! This should also get handled")
def test_unknown_handled(self, line):
"""Should pass"""
def handler(comm, message, *args, **kwargs):
return "handled" in message
self._comm._error_message_hooks["test"] = handler
result = self._comm._handle_errors(line)
self.assertEqual(line, result)
self.assert_nop()
@ddt.data("Error: Printer on fire")
def test_other_error_disconnect(self, line):
"""Should trigger escalation"""
result = self._comm._handle_errors(line)
self.assertEqual(line, result)
# what should have happened
self.assert_m112_sent()
self.assert_disconnected()
# what should not have happened
self.assert_not_handle_ok()
self.assert_not_last_comm_error()
self.assert_not_print_cancelled()
self.assert_not_cleared_to_send()
@ddt.data("Error: Printer on fire")
def test_other_error_no_m112(self, line):
"""Should trigger escalation"""
self._comm._send_m112_on_error = False
result = self._comm._handle_errors(line)
self.assertEqual(line, result)
# what should have happened
self.assert_disconnected()
# what should not have happened
self.assert_not_handle_ok()
self.assert_not_last_comm_error()
self.assert_not_print_cancelled()
self.assert_not_cleared_to_send()
self.assert_not_m112_sent()
@ddt.data("Error: Printer on fire")
def test_other_error_cancel(self, line):
"""Should trigger print cancel"""
self._comm._disconnect_on_errors = False
result = self._comm._handle_errors(line)
self.assertEqual(line, result)
# what should have happened
self.assert_print_cancelled()
self.assert_cleared_to_send()
# what should not have happened
self.assert_not_handle_ok()
self.assert_not_last_comm_error()
self.assert_not_m112_sent()
self.assert_not_disconnected()
@ddt.data("Error: Printer on fire")
def test_other_error_ignored(self, line):
"""Should only log"""
self._comm._ignore_errors = True
result = self._comm._handle_errors(line)
self.assertEqual(line, result)
# what should have happened
self.assert_cleared_to_send()
# what should not have happened
self.assert_not_handle_ok()
self.assert_not_last_comm_error()
self.assert_not_print_cancelled()
self.assert_not_m112_sent()
self.assert_not_disconnected()
def test_not_an_error(self):
"""Should pass"""
result = self._comm._handle_errors("Not an error")
self.assertEqual("Not an error", result)
self.assert_nop()
def test_already_error(self):
"""Should pass"""
self._comm.isError.return_value = True
result = self._comm._handle_errors("Error: Printer on fire")
self.assertEqual("Error: Printer on fire", result)
self.assert_nop()
def test_line_none(self):
"""Should pass"""
self.assertIsNone(self._comm._handle_errors(None))
##~~ assertion helpers
def assert_handle_ok(self):
self._comm._handle_ok.assert_called_once()
def assert_not_handle_ok(self):
self._comm._handle_ok.assert_not_called()
def assert_last_comm_error(self):
self.assertIsNotNone(self._comm._lastCommError)
def assert_not_last_comm_error(self):
self.assertIsNone(self._comm._lastCommError)
def assert_m112_sent(self):
self._comm._trigger_emergency_stop.assert_called_once_with(close=False)
def assert_not_m112_sent(self):
self._comm._trigger_emergency_stop.assert_not_called()
def assert_disconnected(self):
self.assertIsNotNone(self._comm._errorValue)
self._comm._changeState.assert_called_with(self._comm.STATE_ERROR)
self._comm.close.assert_called_once_with(is_error=True)
def assert_not_disconnected(self):
self.assertIsNone(self._comm._errorValue)
self._comm._changeState.assert_not_called()
self._comm.close.assert_not_called()
def assert_print_cancelled(self):
self._comm.cancelPrint.assert_called_once()
def assert_not_print_cancelled(self):
self._comm.cancelPrint.assert_not_called()
def assert_cleared_to_send(self):
self._comm._clear_to_send.set.assert_called_once()
def assert_not_cleared_to_send(self):
self._comm._clear_to_send.set.assert_not_called()
def assert_nop(self):
self.assert_not_handle_ok()
self.assert_not_last_comm_error()
self.assert_not_disconnected()
self.assert_not_print_cancelled()
self.assert_not_cleared_to_send()
def assert_recoverable(self):
self.assert_handle_ok()
self.assert_not_last_comm_error()
self.assert_not_disconnected()
self.assert_not_print_cancelled()
self.assert_not_cleared_to_send()
def assert_resend(self):
self.assert_last_comm_error()
self.assert_not_handle_ok()
self.assert_not_disconnected()
self.assert_not_print_cancelled()
self.assert_not_cleared_to_send()