From 371a33bba076797ce7645b1ac287a94c9ac4105f Mon Sep 17 00:00:00 2001 From: Paul Larson Date: Fri, 12 Jul 2024 13:00:14 -0500 Subject: [PATCH] Fix some issues from the merge --- agent/testflinger_agent/agent.py | 8 ++++---- agent/testflinger_agent/job.py | 4 +++- agent/testflinger_agent/tests/test_job.py | 16 +++++++++++----- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/agent/testflinger_agent/agent.py b/agent/testflinger_agent/agent.py index 3a1e8e74e..f273b239c 100644 --- a/agent/testflinger_agent/agent.py +++ b/agent/testflinger_agent/agent.py @@ -264,16 +264,16 @@ def process_jobs(self): self.set_agent_state(phase) event_emitter.emit_event(TestEvent(phase + "_start")) - exitcode, exit_event, exit_reason = job.run_test_phase( + exit_code, exit_event, exit_reason = job.run_test_phase( phase, rundir ) - self.client.post_influx(phase, exitcode) + self.client.post_influx(phase, exit_code) event_emitter.emit_event(exit_event, exit_reason) - if exitcode: + if exit_code: # exit code 46 is our indication that recovery failed! # In this case, we need to mark the device offline - if exitcode == 46: + if exit_code == 46: self.mark_device_offline() exit_event = TestEvent.RECOVERY_FAIL else: diff --git a/agent/testflinger_agent/job.py b/agent/testflinger_agent/job.py index c3a932aa4..6b7d98550 100644 --- a/agent/testflinger_agent/job.py +++ b/agent/testflinger_agent/job.py @@ -117,8 +117,10 @@ def run_test_phase(self, phase, rundir): ): runner.run(f"echo '{line}'") try: + # Set exit_event to fail for this phase in case of an exception + exit_event = f"{phase}_fail" exitcode, exit_event, exit_reason = runner.run(cmd) - except Exception as e: + except Exception as exc: logger.exception(exc) exitcode = 100 exit_reason = str(exc) # noqa: F841 - ignore this until it's used diff --git a/agent/testflinger_agent/tests/test_job.py b/agent/testflinger_agent/tests/test_job.py index 480bc046a..83fb8e67a 100644 --- a/agent/testflinger_agent/tests/test_job.py +++ b/agent/testflinger_agent/tests/test_job.py @@ -4,7 +4,7 @@ import tempfile import requests_mock as rmock -from unittest.mock import patch, Mock +from unittest.mock import patch import testflinger_agent from testflinger_agent.client import TestflingerClient as _TestflingerClient @@ -161,11 +161,17 @@ def test_run_test_phase_with_run_exception( requests_mock.post(rmock.ANY, status_code=200) job = _TestflingerJob({}, client) job.phase = "setup" - mock_runner = Mock() - mock_runner.run.side_effect = Exception("failed") - with patch("testflinger_agent.job.CommandRunner", mock_runner): - exit_code = job.run_test_phase("setup", tmp_path) + # Don't raise the exception on the 3 banner lines + with patch( + "testflinger_agent.job.CommandRunner.run", + side_effect=[None, None, None, Exception("failed")], + ): + exit_code, exit_event, exit_reason = job.run_test_phase( + "setup", tmp_path + ) assert exit_code == 100 + assert exit_event == "setup_fail" + assert exit_reason == "failed" def test_set_truncate(self, client): """Test the _set_truncate method of TestflingerJob"""