1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-09 11:46:49 +00:00

feat(operations): a job's error and warning show in the reader's language

A job stored its error and its result's warning as English text, much
of it built with the values in it (volume paths, a disk, libvirt's
error), so no catalog could translate it, and the job pages and panel
showed English whatever the language.

A job now stores them as messages: our English text, marked for
translation, and its values; libvirt's own text is a message without
values and is shown as it is. The jobs API and pages put them in the
reader's language. OperationError keeps its text and values apart for
the same reason, so a refusal raised while a job is submitted is shown
translated too. Messages one after another still read as sentences.

Errors stored before are kept as one message each, shown as they were.
This commit is contained in:
catborise 2026-11-02 23:49:45 +03:00
parent 0eab09e19d
commit 1e66367bb4
29 changed files with 927 additions and 333 deletions

View file

@ -12,6 +12,7 @@ from instances.models import Instance
from operations import claims, jobs
from operations.models import Claim, Operation
from operations.tests import fakes
from operations.utils import say
class OperationsApiTestCase(TransactionTestCase):
@ -35,6 +36,17 @@ class OperationsApiTestCase(TransactionTestCase):
Operation.objects.filter(pk=self.op.pk).update(time_limit=60, started_at=timezone.now() - timedelta(seconds=61))
self.assertEqual(self.client.get(url).json()["phase"], "Merging the snapshot (over its time limit)")
def test_the_error_and_warning_come_in_the_users_language_with_their_values(self):
# ours with its values; libvirt's text as it is
Operation.objects.filter(pk=self.op.pk).update(
state=Operation.FAILED,
error=say("The pool has no volume named %(name)s", name="disk.qcow2") + say("unable to open: busy"),
result={"warning": say("The copy was not made")})
self.client.force_login(self.owner)
job = self.client.get(f"/api/v1/operations/{self.op.pk}/", HTTP_ACCEPT_LANGUAGE="tr").json()
self.assertEqual(job["error"], "Havuzda disk.qcow2 adında birim yok. unable to open: busy")
self.assertEqual(job["result"]["warning"], "Kopya oluşturulmadı")
def test_users_list_only_their_own_jobs(self):
self.client.force_login(self.other)
self.assertEqual(self.client.get("/api/v1/operations/").json()["count"], 0)

View file

@ -4,6 +4,7 @@ from unittest.mock import patch
from django.contrib.auth import get_user_model
from django.test import TransactionTestCase
from operations.utils import say, words
from operations import jobs
from operations.jobs import Outcome
from operations.models import Claim, Operation, WorkerLease, acquire
@ -55,20 +56,20 @@ class RecoveryTestCase(TransactionTestCase):
def test_a_proven_success_does_not_carry_the_raised_error(self):
op = self.orphan({}, worker=self.worker.identity)
self.scheduler.inspect_now(op.pk, error="failed after effects")
self.scheduler.inspect_now(op.pk, error=say("failed after effects"))
with patch.object(fakes.ScriptedKind, "reconcile", return_value=Outcome(Operation.SUCCEEDED)):
self.run_due()
op = self.reload(op)
self.assertEqual((op.state, op.error), (Operation.SUCCEEDED, ""))
self.assertEqual((op.state, words(op.error)), (Operation.SUCCEEDED, ""))
def test_an_uncertain_outcome_keeps_the_error_the_job_raised(self):
op = self.orphan({}, worker=self.worker.identity)
self.scheduler.inspect_now(op.pk, error="resource busy: X")
self.scheduler.inspect_now(op.pk, error=say("resource busy: X"))
with patch.object(fakes.ScriptedKind, "reconcile",
return_value=Outcome(Operation.UNCERTAIN, error="check the VM")):
return_value=Outcome(Operation.UNCERTAIN, error=say("check the VM"))):
self.run_due()
op = self.reload(op)
self.assertEqual((op.state, op.error), (Operation.UNCERTAIN, "resource busy: X. check the VM"))
self.assertEqual((op.state, words(op.error)), (Operation.UNCERTAIN, "resource busy: X. check the VM"))
def test_uncertain_keeps_the_claims(self):
op = self.orphan({"outcome": "uncertain"})
@ -116,31 +117,31 @@ class RecoveryTestCase(TransactionTestCase):
self.run_at(1000, Outcome(Operation.RUNNING, stalled=True))
self.run_at(1000 + STALL_LIMIT, Outcome(Operation.RUNNING, stalled=True))
op = self.reload(op)
self.assertEqual((op.state, op.error), (Operation.UNCERTAIN, STALLED))
self.assertEqual((op.state, words(op.error)), (Operation.UNCERTAIN, STALLED))
self.assertTrue(Claim.objects.filter(operation=op).exists())
self.assertNotIn(op.pk, self.scheduler.stalled_since)
def test_a_stalled_merge_keeps_the_error_the_job_raised(self):
op = self.orphan({}, worker=self.worker.identity)
self.scheduler.inspect_now(op.pk, error="resource busy: X")
self.scheduler.inspect_now(op.pk, error=say("resource busy: X"))
self.run_at(1000, Outcome(Operation.RUNNING, stalled=True))
self.scheduler.next_try[op.pk] = 0
self.run_at(1000 + STALL_LIMIT, Outcome(Operation.RUNNING, stalled=True))
self.assertEqual(self.reload(op).error, f"resource busy: X. {STALLED}")
self.assertEqual(words(self.reload(op).error), f"resource busy: X. {STALLED}")
def test_a_raised_error_ending_with_a_dot_is_not_doubled(self):
op = self.orphan({}, worker=self.worker.identity)
self.scheduler.inspect_now(op.pk, error="refused.")
with patch.object(fakes.ScriptedKind, "reconcile", return_value=Outcome(Operation.UNCERTAIN, error="check")):
self.scheduler.inspect_now(op.pk, error=say("refused."))
with patch.object(fakes.ScriptedKind, "reconcile", return_value=Outcome(Operation.UNCERTAIN, error=say("check"))):
self.run_due()
self.assertEqual(self.reload(op).error, "refused. check")
self.assertEqual(words(self.reload(op).error), "refused. check")
def test_an_outcome_without_an_error_stores_the_raised_one(self):
op = self.orphan({}, worker=self.worker.identity)
self.scheduler.inspect_now(op.pk, error="resource busy: X")
self.scheduler.inspect_now(op.pk, error=say("resource busy: X"))
with patch.object(fakes.ScriptedKind, "reconcile", return_value=Outcome(Operation.FAILED)):
self.run_due()
self.assertEqual(self.reload(op).error, "resource busy: X")
self.assertEqual(words(self.reload(op).error), "resource busy: X")
def test_progress_resets_the_stall_timer(self):
op = self.orphan({})
@ -171,8 +172,8 @@ class RecoveryTestCase(TransactionTestCase):
reconcile.assert_not_called()
op = self.reload(op)
# the stored error is shown to the job's owner: the host goes to the worker log only
self.assertEqual((op.state, op.error), (Operation.UNCERTAIN, ABANDONED))
self.assertNotIn("other-host", op.error)
self.assertEqual((op.state, words(op.error)), (Operation.UNCERTAIN, ABANDONED))
self.assertNotIn("other-host", words(op.error))
self.assertIn("other-host", "\n".join(logs.output))
self.assertTrue(Claim.objects.filter(operation=op).exists())
@ -196,7 +197,7 @@ class RecoveryTestCase(TransactionTestCase):
def test_an_own_job_that_raised_before_its_effects_fails_and_releases(self):
op = self.orphan({"outcome": "uncertain"}, worker=self.worker.identity, effects=False)
self.scheduler.inspect_now(op.pk, error="refused before effects")
self.scheduler.inspect_now(op.pk, error=say("refused before effects"))
self.run_due()
op = self.reload(op)
self.assertEqual(op.state, Operation.FAILED)

View file

@ -9,6 +9,7 @@ from django.utils import timezone
from vrtManager.util import OperationError
from logs.models import Logs
from operations.utils import say, words
from operations import claims, jobs
from operations.jobs import Outcome
from operations.models import Claim, LeaseLost, Operation, WorkerLease, acquire
@ -153,7 +154,7 @@ class CancelFinishResolveTestCase(TransactionTestCase):
def test_a_failure_keeps_the_phase_stored_after_the_job_was_loaded(self):
op = jobs.submit("test.noop", self.user, {}, KEY)
Operation.objects.filter(pk=op.pk).update(phase="merging") # op in memory still has ""
jobs.finish(op, Outcome(Operation.FAILED, error="x"))
jobs.finish(op, Outcome(Operation.FAILED, error=say("x")))
op.refresh_from_db()
self.assertEqual(op.phase, "merging")
@ -161,14 +162,14 @@ class CancelFinishResolveTestCase(TransactionTestCase):
op = jobs.submit("test.noop", self.user, {}, KEY)
Operation.objects.filter(pk=op.pk).update(phase="merging", progress_done=5, progress_total=10)
op.refresh_from_db()
jobs.finish(op, Outcome(Operation.FAILED, error="x"))
jobs.finish(op, Outcome(Operation.FAILED, error=say("x")))
op.refresh_from_db()
self.assertEqual((op.phase, op.progress_done), ("merging", 5))
def test_finish_happens_once(self):
op = jobs.submit("test.noop", self.user, {}, KEY)
self.assertTrue(jobs.finish(op, Outcome(Operation.SUCCEEDED, result={"x": 1})))
self.assertFalse(jobs.finish(op, Outcome(Operation.FAILED, error="late")))
self.assertFalse(jobs.finish(op, Outcome(Operation.FAILED, error=say("late"))))
op.refresh_from_db()
self.assertEqual((op.state, op.result), (Operation.SUCCEEDED, {"x": 1}))
self.assertEqual(Logs.objects.count(), 1)
@ -187,14 +188,14 @@ class CancelFinishResolveTestCase(TransactionTestCase):
op = jobs.submit("test.noop", self.user, {}, KEY)
Operation.objects.filter(pk=op.pk).update(username="u" * 150)
op.refresh_from_db()
jobs.finish(op, Outcome(Operation.FAILED, error="qemu+tcp://admin:s3cret@192.0.2.5/system failed"))
jobs.finish(op, Outcome(Operation.FAILED, error=say("qemu+tcp://admin:s3cret@192.0.2.5/system failed")))
op.refresh_from_db()
self.assertNotIn("s3cret", op.error)
self.assertNotIn("s3cret", words(op.error))
self.assertLessEqual(len(Logs.objects.get().user), 150)
def test_an_uncertain_job_keeps_its_claims_until_resolved(self):
op = jobs.submit("test.noop", self.user, {}, KEY)
jobs.finish(op, Outcome(Operation.UNCERTAIN, error="check the disks"))
jobs.finish(op, Outcome(Operation.UNCERTAIN, error=say("check the disks")))
self.assertTrue(Claim.objects.filter(operation=op).exists())
with self.assertRaises(PermissionDenied):
jobs.resolve(op.pk, self.user, Operation.FAILED, "checked")

View file

@ -14,6 +14,7 @@ from operations.jobs import Outcome
from operations.models import Claim, Operation, WorkerLease
from operations.templatetags.operations import STATE_COLORS, STATE_LABELS, state_color, state_label
from operations.tests import fakes
from operations.utils import say
class BusyMessageTestCase(SimpleTestCase):
@ -74,7 +75,7 @@ class JobsPageTestCase(TransactionTestCase):
self.assertNotContains(self.client.get(reverse("operations:jobs"), {"username": "other"}), link)
def test_resolve_needs_a_superuser_an_outcome_and_a_note(self):
jobs.finish(self.op, Outcome(Operation.UNCERTAIN, error="check"))
jobs.finish(self.op, Outcome(Operation.UNCERTAIN, error=say("check")))
self.client.force_login(self.user)
self.assertEqual(self.client.post(reverse("operations:resolve", args=[self.op.pk]),
{"outcome": "failed", "note": "x"}).status_code, 403)
@ -85,7 +86,7 @@ class JobsPageTestCase(TransactionTestCase):
self.assertFalse(Claim.objects.filter(operation=self.op).exists())
def test_the_detail_shows_what_recovery_saw_escaped(self):
Operation.objects.filter(pk=self.op.pk).update(error="<script>x</script>", phase="Waiting for recovery",
Operation.objects.filter(pk=self.op.pk).update(error=say("<script>x</script>"), phase="Waiting for recovery",
state=Operation.UNCERTAIN)
self.client.force_login(self.user)
response = self.client.get(reverse("operations:job", args=[self.op.pk]))
@ -93,7 +94,7 @@ class JobsPageTestCase(TransactionTestCase):
self.assertContains(response, "Waiting for recovery")
def test_the_detail_shows_the_results_warning_escaped(self):
Operation.objects.filter(pk=self.op.pk).update(result={"warning": "not <b>thawed</b>"},
Operation.objects.filter(pk=self.op.pk).update(result={"warning": say("not <b>thawed</b>")},
state=Operation.SUCCEEDED)
self.client.force_login(self.user)
response = self.client.get(reverse("operations:job", args=[self.op.pk]))
@ -169,7 +170,7 @@ class JobsPageTestCase(TransactionTestCase):
self.assertNotContains(self.client.get(url), 'id="follow-active-jobs"')
def test_list_and_detail_show_a_state_badge(self):
Operation.objects.filter(pk=self.op.pk).update(state=Operation.FAILED, error="disk busy")
Operation.objects.filter(pk=self.op.pk).update(state=Operation.FAILED, error=say("disk busy"))
self.client.force_login(self.admin)
for response in (self.client.get(reverse("operations:jobs")),
self.client.get(reverse("operations:job", args=[self.op.pk]))):
@ -178,12 +179,12 @@ class JobsPageTestCase(TransactionTestCase):
self.assertContains(detail, 'class="alert alert-danger"')
def test_the_list_shows_the_phase_while_active_and_the_error_after_a_failure(self):
Operation.objects.filter(pk=self.op.pk).update(state=Operation.RUNNING, phase="Copying", error="")
Operation.objects.filter(pk=self.op.pk).update(state=Operation.RUNNING, phase="Copying", error=say(""))
self.client.force_login(self.admin)
self.assertContains(self.client.get(reverse("operations:jobs")), "Copying")
Operation.objects.filter(pk=self.op.pk).update(state=Operation.SUCCEEDED)
self.assertNotContains(self.client.get(reverse("operations:jobs")), "Copying")
Operation.objects.filter(pk=self.op.pk).update(state=Operation.FAILED, error="x" * 200)
Operation.objects.filter(pk=self.op.pk).update(state=Operation.FAILED, error=say("x" * 200))
response = self.client.get(reverse("operations:jobs"))
self.assertNotContains(response, "Copying")
self.assertContains(response, "x" * 79 + "\u2026")

View file

@ -17,6 +17,7 @@ from django.utils import timezone
from appsettings.models import AppSettings
from appsettings.settings import app_settings
from operations.utils import words
from operations import jobs
from operations.models import Claim, Operation, WorkerLease
from operations.tests import fakes
@ -77,7 +78,7 @@ class WorkerTestCase(TransactionTestCase):
self.start()
self.assertTrue(wait_for(lambda: state(op) in Operation.TERMINAL))
op.refresh_from_db()
self.assertEqual((op.state, op.result), (Operation.SUCCEEDED, {"value": "42"}), op.error)
self.assertEqual((op.state, op.result), (Operation.SUCCEEDED, {"value": "42"}), words(op.error))
def test_an_error_before_effects_fails_the_job(self):
op = self.submit("test.scripted", {"fail_before": True}, "key-00000001")
@ -90,7 +91,7 @@ class WorkerTestCase(TransactionTestCase):
with patch.object(fakes.NoopKind, "precheck", side_effect=failure):
self.start()
self.assertTrue(wait_for(lambda: state(op) == Operation.FAILED))
error = Operation.objects.get(pk=op.pk).error
error = words(Operation.objects.get(pk=op.pk).error)
self.assertEqual(error, "The host of this virtual machine cannot be reached right now.")
self.assertNotIn("example.com", error)
@ -221,7 +222,7 @@ class WorkerTestCase(TransactionTestCase):
with patch("operations.jobs.Context", side_effect=RuntimeError("no context")):
self.start()
self.assertTrue(wait_for(lambda: state(op) == Operation.FAILED))
self.assertIn("no context", Operation.objects.get(pk=op.pk).error)
self.assertIn("no context", words(Operation.objects.get(pk=op.pk).error))
self.assertFalse(Claim.objects.filter(operation=op).exists())
def test_a_failing_manager_loop_ends_the_process(self):