Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Correctly display job creation errors (#133)
Browse files Browse the repository at this point in the history
* load error response json

* fix tests

* face the consequences of copy_model_fields banishment
  • Loading branch information
kevingrismore authored Apr 10, 2024
1 parent 0a16944 commit 8cafd9f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 42 deletions.
5 changes: 3 additions & 2 deletions prefect_kubernetes/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
import asyncio
import base64
import enum
import json
import logging
import math
import os
Expand Down Expand Up @@ -795,8 +796,8 @@ def _create_job(
message = ""
if exc.reason:
message += ": " + exc.reason
if exc.body and "message" in exc.body:
message += ": " + exc.body["message"]
if exc.body and "message" in (body := json.loads(exc.body)):
message += ": " + body["message"]

raise InfrastructureError(
f"Unable to create Kubernetes job{message}"
Expand Down
89 changes: 49 additions & 40 deletions tests/test_worker.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import base64
import json
import re
import uuid
from contextlib import contextmanager
Expand Down Expand Up @@ -1008,7 +1009,7 @@ def flow_run(self):

@pytest.fixture
def deployment(self):
return DeploymentResponse(name="my-deployment-name")
return DeploymentResponse(name="my-deployment-name", flow_id=uuid.uuid4())

@pytest.fixture
def flow(self):
Expand Down Expand Up @@ -1522,16 +1523,18 @@ async def test_create_job_failure(
mock_batch_client,
):
response = MagicMock()
response.data = {
"kind": "Status",
"apiVersion": "v1",
"metadata": {},
"status": "Failure",
"message": 'jobs.batch is forbidden: User "system:serviceaccount:helm-test:prefect-worker-dev" cannot create resource "jobs" in API group "batch" in the namespace "prefect"',
"reason": "Forbidden",
"details": {"group": "batch", "kind": "jobs"},
"code": 403,
}
response.data = json.dumps(
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {},
"status": "Failure",
"message": 'jobs.batch is forbidden: User "system:serviceaccount:helm-test:prefect-worker-dev" cannot create resource "jobs" in API group "batch" in the namespace "prefect"',
"reason": "Forbidden",
"details": {"group": "batch", "kind": "jobs"},
"code": 403,
}
)
response.status = 403
response.reason = "Forbidden"

Expand Down Expand Up @@ -1563,16 +1566,18 @@ async def test_create_job_retries(
):
MAX_ATTEMPTS = 3
response = MagicMock()
response.data = {
"kind": "Status",
"apiVersion": "v1",
"metadata": {},
"status": "Failure",
"message": 'jobs.batch is forbidden: User "system:serviceaccount:helm-test:prefect-worker-dev" cannot create resource "jobs" in API group "batch" in the namespace "prefect"',
"reason": "Forbidden",
"details": {"group": "batch", "kind": "jobs"},
"code": 403,
}
response.data = json.dumps(
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {},
"status": "Failure",
"message": 'jobs.batch is forbidden: User "system:serviceaccount:helm-test:prefect-worker-dev" cannot create resource "jobs" in API group "batch" in the namespace "prefect"',
"reason": "Forbidden",
"details": {"group": "batch", "kind": "jobs"},
"code": 403,
}
)
response.status = 403
response.reason = "Forbidden"

Expand Down Expand Up @@ -1605,16 +1610,18 @@ async def test_create_job_failure_no_reason(
mock_batch_client,
):
response = MagicMock()
response.data = {
"kind": "Status",
"apiVersion": "v1",
"metadata": {},
"status": "Failure",
"message": 'jobs.batch is forbidden: User "system:serviceaccount:helm-test:prefect-worker-dev" cannot create resource "jobs" in API group "batch" in the namespace "prefect"',
"reason": "Forbidden",
"details": {"group": "batch", "kind": "jobs"},
"code": 403,
}
response.data = json.dumps(
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {},
"status": "Failure",
"message": 'jobs.batch is forbidden: User "system:serviceaccount:helm-test:prefect-worker-dev" cannot create resource "jobs" in API group "batch" in the namespace "prefect"',
"reason": "Forbidden",
"details": {"group": "batch", "kind": "jobs"},
"code": 403,
}
)
response.status = 403
response.reason = None

Expand Down Expand Up @@ -1645,15 +1652,17 @@ async def test_create_job_failure_no_message(
mock_batch_client,
):
response = MagicMock()
response.data = {
"kind": "Status",
"apiVersion": "v1",
"metadata": {},
"status": "Failure",
"reason": "Forbidden",
"details": {"group": "batch", "kind": "jobs"},
"code": 403,
}
response.data = json.dumps(
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {},
"status": "Failure",
"reason": "Forbidden",
"details": {"group": "batch", "kind": "jobs"},
"code": 403,
}
)
response.status = 403
response.reason = "Test"

Expand Down

0 comments on commit 8cafd9f

Please sign in to comment.