Skip to content

Commit

Permalink
Fix some issues from the merge
Browse files Browse the repository at this point in the history
  • Loading branch information
plars authored and val500 committed Jul 12, 2024
1 parent b074c27 commit 8a469fc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
8 changes: 4 additions & 4 deletions agent/testflinger_agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion agent/testflinger_agent/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions agent/testflinger_agent/tests/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"""
Expand Down
26 changes: 25 additions & 1 deletion server/src/api/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"""

import uuid
from datetime import datetime
from datetime import datetime, timezone

import pkg_resources
from apiflask import APIBlueprint, abort
Expand Down Expand Up @@ -493,6 +493,30 @@ def agents_post(agent_name, json_data):
return "OK"


@v1.post("/agents/provision_logs/<agent_name>")
@v1.input(schemas.ProvisionLogsIn, location="json")
def agents_provision_logs_post(agent_name, json_data):
"""Post provision logs for the agent to the server"""
agent_record = {}

# timestamp this agent record and provision log entry
timestamp = datetime.now(timezone.utc)
agent_record["updated_at"] = json_data["timestamp"] = timestamp

update_operation = {
"$set": json_data,
"$push": {
"provision_log": {"$each": [json_data], "$slice": -100},
},
}
database.mongo.db.provision_logs.update_one(
{"name": agent_name},
update_operation,
upsert=True,
)
return "OK"


@v1.post("/job/<job_id>/events")
@v1.input(schemas.StatusUpdate, location="json")
def agents_status_post(job_id, json_data):
Expand Down

0 comments on commit 8a469fc

Please sign in to comment.