Skip to content

Commit

Permalink
Change status update endpoint to include job_id
Browse files Browse the repository at this point in the history
  • Loading branch information
val500 committed Jul 11, 2024
1 parent 15fd41c commit b074c27
Show file tree
Hide file tree
Showing 9 changed files with 639 additions and 635 deletions.
5 changes: 5 additions & 0 deletions agent/testflinger_agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,15 @@ def process_jobs(self):
job_data.get("job_queue"),
job_data.get("job_status_webhook"),
self.client,
job.job_id,
)
job_end_reason = TestEvent.NORMAL_EXIT

logger.info("Starting job %s", job.job_id)
event_emitter.emit_event(
TestEvent.JOB_START,
f"{self.client.server}/job/{job.job_id}/events",
)
rundir = os.path.join(
self.client.config.get("execution_basedir"), job.job_id
)
Expand Down
10 changes: 8 additions & 2 deletions agent/testflinger_agent/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,11 @@ def post_provision_log(self, job_id: str, exit_code: int, detail: str):
logger.warning("Unable to post provision log to server: %s", exc)

def post_status_update(
self, job_queue: str, webhook: str, events: List[Dict[str, str]]
self,
job_queue: str,
webhook: str,
events: List[Dict[str, str]],
job_id: str,
):
"""
Posts status updates about the running job as long as there is a
Expand All @@ -402,6 +406,8 @@ def post_status_update(
String URL to post status update to
:param events:
List of accumulated test events
:param job_id:
id for the job on which we want to post results
"""
if webhook is None:
Expand All @@ -413,7 +419,7 @@ def post_status_update(
"job_status_webhook": webhook,
"events": events,
}
status_update_uri = urljoin(self.server, "/v1/agents/status")
status_update_uri = urljoin(self.server, f"/v1/job/{job_id}/events")
try:
job_request = self.session.post(
status_update_uri, json=status_update_request, timeout=30
Expand Down
11 changes: 9 additions & 2 deletions agent/testflinger_agent/event_emitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@

class EventEmitter:
def __init__(
self, job_queue: str, webhook: str, client: TestflingerClient
self,
job_queue: str,
webhook: str,
client: TestflingerClient,
job_id: str,
):
"""
:param job_queue:
Expand All @@ -30,12 +34,15 @@ def __init__(
String url to send status updates to
:param client:
TestflingerClient used to post status updates to the server
:param job_id:
id for the job on which we want to post updates
"""
self.job_queue = job_queue
self.webhook = webhook
self.events = []
self.client = client
self.job_id = job_id

def emit_event(self, test_event: TestEvent, detail: str = ""):
if test_event is not None:
Expand All @@ -46,5 +53,5 @@ def emit_event(self, test_event: TestEvent, detail: str = ""):
}
self.events.append(new_event_json)
self.client.post_status_update(
self.job_queue, self.webhook, self.events
self.job_queue, self.webhook, self.events, self.job_id
)
12 changes: 7 additions & 5 deletions agent/testflinger_agent/tests/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,9 @@ def test_post_agent_data(self, agent):

def test_post_agent_status_update(self, agent, requests_mock):
self.config["test_command"] = "echo test1"
job_id = str(uuid.uuid1())
fake_job_data = {
"job_id": str(uuid.uuid1()),
"job_id": job_id,
"job_queue": "test",
"test_data": {"test_cmds": "foo"},
"job_status_webhook": "https://mywebhook",
Expand All @@ -413,7 +414,7 @@ def test_post_agent_status_update(self, agent, requests_mock):
"http://127.0.0.1:8000/v1/job?queue=test",
[{"text": json.dumps(fake_job_data)}, {"text": "{}"}],
)
status_url = "http://127.0.0.1:8000/v1/agents/status"
status_url = f"http://127.0.0.1:8000/v1/job/{job_id}/events"
requests_mock.post(status_url, status_code=200)
with patch("shutil.rmtree"):
agent.process_jobs()
Expand All @@ -431,6 +432,7 @@ def test_post_agent_status_update(self, agent, requests_mock):
for phase in TestPhase
for postfix in ["_start", "_success"]
]
expected_event_name_list.insert(0, "job_start")
expected_event_name_list.append("job_end")

assert event_list[-1]["detail"] == "normal_exit"
Expand All @@ -449,7 +451,7 @@ def test_post_agent_status_update_cancelled(self, agent, requests_mock):
"http://127.0.0.1:8000/v1/job?queue=test",
[{"text": json.dumps(fake_job_data)}, {"text": "{}"}],
)
status_url = "http://127.0.0.1:8000/v1/agents/status"
status_url = f"http://127.0.0.1:8000/v1/job/{job_id}/events"
requests_mock.post(status_url, status_code=200)

requests_mock.get(
Expand Down Expand Up @@ -486,7 +488,7 @@ def test_post_agent_status_update_global_timeout(
"http://127.0.0.1:8000/v1/job?queue=test",
[{"text": json.dumps(fake_job_data)}, {"text": "{}"}],
)
status_url = "http://127.0.0.1:8000/v1/agents/status"
status_url = f"http://127.0.0.1:8000/v1/job/{job_id}/events"
requests_mock.post(status_url, status_code=200)

with patch("shutil.rmtree"):
Expand Down Expand Up @@ -519,7 +521,7 @@ def test_post_agent_status_update_output_timeout(
"http://127.0.0.1:8000/v1/job?queue=test",
[{"text": json.dumps(fake_job_data)}, {"text": "{}"}],
)
status_url = "http://127.0.0.1:8000/v1/agents/status"
status_url = f"http://127.0.0.1:8000/v1/job/{job_id}/events"
requests_mock.post(status_url, status_code=200)

with patch("shutil.rmtree"):
Expand Down
5 changes: 3 additions & 2 deletions agent/testflinger_agent/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ def test_post_status_update(self, client, requests_mock):
if there is a valid webhook
"""
webhook = "http://foo"
job_id = job_id = str(uuid.uuid1())
requests_mock.post(
"http://127.0.0.1:8000/v1/agents/status", status_code=200
f"http://127.0.0.1:8000/v1/job/{job_id}/events", status_code=200
)
events = [
{
Expand All @@ -148,7 +149,7 @@ def test_post_status_update(self, client, requests_mock):
"detail": "",
},
]
client.post_status_update("myjobqueue", webhook, events)
client.post_status_update("myjobqueue", webhook, events, job_id)
expected_json = {
"agent_id": client.config.get("agent_id"),
"job_queue": "myjobqueue",
Expand Down
3 changes: 2 additions & 1 deletion common/testflinger_common/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ class TestEvent(StrEnum):
CANCELLED = "cancelled"
GLOBAL_TIMEOUT = "global_timeout"
OUTPUT_TIMEOUT = "output_timeout"
RECOVERY_FAILED = "recovery_failed"
RECOVERY_FAIL = "recovery_fail"

NORMAL_EXIT = "normal_exit"
JOB_START = "job_start"
JOB_END = "job_end"
5 changes: 3 additions & 2 deletions server/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,12 @@ This will find jobs tagged with both "foo" and "bar".
--data '{ "job_id": "00000000-0000-0000-0000-000000000000", \
"exit_code": 1, "detail":"foo" }'
**[POST] /v1/status - Receive job status updates from an agent and posts them to the specified webhook.
**[POST] /v1/job/<job_id>/events - Receive job status updates from an agent and posts them to the specified webhook.
The job_status_webhook parameter is required for this endpoint. Other parameters included here will be forwarded to the webhook.

- Parameters:
- job_id: test job identifier as a UUID
- job_status_webhook: webhook URL to post status updates to

- Returns:
Expand All @@ -380,4 +381,4 @@ The job_status_webhook parameter is required for this endpoint. Other parameters
$ curl -X POST \
-H "Content-Type: application/json" \
-d '{"agent_id": "agent-00", "job_queue": "myqueue", "job_status_webhook": "http://mywebhook", "events": [{"event_name": "started_provisioning", "timestamp": "2024-05-03T19:11:33.541130+00:00", "detail": "my_detailed_message"}]}' http://localhost:8000/v1/agents/status
-d '{"agent_id": "agent-00", "job_queue": "myqueue", "job_status_webhook": "http://mywebhook", "events": [{"event_name": "started_provisioning", "timestamp": "2024-05-03T19:11:33.541130+00:00", "detail": "my_detailed_message"}]}' http://localhost:8000/v1/job/00000000-0000-0000-0000-000000000000/events
Loading

0 comments on commit b074c27

Please sign in to comment.