Commit 8a21b588 authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

cyclades: Delete backend networks with network

Delete backend network when network is deleted (marked as deleted=True)
in DB. Backend networks are useless as history and they may be recreated
if needed. Also fix some tests.
parent eea48fa9
......@@ -472,6 +472,8 @@ def update_network_state(network):
for subnet in network.subnets.all():
if subnet.ipversion == 4:
subnet.ip_pools.all().delete()
# And all the backend networks since there are useless
network.backend_networks.all().delete()
# Issue commission
if network.userid:
......
......@@ -509,9 +509,8 @@ class UpdateNetworkTest(TestCase):
with mocked_quotaholder():
update_network(client, msg)
self.assertTrue(client.basic_ack.called)
db_bnet = BackendNetwork.objects.get(id=bn.id)
self.assertEqual(db_bnet.operstate,
'DELETED')
self.assertFalse(BackendNetwork.objects.filter(id=bn.id)
.exists())
db_net = Network.objects.get(id=net.id)
self.assertEqual(db_net.state, 'DELETED', flavor)
self.assertTrue(db_net.deleted)
......@@ -539,8 +538,7 @@ class UpdateNetworkTest(TestCase):
rapi().GetNetwork.side_effect = GanetiApiError(msg="foo", code=404)
with mocked_quotaholder():
update_network(client, msg)
bn = BackendNetwork.objects.get(id=bn.id)
self.assertEqual(bn.operstate, "DELETED")
self.assertFalse(BackendNetwork.objects.filter(id=bn.id) .exists())
def test_remove_offline_backend(self, client):
"""Test network removing when a backend is offline"""
......@@ -562,11 +560,19 @@ class UpdateNetworkTest(TestCase):
self.assertEqual(new_net.state, 'ACTIVE')
self.assertFalse(new_net.deleted)
def test_error_opcode(self, client):
@patch("synnefo.logic.rapi_pool.GanetiRapiClient")
def test_error_opcode(self, rapi, client):
# Mock getting network, because code will lookup if network exists
# in backend
rapi().GetNetwork.return_value = {}
mfactory.MacPrefixPoolTableFactory()
mfactory.BridgePoolTableFactory()
network = mfactory.NetworkFactory()
mfactory.BackendNetworkFactory(network=network,
operstate="ACTIVE")
for state, _ in Network.OPER_STATES:
bn = mfactory.BackendNetworkFactory(operstate="ACTIVE")
bn = mfactory.BackendNetworkFactory(operstate="ACTIVE",
network=network)
bn.operstate = state
bn.save()
network = bn.network
......
......@@ -281,10 +281,9 @@ class NetworkReconciliationTest(TestCase):
self.assertFalse(net1.deleted)
with mocked_quotaholder():
self.reconciler.reconcile_networks()
bn1 = BackendNetwork.objects.get(id=bn1.id)
net1 = Network.objects.get(id=net1.id)
self.assertEqual(bn1.operstate, "DELETED")
self.assertTrue(net1.deleted)
self.assertFalse(net1.backend_networks.filter(id=bn1.id).exists())
# But not if action is not DESTROY
net2 = mfactory.NetworkWithSubnetFactory(public=False, action="CREATE")
mfactory.BackendNetworkFactory(network=net2, backend=self.backend)
......
......@@ -125,7 +125,8 @@ class ServerTest(TransactionTestCase):
vm = mfactory.VirtualMachineFactory(operstate="STARTED")
mfactory.BackendNetworkFactory(network=net, backend=vm.backend)
mrapi().ModifyInstance.return_value = 42
servers.connect(vm, net)
with override_settings(settings, GANETI_USE_HOTPLUG=True):
servers.connect(vm, net)
pool = net.get_ip_pools(locked=False)[0]
self.assertFalse(pool.is_available("192.168.2.2"))
args, kwargs = mrapi().ModifyInstance.call_args
......@@ -142,7 +143,8 @@ class ServerTest(TransactionTestCase):
gateway="2000::1")
net = subnet.network
mfactory.BackendNetworkFactory(network=net, backend=vm.backend)
servers.connect(vm, net)
with override_settings(settings, GANETI_USE_HOTPLUG=True):
servers.connect(vm, net)
args, kwargs = mrapi().ModifyInstance.call_args
nics = kwargs["nics"][0]
self.assertEqual(kwargs["instance"], vm.backend_vm_id)
......@@ -208,7 +210,8 @@ class ServerCommandTest(TransactionTestCase):
serial = vm.serial
mrapi().StartupInstance.return_value = 1
with mocked_quotaholder() as m:
servers.start(vm)
with override_settings(settings, CYCLADES_SERVICE_TOKEN=''):
servers.start(vm)
m.resolve_commissions.assert_called_once_with('', [],
[serial.serial])
self.assertTrue(m.issue_one_commission.called)
......@@ -221,7 +224,8 @@ class ServerCommandTest(TransactionTestCase):
serial = vm.serial
mrapi().StartupInstance.return_value = 1
with mocked_quotaholder() as m:
servers.start(vm)
with override_settings(settings, CYCLADES_SERVICE_TOKEN=''):
servers.start(vm)
m.resolve_commissions.assert_called_once_with('', [],
[serial.serial])
self.assertTrue(m.issue_one_commission.called)
......@@ -234,7 +238,8 @@ class ServerCommandTest(TransactionTestCase):
serial = vm.serial
mrapi().StartupInstance.return_value = 1
with mocked_quotaholder() as m:
servers.start(vm)
with override_settings(settings, CYCLADES_SERVICE_TOKEN=''):
servers.start(vm)
m.resolve_commissions.assert_called_once_with('', [serial.serial],
[])
self.assertTrue(m.issue_one_commission.called)
......@@ -244,11 +249,12 @@ class ServerCommandTest(TransactionTestCase):
vm.serial = None
# Test reject if Ganeti erro
with mocked_quotaholder() as m:
try:
servers.start(vm)
except:
m.resolve_commissions\
.assert_called_once_with('', [], [vm.serial.serial])
with override_settings(settings, CYCLADES_SERVICE_TOKEN=''):
try:
servers.start(vm)
except:
m.resolve_commissions\
.assert_called_once_with('', [], [vm.serial.serial])
def test_task_after(self, mrapi):
return
......
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 to comment