Merge "Fix atomic wrapper"
This commit is contained in:
commit
78c8f445a0
|
@ -16,6 +16,7 @@
|
|||
"""Rally command: task"""
|
||||
|
||||
from __future__ import print_function
|
||||
import collections
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
@ -40,6 +41,7 @@ from rally import exceptions
|
|||
from rally import plugins
|
||||
from rally.task import exporter
|
||||
from rally.task.processing import plot
|
||||
from rally.task import utils as tutils
|
||||
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
@ -455,6 +457,16 @@ class TaskCommands(object):
|
|||
print(_("Task status is %s. Results available when it is one "
|
||||
"of %s.") % (task["status"], ", ".join(finished_statuses)))
|
||||
return 1
|
||||
|
||||
# TODO(chenhb): Ensure `rally task results` puts out old format.
|
||||
for result in task["results"]:
|
||||
for itr in result["data"]["raw"]:
|
||||
if "atomic_actions" in itr:
|
||||
itr["atomic_actions"] = collections.OrderedDict(
|
||||
tutils.WrapperForAtomicActions(
|
||||
itr["atomic_actions"]).items()
|
||||
)
|
||||
|
||||
results = [{"key": x["key"], "result": x["data"]["raw"],
|
||||
"sla": x["data"]["sla"],
|
||||
"hooks": x["data"].get("hooks", []),
|
||||
|
|
|
@ -435,16 +435,8 @@ class Task(object):
|
|||
def add_subtask(self, **subtask):
|
||||
return Subtask(self.task["uuid"], **subtask)
|
||||
|
||||
def _get_results(self):
|
||||
return db.task_result_get_all_by_uuid(self.task["uuid"])
|
||||
|
||||
def get_results(self):
|
||||
results = self._get_results()
|
||||
for result in results:
|
||||
for itr in result["data"]["raw"]:
|
||||
itr["atomic_actions"] = self.convert_atomic_actions(
|
||||
itr["atomic_actions"])
|
||||
return results
|
||||
return db.task_result_get_all_by_uuid(self.task["uuid"])
|
||||
|
||||
"""TODO(chenhb): Remove this method after replacing old format.
|
||||
Now we do not convert children actions, because our output
|
||||
|
|
|
@ -61,9 +61,8 @@ class SLAChecker(object):
|
|||
"""
|
||||
if isinstance(iteration, dict):
|
||||
atomic_actions = iteration.get("atomic_actions", None)
|
||||
if isinstance(atomic_actions, list):
|
||||
iteration["atomic_actions"] = utils.WrapperForAtomicActions(
|
||||
atomic_actions)
|
||||
iteration["atomic_actions"] = utils.WrapperForAtomicActions(
|
||||
atomic_actions)
|
||||
return all([sla.add_iteration(iteration) for sla in self.sla_criteria])
|
||||
|
||||
def merge(self, other):
|
||||
|
|
|
@ -417,20 +417,31 @@ class ActionBuilder(object):
|
|||
# we can use these wrapper to simulate new and old format.
|
||||
class WrapperForAtomicActions(list):
|
||||
|
||||
LOG_INFO = "Atomic actions format is changed. It is a list now."
|
||||
|
||||
def __init__(self, atomic_actions):
|
||||
super(WrapperForAtomicActions, self).__init__(atomic_actions)
|
||||
self.__atomic_actions = atomic_actions
|
||||
self.__old_atomic_actions = objects.Task.convert_atomic_actions(
|
||||
self.__atomic_actions)
|
||||
if isinstance(atomic_actions, list):
|
||||
self.__atomic_actions = atomic_actions
|
||||
self.__old_atomic_actions = objects.Task.convert_atomic_actions(
|
||||
self.__atomic_actions)
|
||||
else:
|
||||
self.__atomic_actions = self._convert_old_atomic_actions(
|
||||
atomic_actions)
|
||||
self.__old_atomic_actions = atomic_actions
|
||||
|
||||
super(WrapperForAtomicActions, self).__init__(self.__atomic_actions)
|
||||
|
||||
def _convert_old_atomic_actions(self, old_atomic_actions):
|
||||
atomic_actions = []
|
||||
for name, duration in old_atomic_actions.items():
|
||||
atomic_actions.append({"name": name,
|
||||
"started_at": 0,
|
||||
"finished_at": duration,
|
||||
"children": []})
|
||||
return atomic_actions
|
||||
|
||||
def items(self):
|
||||
LOG.warning(self.LOG_INFO)
|
||||
return self.__old_atomic_actions.items()
|
||||
|
||||
def get(self, name, default=None):
|
||||
LOG.warning(self.LOG_INFO)
|
||||
return self.__old_atomic_actions.get(name, default)
|
||||
|
||||
def __iter__(self):
|
||||
|
@ -444,5 +455,4 @@ class WrapperForAtomicActions(list):
|
|||
# it is a call to list:
|
||||
return self.__atomic_actions[item]
|
||||
else:
|
||||
LOG.warning(self.LOG_INFO)
|
||||
return self.__old_atomic_actions[item]
|
||||
|
|
|
@ -262,27 +262,13 @@ class TaskTestCase(test.TestCase):
|
|||
|
||||
@mock.patch("rally.common.objects.task.db.task_result_get_all_by_uuid",
|
||||
return_value="foo_results")
|
||||
def test__get_results(self, mock_task_result_get_all_by_uuid):
|
||||
def test_get_results(self, mock_task_result_get_all_by_uuid):
|
||||
task = objects.Task(task=self.task)
|
||||
results = task._get_results()
|
||||
results = task.get_results()
|
||||
mock_task_result_get_all_by_uuid.assert_called_once_with(
|
||||
self.task["uuid"])
|
||||
self.assertEqual(results, "foo_results")
|
||||
|
||||
def test_get_results(self):
|
||||
task = objects.Task(task=self.task)
|
||||
task._get_results = mock.MagicMock()
|
||||
return_value = [{"data": {"raw": [
|
||||
{"atomic_actions": [
|
||||
{"name": "some",
|
||||
"started_at": 1.0,
|
||||
"finished_at": 2.0,
|
||||
"children": []}]}]}}]
|
||||
task._get_results.return_value = return_value
|
||||
self.assertEqual([{"data": {"raw": [
|
||||
{"atomic_actions": {"some": 1.0}}]}}],
|
||||
task.get_results())
|
||||
|
||||
@mock.patch("rally.common.objects.task.db.task_update")
|
||||
def test_set_failed(self, mock_task_update):
|
||||
mock_task_update.return_value = self.task
|
||||
|
|
|
@ -541,6 +541,11 @@ class WrapperForAtomicActionsTestCase(test.TestCase):
|
|||
self.assertEqual(1, atomic_wrapper.get("action_1"))
|
||||
self.assertIsNone(atomic_wrapper.get("action_3"))
|
||||
self.assertEqual(2, len(atomic_wrapper))
|
||||
self.assertEqual([{"name": "action_1", "started_at": 0,
|
||||
"finished_at": 1, "children": []},
|
||||
{"name": "action_2", "started_at": 0,
|
||||
"finished_at": 2, "children": []}
|
||||
], atomic_wrapper)
|
||||
|
||||
def test_list_atomic(self):
|
||||
atomic_actions = [{"name": "action_1", "started_at": 1,
|
||||
|
@ -561,3 +566,14 @@ class WrapperForAtomicActionsTestCase(test.TestCase):
|
|||
self.assertIsNone(None, atomic_wrapper.get("action_3"))
|
||||
self.assertEqual(2, len(atomic_wrapper))
|
||||
self.assertEqual(atomic_actions[0], six.next(iter(atomic_wrapper)))
|
||||
|
||||
def test__convert_new_atomic_actions(self):
|
||||
atomic_actions = collections.OrderedDict(
|
||||
[("action_1", 1), ("action_2", 2)])
|
||||
atomic_wrapper = utils.WrapperForAtomicActions(atomic_actions)
|
||||
self.assertEqual(
|
||||
[{"name": "action_1", "started_at": 0,
|
||||
"finished_at": 1, "children": []},
|
||||
{"name": "action_2", "started_at": 0,
|
||||
"finished_at": 2, "children": []}],
|
||||
atomic_wrapper._convert_old_atomic_actions(atomic_actions))
|
||||
|
|
|
@ -22,8 +22,8 @@ from oslotest import base
|
|||
from oslotest import mockpatch
|
||||
|
||||
from rally.common import db
|
||||
from rally.common import objects
|
||||
from rally import plugins
|
||||
from rally.task import utils as tutils
|
||||
from tests.unit import fakes
|
||||
|
||||
|
||||
|
@ -47,9 +47,8 @@ class TestCase(base.BaseTestCase):
|
|||
plugins.load()
|
||||
|
||||
def _test_atomic_action_timer(self, atomic_actions, name):
|
||||
_old_atomic_actions = objects.Task.convert_atomic_actions(
|
||||
atomic_actions)
|
||||
action_duration = _old_atomic_actions.get(name)
|
||||
atomic_wrapper = tutils.WrapperForAtomicActions(atomic_actions)
|
||||
action_duration = atomic_wrapper.get(name)
|
||||
self.assertIsNotNone(action_duration)
|
||||
self.assertIsInstance(action_duration, float)
|
||||
|
||||
|
|
Loading…
Reference in New Issue