Merge "Convert admin.instances tests into mock"

This commit is contained in:
Zuul 2018-03-18 09:44:39 +00:00 committed by Gerrit Code Review
commit 892607890a

View File

@ -15,10 +15,9 @@
from collections import OrderedDict from collections import OrderedDict
import uuid import uuid
from django import http import mock
from django.urls import reverse
from mox3.mox import IsA from django.urls import reverse
from openstack_dashboard import api from openstack_dashboard import api
from openstack_dashboard.test import helpers as test from openstack_dashboard.test import helpers as test
@ -29,106 +28,102 @@ INDEX_TEMPLATE = 'horizon/common/_data_table_view.html'
class InstanceViewTest(test.BaseAdminViewTests): class InstanceViewTest(test.BaseAdminViewTests):
@test.create_stubs({ @test.create_mocks({
api.nova: ('flavor_list', 'server_list', 'extension_supported',), api.nova: ['flavor_list', 'server_list', 'extension_supported'],
api.keystone: ('tenant_list',), api.keystone: ['tenant_list'],
api.glance: ('image_list_detailed',), api.glance: ['image_list_detailed'],
}) })
def test_index(self): def test_index(self):
servers = self.servers.list() servers = self.servers.list()
flavors = self.flavors.list() self.mock_extension_supported.return_value = True
tenants = self.tenants.list() self.mock_tenant_list.return_value = [self.tenants.list(), False]
images = self.images.list() self.mock_image_list_detailed.return_value = (self.images.list(),
api.nova.extension_supported('AdminActions', IsA(http.HttpRequest)) \ False, False)
.MultipleTimes().AndReturn(True) self.mock_flavor_list.return_value = self.flavors.list()
api.nova.extension_supported('Shelve', IsA(http.HttpRequest)) \ self.mock_server_list.return_value = [servers, False]
.MultipleTimes().AndReturn(True)
api.keystone.tenant_list(IsA(http.HttpRequest)).\
AndReturn([tenants, False])
api.glance.image_list_detailed(IsA(http.HttpRequest))\
.AndReturn((images, False, False))
api.nova.flavor_list(IsA(http.HttpRequest)).AndReturn(flavors)
search_opts = {'marker': None, 'paginate': True, 'all_tenants': True}
api.nova.server_list(IsA(http.HttpRequest),
search_opts=search_opts) \
.AndReturn([servers, False])
self.mox.ReplayAll()
res = self.client.get(INDEX_URL) res = self.client.get(INDEX_URL)
self.assertTemplateUsed(res, INDEX_TEMPLATE) self.assertTemplateUsed(res, INDEX_TEMPLATE)
instances = res.context['table'].data instances = res.context['table'].data
self.assertItemsEqual(instances, servers) self.assertItemsEqual(instances, servers)
@test.create_stubs({ self.mock_extension_supported.assert_has_calls([
api.nova: ('flavor_list', 'flavor_get', 'server_list', mock.call('AdminActions', test.IsHttpRequest()),
'extension_supported',), mock.call('AdminActions', test.IsHttpRequest()),
api.keystone: ('tenant_list',), mock.call('Shelve', test.IsHttpRequest())]*4)
api.glance: ('image_list_detailed',), self.assertEqual(12, self.mock_extension_supported.call_count)
self.mock_tenant_list.assert_called_once_with(test.IsHttpRequest())
self.mock_image_list_detailed.assert_called_once_with(
test.IsHttpRequest())
self.mock_flavor_list.assert_called_once_with(test.IsHttpRequest())
search_opts = {'marker': None, 'paginate': True, 'all_tenants': True}
self.mock_server_list.assert_called_once_with(
test.IsHttpRequest(), search_opts=search_opts)
@test.create_mocks({
api.nova: ['flavor_list', 'flavor_get', 'server_list',
'extension_supported'],
api.keystone: ['tenant_list'],
api.glance: ['image_list_detailed'],
}) })
def test_index_flavor_list_exception(self): def test_index_flavor_list_exception(self):
servers = self.servers.list() servers = self.servers.list()
tenants = self.tenants.list()
flavors = self.flavors.list() flavors = self.flavors.list()
images = self.images.list()
full_flavors = OrderedDict([(f.id, f) for f in flavors]) full_flavors = OrderedDict([(f.id, f) for f in flavors])
search_opts = {'marker': None, 'paginate': True, 'all_tenants': True} self.mock_server_list.return_value = [servers, False]
api.nova.server_list(IsA(http.HttpRequest), self.mock_extension_supported.return_value = True
search_opts=search_opts) \ self.mock_flavor_list.side_effect = self.exceptions.nova
.AndReturn([servers, False]) self.mock_tenant_list.return_value = [self.tenants.list(), False]
api.nova.extension_supported('AdminActions', IsA(http.HttpRequest)) \
.MultipleTimes().AndReturn(True)
api.nova.extension_supported('Shelve', IsA(http.HttpRequest)) \
.MultipleTimes().AndReturn(True)
api.glance.image_list_detailed(IsA(http.HttpRequest))\
.AndReturn((images, False, False))
api.nova.flavor_list(IsA(http.HttpRequest)). \
AndRaise(self.exceptions.nova)
api.keystone.tenant_list(IsA(http.HttpRequest)).\
AndReturn([tenants, False])
for server in servers:
api.nova.flavor_get(IsA(http.HttpRequest), server.flavor["id"]). \
AndReturn(full_flavors[server.flavor["id"]])
self.mox.ReplayAll() def _get_full_flavor(request, id):
return full_flavors[id]
self.mock_flavor_get.side_effect = _get_full_flavor
self.mock_image_list_detailed.return_value = (self.images.list(),
False, False)
res = self.client.get(INDEX_URL) res = self.client.get(INDEX_URL)
self.assertTemplateUsed(res, INDEX_TEMPLATE) self.assertTemplateUsed(res, INDEX_TEMPLATE)
instances = res.context['table'].data instances = res.context['table'].data
self.assertItemsEqual(instances, servers) self.assertItemsEqual(instances, servers)
@test.create_stubs({ search_opts = {'marker': None, 'paginate': True, 'all_tenants': True}
api.nova: ('flavor_list', 'flavor_get', 'server_list', self.mock_server_list.assert_called_once_with(
'extension_supported',), test.IsHttpRequest(), search_opts=search_opts)
api.keystone: ('tenant_list',), self.mock_extension_supported.assert_has_calls([
api.glance: ('image_list_detailed',), mock.call('AdminActions', test.IsHttpRequest()),
mock.call('AdminActions', test.IsHttpRequest()),
mock.call('Shelve', test.IsHttpRequest())]*4)
self.assertEqual(12, self.mock_extension_supported.call_count)
self.mock_flavor_list.assert_called_once_with(test.IsHttpRequest())
self.mock_tenant_list.assert_called_once_with(test.IsHttpRequest())
self.mock_flavor_get.assert_has_calls(
[mock.call(test.IsHttpRequest(), s.flavor['id']) for s in servers])
self.assertEqual(len(servers), self.mock_flavor_get.call_count)
self.mock_image_list_detailed.assert_called_once_with(
test.IsHttpRequest())
@test.create_mocks({
api.nova: ['flavor_list', 'flavor_get', 'server_list',
'extension_supported'],
api.keystone: ['tenant_list'],
api.glance: ['image_list_detailed'],
}) })
def test_index_flavor_get_exception(self): def test_index_flavor_get_exception(self):
servers = self.servers.list() servers = self.servers.list()
flavors = self.flavors.list()
images = self.images.list()
tenants = self.tenants.list()
# UUIDs generated using indexes are unlikely to match # UUIDs generated using indexes are unlikely to match
# any of existing flavor ids and are guaranteed to be deterministic. # any of existing flavor ids and are guaranteed to be deterministic.
for i, server in enumerate(servers): for i, server in enumerate(servers):
server.flavor['id'] = str(uuid.UUID(int=i)) server.flavor['id'] = str(uuid.UUID(int=i))
api.glance.image_list_detailed(IsA(http.HttpRequest))\ self.mock_image_list_detailed.return_value = \
.AndReturn((images, False, False)) (self.images.list(), False, False)
api.nova.flavor_list(IsA(http.HttpRequest)).AndReturn(flavors) self.mock_flavor_list.return_value = self.flavors.list()
search_opts = {'marker': None, 'paginate': True, 'all_tenants': True} self.mock_server_list.return_value = [servers, False]
api.nova.server_list(IsA(http.HttpRequest), self.mock_extension_supported.return_value = True
search_opts=search_opts) \ self.mock_tenant_list.return_value = [self.tenants.list(), False]
.AndReturn([servers, False]) self.mock_flavor_get.side_effect = self.exceptions.nova
api.nova.extension_supported('AdminActions', IsA(http.HttpRequest)) \
.MultipleTimes().AndReturn(True)
api.nova.extension_supported('Shelve', IsA(http.HttpRequest)) \
.MultipleTimes().AndReturn(True)
api.keystone.tenant_list(IsA(http.HttpRequest)).\
AndReturn([tenants, False])
for server in servers:
api.nova.flavor_get(IsA(http.HttpRequest), server.flavor["id"]). \
AndRaise(self.exceptions.nova)
self.mox.ReplayAll()
res = self.client.get(INDEX_URL) res = self.client.get(INDEX_URL)
instances = res.context['table'].data instances = res.context['table'].data
@ -139,51 +134,58 @@ class InstanceViewTest(test.BaseAdminViewTests):
self.assertMessageCount(res, error=1) self.assertMessageCount(res, error=1)
self.assertItemsEqual(instances, servers) self.assertItemsEqual(instances, servers)
@test.create_stubs({ self.mock_image_list_detailed.assert_called_once_with(
api.nova: ('server_list', 'flavor_list',), test.IsHttpRequest())
api.keystone: ('tenant_list',), self.mock_flavor_list.assert_called_once_with(test.IsHttpRequest())
api.glance: ('image_list_detailed',), search_opts = {'marker': None, 'paginate': True, 'all_tenants': True}
self.mock_server_list.assert_called_once_with(
test.IsHttpRequest(), search_opts=search_opts)
self.mock_extension_supported.assert_has_calls([
mock.call('AdminActions', test.IsHttpRequest()),
mock.call('AdminActions', test.IsHttpRequest()),
mock.call('Shelve', test.IsHttpRequest())]*4)
self.assertEqual(12, self.mock_extension_supported.call_count)
self.mock_tenant_list.assert_called_once_with(test.IsHttpRequest())
self.mock_flavor_get.assert_has_calls(
[mock.call(test.IsHttpRequest(), s.flavor['id']) for s in servers])
self.assertEqual(len(servers), self.mock_flavor_get.call_count)
@test.create_mocks({
api.nova: ['server_list', 'flavor_list'],
api.keystone: ['tenant_list'],
api.glance: ['image_list_detailed'],
}) })
def test_index_server_list_exception(self): def test_index_server_list_exception(self):
tenants = self.tenants.list() self.mock_server_list.side_effect = self.exceptions.nova
images = self.images.list() self.mock_flavor_list.return_value = self.flavors.list()
flavors = self.flavors.list() self.mock_tenant_list.return_value = [self.tenants.list(), False]
self.mock_image_list_detailed.return_value = (self.images.list(),
api.keystone.tenant_list(IsA(http.HttpRequest)).\ False, False)
AndReturn([tenants, False])
api.glance.image_list_detailed(IsA(http.HttpRequest))\
.AndReturn((images, False, False))
api.nova.flavor_list(IsA(http.HttpRequest)).AndReturn(flavors)
search_opts = {'marker': None, 'paginate': True, 'all_tenants': True}
api.nova.server_list(IsA(http.HttpRequest),
search_opts=search_opts) \
.AndRaise(self.exceptions.nova)
self.mox.ReplayAll()
res = self.client.get(INDEX_URL) res = self.client.get(INDEX_URL)
self.assertTemplateUsed(res, INDEX_TEMPLATE) self.assertTemplateUsed(res, INDEX_TEMPLATE)
self.assertEqual(len(res.context['instances_table'].data), 0) self.assertEqual(len(res.context['instances_table'].data), 0)
@test.create_stubs({api.nova: ('server_get', 'flavor_get', search_opts = {'marker': None, 'paginate': True, 'all_tenants': True}
'extension_supported', ), self.mock_server_list.assert_called_once_with(
api.network: ('servers_update_addresses',), test.IsHttpRequest(),
api.keystone: ('tenant_get',)}) search_opts=search_opts)
self.mock_tenant_list.assert_called_once_with(test.IsHttpRequest())
self.mock_image_list_detailed.assert_called_once_with(
test.IsHttpRequest())
self.mock_flavor_list.assert_called_once_with(test.IsHttpRequest())
@test.create_mocks({api.nova: ['server_get', 'flavor_get',
'extension_supported'],
api.network: ['servers_update_addresses'],
api.keystone: ['tenant_get']})
def test_ajax_loading_instances(self): def test_ajax_loading_instances(self):
server = self.servers.first() server = self.servers.first()
flavor = self.flavors.list()[0] self.mock_server_get.return_value = server
tenant = self.tenants.list()[0] self.mock_extension_supported.return_value = True
api.nova.server_get(IsA(http.HttpRequest), server.id).AndReturn(server) self.mock_flavor_get.return_value = self.flavors.first()
api.nova.extension_supported('AdminActions', IsA(http.HttpRequest)) \ self.mock_tenant_get.return_value = self.tenants.first()
.MultipleTimes().AndReturn(True) self.mock_servers_update_addresses.return_value = None
api.nova.extension_supported('Shelve', IsA(http.HttpRequest)) \
.MultipleTimes().AndReturn(True)
api.nova.flavor_get(IsA(http.HttpRequest),
server.flavor['id']).AndReturn(flavor)
api.keystone.tenant_get(IsA(http.HttpRequest),
server.tenant_id,
admin=True).AndReturn(tenant)
self.mox.ReplayAll()
url = (INDEX_URL + url = (INDEX_URL +
"?action=row_update&table=instances&obj_id=" + server.id) "?action=row_update&table=instances&obj_id=" + server.id)
@ -203,39 +205,55 @@ class InstanceViewTest(test.BaseAdminViewTests):
self.assertContains(res, "Active", 1, 200) self.assertContains(res, "Active", 1, 200)
self.assertContains(res, "Running", 1, 200) self.assertContains(res, "Running", 1, 200)
@test.create_stubs({ self.mock_server_get.assert_called_once_with(
api.nova: ('flavor_list', 'server_list', 'extension_supported', ), test.IsHttpRequest(), server.id)
api.keystone: ('tenant_list',), self.mock_extension_supported.assert_has_calls([
api.glance: ('image_list_detailed',), mock.call('AdminActions', test.IsHttpRequest()),
mock.call('AdminActions', test.IsHttpRequest()),
mock.call('Shelve', test.IsHttpRequest())])
self.assertEqual(3, self.mock_extension_supported.call_count)
self.mock_flavor_get.assert_called_once_with(
test.IsHttpRequest(), server.flavor['id'])
self.mock_tenant_get.assert_called_once_with(
test.IsHttpRequest(), server.tenant_id, admin=True)
self.mock_servers_update_addresses.assert_called_once_with(
test.IsHttpRequest(), [server])
@test.create_mocks({
api.nova: ['flavor_list', 'server_list', 'extension_supported'],
api.keystone: ['tenant_list'],
api.glance: ['image_list_detailed'],
}) })
def test_index_options_before_migrate(self): def test_index_options_before_migrate(self):
servers = self.servers.list() self.mock_tenant_list.return_value = [self.tenants.list(), False]
images = self.images.list() self.mock_image_list_detailed.return_value = (self.images.list(),
flavors = self.flavors.list() False, False)
api.keystone.tenant_list(IsA(http.HttpRequest)).\ self.mock_flavor_list.return_value = self.flavors.list()
AndReturn([self.tenants.list(), False]) self.mock_server_list.return_value = [self.servers.list(), False]
api.glance.image_list_detailed(IsA(http.HttpRequest)) \ self.mock_extension_supported.return_value = True
.AndReturn((images, False, False))
api.nova.flavor_list(IsA(http.HttpRequest)).AndReturn(flavors)
search_opts = {'marker': None, 'paginate': True, 'all_tenants': True}
api.nova.server_list(IsA(http.HttpRequest),
search_opts=search_opts) \
.AndReturn([servers, False])
api.nova.extension_supported('AdminActions', IsA(http.HttpRequest)) \
.MultipleTimes().AndReturn(True)
api.nova.extension_supported('Shelve', IsA(http.HttpRequest)) \
.MultipleTimes().AndReturn(True)
self.mox.ReplayAll()
res = self.client.get(INDEX_URL) res = self.client.get(INDEX_URL)
self.assertContains(res, "instances__migrate") self.assertContains(res, "instances__migrate")
self.assertNotContains(res, "instances__confirm") self.assertNotContains(res, "instances__confirm")
self.assertNotContains(res, "instances__revert") self.assertNotContains(res, "instances__revert")
@test.create_stubs({ self.mock_tenant_list.assert_called_once_with(test.IsHttpRequest())
api.nova: ('flavor_list', 'server_list', 'extension_supported',), self.mock_image_list_detailed.assert_called_once_with(
api.keystone: ('tenant_list',), test.IsHttpRequest())
api.glance: ('image_list_detailed',), self.mock_flavor_list.assert_called_once_with(test.IsHttpRequest())
search_opts = {'marker': None, 'paginate': True, 'all_tenants': True}
self.mock_server_list.assert_called_once_with(
test.IsHttpRequest(), search_opts=search_opts)
self.mock_extension_supported.assert_has_calls([
mock.call('AdminActions', test.IsHttpRequest()),
mock.call('AdminActions', test.IsHttpRequest()),
mock.call('Shelve', test.IsHttpRequest())]*4)
self.assertEqual(12, self.mock_extension_supported.call_count)
@test.create_mocks({
api.nova: ['flavor_list', 'server_list', 'extension_supported'],
api.keystone: ['tenant_list'],
api.glance: ['image_list_detailed'],
}) })
def test_index_options_after_migrate(self): def test_index_options_after_migrate(self):
servers = self.servers.list() servers = self.servers.list()
@ -243,40 +261,39 @@ class InstanceViewTest(test.BaseAdminViewTests):
server1.status = "VERIFY_RESIZE" server1.status = "VERIFY_RESIZE"
server2 = servers[2] server2 = servers[2]
server2.status = "VERIFY_RESIZE" server2.status = "VERIFY_RESIZE"
images = self.images.list() self.mock_tenant_list.return_value = [self.tenants.list(), False]
flavors = self.flavors.list() self.mock_image_list_detailed.return_value = (self.images.list(),
api.keystone.tenant_list(IsA(http.HttpRequest)) \ False, False)
.AndReturn([self.tenants.list(), False]) self.mock_flavor_list.return_value = self.flavors.list()
api.glance.image_list_detailed(IsA(http.HttpRequest)) \ self.mock_extension_supported.return_value = True
.AndReturn((images, False, False)) self.mock_server_list.return_value = [servers, False]
api.nova.flavor_list(IsA(http.HttpRequest)).AndReturn(flavors)
api.nova.extension_supported('AdminActions', IsA(http.HttpRequest)) \
.MultipleTimes().AndReturn(True)
api.nova.extension_supported('Shelve', IsA(http.HttpRequest)) \
.MultipleTimes().AndReturn(True)
search_opts = {'marker': None, 'paginate': True, 'all_tenants': True}
api.nova.server_list(IsA(http.HttpRequest),
search_opts=search_opts) \
.AndReturn([servers, False])
self.mox.ReplayAll()
res = self.client.get(INDEX_URL) res = self.client.get(INDEX_URL)
self.assertContains(res, "instances__confirm") self.assertContains(res, "instances__confirm")
self.assertContains(res, "instances__revert") self.assertContains(res, "instances__revert")
self.assertNotContains(res, "instances__migrate") self.assertNotContains(res, "instances__migrate")
@test.create_stubs({api.nova: ('service_list', self.mock_tenant_list.assert_called_once_with(test.IsHttpRequest())
'server_get',)}) self.mock_image_list_detailed.assert_called_once_with(
test.IsHttpRequest())
self.mock_flavor_list.assert_called_once_with(test.IsHttpRequest())
self.mock_extension_supported.assert_has_calls([
mock.call('AdminActions', test.IsHttpRequest()),
mock.call('AdminActions', test.IsHttpRequest()),
mock.call('Shelve', test.IsHttpRequest())]*4)
self.assertEqual(12, self.mock_extension_supported.call_count)
search_opts = {'marker': None, 'paginate': True, 'all_tenants': True}
self.mock_server_list.assert_called_once_with(
test.IsHttpRequest(), search_opts=search_opts)
@test.create_mocks({api.nova: ['service_list',
'server_get']})
def test_instance_live_migrate_get(self): def test_instance_live_migrate_get(self):
server = self.servers.first() server = self.servers.first()
compute_services = [s for s in self.services.list() compute_services = [s for s in self.services.list()
if s.binary == 'nova-compute'] if s.binary == 'nova-compute']
api.nova.server_get(IsA(http.HttpRequest), server.id) \ self.mock_server_get.return_value = server
.AndReturn(server) self.mock_service_list.return_value = compute_services
api.nova.service_list(IsA(http.HttpRequest), binary='nova-compute') \
.AndReturn(compute_services)
self.mox.ReplayAll()
url = reverse('horizon:admin:instances:live_migrate', url = reverse('horizon:admin:instances:live_migrate',
args=[server.id]) args=[server.id])
@ -284,13 +301,15 @@ class InstanceViewTest(test.BaseAdminViewTests):
self.assertTemplateUsed(res, 'admin/instances/live_migrate.html') self.assertTemplateUsed(res, 'admin/instances/live_migrate.html')
@test.create_stubs({api.nova: ('server_get',)}) self.mock_server_get.assert_called_once_with(
test.IsHttpRequest(), server.id)
self.mock_service_list.assert_called_once_with(
test.IsHttpRequest(), binary='nova-compute')
@test.create_mocks({api.nova: ['server_get']})
def test_instance_live_migrate_get_server_get_exception(self): def test_instance_live_migrate_get_server_get_exception(self):
server = self.servers.first() server = self.servers.first()
api.nova.server_get(IsA(http.HttpRequest), server.id) \ self.mock_server_get.side_effect = self.exceptions.nova
.AndRaise(self.exceptions.nova)
self.mox.ReplayAll()
url = reverse('horizon:admin:instances:live_migrate', url = reverse('horizon:admin:instances:live_migrate',
args=[server.id]) args=[server.id])
@ -298,38 +317,40 @@ class InstanceViewTest(test.BaseAdminViewTests):
self.assertRedirectsNoFollow(res, INDEX_URL) self.assertRedirectsNoFollow(res, INDEX_URL)
@test.create_stubs({api.nova: ('service_list', self.mock_server_get.assert_called_once_with(
'server_get',)}) test.IsHttpRequest(), server.id)
@test.create_mocks({api.nova: ['service_list',
'server_get']})
def test_instance_live_migrate_list_host_get_exception(self): def test_instance_live_migrate_list_host_get_exception(self):
server = self.servers.first() server = self.servers.first()
api.nova.server_get(IsA(http.HttpRequest), server.id) \ self.mock_server_get.return_value = server
.AndReturn(server) self.mock_service_list.side_effect = self.exceptions.nova
api.nova.service_list(IsA(http.HttpRequest), binary='nova-compute') \
.AndRaise(self.exceptions.nova)
self.mox.ReplayAll()
url = reverse('horizon:admin:instances:live_migrate', url = reverse('horizon:admin:instances:live_migrate',
args=[server.id]) args=[server.id])
res = self.client.get(url) res = self.client.get(url)
self.assertRedirectsNoFollow(res, INDEX_URL) self.assertRedirectsNoFollow(res, INDEX_URL)
@test.create_stubs({api.nova: ('service_list', self.mock_server_get.assert_called_once_with(
'server_get',)}) test.IsHttpRequest(), server.id)
self.mock_service_list.assert_called_once_with(
test.IsHttpRequest(), binary='nova-compute')
@test.create_mocks({api.nova: ['service_list',
'server_get']})
def test_instance_live_migrate_list_host_without_current(self): def test_instance_live_migrate_list_host_without_current(self):
server = self.servers.first() server = self.servers.first()
compute_services = [s for s in self.services.list() compute_services = [s for s in self.services.list()
if s.binary == 'nova-compute'] if s.binary == 'nova-compute']
api.nova.server_get(IsA(http.HttpRequest), server.id) \ self.mock_server_get.return_value = server
.AndReturn(server) self.mock_service_list.return_value = compute_services
api.nova.service_list(IsA(http.HttpRequest), binary='nova-compute') \
.AndReturn(compute_services)
self.mox.ReplayAll()
url = reverse('horizon:admin:instances:live_migrate', url = reverse('horizon:admin:instances:live_migrate',
args=[server.id]) args=[server.id])
res = self.client.get(url) res = self.client.get(url)
self.assertContains( self.assertContains(
res, "<option value=\"devstack001\">devstack001</option>") res, "<option value=\"devstack001\">devstack001</option>")
self.assertContains( self.assertContains(
@ -337,90 +358,100 @@ class InstanceViewTest(test.BaseAdminViewTests):
self.assertNotContains( self.assertNotContains(
res, "<option value=\"instance-host\">instance-host</option>") res, "<option value=\"instance-host\">instance-host</option>")
@test.create_stubs({api.nova: ('service_list', self.mock_server_get.assert_called_once_with(
test.IsHttpRequest(), server.id)
self.mock_service_list.assert_called_once_with(
test.IsHttpRequest(), binary='nova-compute')
@test.create_mocks({api.nova: ['service_list',
'server_get', 'server_get',
'server_live_migrate',)}) 'server_live_migrate']})
def test_instance_live_migrate_post(self): def test_instance_live_migrate_post(self):
server = self.servers.first() server = self.servers.first()
compute_services = [s for s in self.services.list() compute_services = [s for s in self.services.list()
if s.binary == 'nova-compute'] if s.binary == 'nova-compute']
host = compute_services[0].host host = compute_services[0].host
api.nova.server_get(IsA(http.HttpRequest), server.id) \ self.mock_server_get.return_value = server
.AndReturn(server) self.mock_service_list.return_value = compute_services
api.nova.service_list(IsA(http.HttpRequest), binary='nova-compute') \ self.mock_server_live_migrate.return_value = []
.AndReturn(compute_services)
api.nova.server_live_migrate(IsA(http.HttpRequest), server.id, host,
block_migration=False,
disk_over_commit=False) \
.AndReturn([])
self.mox.ReplayAll()
url = reverse('horizon:admin:instances:live_migrate', url = reverse('horizon:admin:instances:live_migrate',
args=[server.id]) args=[server.id])
res = self.client.post(url, {'host': host, 'instance_id': server.id}) res = self.client.post(url, {'host': host, 'instance_id': server.id})
self.assertNoFormErrors(res) self.assertNoFormErrors(res)
self.assertRedirectsNoFollow(res, INDEX_URL) self.assertRedirectsNoFollow(res, INDEX_URL)
@test.create_stubs({api.nova: ('service_list', self.mock_server_get.assert_called_once_with(
test.IsHttpRequest(), server.id)
self.mock_service_list.assert_called_once_with(
test.IsHttpRequest(), binary='nova-compute')
self.mock_server_live_migrate.assert_called_once_with(
test.IsHttpRequest(), server.id, host,
block_migration=False,
disk_over_commit=False)
@test.create_mocks({api.nova: ['service_list',
'server_get', 'server_get',
'server_live_migrate',)}) 'server_live_migrate']})
def test_instance_live_migrate_auto_sched(self): def test_instance_live_migrate_auto_sched(self):
server = self.servers.first() server = self.servers.first()
host = "AUTO_SCHEDULE" host = "AUTO_SCHEDULE"
api.nova.server_get(IsA(http.HttpRequest), server.id) \ self.mock_server_get.return_value = server
.AndReturn(server)
compute_services = [s for s in self.services.list() compute_services = [s for s in self.services.list()
if s.binary == 'nova-compute'] if s.binary == 'nova-compute']
api.nova.service_list(IsA(http.HttpRequest), binary='nova-compute') \ self.mock_service_list.return_value = compute_services
.AndReturn(compute_services) self.mock_server_live_migrate.return_value = []
api.nova.server_live_migrate(IsA(http.HttpRequest), server.id, None,
block_migration=False,
disk_over_commit=False) \
.AndReturn([])
self.mox.ReplayAll()
url = reverse('horizon:admin:instances:live_migrate', url = reverse('horizon:admin:instances:live_migrate',
args=[server.id]) args=[server.id])
res = self.client.post(url, {'host': host, 'instance_id': server.id}) res = self.client.post(url, {'host': host, 'instance_id': server.id})
self.assertNoFormErrors(res) self.assertNoFormErrors(res)
self.assertRedirectsNoFollow(res, INDEX_URL) self.assertRedirectsNoFollow(res, INDEX_URL)
@test.create_stubs({api.nova: ('service_list', self.mock_server_get.assert_called_once_with(
test.IsHttpRequest(), server.id)
self.mock_service_list.assert_called_once_with(
test.IsHttpRequest(), binary='nova-compute')
self.mock_server_live_migrate(
test.IsHttpRequest(), server.id, None,
block_migration=False,
disk_over_commit=False)
@test.create_mocks({api.nova: ['service_list',
'server_get', 'server_get',
'server_live_migrate',)}) 'server_live_migrate']})
def test_instance_live_migrate_post_api_exception(self): def test_instance_live_migrate_post_api_exception(self):
server = self.servers.first() server = self.servers.first()
compute_services = [s for s in self.services.list() compute_services = [s for s in self.services.list()
if s.binary == 'nova-compute'] if s.binary == 'nova-compute']
host = compute_services[0].host host = compute_services[0].host
api.nova.server_get(IsA(http.HttpRequest), server.id) \ self.mock_server_get.return_value = server
.AndReturn(server) self.mock_service_list.return_value = compute_services
api.nova.service_list(IsA(http.HttpRequest), binary='nova-compute') \ self.mock_server_live_migrate.side_effect = self.exceptions.nova
.AndReturn(compute_services)
api.nova.server_live_migrate(IsA(http.HttpRequest), server.id, host,
block_migration=False,
disk_over_commit=False) \
.AndRaise(self.exceptions.nova)
self.mox.ReplayAll()
url = reverse('horizon:admin:instances:live_migrate', url = reverse('horizon:admin:instances:live_migrate',
args=[server.id]) args=[server.id])
res = self.client.post(url, {'host': host, 'instance_id': server.id}) res = self.client.post(url, {'host': host, 'instance_id': server.id})
self.assertRedirectsNoFollow(res, INDEX_URL) self.assertRedirectsNoFollow(res, INDEX_URL)
@test.create_stubs({api.nova: ('server_get',)}) self.mock_server_get.assert_called_once_with(
test.IsHttpRequest(), server.id)
self.mock_service_list.assert_called_once_with(
test.IsHttpRequest(), binary='nova-compute')
self.mock_server_live_migrate.assert_called_once_with(
test.IsHttpRequest(), server.id, host,
block_migration=False,
disk_over_commit=False)
@test.create_mocks({api.nova: ['server_get']})
def test_instance_details_exception(self): def test_instance_details_exception(self):
server = self.servers.first() server = self.servers.first()
self.mock_server_get.side_effect = self.exceptions.nova
api.nova.server_get(IsA(http.HttpRequest), server.id) \
.AndRaise(self.exceptions.nova)
self.mox.ReplayAll()
url = reverse('horizon:admin:instances:detail', url = reverse('horizon:admin:instances:detail',
args=[server.id]) args=[server.id])
@ -428,6 +459,9 @@ class InstanceViewTest(test.BaseAdminViewTests):
self.assertRedirectsNoFollow(res, INDEX_URL) self.assertRedirectsNoFollow(res, INDEX_URL)
self.mock_server_get.assert_called_once_with(
test.IsHttpRequest(), server.id)
@test.update_settings(FILTER_DATA_FIRST={'admin.instances': True}) @test.update_settings(FILTER_DATA_FIRST={'admin.instances': True})
def test_index_with_admin_filter_first(self): def test_index_with_admin_filter_first(self):
res = self.client.get(INDEX_URL) res = self.client.get(INDEX_URL)