Commit e9f8b19f by Bach Dániel

Merge branch 'feature-celery-mock' into 'master'

Feature celery mock 

See merge request !306
parents 89e37091 c2804735
# Copyright 2014 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE. If not, see <http://www.gnu.org/licenses/>.
from mock import patch
class MockCeleryMixin(object):
def _pre_setup(self):
self.reloadtask_patcher = patch(
'firewall.tasks.local_tasks.reloadtask.apply_async', spec=True)
self.reloadtask_patcher.start()
self.kombu_patcher = patch('kombu.connection.Connection.ensure',
side_effect=RuntimeError())
self.kombu_patcher.start()
self.check_queue_patcher = patch('vm.tasks.vm_tasks.check_queue',
return_value=True)
self.check_queue_patcher.start()
super(MockCeleryMixin, self)._pre_setup()
def _post_teardown(self):
self.reloadtask_patcher.stop()
self.kombu_patcher.stop()
super(MockCeleryMixin, self)._post_teardown()
......@@ -20,12 +20,13 @@ from collections import deque
from django.test import TestCase
from mock import MagicMock
from .celery_mock import MockCeleryMixin
from .models import TestClass
from ..models import HumanSortField
from ..models import activitycontextimpl
class MethodCacheTestCase(TestCase):
class MethodCacheTestCase(MockCeleryMixin, TestCase):
def test_cache(self):
t1 = TestClass(1)
t2 = TestClass(2)
......
......@@ -24,10 +24,12 @@ from django.contrib.auth.models import User, Group
from django.contrib.auth.models import Permission
from django.contrib.auth import authenticate
from common.tests.celery_mock import MockCeleryMixin
from dashboard.views import VmAddInterfaceView
from vm.models import Instance, InstanceTemplate, Lease, Node, Trait
from vm.operations import (WakeUpOperation, AddInterfaceOperation,
AddPortOperation, RemoveInterfaceOperation)
AddPortOperation, RemoveInterfaceOperation,
DeployOperation)
from ..models import Profile
from firewall.models import Vlan, Host, VlanGroup
from mock import Mock, patch
......@@ -44,7 +46,7 @@ class LoginMixin(object):
self.assertNotEqual(response.status_code, 403)
class VmDetailTest(LoginMixin, TestCase):
class VmDetailTest(LoginMixin, MockCeleryMixin, TestCase):
fixtures = ['test-vm-fixture.json', 'node.json']
def setUp(self):
......@@ -217,21 +219,25 @@ class VmDetailTest(LoginMixin, TestCase):
self.login(c, 'user1')
InstanceTemplate.objects.get(id=1).set_level(self.u1, 'user')
Vlan.objects.get(id=1).set_level(self.u1, 'user')
with patch.object(DeployOperation, 'async') as async:
response = c.post('/dashboard/vm/create/',
{'template': 1,
'system': "bubi",
'cpu_priority': 1, 'cpu_count': 1,
'ram_size': 1000})
assert async.called
self.assertEqual(response.status_code, 302)
def test_use_permitted_template_superuser(self):
c = Client()
self.login(c, 'superuser')
with patch.object(DeployOperation, 'async') as async:
response = c.post('/dashboard/vm/create/',
{'template': 1,
'system': "bubi",
'cpu_priority': 1, 'cpu_count': 1,
'ram_size': 1000})
assert async.called
self.assertEqual(response.status_code, 302)
def test_edit_unpermitted_template(self):
......@@ -537,6 +543,7 @@ class VmDetailTest(LoginMixin, TestCase):
self.login(c, "superuser")
instance_count = Instance.objects.all().count()
with patch.object(DeployOperation, 'async') as async:
response = c.post("/dashboard/vm/create/", {
'name': 'vm',
'amount': 2,
......@@ -546,6 +553,7 @@ class VmDetailTest(LoginMixin, TestCase):
'network': [],
})
assert async.called
self.assertEqual(response.status_code, 302)
self.assertEqual(instance_count + 2, Instance.objects.all().count())
......@@ -585,7 +593,7 @@ class VmDetailTest(LoginMixin, TestCase):
self.assertEqual(Instance.objects.get(pk=1).description, "naonyo")
class NodeDetailTest(LoginMixin, TestCase):
class NodeDetailTest(LoginMixin, MockCeleryMixin, TestCase):
fixtures = ['test-vm-fixture.json', 'node.json']
def setUp(self):
......@@ -756,7 +764,7 @@ class NodeDetailTest(LoginMixin, TestCase):
self.assertEqual(len(Node.objects.get(pk=1).traits.all()), trait_count)
class GroupCreateTest(LoginMixin, TestCase):
class GroupCreateTest(LoginMixin, MockCeleryMixin, TestCase):
fixtures = ['test-vm-fixture.json', 'node.json']
def setUp(self):
......@@ -858,7 +866,7 @@ class GroupCreateTest(LoginMixin, TestCase):
self.assertTrue(newgroup.profile.has_level(self.u0, 'owner'))
class GroupDeleteTest(LoginMixin, TestCase):
class GroupDeleteTest(LoginMixin, MockCeleryMixin, TestCase):
fixtures = ['test-vm-fixture.json', 'node.json']
def setUp(self):
......@@ -948,7 +956,7 @@ class GroupDeleteTest(LoginMixin, TestCase):
self.assertEqual(Group.objects.count(), groupnum - 1)
class GroupDetailTest(LoginMixin, TestCase):
class GroupDetailTest(LoginMixin, MockCeleryMixin, TestCase):
fixtures = ['test-vm-fixture.json', 'node.json']
def setUp(self):
......@@ -1332,7 +1340,7 @@ class GroupDetailTest(LoginMixin, TestCase):
self.assertEqual(response.status_code, 302)
class GroupListTest(LoginMixin, TestCase):
class GroupListTest(LoginMixin, MockCeleryMixin, TestCase):
fixtures = ['test-vm-fixture.json', 'node.json']
def setUp(self):
......@@ -1375,7 +1383,7 @@ class GroupListTest(LoginMixin, TestCase):
self.g2.delete()
class VmDetailVncTest(LoginMixin, TestCase):
class VmDetailVncTest(LoginMixin, MockCeleryMixin, TestCase):
fixtures = ['test-vm-fixture.json', 'node.json']
def setUp(self):
......@@ -1408,7 +1416,7 @@ class VmDetailVncTest(LoginMixin, TestCase):
self.assertEqual(response.status_code, 403)
class TransferOwnershipViewTest(LoginMixin, TestCase):
class TransferOwnershipViewTest(LoginMixin, MockCeleryMixin, TestCase):
fixtures = ['test-vm-fixture.json']
def setUp(self):
......@@ -1446,7 +1454,7 @@ class TransferOwnershipViewTest(LoginMixin, TestCase):
self.assertEqual(self.u2.notification_set.count(), c2 + 1)
class IndexViewTest(LoginMixin, TestCase):
class IndexViewTest(LoginMixin, MockCeleryMixin, TestCase):
fixtures = ['test-vm-fixture.json', 'node.json']
def setUp(self):
......@@ -1548,7 +1556,7 @@ class ProfileViewTest(LoginMixin, TestCase):
self.assertIsNone(authenticate(username="user1", password="asd"))
class AclViewTest(LoginMixin, TestCase):
class AclViewTest(LoginMixin, MockCeleryMixin, TestCase):
fixtures = ['test-vm-fixture.json', 'node.json']
def setUp(self):
......@@ -1666,7 +1674,7 @@ class AclViewTest(LoginMixin, TestCase):
self.assertEqual(resp.status_code, 302)
class VmListTest(LoginMixin, TestCase):
class VmListTest(LoginMixin, MockCeleryMixin, TestCase):
fixtures = ['test-vm-fixture.json', 'node.json']
def setUp(self):
......
......@@ -15,20 +15,22 @@
# You should have received a copy of the GNU General Public License along
# with CIRCLE. If not, see <http://www.gnu.org/licenses/>.
from mock import patch
from netaddr import IPSet, AddrFormatError
from django.test import TestCase
import django.conf
from django.contrib.auth.models import User
from ..admin import HostAdmin
from django.forms import ValidationError
from django.test import TestCase
from common.tests.celery_mock import MockCeleryMixin
from firewall.admin import HostAdmin
from firewall.fw import dns, ipv6_to_octal
from firewall.iptables import IptRule, IptChain, InvalidRuleExcepion
from firewall.models import (Vlan, Domain, Record, Host, VlanGroup, Group,
Rule, Firewall)
from firewall.fw import dns, ipv6_to_octal
from firewall.tasks.local_tasks import reloadtask_worker, reloadtask
from django.forms import ValidationError
from ..iptables import IptRule, IptChain, InvalidRuleExcepion
from mock import patch
import django.conf
settings = django.conf.settings.FIREWALL_SETTINGS
......@@ -68,7 +70,7 @@ class HostAdminTestCase(TestCase):
self.assertEqual(l, "alma, korte, szilva")
class GetNewAddressTestCase(TestCase):
class GetNewAddressTestCase(MockCeleryMixin, TestCase):
def setUp(self):
self.u1 = User.objects.create(username='user1')
self.u1.save()
......@@ -105,7 +107,7 @@ class GetNewAddressTestCase(TestCase):
assert self.vlan.get_new_address()['ipv4'] not in used_v4
class HostGetHostnameTestCase(TestCase):
class HostGetHostnameTestCase(MockCeleryMixin, TestCase):
def setUp(self):
self.u1 = User.objects.create(username='user1')
self.u1.save()
......@@ -187,7 +189,7 @@ class IptablesTestCase(TestCase):
self.assertEqual(len(compiled_v6.splitlines()), 0)
class ReloadTestCase(TestCase):
class ReloadTestCase(MockCeleryMixin, TestCase):
def setUp(self):
self.u1 = User.objects.create(username='user1')
self.u1.save()
......@@ -313,7 +315,22 @@ class ReloadTestCase(TestCase):
def test_periodic_task(self):
# TODO
with patch('firewall.tasks.local_tasks.cache') as cache:
cache = patch('firewall.tasks.local_tasks.cache')
grqn = patch('firewall.models.Firewall.get_remote_queue_name',
return_value='fw.firewall')
worker = patch(
'firewall.tasks.local_tasks.reloadtask_worker.apply_async')
dns = patch('firewall.tasks.remote_tasks.reload_dns.apply_async')
fw = patch(
'firewall.tasks.remote_tasks.reload_firewall.apply_async')
fw_vlan = patch(
'firewall.tasks.remote_tasks.reload_firewall_vlan.apply_async')
blacklist = patch(
'firewall.tasks.remote_tasks.reload_blacklist.apply_async')
dhcp = patch('firewall.tasks.remote_tasks.reload_dhcp.apply_async')
with cache as cache, grqn, dns, fw, fw_vlan, blacklist, dhcp, worker:
self.test_host_add_port()
self.test_host_add_port2()
reloadtask_worker()
......
......@@ -20,6 +20,7 @@ from django.test.client import Client
from django.contrib.auth.models import User, Group
from mock import Mock
from common.tests.celery_mock import MockCeleryMixin
from dashboard.tests.test_views import LoginMixin
from vm.models import Instance
......@@ -29,7 +30,7 @@ import django.conf
settings = django.conf.settings.FIREWALL_SETTINGS
class VlanAclTest(LoginMixin, TestCase):
class VlanAclTest(LoginMixin, MockCeleryMixin, TestCase):
fixtures = ['test-vm-fixture.json', 'node.json']
def setUp(self):
......
......@@ -24,6 +24,8 @@ from django.contrib.auth.models import User
from django.test import TestCase
from django.utils.translation import ugettext_lazy as _
from common.tests.celery_mock import MockCeleryMixin
from ..models import (
Lease, Node, Interface, Instance, InstanceTemplate, InstanceActivity,
)
......@@ -166,7 +168,7 @@ class InstanceTestCase(TestCase):
self.assertEqual(Instance.get_status_icon(inst), 'fa-play')
class InterfaceTestCase(TestCase):
class InterfaceTestCase(MockCeleryMixin, TestCase):
def test_interface_create(self):
from firewall.models import Vlan, Domain
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment