tests for tenant_setup actions
mocking for neutronclient, tests for network setup and adding admin to project. Change-Id: Ia775822ee6a08068bfb1e81a99e2186bbb5a8d8c
This commit is contained in:
parent
7d4af7521b
commit
497e851911
@ -13,5 +13,282 @@
|
||||
# under the License.
|
||||
|
||||
from django.test import TestCase
|
||||
from api_v1.models import Registration
|
||||
from api_v1.tests import FakeManager, setup_temp_cache
|
||||
from api_v1 import tests
|
||||
from tenant_setup.models import DefaultProjectResources, AddAdminToProject
|
||||
import mock
|
||||
|
||||
# Create your tests here.
|
||||
|
||||
neutron_cache = {}
|
||||
|
||||
|
||||
class FakeNeutronClient(object):
|
||||
|
||||
def create_network(self, body):
|
||||
global neutron_cache
|
||||
net = {'network': {'id': 'net_id_%s' % neutron_cache['i'],
|
||||
'body': body}}
|
||||
neutron_cache['networks'][net['network']['id']] = net
|
||||
neutron_cache['i'] += 1
|
||||
return net
|
||||
|
||||
def create_subnet(self, body):
|
||||
global neutron_cache
|
||||
subnet = {'subnet': {'id': 'subnet_id_%s' % neutron_cache['i'],
|
||||
'body': body}}
|
||||
neutron_cache['subnets'][subnet['subnet']['id']] = subnet
|
||||
neutron_cache['i'] += 1
|
||||
return subnet
|
||||
|
||||
def create_router(self, body):
|
||||
global neutron_cache
|
||||
router = {'router': {'id': 'router_id_%s' % neutron_cache['i'],
|
||||
'body': body}}
|
||||
neutron_cache['routers'][router['router']['id']] = router
|
||||
neutron_cache['i'] += 1
|
||||
return router
|
||||
|
||||
def add_interface_router(self, router_id, body):
|
||||
global neutron_cache
|
||||
router = neutron_cache['routers'][router_id]
|
||||
router['router']['interface'] = body
|
||||
return router
|
||||
|
||||
|
||||
def setup_neutron_cache():
|
||||
global neutron_cache
|
||||
neutron_cache = {
|
||||
'i': 0,
|
||||
'networks': {},
|
||||
'subnets': {},
|
||||
'routers': {}
|
||||
}
|
||||
|
||||
|
||||
def get_fake_neutron():
|
||||
return FakeNeutronClient()
|
||||
|
||||
|
||||
class TenantSetupActionTests(TestCase):
|
||||
|
||||
@mock.patch('tenant_setup.models.IdentityManager', FakeManager)
|
||||
@mock.patch('tenant_setup.models.openstack_clients.get_neutronclient',
|
||||
get_fake_neutron)
|
||||
def test_resource_setup(self):
|
||||
"""
|
||||
Base case, setup resources, no issues.
|
||||
"""
|
||||
setup_neutron_cache()
|
||||
registration = Registration.objects.create(
|
||||
reg_ip="0.0.0.0", keystone_user={'roles': ['admin']})
|
||||
|
||||
registration.cache = {'project_id': "1"}
|
||||
|
||||
data = {
|
||||
'setup_resources': True,
|
||||
}
|
||||
|
||||
action = DefaultProjectResources(data, registration=registration,
|
||||
order=1)
|
||||
|
||||
action.pre_approve()
|
||||
self.assertEquals(action.valid, True)
|
||||
|
||||
action.post_approve()
|
||||
self.assertEquals(action.valid, True)
|
||||
|
||||
self.assertEquals(
|
||||
action.action.cache,
|
||||
{'network_id': 'net_id_0',
|
||||
'router_id': 'router_id_2',
|
||||
'subnet_id': 'subnet_id_1'}
|
||||
)
|
||||
|
||||
global neutron_cache
|
||||
self.assertEquals(len(neutron_cache['networks']), 1)
|
||||
self.assertEquals(len(neutron_cache['routers']), 1)
|
||||
self.assertEquals(len(neutron_cache['subnets']), 1)
|
||||
|
||||
@mock.patch('tenant_setup.models.IdentityManager', FakeManager)
|
||||
@mock.patch('tenant_setup.models.openstack_clients.get_neutronclient',
|
||||
get_fake_neutron)
|
||||
def test_resource_setup_no_id(self):
|
||||
"""
|
||||
No project id given, should do nothing.
|
||||
"""
|
||||
setup_neutron_cache()
|
||||
registration = Registration.objects.create(
|
||||
reg_ip="0.0.0.0", keystone_user={'roles': ['admin']})
|
||||
|
||||
data = {
|
||||
'setup_resources': True,
|
||||
}
|
||||
|
||||
action = DefaultProjectResources(data, registration=registration,
|
||||
order=1)
|
||||
|
||||
action.pre_approve()
|
||||
self.assertEquals(action.valid, True)
|
||||
|
||||
action.post_approve()
|
||||
self.assertEquals(action.valid, False)
|
||||
|
||||
self.assertEquals(action.action.cache, {})
|
||||
|
||||
global neutron_cache
|
||||
self.assertEquals(len(neutron_cache['networks']), 0)
|
||||
self.assertEquals(len(neutron_cache['routers']), 0)
|
||||
self.assertEquals(len(neutron_cache['subnets']), 0)
|
||||
|
||||
@mock.patch('tenant_setup.models.IdentityManager', FakeManager)
|
||||
@mock.patch('tenant_setup.models.openstack_clients.get_neutronclient',
|
||||
get_fake_neutron)
|
||||
def test_resource_setup_no_setup(self):
|
||||
"""
|
||||
Told not to setup, should do nothing.
|
||||
"""
|
||||
setup_neutron_cache()
|
||||
registration = Registration.objects.create(
|
||||
reg_ip="0.0.0.0", keystone_user={'roles': ['admin']})
|
||||
|
||||
data = {
|
||||
'setup_resources': False,
|
||||
}
|
||||
|
||||
registration.cache = {'project_id': "1"}
|
||||
|
||||
action = DefaultProjectResources(data, registration=registration,
|
||||
order=1)
|
||||
|
||||
action.pre_approve()
|
||||
self.assertEquals(action.valid, True)
|
||||
|
||||
action.post_approve()
|
||||
self.assertEquals(action.valid, True)
|
||||
|
||||
self.assertEquals(action.action.cache, {})
|
||||
|
||||
global neutron_cache
|
||||
self.assertEquals(len(neutron_cache['networks']), 0)
|
||||
self.assertEquals(len(neutron_cache['routers']), 0)
|
||||
self.assertEquals(len(neutron_cache['subnets']), 0)
|
||||
|
||||
@mock.patch('tenant_setup.models.IdentityManager', FakeManager)
|
||||
@mock.patch('tenant_setup.models.openstack_clients.get_neutronclient',
|
||||
get_fake_neutron)
|
||||
def test_resource_setup_fail(self):
|
||||
"""
|
||||
Should fail, but on re_approve will continue where it left off.
|
||||
"""
|
||||
setup_neutron_cache()
|
||||
global neutron_cache
|
||||
registration = Registration.objects.create(
|
||||
reg_ip="0.0.0.0", keystone_user={'roles': ['admin']})
|
||||
|
||||
data = {
|
||||
'setup_resources': True,
|
||||
}
|
||||
|
||||
registration.cache = {'project_id': "1"}
|
||||
|
||||
action = DefaultProjectResources(data, registration=registration,
|
||||
order=1)
|
||||
|
||||
action.pre_approve()
|
||||
self.assertEquals(action.valid, True)
|
||||
|
||||
neutron_cache['routers'] = []
|
||||
|
||||
try:
|
||||
action.post_approve()
|
||||
self.fail("Shouldn't get here.")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
self.assertEquals(
|
||||
action.action.cache,
|
||||
{'network_id': 'net_id_0',
|
||||
'subnet_id': 'subnet_id_1'}
|
||||
)
|
||||
|
||||
self.assertEquals(len(neutron_cache['networks']), 1)
|
||||
self.assertEquals(len(neutron_cache['subnets']), 1)
|
||||
self.assertEquals(len(neutron_cache['routers']), 0)
|
||||
|
||||
neutron_cache['routers'] = {}
|
||||
|
||||
action.post_approve()
|
||||
|
||||
self.assertEquals(
|
||||
action.action.cache,
|
||||
{'network_id': 'net_id_0',
|
||||
'router_id': 'router_id_2',
|
||||
'subnet_id': 'subnet_id_1'}
|
||||
)
|
||||
|
||||
self.assertEquals(len(neutron_cache['networks']), 1)
|
||||
self.assertEquals(len(neutron_cache['routers']), 1)
|
||||
self.assertEquals(len(neutron_cache['subnets']), 1)
|
||||
|
||||
@mock.patch('tenant_setup.models.IdentityManager', FakeManager)
|
||||
def test_add_admin(self):
|
||||
"""
|
||||
Base case, adds admin user with admin role to project.
|
||||
"""
|
||||
project = mock.Mock()
|
||||
project.id = 'test_project_id'
|
||||
project.name = 'test_project'
|
||||
project.roles = {}
|
||||
|
||||
setup_temp_cache({'test_project': project}, {})
|
||||
|
||||
registration = Registration.objects.create(
|
||||
reg_ip="0.0.0.0", keystone_user={'roles': ['admin']})
|
||||
|
||||
registration.cache = {'project_id': "test_project_id"}
|
||||
|
||||
action = AddAdminToProject({}, registration=registration, order=1)
|
||||
|
||||
action.pre_approve()
|
||||
self.assertEquals(action.valid, True)
|
||||
|
||||
action.post_approve()
|
||||
self.assertEquals(action.valid, True)
|
||||
|
||||
project = tests.temp_cache['projects']['test_project']
|
||||
self.assertEquals(project.roles['admin'], ['admin'])
|
||||
|
||||
@mock.patch('tenant_setup.models.IdentityManager', FakeManager)
|
||||
def test_add_admin_reapprove(self):
|
||||
"""
|
||||
Ensure nothing happens or changes if rerun of approve.
|
||||
"""
|
||||
project = mock.Mock()
|
||||
project.id = 'test_project_id'
|
||||
project.name = 'test_project'
|
||||
project.roles = {}
|
||||
|
||||
setup_temp_cache({'test_project': project}, {})
|
||||
|
||||
registration = Registration.objects.create(
|
||||
reg_ip="0.0.0.0", keystone_user={'roles': ['admin']})
|
||||
|
||||
registration.cache = {'project_id': "test_project_id"}
|
||||
|
||||
action = AddAdminToProject({}, registration=registration, order=1)
|
||||
|
||||
action.pre_approve()
|
||||
self.assertEquals(action.valid, True)
|
||||
|
||||
action.post_approve()
|
||||
self.assertEquals(action.valid, True)
|
||||
|
||||
project = tests.temp_cache['projects']['test_project']
|
||||
self.assertEquals(project.roles['admin'], ['admin'])
|
||||
|
||||
action.post_approve()
|
||||
self.assertEquals(action.valid, True)
|
||||
|
||||
project = tests.temp_cache['projects']['test_project']
|
||||
self.assertEquals(project.roles['admin'], ['admin'])
|
||||
|
Loading…
x
Reference in New Issue
Block a user