Convert admin.hypervisors tests into mock

Change-Id: I9ccbb9c5e83dc2c2df0715bf8a0c9aec3db37229
Partially-Implements: blueprint mock-framework-in-unit-tests
This commit is contained in:
Vladislav Kuzmin 2018-03-13 15:14:18 +04:00
parent 73a0bbd43e
commit f25b924857

View File

@ -12,33 +12,27 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from django import http
from django.urls import reverse from django.urls import reverse
from mox3.mox import IsA
import mock
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
class HypervisorViewTest(test.BaseAdminViewTests): class HypervisorViewTest(test.BaseAdminViewTests):
@test.create_stubs({api.nova: ('extension_supported', @test.create_mocks({api.nova: ['extension_supported',
'hypervisor_list', 'hypervisor_list',
'hypervisor_stats', 'hypervisor_stats',
'service_list')}) 'service_list']})
def test_index(self): def test_index(self):
hypervisors = self.hypervisors.list() hypervisors = self.hypervisors.list()
services = self.services.list() compute_services = [service for service in self.services.list()
stats = self.hypervisors.stats
compute_services = [service for service in services
if service.binary == 'nova-compute'] if service.binary == 'nova-compute']
api.nova.extension_supported('AdminActions', self.mock_extension_supported.return_value = True
IsA(http.HttpRequest)) \ self.mock_hypervisor_list.return_value = hypervisors
.MultipleTimes().AndReturn(True) self.mock_hypervisor_stats.return_value = self.hypervisors.stats
api.nova.hypervisor_list(IsA(http.HttpRequest)).AndReturn(hypervisors) self.mock_service_list.return_value = compute_services
api.nova.hypervisor_stats(IsA(http.HttpRequest)).AndReturn(stats)
api.nova.service_list(IsA(http.HttpRequest), binary='nova-compute') \
.AndReturn(compute_services)
self.mox.ReplayAll()
res = self.client.get(reverse('horizon:admin:hypervisors:index')) res = self.client.get(reverse('horizon:admin:hypervisors:index'))
self.assertTemplateUsed(res, 'admin/hypervisors/index.html') self.assertTemplateUsed(res, 'admin/hypervisors/index.html')
@ -67,35 +61,44 @@ class HypervisorViewTest(test.BaseAdminViewTests):
self.assertEqual('migrate_maintenance', self.assertEqual('migrate_maintenance',
actions_service_disabled[1].name) actions_service_disabled[1].name)
@test.create_stubs({api.nova: ('hypervisor_list', self.assert_mock_multiple_calls_with_same_arguments(
self.mock_extension_supported, 28,
mock.call('AdminActions', test.IsHttpRequest()))
self.mock_hypervisor_list.assert_called_once_with(
test.IsHttpRequest())
self.mock_hypervisor_stats.assert_called_once_with(
test.IsHttpRequest())
self.mock_service_list.assert_called_once_with(
test.IsHttpRequest(), binary='nova-compute')
@test.create_mocks({api.nova: ['hypervisor_list',
'hypervisor_stats', 'hypervisor_stats',
'service_list')}) 'service_list']})
def test_service_list_unavailable(self): def test_service_list_unavailable(self):
# test that error message should be returned when # test that error message should be returned when
# nova.service_list isn't available. # nova.service_list isn't available.
hypervisors = self.hypervisors.list() self.mock_hypervisor_list.return_value = self.hypervisors.list()
stats = self.hypervisors.stats self.mock_hypervisor_stats.return_value = self.hypervisors.stats
api.nova.hypervisor_list(IsA(http.HttpRequest)).AndReturn(hypervisors) self.mock_service_list.side_effect = self.exceptions.nova
api.nova.hypervisor_stats(IsA(http.HttpRequest)).AndReturn(stats)
api.nova.service_list(IsA(http.HttpRequest), binary='nova-compute') \
.AndRaise(self.exceptions.nova)
self.mox.ReplayAll()
resp = self.client.get(reverse('horizon:admin:hypervisors:index')) resp = self.client.get(reverse('horizon:admin:hypervisors:index'))
self.assertMessageCount(resp, error=1, warning=0) self.assertMessageCount(resp, error=1, warning=0)
self.mock_hypervisor_list.assert_called_once_with(
test.IsHttpRequest())
self.mock_hypervisor_stats.assert_called_once_with(
test.IsHttpRequest())
self.mock_service_list.assert_called_once_with(
test.IsHttpRequest(), binary='nova-compute')
class HypervisorDetailViewTest(test.BaseAdminViewTests): class HypervisorDetailViewTest(test.BaseAdminViewTests):
@test.create_stubs({api.nova: ('hypervisor_search',)}) @test.create_mocks({api.nova: ['hypervisor_search']})
def test_index(self): def test_index(self):
hypervisor = self.hypervisors.first() hypervisor = self.hypervisors.first()
api.nova.hypervisor_search( self.mock_hypervisor_search.return_value = [
IsA(http.HttpRequest), hypervisor, self.hypervisors.list()[1]]
hypervisor.hypervisor_hostname).AndReturn([
hypervisor,
self.hypervisors.list()[1]])
self.mox.ReplayAll()
url = reverse('horizon:admin:hypervisors:detail', url = reverse('horizon:admin:hypervisors:detail',
args=["%s_%s" % (hypervisor.id, args=["%s_%s" % (hypervisor.id,
@ -103,3 +106,6 @@ class HypervisorDetailViewTest(test.BaseAdminViewTests):
res = self.client.get(url) res = self.client.get(url)
self.assertTemplateUsed(res, 'admin/hypervisors/detail.html') self.assertTemplateUsed(res, 'admin/hypervisors/detail.html')
self.assertItemsEqual(res.context['table'].data, hypervisor.servers) self.assertItemsEqual(res.context['table'].data, hypervisor.servers)
self.mock_hypervisor_search.assert_called_once_with(
test.IsHttpRequest(), hypervisor.hypervisor_hostname)