Skip to content

Commit

Permalink
Capture provision error logs in device connector to be processed by a…
Browse files Browse the repository at this point in the history
…gent
  • Loading branch information
val500 committed Aug 1, 2024
1 parent 4ce5fad commit 03f6ebf
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6 deletions.
13 changes: 12 additions & 1 deletion agent/testflinger_agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,12 @@ def process_jobs(self):

self.client.post_job_state(job.job_id, phase)
self.set_agent_state(phase)

provision_error_log = os.path.join(
rundir, "provision_error.log"
)
if phase == "provision":
# Clear provision error log before starting
open(provision_error_log, "w").close()
event_emitter.emit_event(TestEvent(phase + "_start"))
exit_code, exit_event, exit_reason = job.run_test_phase(
phase, rundir
Expand All @@ -279,10 +284,16 @@ def process_jobs(self):
else:
exit_event = TestEvent(phase + "_fail")
event_emitter.emit_event(exit_event)
detail = ""
if phase == "provision":
self.client.post_provision_log(
job.job_id, exit_code, exit_event
)
with open(
provision_error_log, "a"
) as provision_error:
detail = provision_error.read()
event_emitter.emit_event(exit_event, detail)
if phase != "test":
logger.debug(
"Phase %s failed, aborting job" % phase
Expand Down
3 changes: 1 addition & 2 deletions agent/testflinger_agent/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,5 @@ def post_status_update(
job_request = None
if not job_request:
logger.error(
"Unable to post status updates to: %s (error: %s)"
% (status_update_uri, job_request.status_code)
f"Unable to post status updates to: {status_update_uri}"
)
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,27 @@


class ProvisioningError(Exception):
pass
def __init__(self, message: str = None):
if message is not None:
with open(
"provision_error.log", "w", encoding="utf-8"
) as provision_error_file:
provision_error_file.write(message)
super().__init__(message)
else:
super().__init__()


class RecoveryError(Exception):
pass
def __init__(self, message: str = None):
if message is not None:
with open(
"provision_error.log", "w", encoding="utf-8"
) as provision_error_file:
provision_error_file.write(message)
super().__init__(message)
else:
super().__init__()


def SerialLogger(host=None, port=None, filename=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@


class MaasStorageError(ProvisioningError):
pass
def __init__(self, message: str = None):
if message is None:
super().__init__("MAAS Storage Error")
else:
super().__init__(f"MAAS Storage Error: {message}")


class MaasStorage:
Expand Down
24 changes: 24 additions & 0 deletions device-connectors/src/tests/test_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
from testflinger_device_connectors.devices import (
DEVICE_CONNECTORS,
get_device_stage_func,
ProvisioningError,
)
from testflinger_device_connectors.devices.maas2.maas_storage import (
MaasStorageError,
)

STAGES_CONNECTORS_PRODUCT = tuple(product(STAGES, DEVICE_CONNECTORS))
Expand All @@ -36,3 +40,23 @@ def test_get_device_stage_func(stage, device):
orig_func = getattr(connector_instance, stage)
func = get_device_stage_func(device, stage)
assert func.__func__ is orig_func.__func__


def test_provision_error_file_logging():
open("provision_error.log", "w").close()
error_message = "my error message"
try:
raise ProvisioningError(error_message)
except ProvisioningError:
with open("provision_error.log") as error_file:
assert error_file.read() == error_message


def test_maas_storage_error_file_logging():
open("provision_error.log", "w").close()
error_message = "MAAS Storage Error"
try:
raise MaasStorageError()
except MaasStorageError:
with open("provision_error.log") as error_file:
assert error_file.read() == error_message

0 comments on commit 03f6ebf

Please sign in to comment.