def test_remove(self, client): mfactory.MacPrefixPoolTableFactory() mfactory.BridgePoolTableFactory() bn = mfactory.BackendNetworkFactory(operstate='ACTIVE') for old_state in ['success', 'canceled', 'error']: for flavor in Network.FLAVORS.keys(): bn.operstate = old_state bn.save() net = bn.network net.state = 'ACTIVE' net.flavor = flavor if flavor == 'PHYSICAL_VLAN': net.link = allocate_resource('bridge') if flavor == 'MAC_FILTERED': net.mac_prefix = allocate_resource('mac_prefix') net.save() msg = self.create_msg(operation='OP_NETWORK_REMOVE', network=net.backend_id, cluster=bn.backend.clustername) with mocked_quotaholder(): update_network(client, msg) self.assertTrue(client.basic_ack.called) 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) if flavor == 'PHYSICAL_VLAN': pool = BridgePoolTable.get_pool() self.assertTrue(pool.is_available(net.link)) if flavor == 'MAC_FILTERED': pool = MacPrefixPoolTable.get_pool() self.assertTrue(pool.is_available(net.mac_prefix))
def reconcile_bridges(self): networks = Network.objects.filter(deleted=False, flavor="PHYSICAL_VLAN") check_unique_values(objects=networks, field='link', logger=self.log) try: pool = BridgePoolTable.get_pool() except pools.EmptyPool: self.log.info("There is no available pool for bridges.") return # Since pool is locked, no new networks may be created used_bridges = set(networks.values_list('link', flat=True)) check_pool_consistent(pool=pool, pool_class=pools.BridgePool, used_values=used_bridges, fix=self.fix, logger=self.log)
def detect_bridges(self): write = self.stdout.write write("---------------------------------------\n") write("Checking consistency of the Bridge Pool\n") write("---------------------------------------\n") try: bridge_pool = BridgePoolTable.get_pool() except EmptyPool: write("No Bridge Pool\n") return bridges = [] for i in xrange(0, bridge_pool.size()): used_bridge = not (bridge_pool.is_available(i, index=True) or bridge_pool.is_reserved(i, index=True)) if used_bridge: bridges.append(bridge_pool.index_to_value(i)) write("Used bridges from Pool: %d\n" % len(bridges)) network_bridges = Network.objects.filter(flavor='PHYSICAL_VLAN', deleted=False)\ .values_list('link', flat=True) write("Used bridges from Networks: %d\n" % len(network_bridges)) set_network_bridges = set(network_bridges) if len(network_bridges) > len(set_network_bridges): write("Found duplicated bridges:\n") duplicates = list(network_bridges) for bridge in set_network_bridges: duplicates.remove(bridge) for bridge in set(duplicates): write("Duplicated bridge: %s. " % bridge) write("Used by the following Networks:\n") nets = Network.objects.filter(deleted=False, link=bridge) write(" " + "\n ".join([str(net.id) for net in nets]) + "\n")
def test_create(self): kwargs = { "name": "test", "userid": "user", "flavor": "CUSTOM", } # wrong flavor kw = copy(kwargs) kw["flavor"] = "UNKNOWN" self.assertRaises(faults.BadRequest, networks.create, **kw) # Test create objet kwargs["public"] = True with mocked_quotaholder(): net = networks.create(**kwargs) self.assertEqual(net.public, True) self.assertEqual(net.flavor, "CUSTOM") self.assertEqual(net.action, "CREATE") self.assertEqual(net.state, "ACTIVE") self.assertEqual(net.name, "test") self.assertEqual(net.userid, "user") # Test for each for flavor type # MAC_FILTERED kwargs["flavor"] = "MAC_FILTERED" # Test exception if no rules exists self.assertRaises(faults.ServiceUnavailable, networks.create, **kwargs) mfactory.MacPrefixPoolTableFactory(base="aa:bb:0") with mocked_quotaholder(): net = networks.create(**kwargs) self.assertEqual(net.mode, "bridged") self.assertEqual(net.mac_prefix, "aa:bb:1") self.assertEqual(net.link, settings.DEFAULT_MAC_FILTERED_BRIDGE) self.assertEqual(net.backend_tag, ["private-filtered"]) with transaction.atomic(): pool = MacPrefixPoolTable.get_pool() self.assertFalse(pool.is_available("aa:bb:1")) # PHYSICAL_VLAN kwargs["flavor"] = "PHYSICAL_VLAN" # Test exception if no rules exists self.assertRaises(faults.ServiceUnavailable, networks.create, **kwargs) mfactory.BridgePoolTableFactory(base="prv") with mocked_quotaholder(): net = networks.create(**kwargs) self.assertEqual(net.mode, "bridged") self.assertEqual(net.mac_prefix, settings.DEFAULT_MAC_PREFIX) self.assertEqual(net.link, "prv1") self.assertEqual(net.backend_tag, ["physical-vlan"]) with transaction.atomic(): pool = BridgePoolTable.get_pool() self.assertFalse(pool.is_available(net.link)) # IP_LESS_ROUTED kwargs["flavor"] = "IP_LESS_ROUTED" with mocked_quotaholder(): net = networks.create(**kwargs) self.assertEqual(net.mode, "routed") self.assertEqual(net.mac_prefix, settings.DEFAULT_MAC_PREFIX) self.assertEqual(net.link, "%slink-%d" % (settings.BACKEND_PREFIX_ID, net.id)) self.assertEqual(net.backend_tag, ["ip-less-routed"]) # CUSTOM kwargs["flavor"] = "CUSTOM" with mocked_quotaholder(): net = networks.create(**kwargs) self.assertEqual(net.mode, "bridged") self.assertEqual(net.mac_prefix, settings.DEFAULT_MAC_PREFIX) self.assertEqual(net.link, settings.DEFAULT_BRIDGE) self.assertEqual(net.backend_tag, [])