1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import collections
from unittest import mock
from rally.cli import task_results_loader
from tests.unit import test
PATH = "rally.cli.task_results_loader"
class LoaderTestCase(test.TestCase):
@mock.patch("%s._update_old_results" % PATH)
@mock.patch("%s._update_new_results" % PATH)
@mock.patch("%s.open" % PATH)
def test_load(self, mock_open,
mock__update_new_results,
mock__update_old_results):
r_file = mock_open.return_value.__enter__.return_value
# case 1: the file contains invalid JSON
r_file.read.return_value = ""
self.assertRaises(task_results_loader.FailedToLoadResults,
task_results_loader.load,
"/some/path")
self.assertFalse(mock__update_new_results.called)
self.assertFalse(mock__update_old_results.called)
mock__update_new_results.reset_mock()
mock__update_old_results.reset_mock()
# case 2: the file contains valid JSON with a dict that doesn't have
# 'tasks' key
r_file.read.return_value = "{}"
self.assertRaises(task_results_loader.FailedToLoadResults,
task_results_loader.load,
"/some/path")
self.assertFalse(mock__update_new_results.called)
self.assertFalse(mock__update_old_results.called)
mock__update_new_results.reset_mock()
mock__update_old_results.reset_mock()
# case 3: the file contains valid JSON with a dict that doesn't have
# 'tasks' key
r_file.read.return_value = "{\"tasks\": \"foo\"}"
self.assertEqual(mock__update_new_results.return_value,
task_results_loader.load("/some/path"))
mock__update_new_results.assert_called_once_with({"tasks": "foo"})
self.assertFalse(mock__update_old_results.called)
mock__update_new_results.reset_mock()
mock__update_old_results.reset_mock()
# case 4: the file contains valid JSON with a list
r_file.read.return_value = "[\"foo\"]"
self.assertEqual(mock__update_old_results.return_value,
task_results_loader.load("/some/path"))
self.assertFalse(mock__update_new_results.called)
mock__update_old_results.assert_called_once_with(["foo"], "/some/path")
def test__update_new_results(self):
results = {
"tasks": [{
"env_uuid": "env-uuid-1",
"env_name": "env-name-1",
"subtasks": [{
"workloads": [{
"contexts": {"xxx": {}},
"scenario": {"Foo.bar": {}},
"runner": {
"constant": {
"times": 100,
"concurrency": 5
}
},
"sla": {},
"sla_results": {},
"position": 0,
"pass_sla": True,
"statistics": {},
"data": [],
"full_duration": 5,
"load_duration": 2,
"total_iteration_count": 3,
"failed_iteration_count": 0
}]
}]
}]
}
self.assertEqual(
[
{
"env_uuid": "env-uuid-1",
"env_name": "env-name-1",
"subtasks": [{
"workloads": [{
"args": {},
"name": "Foo.bar",
"contexts": {"xxx": {}},
"contexts_results": [],
"runner_type": "constant",
"runner": {
"times": 100,
"concurrency": 5
},
"sla": {},
"sla_results": {},
"position": 0,
"pass_sla": True,
"statistics": {},
"data": [],
"full_duration": 5,
"load_duration": 2,
"total_iteration_count": 3,
"failed_iteration_count": 0
}]
}]
}
],
task_results_loader._update_new_results(results)
)
def test__update_old_results(self):
workload = {
"uuid": "n/a",
"full_duration": 2, "load_duration": 1,
"created_at": "2017-07-01T07:03:01",
"updated_at": "2017-07-01T07:03:03",
"total_iteration_count": 2,
"failed_iteration_count": 1,
"min_duration": 3,
"max_duration": 5,
"start_time": 1,
"name": "Foo.bar", "description": "descr",
"position": 2,
"args": {"key1": "value1"},
"runner_type": "constant",
"runner": {"time": 3},
"hooks": [{"config": {
"description": "descr",
"action": ("foo", {"arg1": "v1"}),
"trigger": ("t", {"a2", "v2"})}}],
"pass_sla": True,
"sla": {"failure_rate": {"max": 0}},
"sla_results": {"sla": [{"success": True}]},
"contexts": {"users": {}},
"contexts_results": [],
"data": [{"timestamp": 1, "atomic_actions": {"foo": 1.0,
"bar": 1.0},
"duration": 5, "idle_duration": 0, "error": [{}]},
{"timestamp": 2, "atomic_actions": {"bar": 1.1},
"duration": 3, "idle_duration": 0, "error": []}],
"statistics": {"durations": mock.ANY}
}
results = [{
"hooks": [{
"config": {
"name": "foo",
"args": {"arg1": "v1"},
"description": "descr",
"trigger": {"name": "t", "args": {"a2", "v2"}}}}],
"key": {
"name": workload["name"],
"description": workload["description"],
"pos": workload["position"],
"kw": {
"args": workload["args"],
"runner": {"type": "constant", "time": 3},
"hooks": [{
"name": "foo",
"args": {"arg1": "v1"},
"description": "descr",
"trigger": {"name": "t", "args": {"a2", "v2"}}}],
"sla": workload["sla"],
"context": workload["contexts"]}},
"sla": workload["sla_results"]["sla"],
"result": workload["data"],
"full_duration": workload["full_duration"],
"load_duration": workload["load_duration"],
"created_at": "2017-01-07T07:03:01"}
]
self.assertEqual(
[
{
"version": 2,
"title": "Task loaded from a file.",
"description": "Auto-ported from task format V1.",
"uuid": "n/a",
"env_uuid": "n/a",
"env_name": "n/a",
"status": "finished",
"tags": [],
"subtasks": [{
"title": "A SubTask",
"description": "",
"workloads": [workload]}]
}
],
task_results_loader._update_old_results(results, "xxx")
)
def test__update_atomic_actions(self):
atomic_actions = collections.OrderedDict(
[("action_1", 1), ("action_2", 2)])
self.assertEqual(
[
{
"name": "action_1",
"started_at": 1,
"finished_at": 2,
"children": []
},
{
"name": "action_2",
"started_at": 2,
"finished_at": 4,
"children": []
}
],
task_results_loader._update_atomic_actions(atomic_actions, 1)
)
|