Example #1
0
 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))
Example #2
0
 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))
Example #3
0
    def reconcile_mac_prefixes(self):
        networks = Network.objects.filter(deleted=False, flavor="MAC_FILTERED")
        check_unique_values(objects=networks, field='mac_prefix',
                            logger=self.log)
        try:
            pool = MacPrefixPoolTable.get_pool()
        except pools.EmptyPool:
            self.log.info("There is no available pool for MAC prefixes.")
            return

        # Since pool is locked, no new network may be created
        used_mac_prefixes = set(networks.values_list('mac_prefix', flat=True))
        check_pool_consistent(pool=pool, pool_class=pools.MacPrefixPool,
                              used_values=used_mac_prefixes, fix=self.fix,
                              logger=self.log)
Example #4
0
    def reconcile_mac_prefixes(self):
        networks = Network.objects.filter(deleted=False, flavor="MAC_FILTERED")
        check_unique_values(objects=networks,
                            field='mac_prefix',
                            logger=self.log)
        try:
            pool = MacPrefixPoolTable.get_pool()
        except pools.EmptyPool:
            self.log.info("There is no available pool for MAC prefixes.")
            return

        # Since pool is locked, no new network may be created
        used_mac_prefixes = set(networks.values_list('mac_prefix', flat=True))
        check_pool_consistent(pool=pool,
                              pool_class=pools.MacPrefixPool,
                              used_values=used_mac_prefixes,
                              fix=self.fix,
                              logger=self.log)
Example #5
0
    def detect_mac_prefixes(self):
        write = self.stdout.write

        write("---------------------------------------\n")
        write("Checking consistency of the MAC Prefix Pool\n")
        write("---------------------------------------\n")

        try:
            macp_pool = MacPrefixPoolTable.get_pool()
        except EmptyPool:
            write("No mac-prefix pool\n")
            return

        macs = []
        for i in xrange(1, macp_pool.size()):
            used_macp = not (macp_pool.is_available(i, index=True) or
                             macp_pool.is_reserved(i, index=True))
            if used_macp:
                value = macp_pool.index_to_value(i)
                macs.append(value)

        write("Used MAC prefixes from Pool: %d\n" % len(macs))

        network_mac_prefixes = \
            Network.objects.filter(deleted=False, flavor='MAC_FILTERED')\
                           .values_list('mac_prefix', flat=True)
        write("Used MAC prefixes from Networks: %d\n" %
              len(network_mac_prefixes))

        set_network_mac_prefixes = set(network_mac_prefixes)
        if len(network_mac_prefixes) > len(set_network_mac_prefixes):
            write("Found duplicated mac_prefixes:\n")
            duplicates = list(network_mac_prefixes)
            for mac_prefix in set_network_mac_prefixes:
                duplicates.remove(mac_prefix)
            for mac_prefix in set(duplicates):
                write("Duplicated mac_prefix: %s. " % mac_prefix)
                write("Used by the following Networks:\n")
                nets = Network.objects.filter(deleted=False,
                                              mac_prefix=mac_prefix)
                write("  " + "\n  ".join([str(net.id) for net in nets]) + "\n")
Example #6
0
    def detect_mac_prefixes(self):
        write = self.stdout.write

        write("---------------------------------------\n")
        write("Checking consistency of the MAC Prefix Pool\n")
        write("---------------------------------------\n")

        try:
            macp_pool = MacPrefixPoolTable.get_pool()
        except EmptyPool:
            write("No mac-prefix pool\n")
            return

        macs = []
        for i in xrange(1, macp_pool.size()):
            used_macp = not (macp_pool.is_available(i, index=True)
                             or macp_pool.is_reserved(i, index=True))
            if used_macp:
                value = macp_pool.index_to_value(i)
                macs.append(value)

        write("Used MAC prefixes from Pool: %d\n" % len(macs))

        network_mac_prefixes = \
            Network.objects.filter(deleted=False, flavor='MAC_FILTERED')\
                           .values_list('mac_prefix', flat=True)
        write("Used MAC prefixes from Networks: %d\n" %
              len(network_mac_prefixes))

        set_network_mac_prefixes = set(network_mac_prefixes)
        if len(network_mac_prefixes) > len(set_network_mac_prefixes):
            write("Found duplicated mac_prefixes:\n")
            duplicates = list(network_mac_prefixes)
            for mac_prefix in set_network_mac_prefixes:
                duplicates.remove(mac_prefix)
            for mac_prefix in set(duplicates):
                write("Duplicated mac_prefix: %s. " % mac_prefix)
                write("Used by the following Networks:\n")
                nets = Network.objects.filter(deleted=False,
                                              mac_prefix=mac_prefix)
                write("  " + "\n  ".join([str(net.id) for net in nets]) + "\n")
Example #7
0
    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, [])
Example #8
0
    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"])
        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"])
        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, [])