Cleans up the unit tests that are meant to be run with nosetests

* Renames all test modules to start with test_ so that nosetests does
not need to be run with the --all-modules flag in order to pick them up
* Renames test_helper to fakes and removes imports in unit tests that
did not reference the fakes
* Adds nose to pip-requires so that run_tests.sh -V will install nose
into the virtualenv instead of having to manually install it after running
into import errors :)
This commit is contained in:
jaypipes@gmail.com
2010-10-01 14:02:51 -04:00
parent 4d13a85544
commit bf22bbd2d4
12 changed files with 63 additions and 43 deletions

View File

@@ -27,7 +27,8 @@ import webob.dec
import nova.exception
from nova import api
from nova.tests.api.test_helper import *
from nova.tests.api.fakes import APIStub
class Test(unittest.TestCase):

View File

@@ -19,7 +19,7 @@ import unittest
from nova.api.rackspace import limited
from nova.api.rackspace import RateLimitingMiddleware
from nova.tests.api.test_helper import *
from nova.tests.api.fakes import APIStub
from webob import Request
@@ -82,7 +82,7 @@ class RateLimitingMiddlewareTest(unittest.TestCase):
class LimiterTest(unittest.TestCase):
def testLimiter(self):
def test_limiter(self):
items = range(2000)
req = Request.blank('/')
self.assertEqual(limited(items, req), items[ :1000])

View File

@@ -12,11 +12,14 @@ import nova.api.rackspace._id_translator
from nova.image import service
from nova.wsgi import Router
FLAGS = flags.FLAGS
class Context(object):
pass
class FakeRouter(Router):
def __init__(self):
pass
@@ -28,12 +31,14 @@ class FakeRouter(Router):
res.headers['X-Test-Success'] = 'True'
return res
def fake_auth_init(self):
self.db = FakeAuthDatabase()
self.context = Context()
self.auth = FakeAuthManager()
self.host = 'foo'
@webob.dec.wsgify
def fake_wsgi(self, req):
req.environ['nova.context'] = dict(user=dict(id=1))
@@ -41,18 +46,21 @@ def fake_wsgi(self, req):
req.environ['inst_dict'] = json.loads(req.body)
return self.application
def stub_out_key_pair_funcs(stubs):
def key_pair(context, user_id):
return [dict(name='key', public_key='public_key')]
stubs.Set(nova.db.api, 'key_pair_get_all_by_user',
key_pair)
def stub_out_image_service(stubs):
def fake_image_show(meh, id):
return dict(kernelId=1, ramdiskId=1)
stubs.Set(nova.image.service.LocalImageService, 'show', fake_image_show)
def stub_out_id_translator(stubs):
class FakeTranslator(object):
def __init__(self, id_type, service_name):
@@ -67,6 +75,7 @@ def stub_out_id_translator(stubs):
stubs.Set(nova.api.rackspace._id_translator,
'RackspaceAPIIdTranslator', FakeTranslator)
def stub_out_auth(stubs):
def fake_auth_init(self, app):
self.application = app
@@ -76,6 +85,7 @@ def stub_out_auth(stubs):
stubs.Set(nova.api.rackspace.AuthMiddleware,
'__call__', fake_wsgi)
def stub_out_rate_limiting(stubs):
def fake_rate_init(self, app):
super(nova.api.rackspace.RateLimitingMiddleware, self).__init__(app)
@@ -87,12 +97,14 @@ def stub_out_rate_limiting(stubs):
stubs.Set(nova.api.rackspace.RateLimitingMiddleware,
'__call__', fake_wsgi)
def stub_for_testing(stubs):
def stub_out_networking(stubs):
def get_my_ip():
return '127.0.0.1'
stubs.Set(nova.utils, 'get_my_ip', get_my_ip)
FLAGS.FAKE_subdomain = 'rs'
class FakeAuthDatabase(object):
data = {}
@@ -110,6 +122,7 @@ class FakeAuthDatabase(object):
if FakeAuthDatabase.data.has_key(token['token_hash']):
del FakeAuthDatabase.data['token_hash']
class FakeAuthManager(object):
auth_data = {}
@@ -125,6 +138,7 @@ class FakeAuthManager(object):
def get_user_from_access_key(self, key):
return FakeAuthManager.auth_data.get(key, None)
class FakeRateLimiter(object):
def __init__(self, application):
self.application = application

View File

@@ -8,24 +8,24 @@ import webob.dec
import nova.api
import nova.api.rackspace.auth
from nova import auth
from nova.tests.api.rackspace import test_helper
from nova.tests.api.rackspace import fakes
class Test(unittest.TestCase):
def setUp(self):
self.stubs = stubout.StubOutForTesting()
self.stubs.Set(nova.api.rackspace.auth.BasicApiAuthManager,
'__init__', test_helper.fake_auth_init)
test_helper.FakeAuthManager.auth_data = {}
test_helper.FakeAuthDatabase.data = {}
test_helper.stub_out_rate_limiting(self.stubs)
test_helper.stub_for_testing(self.stubs)
'__init__', fakes.fake_auth_init)
fakes.FakeAuthManager.auth_data = {}
fakes.FakeAuthDatabase.data = {}
fakes.stub_out_rate_limiting(self.stubs)
fakes.stub_out_networking(self.stubs)
def tearDown(self):
self.stubs.UnsetAll()
test_helper.fake_data_store = {}
fakes.fake_data_store = {}
def test_authorize_user(self):
f = test_helper.FakeAuthManager()
f = fakes.FakeAuthManager()
f.add_user('derp', { 'uid': 1, 'name':'herp' } )
req = webob.Request.blank('/v1.0/')
@@ -39,7 +39,7 @@ class Test(unittest.TestCase):
self.assertEqual(result.headers['X-Storage-Url'], "")
def test_authorize_token(self):
f = test_helper.FakeAuthManager()
f = fakes.FakeAuthManager()
f.add_user('derp', { 'uid': 1, 'name':'herp' } )
req = webob.Request.blank('/v1.0/')
@@ -56,7 +56,7 @@ class Test(unittest.TestCase):
token = result.headers['X-Auth-Token']
self.stubs.Set(nova.api.rackspace, 'APIRouter',
test_helper.FakeRouter)
fakes.FakeRouter)
req = webob.Request.blank('/v1.0/fake')
req.headers['X-Auth-Token'] = token
result = req.get_response(nova.api.API())
@@ -74,10 +74,10 @@ class Test(unittest.TestCase):
return { 'token_hash':token_hash,
'created_at':datetime.datetime(1990, 1, 1) }
self.stubs.Set(test_helper.FakeAuthDatabase, 'auth_destroy_token',
self.stubs.Set(fakes.FakeAuthDatabase, 'auth_destroy_token',
destroy_token_mock)
self.stubs.Set(test_helper.FakeAuthDatabase, 'auth_get_token',
self.stubs.Set(fakes.FakeAuthDatabase, 'auth_get_token',
bad_token)
req = webob.Request.blank('/v1.0/')

View File

@@ -16,21 +16,23 @@
# under the License.
import unittest
import stubout
import webob
import nova.api
from nova.api.rackspace import flavors
from nova.tests.api.rackspace import test_helper
from nova.tests.api.test_helper import *
from nova.tests.api.rackspace import fakes
class FlavorsTest(unittest.TestCase):
def setUp(self):
self.stubs = stubout.StubOutForTesting()
test_helper.FakeAuthManager.auth_data = {}
test_helper.FakeAuthDatabase.data = {}
test_helper.stub_for_testing(self.stubs)
test_helper.stub_out_rate_limiting(self.stubs)
test_helper.stub_out_auth(self.stubs)
fakes.FakeAuthManager.auth_data = {}
fakes.FakeAuthDatabase.data = {}
fakes.stub_out_networking(self.stubs)
fakes.stub_out_rate_limiting(self.stubs)
fakes.stub_out_auth(self.stubs)
def tearDown(self):
self.stubs.UnsetAll()

View File

@@ -15,11 +15,12 @@
# License for the specific language governing permissions and limitations
# under the License.
import stubout
import unittest
import stubout
from nova.api.rackspace import images
from nova.tests.api.test_helper import *
class ImagesTest(unittest.TestCase):
def setUp(self):
@@ -36,5 +37,3 @@ class ImagesTest(unittest.TestCase):
def test_create_image(self):
pass

View File

@@ -19,6 +19,7 @@ import json
import unittest
import stubout
import webob
from nova import db
from nova import flags
@@ -27,14 +28,16 @@ from nova.api.rackspace import servers
import nova.db.api
from nova.db.sqlalchemy.models import Instance
import nova.rpc
from nova.tests.api.test_helper import *
from nova.tests.api.rackspace import test_helper
from nova.tests.api.rackspace import fakes
FLAGS = flags.FLAGS
def return_server(context, id):
return stub_instance(id)
def return_servers(context, user_id=1):
return [stub_instance(i, user_id) for i in xrange(5)]
@@ -45,17 +48,18 @@ def stub_instance(id, user_id=1):
user_id=user_id
)
class ServersTest(unittest.TestCase):
def setUp(self):
self.stubs = stubout.StubOutForTesting()
test_helper.FakeAuthManager.auth_data = {}
test_helper.FakeAuthDatabase.data = {}
test_helper.stub_for_testing(self.stubs)
test_helper.stub_out_rate_limiting(self.stubs)
test_helper.stub_out_auth(self.stubs)
test_helper.stub_out_id_translator(self.stubs)
test_helper.stub_out_key_pair_funcs(self.stubs)
test_helper.stub_out_image_service(self.stubs)
fakes.FakeAuthManager.auth_data = {}
fakes.FakeAuthDatabase.data = {}
fakes.stub_out_networking(self.stubs)
fakes.stub_out_rate_limiting(self.stubs)
fakes.stub_out_auth(self.stubs)
fakes.stub_out_id_translator(self.stubs)
fakes.stub_out_key_pair_funcs(self.stubs)
fakes.stub_out_image_service(self.stubs)
self.stubs.Set(nova.db.api, 'instance_get_all', return_servers)
self.stubs.Set(nova.db.api, 'instance_get_by_ec2_id', return_server)
self.stubs.Set(nova.db.api, 'instance_get_all_by_user',
@@ -111,7 +115,7 @@ class ServersTest(unittest.TestCase):
self.stubs.Set(nova.network.manager.FlatManager, 'allocate_fixed_ip',
fake_method)
test_helper.stub_out_id_translator(self.stubs)
fakes.stub_out_id_translator(self.stubs)
body = dict(server=dict(
name='server_test', imageId=2, flavorId=2, metadata={},
personality = {}
@@ -241,5 +245,6 @@ class ServersTest(unittest.TestCase):
self.assertEqual(res.status, '202 Accepted')
self.assertEqual(self.server_delete_called, True)
if __name__ == "__main__":
unittest.main()

View File

@@ -15,11 +15,12 @@
# License for the specific language governing permissions and limitations
# under the License.
import stubout
import unittest
import stubout
from nova.api.rackspace import sharedipgroups
from nova.tests.api.test_helper import *
class SharedIpGroupsTest(unittest.TestCase):
def setUp(self):
@@ -36,6 +37,3 @@ class SharedIpGroupsTest(unittest.TestCase):
def test_delete_shared_ip_group(self):
pass

View File

@@ -20,3 +20,4 @@ zope.interface==3.6.1
mox==0.5.0
-f http://pymox.googlecode.com/files/mox-0.5.0.tar.gz
greenlet==0.3.1
nose