-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds task groups status API endpoint
- Loading branch information
Showing
4 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
""" | ||
This module contains views related to Pulp's task groups. | ||
""" | ||
from django.views.generic import View | ||
|
||
from pulp.common.constants import CALL_STATES | ||
from pulp.server.auth import authorization | ||
from pulp.server.db.model import TaskStatus | ||
from pulp.server.exceptions import MissingResource | ||
from pulp.server.webservices.views.decorators import auth_required | ||
from pulp.server.webservices.views.util import generate_json_response_with_pulp_encoder | ||
|
||
|
||
class TaskGroupSummaryView(View): | ||
""" | ||
View for a task group summary. | ||
""" | ||
|
||
@auth_required(authorization.READ) | ||
def get(self, request, group_id): | ||
""" | ||
Return a response containing a summary of task states for task group. | ||
:param request: WSGI request object | ||
:type request: django.core.handlers.wsgi.WSGIRequest | ||
:param group_id: The ID of the task group you wish to summarize | ||
:type group_id: basestring | ||
:return: Response containing a serialized dict of the task group summary | ||
:rtype : django.http.HttpResponse | ||
:raises MissingResource: if group id is not found | ||
""" | ||
tasks = TaskStatus.objects(group_id=group_id) | ||
task_group_total = tasks.count() | ||
|
||
if task_group_total == 0: | ||
raise MissingResource(group_id) | ||
|
||
summary = {'total': task_group_total} | ||
for state in CALL_STATES: | ||
summary[state] = tasks.filter(state=state).count() | ||
|
||
return generate_json_response_with_pulp_encoder(summary) |
71 changes: 71 additions & 0 deletions
71
server/test/unit/server/webservices/views/test_task_groups.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
""" | ||
This module contains tests for the pulp.server.webservices.views.task_groups module. | ||
""" | ||
import mock | ||
|
||
from .base import assert_auth_READ | ||
from pulp.common.compat import unittest | ||
|
||
from pulp.server.exceptions import MissingResource | ||
from pulp.server.webservices.views.task_groups import TaskGroupSummaryView | ||
|
||
|
||
class TestTaskGroupSummary(unittest.TestCase): | ||
""" | ||
Tests for TaskGroupSummaryView | ||
""" | ||
|
||
@mock.patch('pulp.server.webservices.views.decorators._verify_auth', | ||
new=assert_auth_READ()) | ||
@mock.patch('pulp.server.webservices.views.task_groups.TaskStatus.objects') | ||
def test_get_task_group_summary_nonexistant(self, mock_objects): | ||
""" | ||
Test get task_group_summary with no tasks | ||
""" | ||
|
||
mock_request = mock.MagicMock() | ||
mock_objects.return_value.count.return_value = 0 | ||
task_group_summary = TaskGroupSummaryView() | ||
self.assertRaises(MissingResource, task_group_summary.get, mock_request, 'mock_task') | ||
|
||
@mock.patch('pulp.server.webservices.views.decorators._verify_auth', | ||
new=assert_auth_READ()) | ||
@mock.patch('pulp.server.webservices.views.task_groups.TaskStatus.objects') | ||
@mock.patch( | ||
'pulp.server.webservices.views.task_groups.generate_json_response_with_pulp_encoder') | ||
def test_get_task_group_summary(self, mock_resp, mock_objects): | ||
""" | ||
Test get task_group_summary with multiple tasks | ||
""" | ||
class MockQuerySet(object): | ||
|
||
def __init__(self, list_of_objects): | ||
self.items = list_of_objects | ||
self.i = 0 | ||
self.n = len(self.items) | ||
|
||
def count(self): | ||
return len(self.items) | ||
|
||
def filter(self, state=None): | ||
filtered_list = [] | ||
for item in self.items: | ||
if item['state'] == state: | ||
filtered_list.append(item) | ||
return MockQuerySet(filtered_list) | ||
|
||
mock_request = mock.MagicMock() | ||
mock_objects.return_value = MockQuerySet([{'id': 'mock_task', 'worker_name': 'mock', | ||
'state': 'running'}, | ||
{'id': 'mock_task', 'worker_name': 'mock', | ||
'state': 'finished'}, | ||
{'id': 'mock_task', 'worker_name': 'mock', | ||
'state': 'waiting'}]) | ||
|
||
task_group_summary = TaskGroupSummaryView() | ||
response = task_group_summary.get(mock_request, 'mock_task') | ||
|
||
expected_content = {'accepted': 0, 'finished': 1, 'running': 1, 'canceled': 0, | ||
'waiting': 1, 'skipped': 0, 'suspended': 0, 'error': 0, 'total': 3} | ||
mock_resp.assert_called_with(expected_content) | ||
self.assertTrue(response is mock_resp.return_value) |