authentik.tasks.tests.test_aggregated_status

Test aggregated status of tasks

 1"""Test aggregated status of tasks"""
 2
 3from django.test import TestCase
 4from django_dramatiq_postgres.models import TaskState
 5from dramatiq import actor, get_broker
 6from dramatiq.actor import Actor
 7
 8from authentik.tasks.middleware import CurrentTask
 9from authentik.tasks.models import Task, TaskStatus
10
11
12class TestAggregatedStatus(TestCase):
13    """Test that a task's aggregated status reflects the logs it wrote
14
15    The status is set by a trigger on the task table, which aggregates the task's logs
16    once it is finished.
17    """
18
19    def _run(self, task_actor: Actor, state: str = TaskState.DONE) -> str:
20        """Run an actor, move the resulting task to the given state, and return its status"""
21        message = task_actor.send()
22        del get_broker().actors[task_actor.actor_name]
23        # The test broker processes a copy of the message on its own queue, while the task
24        # keeps the queue it was enqueued on, so the state updates never match it and the
25        # task is left as queued (see authentik/tasks/test.py). Move it along the way the
26        # broker would, now that all of its logs have been written.
27        Task.objects.filter(message_id=message.message_id).update(state=state)
28        return Task.objects.get(message_id=message.message_id).aggregated_status
29
30    def test_error(self):
31        """Test that a task logging an error is marked as errored"""
32
33        @actor
34        def error_task():
35            CurrentTask.get_task().error("something went wrong")
36
37        self.assertEqual(self._run(error_task), TaskStatus.ERROR)
38
39    def test_warning(self):
40        """Test that a task logging a warning is marked as warned"""
41
42        @actor
43        def warning_task():
44            CurrentTask.get_task().warning("something looked odd")
45
46        self.assertEqual(self._run(warning_task), TaskStatus.WARNING)
47
48    def test_most_severe_log_wins(self):
49        """Test that the most severe log level of a task determines its status"""
50
51        @actor
52        def mixed_task():
53            self = CurrentTask.get_task()
54            self.info("first")
55            self.warning("second")
56            self.error("third")
57            self.info("fourth")
58
59        self.assertEqual(self._run(mixed_task), TaskStatus.ERROR)
60
61    def test_successful(self):
62        """Test that a task which only logs informational messages is not marked as failed"""
63
64        @actor
65        def successful_task():
66            CurrentTask.get_task().info("all good")
67
68        self.assertEqual(self._run(successful_task), TaskStatus.INFO)
69
70    def test_unfinished_task_keeps_state(self):
71        """Test that a task which has not finished is reported with its state"""
72
73        @actor
74        def rejected_task():
75            CurrentTask.get_task().error("something went wrong")
76
77        self.assertEqual(self._run(rejected_task, state=TaskState.REJECTED), TaskStatus.REJECTED)
class TestAggregatedStatus(django.test.testcases.TestCase):
13class TestAggregatedStatus(TestCase):
14    """Test that a task's aggregated status reflects the logs it wrote
15
16    The status is set by a trigger on the task table, which aggregates the task's logs
17    once it is finished.
18    """
19
20    def _run(self, task_actor: Actor, state: str = TaskState.DONE) -> str:
21        """Run an actor, move the resulting task to the given state, and return its status"""
22        message = task_actor.send()
23        del get_broker().actors[task_actor.actor_name]
24        # The test broker processes a copy of the message on its own queue, while the task
25        # keeps the queue it was enqueued on, so the state updates never match it and the
26        # task is left as queued (see authentik/tasks/test.py). Move it along the way the
27        # broker would, now that all of its logs have been written.
28        Task.objects.filter(message_id=message.message_id).update(state=state)
29        return Task.objects.get(message_id=message.message_id).aggregated_status
30
31    def test_error(self):
32        """Test that a task logging an error is marked as errored"""
33
34        @actor
35        def error_task():
36            CurrentTask.get_task().error("something went wrong")
37
38        self.assertEqual(self._run(error_task), TaskStatus.ERROR)
39
40    def test_warning(self):
41        """Test that a task logging a warning is marked as warned"""
42
43        @actor
44        def warning_task():
45            CurrentTask.get_task().warning("something looked odd")
46
47        self.assertEqual(self._run(warning_task), TaskStatus.WARNING)
48
49    def test_most_severe_log_wins(self):
50        """Test that the most severe log level of a task determines its status"""
51
52        @actor
53        def mixed_task():
54            self = CurrentTask.get_task()
55            self.info("first")
56            self.warning("second")
57            self.error("third")
58            self.info("fourth")
59
60        self.assertEqual(self._run(mixed_task), TaskStatus.ERROR)
61
62    def test_successful(self):
63        """Test that a task which only logs informational messages is not marked as failed"""
64
65        @actor
66        def successful_task():
67            CurrentTask.get_task().info("all good")
68
69        self.assertEqual(self._run(successful_task), TaskStatus.INFO)
70
71    def test_unfinished_task_keeps_state(self):
72        """Test that a task which has not finished is reported with its state"""
73
74        @actor
75        def rejected_task():
76            CurrentTask.get_task().error("something went wrong")
77
78        self.assertEqual(self._run(rejected_task, state=TaskState.REJECTED), TaskStatus.REJECTED)

Test that a task's aggregated status reflects the logs it wrote

The status is set by a trigger on the task table, which aggregates the task's logs once it is finished.

def test_error(self):
31    def test_error(self):
32        """Test that a task logging an error is marked as errored"""
33
34        @actor
35        def error_task():
36            CurrentTask.get_task().error("something went wrong")
37
38        self.assertEqual(self._run(error_task), TaskStatus.ERROR)

Test that a task logging an error is marked as errored

def test_warning(self):
40    def test_warning(self):
41        """Test that a task logging a warning is marked as warned"""
42
43        @actor
44        def warning_task():
45            CurrentTask.get_task().warning("something looked odd")
46
47        self.assertEqual(self._run(warning_task), TaskStatus.WARNING)

Test that a task logging a warning is marked as warned

def test_most_severe_log_wins(self):
49    def test_most_severe_log_wins(self):
50        """Test that the most severe log level of a task determines its status"""
51
52        @actor
53        def mixed_task():
54            self = CurrentTask.get_task()
55            self.info("first")
56            self.warning("second")
57            self.error("third")
58            self.info("fourth")
59
60        self.assertEqual(self._run(mixed_task), TaskStatus.ERROR)

Test that the most severe log level of a task determines its status

def test_successful(self):
62    def test_successful(self):
63        """Test that a task which only logs informational messages is not marked as failed"""
64
65        @actor
66        def successful_task():
67            CurrentTask.get_task().info("all good")
68
69        self.assertEqual(self._run(successful_task), TaskStatus.INFO)

Test that a task which only logs informational messages is not marked as failed

def test_unfinished_task_keeps_state(self):
71    def test_unfinished_task_keeps_state(self):
72        """Test that a task which has not finished is reported with its state"""
73
74        @actor
75        def rejected_task():
76            CurrentTask.get_task().error("something went wrong")
77
78        self.assertEqual(self._run(rejected_task, state=TaskState.REJECTED), TaskStatus.REJECTED)

Test that a task which has not finished is reported with its state