Use the same root test class.

Instead of being strongly tied to unittest2 make
it easier for taskflow to switch to another root
class (testr?) by abstracting out the unittest2
usage. This also enables some useful functionality
to exist in that root test class that other tasks
can take advantage of.

Change-Id: I381b6fb07e47f984b44cde439a17f39a1c1d32ac
This commit is contained in:
Joshua Harlow
2013-08-24 08:56:48 -07:00
committed by Joshua Harlow
parent 49a9ee3b0d
commit 7a09c044e3
9 changed files with 100 additions and 54 deletions

55
taskflow/test.py Normal file
View File

@@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright (C) 2012 Yahoo! Inc. 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 unittest2
from oslo.config import cfg
CONF = cfg.CONF
class TestCase(unittest2.TestCase):
"""Test case base class for all unit tests."""
def setUp(self):
"""Run before each test method to initialize test environment."""
super(TestCase, self).setUp()
self.overriden = []
self.addCleanup(self._clear_attrs)
def tearDown(self):
super(TestCase, self).tearDown()
self._reset_flags()
def _reset_flags(self):
for k, group in self.overriden:
CONF.clear_override(k, group=group)
def _clear_attrs(self):
# Delete attributes that don't start with _ so they don't pin
# memory around unnecessarily for the duration of the test
# suite
for key in [k for k in self.__dict__.keys() if k[0] != '_']:
del self.__dict__[key]
def flags(self, **kw):
"""Override flag variables for a test."""
group = kw.pop('group', None)
for k, v in kw.iteritems():
CONF.set_override(k, v, group)
self.overriden.append((k, group))

View File

@@ -17,12 +17,11 @@
# under the License.
from taskflow.persistence.backends import api as b_api
from taskflow import test
from taskflow.tests.unit.persistence import base
import unittest2
class MemoryPersistenceTest(unittest2.TestCase, base.PersistenceTestMixin):
class MemoryPersistenceTest(test.TestCase, base.PersistenceTestMixin):
def _get_backend(self):
return 'memory'

View File

@@ -22,12 +22,11 @@ import tempfile
from taskflow.openstack.common.db.sqlalchemy import session
from taskflow.persistence.backends import api as b_api
from taskflow.persistence.backends.sqlalchemy import migration
from taskflow import test
from taskflow.tests.unit.persistence import base
import unittest2
class SqlPersistenceTest(unittest2.TestCase, base.PersistenceTestMixin):
class SqlPersistenceTest(test.TestCase, base.PersistenceTestMixin):
"""Inherits from the base test and sets up a sqlite temporary db."""
def _get_backend(self):
return 'sqlalchemy'

View File

@@ -16,13 +16,12 @@
# License for the specific language governing permissions and limitations
# under the License.
import unittest2
from taskflow import decorators
from taskflow.patterns import linear_flow
from taskflow import test
class WrapableObjectsTest(unittest2.TestCase):
class WrapableObjectsTest(test.TestCase):
def test_simple_function(self):
values = []

View File

@@ -16,10 +16,9 @@
# License for the specific language governing permissions and limitations
# under the License.
import unittest2
from taskflow import functor_task
from taskflow.patterns import linear_flow
from taskflow import test
def add(a, b):
@@ -42,7 +41,7 @@ class BunchOfFunctions(object):
raise RuntimeError('Woot!')
class FunctorTaskTest(unittest2.TestCase):
class FunctorTaskTest(test.TestCase):
def test_simple(self):
task = functor_task.FunctorTask(add)

View File

@@ -17,17 +17,16 @@
# under the License.
import collections
import unittest2
from taskflow import decorators
from taskflow import exceptions as excp
from taskflow import states
from taskflow.patterns import graph_flow as gw
from taskflow import states
from taskflow import test
from taskflow.tests import utils
class GraphFlowTest(unittest2.TestCase):
class GraphFlowTest(test.TestCase):
def test_reverting_flow(self):
flo = gw.Flow("test-flow")
reverted = []

View File

@@ -16,19 +16,16 @@
# License for the specific language governing permissions and limitations
# under the License.
import unittest2
from taskflow import decorators
from taskflow import exceptions as exc
from taskflow import states
from taskflow import test
from taskflow.patterns import linear_flow as lw
from taskflow.patterns.resumption import logbook as lr
from taskflow.persistence.backends import memory
from taskflow.tests import utils
class LinearFlowTest(unittest2.TestCase):
class LinearFlowTest(test.TestCase):
def make_reverting_task(self, token, blowup=False):
def do_revert(context, *args, **kwargs):
@@ -210,35 +207,34 @@ class LinearFlowTest(unittest2.TestCase):
wf.reset()
wf.run({})
@unittest2.skip('')
def test_interrupt_flow(self):
wf = lw.Flow("the-int-action")
# If we interrupt we need to know how to resume so attach the needed
# parts to do that...
tracker = lr.Resumption(memory.MemoryLogBook())
tracker.record_for(wf)
wf.resumer = tracker
wf.add(self.make_reverting_task(1))
wf.add(self.make_interrupt_task(wf))
wf.add(self.make_reverting_task(2))
self.assertEquals(states.PENDING, wf.state)
context = {}
wf.run(context)
# Interrupt should have been triggered after task 1
self.assertEquals(1, len(context))
self.assertEquals(states.INTERRUPTED, wf.state)
# And now reset and resume.
wf.reset()
tracker.record_for(wf)
wf.resumer = tracker
self.assertEquals(states.PENDING, wf.state)
wf.run(context)
self.assertEquals(2, len(context))
# def test_interrupt_flow(self):
# wf = lw.Flow("the-int-action")
#
# # If we interrupt we need to know how to resume so attach the needed
# # parts to do that...
# tracker = lr.Resumption(memory.MemoryLogBook())
# tracker.record_for(wf)
# wf.resumer = tracker
#
# wf.add(self.make_reverting_task(1))
# wf.add(self.make_interrupt_task(wf))
# wf.add(self.make_reverting_task(2))
#
# self.assertEquals(states.PENDING, wf.state)
# context = {}
# wf.run(context)
#
# # Interrupt should have been triggered after task 1
# self.assertEquals(1, len(context))
# self.assertEquals(states.INTERRUPTED, wf.state)
#
# # And now reset and resume.
# wf.reset()
# tracker.record_for(wf)
# wf.resumer = tracker
# self.assertEquals(states.PENDING, wf.state)
# wf.run(context)
# self.assertEquals(2, len(context))
def test_parent_reverting_flow(self):
happy_wf = lw.Flow("the-happy-action")

View File

@@ -18,13 +18,13 @@
import threading
import time
import unittest2
from taskflow import decorators
from taskflow import exceptions as excp
from taskflow import states
from taskflow.patterns import threaded_flow as tf
from taskflow import test
from taskflow.tests import utils
@@ -35,7 +35,7 @@ def _find_idx(what, search_where):
return -1
class ThreadedFlowTest(unittest2.TestCase):
class ThreadedFlowTest(test.TestCase):
def _make_tracking_flow(self, name):
notify_lock = threading.RLock()
flo = tf.Flow(name)

View File

@@ -17,12 +17,12 @@
# under the License.
import functools
import unittest
from taskflow import test
from taskflow import utils
class UtilTest(unittest.TestCase):
class UtilTest(test.TestCase):
def test_rollback_accum(self):
context = {}