| 1 |
# Copyright (C) 2013 Rackspace Hosting All Rights Reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
import contextlib |
| 16 |
|
| 17 |
from oslo_utils import uuidutils |
| 18 |
|
| 19 |
from taskflow import exceptions as exc |
| 20 |
from taskflow.persistence import models |
| 21 |
from taskflow import states |
| 22 |
from taskflow.types import failure |
| 23 |
|
| 24 |
|
| 25 |
class PersistenceTestMixin: |
| 26 |
def _get_connection(self): |
| 27 |
raise NotImplementedError('_get_connection() implementation required') |
| 28 |
|
| 29 |
def test_task_detail_update_not_existing(self): |
| 30 |
lb_id = uuidutils.generate_uuid() |
| 31 |
lb_name = 'lb-%s' % (lb_id) |
| 32 |
lb = models.LogBook(name=lb_name, uuid=lb_id) |
| 33 |
fd = models.FlowDetail('test', uuid=uuidutils.generate_uuid()) |
| 34 |
lb.add(fd) |
| 35 |
td = models.TaskDetail("detail-1", uuid=uuidutils.generate_uuid()) |
| 36 |
fd.add(td) |
| 37 |
with contextlib.closing(self._get_connection()) as conn: |
| 38 |
conn.save_logbook(lb) |
| 39 |
|
| 40 |
td2 = models.TaskDetail("detail-1", uuid=uuidutils.generate_uuid()) |
| 41 |
fd.add(td2) |
| 42 |
with contextlib.closing(self._get_connection()) as conn: |
| 43 |
conn.update_flow_details(fd) |
| 44 |
|
| 45 |
with contextlib.closing(self._get_connection()) as conn: |
| 46 |
lb2 = conn.get_logbook(lb.uuid) |
| 47 |
fd2 = lb2.find(fd.uuid) |
| 48 |
self.assertIsNotNone(fd2.find(td.uuid)) |
| 49 |
self.assertIsNotNone(fd2.find(td2.uuid)) |
| 50 |
|
| 51 |
def test_flow_detail_update_not_existing(self): |
| 52 |
lb_id = uuidutils.generate_uuid() |
| 53 |
lb_name = 'lb-%s' % (lb_id) |
| 54 |
lb = models.LogBook(name=lb_name, uuid=lb_id) |
| 55 |
fd = models.FlowDetail('test', uuid=uuidutils.generate_uuid()) |
| 56 |
lb.add(fd) |
| 57 |
with contextlib.closing(self._get_connection()) as conn: |
| 58 |
conn.save_logbook(lb) |
| 59 |
|
| 60 |
fd2 = models.FlowDetail('test-2', uuid=uuidutils.generate_uuid()) |
| 61 |
lb.add(fd2) |
| 62 |
with contextlib.closing(self._get_connection()) as conn: |
| 63 |
conn.save_logbook(lb) |
| 64 |
|
| 65 |
with contextlib.closing(self._get_connection()) as conn: |
| 66 |
lb2 = conn.get_logbook(lb.uuid) |
| 67 |
self.assertIsNotNone(lb2.find(fd.uuid)) |
| 68 |
self.assertIsNotNone(lb2.find(fd2.uuid)) |
| 69 |
|
| 70 |
def test_logbook_save_retrieve_many(self): |
| 71 |
lb_ids = {} |
| 72 |
for i in range(0, 10): |
| 73 |
lb_id = uuidutils.generate_uuid() |
| 74 |
lb_name = f'lb-{i}-{lb_id}' |
| 75 |
lb = models.LogBook(name=lb_name, uuid=lb_id) |
| 76 |
lb_ids[lb_id] = True |
| 77 |
|
| 78 |
# Should not already exist |
| 79 |
with contextlib.closing(self._get_connection()) as conn: |
| 80 |
self.assertRaises(exc.NotFound, conn.get_logbook, lb_id) |
| 81 |
conn.save_logbook(lb) |
| 82 |
|
| 83 |
# Now fetch them all |
| 84 |
with contextlib.closing(self._get_connection()) as conn: |
| 85 |
lbs = conn.get_logbooks() |
| 86 |
for lb in lbs: |
| 87 |
self.assertIn(lb.uuid, lb_ids) |
| 88 |
lb_ids.pop(lb.uuid) |
| 89 |
self.assertEqual(0, len(lb_ids)) |
| 90 |
|
| 91 |
def test_logbook_save_retrieve(self): |
| 92 |
lb_id = uuidutils.generate_uuid() |
| 93 |
lb_meta = {'1': 2} |
| 94 |
lb_name = 'lb-%s' % (lb_id) |
| 95 |
lb = models.LogBook(name=lb_name, uuid=lb_id) |
| 96 |
lb.meta = lb_meta |
| 97 |
|
| 98 |
# Should not already exist |
| 99 |
with contextlib.closing(self._get_connection()) as conn: |
| 100 |
self.assertRaises(exc.NotFound, conn.get_logbook, lb_id) |
| 101 |
conn.save_logbook(lb) |
| 102 |
|
| 103 |
# Make sure we can reload it (and all of its attributes are what |
| 104 |
# we expect them to be). |
| 105 |
with contextlib.closing(self._get_connection()) as conn: |
| 106 |
lb = conn.get_logbook(lb_id) |
| 107 |
self.assertEqual(lb_name, lb.name) |
| 108 |
self.assertEqual(0, len(lb)) |
| 109 |
self.assertEqual(lb_meta, lb.meta) |
| 110 |
self.assertIsNone(lb.updated_at) |
| 111 |
self.assertIsNotNone(lb.created_at) |
| 112 |
|
| 113 |
def test_flow_detail_save(self): |
| 114 |
lb_id = uuidutils.generate_uuid() |
| 115 |
lb_name = 'lb-%s' % (lb_id) |
| 116 |
lb = models.LogBook(name=lb_name, uuid=lb_id) |
| 117 |
fd = models.FlowDetail('test', uuid=uuidutils.generate_uuid()) |
| 118 |
lb.add(fd) |
| 119 |
|
| 120 |
# Ensure we can't save it since its owning logbook hasn't been |
| 121 |
# saved (flow details can not exist on their own without a connection |
| 122 |
# to a logbook). |
| 123 |
with contextlib.closing(self._get_connection()) as conn: |
| 124 |
self.assertRaises(exc.NotFound, conn.get_logbook, lb_id) |
| 125 |
self.assertRaises(exc.NotFound, conn.update_flow_details, fd) |
| 126 |
|
| 127 |
# Ok now we should be able to save both. |
| 128 |
with contextlib.closing(self._get_connection()) as conn: |
| 129 |
conn.save_logbook(lb) |
| 130 |
conn.update_flow_details(fd) |
| 131 |
|
| 132 |
def test_flow_detail_meta_update(self): |
| 133 |
lb_id = uuidutils.generate_uuid() |
| 134 |
lb_name = 'lb-%s' % (lb_id) |
| 135 |
lb = models.LogBook(name=lb_name, uuid=lb_id) |
| 136 |
fd = models.FlowDetail('test', uuid=uuidutils.generate_uuid()) |
| 137 |
fd.meta = {'test': 42} |
| 138 |
lb.add(fd) |
| 139 |
|
| 140 |
with contextlib.closing(self._get_connection()) as conn: |
| 141 |
conn.save_logbook(lb) |
| 142 |
conn.update_flow_details(fd) |
| 143 |
|
| 144 |
fd.meta['test'] = 43 |
| 145 |
with contextlib.closing(self._get_connection()) as conn: |
| 146 |
conn.update_flow_details(fd) |
| 147 |
with contextlib.closing(self._get_connection()) as conn: |
| 148 |
lb2 = conn.get_logbook(lb_id) |
| 149 |
fd2 = lb2.find(fd.uuid) |
| 150 |
self.assertEqual(43, fd2.meta.get('test')) |
| 151 |
|
| 152 |
def test_flow_detail_lazy_fetch(self): |
| 153 |
lb_id = uuidutils.generate_uuid() |
| 154 |
lb_name = 'lb-%s' % (lb_id) |
| 155 |
lb = models.LogBook(name=lb_name, uuid=lb_id) |
| 156 |
fd = models.FlowDetail('test', uuid=uuidutils.generate_uuid()) |
| 157 |
td = models.TaskDetail("detail-1", uuid=uuidutils.generate_uuid()) |
| 158 |
td.version = '4.2' |
| 159 |
fd.add(td) |
| 160 |
lb.add(fd) |
| 161 |
with contextlib.closing(self._get_connection()) as conn: |
| 162 |
conn.save_logbook(lb) |
| 163 |
with contextlib.closing(self._get_connection()) as conn: |
| 164 |
fd2 = conn.get_flow_details(fd.uuid, lazy=True) |
| 165 |
self.assertEqual(0, len(fd2)) |
| 166 |
self.assertEqual(1, len(fd)) |
| 167 |
|
| 168 |
def test_task_detail_save(self): |
| 169 |
lb_id = uuidutils.generate_uuid() |
| 170 |
lb_name = 'lb-%s' % (lb_id) |
| 171 |
lb = models.LogBook(name=lb_name, uuid=lb_id) |
| 172 |
fd = models.FlowDetail('test', uuid=uuidutils.generate_uuid()) |
| 173 |
lb.add(fd) |
| 174 |
td = models.TaskDetail("detail-1", uuid=uuidutils.generate_uuid()) |
| 175 |
fd.add(td) |
| 176 |
|
| 177 |
# Ensure we can't save it since its owning logbook hasn't been |
| 178 |
# saved (flow details/task details can not exist on their own without |
| 179 |
# their parent existing). |
| 180 |
with contextlib.closing(self._get_connection()) as conn: |
| 181 |
self.assertRaises(exc.NotFound, conn.update_flow_details, fd) |
| 182 |
self.assertRaises(exc.NotFound, conn.update_atom_details, td) |
| 183 |
|
| 184 |
# Ok now we should be able to save them. |
| 185 |
with contextlib.closing(self._get_connection()) as conn: |
| 186 |
conn.save_logbook(lb) |
| 187 |
conn.update_flow_details(fd) |
| 188 |
conn.update_atom_details(td) |
| 189 |
|
| 190 |
def test_task_detail_meta_update(self): |
| 191 |
lb_id = uuidutils.generate_uuid() |
| 192 |
lb_name = 'lb-%s' % (lb_id) |
| 193 |
lb = models.LogBook(name=lb_name, uuid=lb_id) |
| 194 |
fd = models.FlowDetail('test', uuid=uuidutils.generate_uuid()) |
| 195 |
lb.add(fd) |
| 196 |
td = models.TaskDetail("detail-1", uuid=uuidutils.generate_uuid()) |
| 197 |
td.meta = {'test': 42} |
| 198 |
fd.add(td) |
| 199 |
|
| 200 |
with contextlib.closing(self._get_connection()) as conn: |
| 201 |
conn.save_logbook(lb) |
| 202 |
conn.update_flow_details(fd) |
| 203 |
conn.update_atom_details(td) |
| 204 |
|
| 205 |
td.meta['test'] = 43 |
| 206 |
with contextlib.closing(self._get_connection()) as conn: |
| 207 |
conn.update_atom_details(td) |
| 208 |
|
| 209 |
with contextlib.closing(self._get_connection()) as conn: |
| 210 |
lb2 = conn.get_logbook(lb_id) |
| 211 |
fd2 = lb2.find(fd.uuid) |
| 212 |
td2 = fd2.find(td.uuid) |
| 213 |
self.assertEqual(43, td2.meta.get('test')) |
| 214 |
self.assertIsInstance(td2, models.TaskDetail) |
| 215 |
|
| 216 |
def test_task_detail_with_failure(self): |
| 217 |
lb_id = uuidutils.generate_uuid() |
| 218 |
lb_name = 'lb-%s' % (lb_id) |
| 219 |
lb = models.LogBook(name=lb_name, uuid=lb_id) |
| 220 |
fd = models.FlowDetail('test', uuid=uuidutils.generate_uuid()) |
| 221 |
lb.add(fd) |
| 222 |
td = models.TaskDetail("detail-1", uuid=uuidutils.generate_uuid()) |
| 223 |
|
| 224 |
try: |
| 225 |
raise RuntimeError('Woot!') |
| 226 |
except Exception: |
| 227 |
td.failure = failure.Failure() |
| 228 |
|
| 229 |
fd.add(td) |
| 230 |
|
| 231 |
with contextlib.closing(self._get_connection()) as conn: |
| 232 |
conn.save_logbook(lb) |
| 233 |
conn.update_flow_details(fd) |
| 234 |
conn.update_atom_details(td) |
| 235 |
|
| 236 |
# Read failure back |
| 237 |
with contextlib.closing(self._get_connection()) as conn: |
| 238 |
lb2 = conn.get_logbook(lb_id) |
| 239 |
fd2 = lb2.find(fd.uuid) |
| 240 |
td2 = fd2.find(td.uuid) |
| 241 |
self.assertEqual('Woot!', td2.failure.exception_str) |
| 242 |
self.assertIs(td2.failure.check(RuntimeError), RuntimeError) |
| 243 |
self.assertEqual(td.failure.traceback_str, td2.failure.traceback_str) |
| 244 |
self.assertIsInstance(td2, models.TaskDetail) |
| 245 |
|
| 246 |
def test_logbook_merge_flow_detail(self): |
| 247 |
lb_id = uuidutils.generate_uuid() |
| 248 |
lb_name = 'lb-%s' % (lb_id) |
| 249 |
lb = models.LogBook(name=lb_name, uuid=lb_id) |
| 250 |
fd = models.FlowDetail('test', uuid=uuidutils.generate_uuid()) |
| 251 |
lb.add(fd) |
| 252 |
with contextlib.closing(self._get_connection()) as conn: |
| 253 |
conn.save_logbook(lb) |
| 254 |
lb2 = models.LogBook(name=lb_name, uuid=lb_id) |
| 255 |
fd2 = models.FlowDetail('test2', uuid=uuidutils.generate_uuid()) |
| 256 |
lb2.add(fd2) |
| 257 |
with contextlib.closing(self._get_connection()) as conn: |
| 258 |
conn.save_logbook(lb2) |
| 259 |
with contextlib.closing(self._get_connection()) as conn: |
| 260 |
lb3 = conn.get_logbook(lb_id) |
| 261 |
self.assertEqual(2, len(lb3)) |
| 262 |
|
| 263 |
def test_logbook_add_flow_detail(self): |
| 264 |
lb_id = uuidutils.generate_uuid() |
| 265 |
lb_name = 'lb-%s' % (lb_id) |
| 266 |
lb = models.LogBook(name=lb_name, uuid=lb_id) |
| 267 |
fd = models.FlowDetail('test', uuid=uuidutils.generate_uuid()) |
| 268 |
lb.add(fd) |
| 269 |
with contextlib.closing(self._get_connection()) as conn: |
| 270 |
conn.save_logbook(lb) |
| 271 |
with contextlib.closing(self._get_connection()) as conn: |
| 272 |
lb2 = conn.get_logbook(lb_id) |
| 273 |
self.assertEqual(1, len(lb2)) |
| 274 |
self.assertEqual(1, len(lb)) |
| 275 |
self.assertEqual(fd.name, lb2.find(fd.uuid).name) |
| 276 |
|
| 277 |
def test_logbook_lazy_fetch(self): |
| 278 |
lb_id = uuidutils.generate_uuid() |
| 279 |
lb_name = 'lb-%s' % (lb_id) |
| 280 |
lb = models.LogBook(name=lb_name, uuid=lb_id) |
| 281 |
fd = models.FlowDetail('test', uuid=uuidutils.generate_uuid()) |
| 282 |
lb.add(fd) |
| 283 |
with contextlib.closing(self._get_connection()) as conn: |
| 284 |
conn.save_logbook(lb) |
| 285 |
with contextlib.closing(self._get_connection()) as conn: |
| 286 |
lb2 = conn.get_logbook(lb_id, lazy=True) |
| 287 |
self.assertEqual(0, len(lb2)) |
| 288 |
self.assertEqual(1, len(lb)) |
| 289 |
|
| 290 |
def test_logbook_add_task_detail(self): |
| 291 |
lb_id = uuidutils.generate_uuid() |
| 292 |
lb_name = 'lb-%s' % (lb_id) |
| 293 |
lb = models.LogBook(name=lb_name, uuid=lb_id) |
| 294 |
fd = models.FlowDetail('test', uuid=uuidutils.generate_uuid()) |
| 295 |
td = models.TaskDetail("detail-1", uuid=uuidutils.generate_uuid()) |
| 296 |
td.version = '4.2' |
| 297 |
fd.add(td) |
| 298 |
lb.add(fd) |
| 299 |
with contextlib.closing(self._get_connection()) as conn: |
| 300 |
conn.save_logbook(lb) |
| 301 |
with contextlib.closing(self._get_connection()) as conn: |
| 302 |
lb2 = conn.get_logbook(lb_id) |
| 303 |
self.assertEqual(1, len(lb2)) |
| 304 |
tasks = 0 |
| 305 |
for fd in lb: |
| 306 |
tasks += len(fd) |
| 307 |
self.assertEqual(1, tasks) |
| 308 |
with contextlib.closing(self._get_connection()) as conn: |
| 309 |
lb2 = conn.get_logbook(lb_id) |
| 310 |
fd2 = lb2.find(fd.uuid) |
| 311 |
td2 = fd2.find(td.uuid) |
| 312 |
self.assertIsNotNone(td2) |
| 313 |
self.assertEqual('detail-1', td2.name) |
| 314 |
self.assertEqual('4.2', td2.version) |
| 315 |
self.assertEqual(states.EXECUTE, td2.intention) |
| 316 |
|
| 317 |
def test_logbook_delete(self): |
| 318 |
lb_id = uuidutils.generate_uuid() |
| 319 |
lb_name = 'lb-%s' % (lb_id) |
| 320 |
lb = models.LogBook(name=lb_name, uuid=lb_id) |
| 321 |
with contextlib.closing(self._get_connection()) as conn: |
| 322 |
self.assertRaises(exc.NotFound, conn.destroy_logbook, lb_id) |
| 323 |
with contextlib.closing(self._get_connection()) as conn: |
| 324 |
conn.save_logbook(lb) |
| 325 |
with contextlib.closing(self._get_connection()) as conn: |
| 326 |
lb2 = conn.get_logbook(lb_id) |
| 327 |
self.assertIsNotNone(lb2) |
| 328 |
with contextlib.closing(self._get_connection()) as conn: |
| 329 |
conn.destroy_logbook(lb_id) |
| 330 |
self.assertRaises(exc.NotFound, conn.destroy_logbook, lb_id) |
| 331 |
|
| 332 |
def test_task_detail_retry_type_(self): |
| 333 |
lb_id = uuidutils.generate_uuid() |
| 334 |
lb_name = 'lb-%s' % (lb_id) |
| 335 |
lb = models.LogBook(name=lb_name, uuid=lb_id) |
| 336 |
fd = models.FlowDetail('test', uuid=uuidutils.generate_uuid()) |
| 337 |
lb.add(fd) |
| 338 |
rd = models.RetryDetail("detail-1", uuid=uuidutils.generate_uuid()) |
| 339 |
rd.intention = states.REVERT |
| 340 |
fd.add(rd) |
| 341 |
|
| 342 |
with contextlib.closing(self._get_connection()) as conn: |
| 343 |
conn.save_logbook(lb) |
| 344 |
conn.update_flow_details(fd) |
| 345 |
conn.update_atom_details(rd) |
| 346 |
|
| 347 |
with contextlib.closing(self._get_connection()) as conn: |
| 348 |
lb2 = conn.get_logbook(lb_id) |
| 349 |
fd2 = lb2.find(fd.uuid) |
| 350 |
rd2 = fd2.find(rd.uuid) |
| 351 |
self.assertEqual(states.REVERT, rd2.intention) |
| 352 |
self.assertIsInstance(rd2, models.RetryDetail) |
| 353 |
|
| 354 |
def test_retry_detail_save_with_task_failure(self): |
| 355 |
lb_id = uuidutils.generate_uuid() |
| 356 |
lb_name = 'lb-%s' % (lb_id) |
| 357 |
lb = models.LogBook(name=lb_name, uuid=lb_id) |
| 358 |
fd = models.FlowDetail('test', uuid=uuidutils.generate_uuid()) |
| 359 |
lb.add(fd) |
| 360 |
rd = models.RetryDetail("retry-1", uuid=uuidutils.generate_uuid()) |
| 361 |
fail = failure.Failure.from_exception(RuntimeError('fail')) |
| 362 |
rd.results.append((42, {'some-task': fail})) |
| 363 |
fd.add(rd) |
| 364 |
|
| 365 |
# save it |
| 366 |
with contextlib.closing(self._get_connection()) as conn: |
| 367 |
conn.save_logbook(lb) |
| 368 |
conn.update_flow_details(fd) |
| 369 |
conn.update_atom_details(rd) |
| 370 |
|
| 371 |
# now read it back |
| 372 |
with contextlib.closing(self._get_connection()) as conn: |
| 373 |
lb2 = conn.get_logbook(lb_id) |
| 374 |
fd2 = lb2.find(fd.uuid) |
| 375 |
rd2 = fd2.find(rd.uuid) |
| 376 |
self.assertIsInstance(rd2, models.RetryDetail) |
| 377 |
fail2 = rd2.results[0][1].get('some-task') |
| 378 |
self.assertIsInstance(fail2, failure.Failure) |
| 379 |
self.assertTrue(fail.matches(fail2)) |
| 380 |
|
| 381 |
def test_retry_detail_save_intention(self): |
| 382 |
lb_id = uuidutils.generate_uuid() |
| 383 |
lb_name = 'lb-%s' % (lb_id) |
| 384 |
lb = models.LogBook(name=lb_name, uuid=lb_id) |
| 385 |
fd = models.FlowDetail('test', uuid=uuidutils.generate_uuid()) |
| 386 |
lb.add(fd) |
| 387 |
rd = models.RetryDetail("retry-1", uuid=uuidutils.generate_uuid()) |
| 388 |
fd.add(rd) |
| 389 |
|
| 390 |
# save it |
| 391 |
with contextlib.closing(self._get_connection()) as conn: |
| 392 |
conn.save_logbook(lb) |
| 393 |
conn.update_flow_details(fd) |
| 394 |
conn.update_atom_details(rd) |
| 395 |
|
| 396 |
# change intention and save |
| 397 |
rd.intention = states.REVERT |
| 398 |
with contextlib.closing(self._get_connection()) as conn: |
| 399 |
conn.update_atom_details(rd) |
| 400 |
|
| 401 |
# now read it back |
| 402 |
with contextlib.closing(self._get_connection()) as conn: |
| 403 |
lb2 = conn.get_logbook(lb_id) |
| 404 |
fd2 = lb2.find(fd.uuid) |
| 405 |
rd2 = fd2.find(rd.uuid) |
| 406 |
self.assertEqual(states.REVERT, rd2.intention) |
| 407 |
self.assertIsInstance(rd2, models.RetryDetail) |