intermediate commit

This commit is contained in:
termie
2011-07-13 17:10:05 -07:00
parent dda978611d
commit 6b2c9d2b84
9 changed files with 132 additions and 183 deletions

View File

@@ -256,25 +256,26 @@ def check_openstackx(f):
def compute_api(request): def compute_api(request):
compute = openstack.compute.Compute( compute = openstack.compute.Compute(
auth_token=request.session['token'], auth_token=request.user.token,
management_url=url_for(request, 'nova')) management_url=url_for(request, 'nova'))
# this below hack is necessary to make the jacobian compute client work # this below hack is necessary to make the jacobian compute client work
# TODO(mgius): It looks like this is unused now? # TODO(mgius): It looks like this is unused now?
compute.client.auth_token = request.session['token'] compute.client.auth_token = request.user.token
compute.client.management_url = url_for(request, 'nova') compute.client.management_url = url_for(request, 'nova')
LOG.debug('compute_api connection created using token "%s"' LOG.debug('compute_api connection created using token "%s"'
' and url "%s"' % ' and url "%s"' %
(request.session['token'], url_for(request, 'nova'))) (request.user.token, url_for(request, 'nova')))
return compute return compute
def account_api(request): def account_api(request):
LOG.error(dir(request))
LOG.debug('account_api connection created using token "%s"' LOG.debug('account_api connection created using token "%s"'
' and url "%s"' % ' and url "%s"' %
(request.session['token'], (request.user.token,
url_for(request, 'identity', True))) url_for(request, 'identity', True)))
return openstackx.extras.Account( return openstackx.extras.Account(
auth_token=request.session['token'], auth_token=request.user.token,
management_url=url_for(request, 'identity', True)) management_url=url_for(request, 'identity', True))
@@ -288,16 +289,16 @@ def glance_api(request):
def admin_api(request): def admin_api(request):
LOG.debug('admin_api connection created using token "%s"' LOG.debug('admin_api connection created using token "%s"'
' and url "%s"' % ' and url "%s"' %
(request.session['token'], url_for(request, 'nova', True))) (request.user.token, url_for(request, 'nova', True)))
return openstackx.admin.Admin(auth_token=request.session['token'], return openstackx.admin.Admin(auth_token=request.user.token,
management_url=url_for(request, 'nova', True)) management_url=url_for(request, 'nova', True))
def extras_api(request): def extras_api(request):
LOG.debug('extras_api connection created using token "%s"' LOG.debug('extras_api connection created using token "%s"'
' and url "%s"' % ' and url "%s"' %
(request.session['token'], url_for(request, 'nova'))) (request.user.token, url_for(request, 'nova')))
return openstackx.extras.Extras(auth_token=request.session['token'], return openstackx.extras.Extras(auth_token=request.user.token,
management_url=url_for(request, 'nova')) management_url=url_for(request, 'nova'))
@@ -418,7 +419,7 @@ def service_update(request, name, enabled):
def token_get_tenant(request, tenant_id): def token_get_tenant(request, tenant_id):
tenants = auth_api().tenants.for_token(request.session['token']) tenants = auth_api().tenants.for_token(request.user.token)
for t in tenants: for t in tenants:
if str(t.id) == str(tenant_id): if str(t.id) == str(tenant_id):
return Tenant(t) return Tenant(t)

View File

@@ -146,6 +146,7 @@ def index(request, tenant_id):
@login_required @login_required
def launch(request, tenant_id, image_id): def launch(request, tenant_id, image_id):
LOG.error(dir(request))
def flavorlist(): def flavorlist():
try: try:
fl = api.flavor_list(request) fl = api.flavor_list(request)

View File

@@ -103,10 +103,6 @@ def index(request, tenant_id):
instances = [] instances = []
try: try:
instances = api.server_list(request) instances = api.server_list(request)
for instance in instances:
# FIXME - ported this over, but it is hacky
instance.attrs['image_name'] =\
image_dict.get(int(instance.attrs['image_ref']),{}).get('name')
except api_exceptions.ApiException as e: except api_exceptions.ApiException as e:
LOG.error('Exception in instance index', exc_info=True) LOG.error('Exception in instance index', exc_info=True)
messages.error(request, 'Unable to get instance list: %s' % e.message) messages.error(request, 'Unable to get instance list: %s' % e.message)

View File

@@ -0,0 +1,75 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
from django import http
from django import shortcuts
from django import test
from django.conf import settings
import mox
from django_openstack.middleware import keystone
def fake_render_to_response(template_name, context, context_instance=None,
mimetype='text/html'):
"""Replacement for render_to_response so that views can be tested
without having to stub out templates that belong in the frontend
implementation.
Should be able to be tested using the django unit test assertions like a
normal render_to_response return value can be.
"""
class Template(object):
def __init__(self, name):
self.name = name
if context_instance is None:
context_instance = django_template.Context(context)
else:
context_instance.update(context)
resp = http.HttpResponse()
template = Template(template_name)
resp.write('<html><body><p>'
'This is a fake httpresponse for testing purposes only'
'</p></body></html>')
# Allows django.test.client to populate fields on the response object
test.signals.template_rendered.send(template, template=template,
context=context_instance)
return resp
class TestCase(test.TestCase):
TEST_PROJECT = 'test'
TEST_REGION = 'test'
TEST_STAFF_USER = 'staffUser'
TEST_TENANT = 'aTenant'
TEST_TOKEN = 'aToken'
TEST_USER = 'test'
def setUp(self):
self.mox = mox.Mox()
self._real_render_to_response = shortcuts.render_to_response
shortcuts.render_to_response = fake_render_to_response
self._real_get_user_from_request = keystone.get_user_from_request
self.setActiveUser(self.TEST_TOKEN, self.TEST_USER, self.TEST_TENANT,
True)
self.request = http.HttpRequest()
keystone.AuthenticationMiddleware().process_request(self.request)
def tearDown(self):
self.mox.UnsetStubs()
shortcuts.render_to_response = self._real_render_to_response
keystone.get_user_from_request = self._real_get_user_from_request
def assertRedirectsNoFollow(self, response, expected_url):
self.assertEqual(response._headers['location'],
('Location', settings.TESTSERVER + expected_url))
self.assertEqual(response.status_code, 302)
def setActiveUser(self, token, username, tenant, is_admin):
keystone.get_user_from_request = \
lambda x: keystone.User(token, username, tenant, is_admin)

View File

@@ -24,7 +24,6 @@ import json
import mox import mox
from django import http from django import http
from django import test
from django.conf import settings from django.conf import settings
from django_openstack import api from django_openstack import api
from glance import client as glance_client from glance import client as glance_client
@@ -35,6 +34,10 @@ from openstackx import auth as OSAuth
from openstackx import extras as OSExtras from openstackx import extras as OSExtras
from django_openstack import test
from django_openstack.middleware import keystone
TEST_CONSOLE_KIND = 'vnc' TEST_CONSOLE_KIND = 'vnc'
TEST_EMAIL = 'test@test.com' TEST_EMAIL = 'test@test.com'
TEST_HOSTNAME = 'hostname' TEST_HOSTNAME = 'hostname'
@@ -228,7 +231,7 @@ class ServerWrapperTests(test.TestCase):
IMAGE_REF = '3' IMAGE_REF = '3'
def setUp(self): def setUp(self):
self.mox = mox.Mox() super(ServerWrapperTests, self).setUp()
# these are all objects "fetched" from the api # these are all objects "fetched" from the api
self.inner_attrs = {'host': self.HOST} self.inner_attrs = {'host': self.HOST}
@@ -236,10 +239,7 @@ class ServerWrapperTests(test.TestCase):
self.inner_server = Server(self.ID, self.IMAGE_REF, self.inner_attrs) self.inner_server = Server(self.ID, self.IMAGE_REF, self.inner_attrs)
self.inner_server_no_attrs = Server(self.ID, self.IMAGE_REF) self.inner_server_no_attrs = Server(self.ID, self.IMAGE_REF)
self.request = self.mox.CreateMock(http.HttpRequest) #self.request = self.mox.CreateMock(http.HttpRequest)
def tearDown(self):
self.mox.UnsetStubs()
def test_get_attrs(self): def test_get_attrs(self):
server = api.Server(self.inner_server, self.request) server = api.Server(self.inner_server, self.request)
@@ -284,13 +284,6 @@ class ServerWrapperTests(test.TestCase):
class ApiHelperTests(test.TestCase): class ApiHelperTests(test.TestCase):
""" Tests for functions that don't use one of the api objects """ """ Tests for functions that don't use one of the api objects """
def setUp(self):
self.mox = mox.Mox()
self.request = http.HttpRequest()
self.request.session = dict()
def tearDown(self):
self.mox.UnsetStubs()
def test_url_for(self): def test_url_for(self):
GLANCE_URL = 'http://glance/glanceapi/' GLANCE_URL = 'http://glance/glanceapi/'
@@ -422,15 +415,6 @@ class ApiHelperTests(test.TestCase):
class AccountApiTests(test.TestCase): class AccountApiTests(test.TestCase):
def setUp(self):
self.mox = mox.Mox()
self.request = http.HttpRequest()
self.request.session = dict()
self.request.session['token'] = TEST_TOKEN
def tearDown(self):
self.mox.UnsetStubs()
def stub_account_api(self): def stub_account_api(self):
self.mox.StubOutWithMock(api, 'account_api') self.mox.StubOutWithMock(api, 'account_api')
account_api = self.mox.CreateMock(OSExtras.Account) account_api = self.mox.CreateMock(OSExtras.Account)
@@ -537,7 +521,7 @@ class AccountApiTests(test.TestCase):
self.mox.ReplayAll() self.mox.ReplayAll()
ret_val = api.user_create(self.request, TEST_USERNAME, TEST_EMAIL, ret_val = api.user_create(self.request, TEST_USERNAME, TEST_EMAIL,
TEST_PASSWORD, TEST_TENANT_ID) TEST_PASSWORD, TEST_TENANT_ID, True)
self.assertIsInstance(ret_val, api.User) self.assertIsInstance(ret_val, api.User)
self.assertEqual(ret_val._apiresource, TEST_RETURN) self.assertEqual(ret_val._apiresource, TEST_RETURN)
@@ -641,14 +625,15 @@ class AccountApiTests(test.TestCase):
class AdminApiTests(test.TestCase): class AdminApiTests(test.TestCase):
def setUp(self): #def setUp(self):
self.mox = mox.Mox() # super(AdminApiTests, self).setUp()
self.request = http.HttpRequest() # self.request = http.HttpRequest()
self.request.session = dict() # keystone.AuthenticationMiddleware().process_request(self.request)
self.request.session['token'] = TEST_TOKEN # #self.request.session = dict()
# #self.request.session['token'] = TEST_TOKEN
def tearDown(self): #def tearDown(self):
self.mox.UnsetStubs() # super(AdminApiTests, self).tearDown()
def stub_admin_api(self, count=1): def stub_admin_api(self, count=1):
self.mox.StubOutWithMock(api, 'admin_api') self.mox.StubOutWithMock(api, 'admin_api')
@@ -764,12 +749,6 @@ class AdminApiTests(test.TestCase):
class AuthApiTests(test.TestCase): class AuthApiTests(test.TestCase):
def setUp(self):
self.mox = mox.Mox()
def tearDown(self):
self.mox.UnsetStubs()
def test_get_auth_api(self): def test_get_auth_api(self):
settings.OPENSTACK_KEYSTONE_URL = TEST_URL settings.OPENSTACK_KEYSTONE_URL = TEST_URL
self.mox.StubOutClassWithMocks(OSAuth, 'Auth') self.mox.StubOutClassWithMocks(OSAuth, 'Auth')
@@ -885,15 +864,6 @@ class AuthApiTests(test.TestCase):
class ComputeApiTests(test.TestCase): class ComputeApiTests(test.TestCase):
def setUp(self):
self.mox = mox.Mox()
self.request = http.HttpRequest()
self.request.session = {}
self.request.session['token'] = TEST_TOKEN
def tearDown(self):
self.mox.UnsetStubs()
def stub_compute_api(self, count=1): def stub_compute_api(self, count=1):
self.mox.StubOutWithMock(api, 'compute_api') self.mox.StubOutWithMock(api, 'compute_api')
compute_api = self.mox.CreateMock(OSCompute.Compute) compute_api = self.mox.CreateMock(OSCompute.Compute)
@@ -1002,15 +972,6 @@ class ComputeApiTests(test.TestCase):
class ExtrasApiTests(test.TestCase): class ExtrasApiTests(test.TestCase):
def setUp(self):
self.mox = mox.Mox()
self.request = http.HttpRequest()
self.request.session = dict()
self.request.session['token'] = TEST_TOKEN
def tearDown(self):
self.mox.UnsetStubs()
def stub_extras_api(self, count=1): def stub_extras_api(self, count=1):
self.mox.StubOutWithMock(api, 'extras_api') self.mox.StubOutWithMock(api, 'extras_api')
extras_api = self.mox.CreateMock(OSExtras.Extras) extras_api = self.mox.CreateMock(OSExtras.Extras)
@@ -1197,16 +1158,6 @@ class ExtrasApiTests(test.TestCase):
class GlanceApiTests(test.TestCase): class GlanceApiTests(test.TestCase):
def setUp(self):
self.mox = mox.Mox()
self.request = http.HttpRequest()
self.request.session = dict()
self.request.session['token'] = TEST_TOKEN
def tearDown(self):
self.mox.UnsetStubs()
def stub_glance_api(self, count=1): def stub_glance_api(self, count=1):
self.mox.StubOutWithMock(api, 'glance_api') self.mox.StubOutWithMock(api, 'glance_api')
glance_api = self.mox.CreateMock(glance_client.Client) glance_api = self.mox.CreateMock(glance_client.Client)
@@ -1313,12 +1264,6 @@ class GlanceApiTests(test.TestCase):
class SwiftApiTests(test.TestCase): class SwiftApiTests(test.TestCase):
def setUp(self):
self.mox = mox.Mox()
def tearDown(self):
self.mox.UnsetStubs()
def stub_swift_api(self, count=1): def stub_swift_api(self, count=1):
self.mox.StubOutWithMock(api, 'swift_api') self.mox.StubOutWithMock(api, 'swift_api')
swift_api = self.mox.CreateMock(cloudfiles.connection.Connection) swift_api = self.mox.CreateMock(cloudfiles.connection.Connection)

View File

@@ -22,14 +22,7 @@
Base classes for view based unit tests. Base classes for view based unit tests.
""" """
import mox from django_openstack import test
from django import http
from django import shortcuts
from django import template as django_template
from django import test
from django.conf import settings
from django_openstack.middleware import keystone
class Object(object): class Object(object):
@@ -37,70 +30,5 @@ class Object(object):
pass pass
def fake_render_to_response(template_name, context, context_instance=None,
mimetype='text/html'):
"""Replacement for render_to_response so that views can be tested
without having to stub out templates that belong in the frontend
implementation.
Should be able to be tested using the django unit test assertions like a
normal render_to_response return value can be.
"""
class Template(object):
def __init__(self, name):
self.name = name
if context_instance is None:
context_instance = django_template.Context(context)
else:
context_instance.update(context)
resp = http.HttpResponse()
template = Template(template_name)
resp.write('<html><body><p>'
'This is a fake httpresponse for testing purposes only'
'</p></body></html>')
# Allows django.test.client to populate fields on the response object
test.signals.template_rendered.send(template, template=template,
context=context_instance)
return resp
class BaseViewTests(test.TestCase): class BaseViewTests(test.TestCase):
TEST_PROJECT = 'test' pass
TEST_REGION = 'test'
TEST_STAFF_USER = 'staffUser'
TEST_TENANT = 'aTenant'
TEST_TOKEN = 'aToken'
TEST_USER = 'test'
@classmethod
def setUpClass(cls):
cls._real_render_to_response = shortcuts.render_to_response
shortcuts.render_to_response = fake_render_to_response
cls._real_get_user_from_request = keystone.get_user_from_request
@classmethod
def tearDownClass(cls):
shortcuts.render_to_response = cls._real_render_to_response
keystone.get_user_from_request = cls._real_get_user_from_request
def setUp(self):
self.mox = mox.Mox()
self.setActiveUser(self.TEST_TOKEN, self.TEST_USER, self.TEST_TENANT,
True)
def tearDown(self):
self.mox.UnsetStubs()
def assertRedirectsNoFollow(self, response, expected_url):
self.assertEqual(response._headers['location'],
('Location', settings.TESTSERVER + expected_url))
self.assertEqual(response.status_code, 302)
def setActiveUser(self, token, username, tenant, is_admin):
keystone.get_user_from_request = \
lambda x: keystone.User(token, username, tenant, is_admin)

View File

@@ -1,4 +1,6 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4 # vim: tabstop=4 shiftwidth=4 softtabstop=4
import logging
from django import http from django import http
from django.contrib import messages from django.contrib import messages
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
@@ -287,43 +289,42 @@ class ImageViewTests(base.BaseViewTests):
} }
self.mox.StubOutWithMock(api, 'image_get') self.mox.StubOutWithMock(api, 'image_get')
api.image_get(IsA(http.HttpRequest), api.image_get(IgnoreArg(),
IMAGE_ID).AndReturn(self.visibleImage) IMAGE_ID).AndReturn(self.visibleImage)
self.mox.StubOutWithMock(api, 'token_get_tenant') self.mox.StubOutWithMock(api, 'token_get_tenant')
api.token_get_tenant(IsA(http.HttpRequest), api.token_get_tenant(IgnoreArg(),
self.TEST_TENANT).AndReturn(self.TEST_TENANT) self.TEST_TENANT).AndReturn(self.TEST_TENANT)
self.mox.StubOutWithMock(api, 'flavor_list') self.mox.StubOutWithMock(api, 'flavor_list')
api.flavor_list(IsA(http.HttpRequest)).AndReturn(self.flavors) api.flavor_list(IgnoreArg()).AndReturn(self.flavors)
self.mox.StubOutWithMock(api, 'keypair_list') self.mox.StubOutWithMock(api, 'keypair_list')
api.keypair_list(IsA(http.HttpRequest)).AndReturn(self.keypairs) api.keypair_list(IgnoreArg()).AndReturn(self.keypairs)
# called again by the form # called again by the form
api.image_get(IsA(http.HttpRequest), api.image_get(IgnoreArg(),
IMAGE_ID).AndReturn(self.visibleImage) IMAGE_ID).AndReturn(self.visibleImage)
self.mox.StubOutWithMock(api, 'flavor_get') self.mox.StubOutWithMock(api, 'flavor_get')
api.flavor_get(IsA(http.HttpRequest), api.flavor_get(IgnoreArg(),
IsA(unicode)).AndReturn(self.flavors[0]) IsA(unicode)).AndReturn(self.flavors[0])
self.mox.StubOutWithMock(api, 'server_create') self.mox.StubOutWithMock(api, 'server_create')
exception = api_exceptions.ApiException('apiException') exception = api_exceptions.ApiException('apiException')
api.server_create(IsA(http.HttpRequest), SERVER_NAME, api.server_create(IgnoreArg(), SERVER_NAME,
self.visibleImage, self.flavors[0], self.visibleImage, self.flavors[0],
user_data=USER_DATA, user_data=USER_DATA,
key_name=KEY_NAME).AndRaise(exception) key_name=KEY_NAME).AndRaise(exception)
self.mox.StubOutWithMock(messages, 'error') self.mox.StubOutWithMock(messages, 'error')
messages.error(IsA(http.HttpRequest), IsA(str)) messages.error(IgnoreArg(), IsA(unicode))
self.mox.ReplayAll() self.mox.ReplayAll()
url = reverse('dash_images_launch',
res = self.client.post(reverse('dash_images_launch', args=[self.TEST_TENANT, IMAGE_ID])
args=[self.TEST_TENANT, IMAGE_ID]), res = self.client.post(url, form_data)
form_data)
self.assertTemplateUsed(res, 'dash_launch.html') self.assertTemplateUsed(res, 'dash_launch.html')

View File

@@ -8,7 +8,7 @@ from django_openstack import api
from django_openstack import utils from django_openstack import utils
from django_openstack.tests.view_tests import base from django_openstack.tests.view_tests import base
from openstackx.api import exceptions as api_exceptions from openstackx.api import exceptions as api_exceptions
from mox import IsA from mox import IsA, IgnoreArg
class InstanceViewTests(base.BaseViewTests): class InstanceViewTests(base.BaseViewTests):
@@ -238,8 +238,9 @@ class InstanceViewTests(base.BaseViewTests):
console_mock.output = CONSOLE_OUTPUT console_mock.output = CONSOLE_OUTPUT
self.mox.StubOutWithMock(api, 'console_create') self.mox.StubOutWithMock(api, 'console_create')
api.console_create(IsA(http.HttpRequest), api.console_create(IgnoreArg(),
unicode(INSTANCE_ID)).AndReturn(console_mock) unicode(INSTANCE_ID),
IgnoreArg()).AndReturn(console_mock)
self.mox.ReplayAll() self.mox.ReplayAll()
@@ -258,11 +259,12 @@ class InstanceViewTests(base.BaseViewTests):
message='apiException') message='apiException')
self.mox.StubOutWithMock(api, 'console_create') self.mox.StubOutWithMock(api, 'console_create')
api.console_create(IsA(http.HttpRequest), api.console_create(IgnoreArg(),
unicode(INSTANCE_ID)).AndRaise(exception) unicode(INSTANCE_ID),
IgnoreArg()).AndRaise(exception)
self.mox.StubOutWithMock(messages, 'error') self.mox.StubOutWithMock(messages, 'error')
messages.error(IsA(http.HttpRequest), IsA(unicode)) messages.error(IgnoreArg(), IsA(unicode))
self.mox.ReplayAll() self.mox.ReplayAll()
@@ -282,7 +284,7 @@ class InstanceViewTests(base.BaseViewTests):
console_mock.output = CONSOLE_OUTPUT console_mock.output = CONSOLE_OUTPUT
self.mox.StubOutWithMock(api, 'console_create') self.mox.StubOutWithMock(api, 'console_create')
api.console_create(IsA(http.HttpRequest), api.console_create(IgnoreArg(),
unicode(INSTANCE_ID), unicode(INSTANCE_ID),
'vnc').AndReturn(console_mock) 'vnc').AndReturn(console_mock)

View File

@@ -7,12 +7,12 @@ bin/test
OPENSTACK_RESULT=$? OPENSTACK_RESULT=$?
cd ../openstack-dashboard #cd ../openstack-dashboard
python tools/install_venv.py #python tools/install_venv.py
cp local/local_settings.py.example local/local_settings.py #cp local/local_settings.py.example local/local_settings.py
tools/with_venv.sh dashboard/manage.py test #tools/with_venv.sh dashboard/manage.py test
DASHBOARD_RESULT=$? #DASHBOARD_RESULT=$?
exit $(($OPENSTACK_RESULT || $DASHBOARD_RESULT)) #exit $(($OPENSTACK_RESULT || $DASHBOARD_RESULT))