def test_get_ipv6_for_subscriber_with_apn_dot(self): """ test get_ip_for_sid with static IP """ apn = 'magma.ipv6' imsi = 'IMSI110' sid = imsi + '.' + apn + ",ipv6" assigned_ip = '2022::1' MockedSubscriberDBStub.add_sub(sid=imsi, apn=apn, ip=assigned_ip) ip0, _ = self._allocator.alloc_ip_address(sid, version=IPAddress.IPV6) ip0_returned = self._allocator.get_ip_for_sid(sid) # check if retrieved ip is the same as the one allocated self.assertEqual(ip0, ip0_returned) self.assertEqual(ip0, ipaddress.ip_address(assigned_ip)) self.check_type(sid, IPType.STATIC)
def test_get_ip_for_subscriber_with_wildcard_apn(self): """ test wildcard apn""" apn = 'magma' imsi = 'IMSI110' sid = imsi + '.' + apn + ",ipv4" assigned_ip = '1.2.3.4' MockedSubscriberDBStub.add_sub(sid=imsi, apn="*", ip=assigned_ip) ip0, _ = self._allocator.alloc_ip_address(sid) ip0_returned = self._allocator.get_ip_for_sid(sid) # check if retrieved ip is the same as the one allocated self.assertEqual(ip0, ip0_returned) self.assertEqual(ip0, ipaddress.ip_address(assigned_ip)) self.check_type(sid, IPType.STATIC)
def test_get_ip_for_subscriber_with_different_apn(self): """ test get_ip_for_sid with different APN assigned ip""" apn = 'magma' imsi = 'IMSI110' sid = imsi + '.' + apn assigned_ip = '1.2.3.4' MockedSubscriberDBStub.add_sub(sid=imsi, apn="xyz", ip=assigned_ip) ip0, _ = self._allocator.alloc_ip_address(sid) ip0_returned = self._allocator.get_ip_for_sid(sid) # check if retrieved ip is the same as the one allocated self.assertEqual(ip0, ip0_returned) self.assertNotEqual(ip0, ipaddress.ip_address(assigned_ip)) self.check_type(sid, IPType.IP_POOL)
def test_get_ip_for_subscriber_with_invalid_ip(self): """ test invalid data from DB """ apn = 'magma' imsi = 'IMSI110' sid = imsi + '.' + apn + ",ipv4" assigned_ip = '1.2.3.hh' MockedSubscriberDBStub.add_sub(sid=imsi, apn=apn, ip=assigned_ip) ip0, _ = self._allocator.alloc_ip_address(sid) ip0_returned = self._allocator.get_ip_for_sid(sid) # check if retrieved ip is the same as the one allocated self.assertEqual(ip0, ip0_returned) self.assertNotEqual(str(ip0), assigned_ip) self.check_type(sid, IPType.IP_POOL)
def test_get_ip_for_subscriber_with_apn_with_gw_invalid_vlan(self): """ test get_ip_for_sid with static IP """ apn = 'magma' imsi = 'IMSI110' sid = imsi + '.' + apn + ",ipv4" assigned_ip = '1.2.3.4' gw_ip = "1.2.3.1" gw_mac = "11:22:33:11:77:44" vlan = "20000" MockedSubscriberDBStub.add_sub( sid=imsi, apn=apn, ip=assigned_ip, gw_ip=gw_ip, gw_mac=gw_mac, vlan=vlan, ) with self.assertRaises(InvalidVlanId): self._allocator.alloc_ip_address(sid)
def test_get_ip_for_subscriber_with_wildcard_and_exact_apn_no_ip(self): """ test IP assignement from multiple APNs""" apn = 'magma' imsi = 'IMSI110' sid = imsi + '.' + apn + ",ipv4" assigned_ip_wild = '22.22.22.22' MockedSubscriberDBStub.add_sub(sid=imsi, apn="*", ip=assigned_ip_wild) MockedSubscriberDBStub.add_sub_ip(sid=imsi, apn=apn, ip=None) ip0, _ = self._allocator.alloc_ip_address(sid) ip0_returned = self._allocator.get_ip_for_sid(sid) # check if retrieved ip is the same as the one allocated self.assertEqual(ip0, ip0_returned) self.assertNotEqual(ip0, ipaddress.ip_address(assigned_ip_wild)) self.check_type(sid, IPType.IP_POOL)
def test_get_ipv6_for_subscriber_with_wildcard_and_no_exact_apn(self): """ test IP assignement from multiple APNs""" apn = 'magma' imsi = 'IMSI110' sid = imsi + '.' + apn + ",ipv6" assigned_ip = '2022::1' assigned_ip_wild = '2022::22:22' MockedSubscriberDBStub.add_sub(sid=imsi, apn="*", ip=assigned_ip_wild) MockedSubscriberDBStub.add_sub_ip(sid=imsi, apn="xyz", ip=assigned_ip) ip0, _ = self._allocator.alloc_ip_address(sid, version=IPAddress.IPV6) ip0_returned = self._allocator.get_ip_for_sid(sid) # check if retrieved ip is the same as the one allocated self.assertEqual(ip0, ip0_returned) self.assertEqual(ip0, ipaddress.ip_address(assigned_ip_wild)) self.check_type(sid, IPType.STATIC)
def test_get_ip_for_subscriber_with_multi_apn_but_no_match(self): """ test IP assignment from multiple APNs""" apn = 'magma' imsi = 'IMSI110' sid = imsi + '.' + apn assigned_ip = '1.2.3.4' assigned_ip_wild = '22.22.22.22' MockedSubscriberDBStub.add_sub(sid=imsi, apn="abc", ip=assigned_ip_wild) MockedSubscriberDBStub.add_sub_ip(sid=imsi, apn="xyz", ip=assigned_ip) ip0, _ = self._allocator.alloc_ip_address(sid) ip0_returned = self._allocator.get_ip_for_sid(sid) # check if retrieved ip is the same as the one allocated self.assertEqual(ip0, ip0_returned) self.assertNotEqual(ip0, ipaddress.ip_address(assigned_ip)) self.check_type(sid, IPType.IP_POOL)
def test_get_ip_for_subscriber_with_apn_with_gw(self): """ test get_ip_for_sid with static IP """ apn = 'magma' imsi = 'IMSI110' sid = imsi + '.' + apn + ",ipv4" assigned_ip = '1.2.3.4' gw_ip = "1.2.3.1" gw_mac = "11:22:33:11:77:28" MockedSubscriberDBStub.add_sub(sid=imsi, apn=apn, ip=assigned_ip, gw_ip=gw_ip, gw_mac=gw_mac) ip0, _ = self._allocator.alloc_ip_address(sid) ip0_returned = self._allocator.get_ip_for_sid(sid) # check if retrieved ip is the same as the one allocated self.assertEqual(ip0, ip0_returned) self.assertEqual(ip0, ipaddress.ip_address(assigned_ip)) self.check_type(sid, IPType.STATIC) self.check_gw_info(None, gw_ip, gw_mac)
def test_get_ip_for_subscriber_with_wildcard_apn_gw(self): """ test wildcard apn""" apn = 'magma' imsi = 'IMSI110' sid = imsi + '.' + apn + ",ipv4" assigned_ip = '1.2.3.4' gw_ip = "1.2.3.100" gw_mac = "11:22:33:11:77:81" vlan = "300" MockedSubscriberDBStub.add_sub( sid=imsi, apn=apn, ip=assigned_ip, gw_ip=gw_ip, gw_mac=gw_mac, vlan=vlan, ) wildcard_assigned_ip = "20.20.20.20" wildcard_gw_ip = "1.2.7.7" wildcard_gw_mac = "11:22:33:88:77:99" wildcard_vlan = "400" MockedSubscriberDBStub.add_sub_ip( sid=imsi, apn="*", ip=wildcard_assigned_ip, gw_ip=wildcard_gw_ip, gw_mac=wildcard_gw_mac, vlan=wildcard_vlan, ) ip0, _ = self._allocator.alloc_ip_address(sid) ip0_returned = self._allocator.get_ip_for_sid(sid) # check if retrieved ip is the same as the one allocated self.assertEqual(ip0, ip0_returned) self.assertEqual(ip0, ipaddress.ip_address(assigned_ip)) self.check_type(sid, IPType.STATIC) self.check_gw_info(vlan, gw_ip, gw_mac) self.check_gw_info(wildcard_vlan, None, None)
def test_get_ip_for_subscriber_with_apn_with_gw_nul_mac(self): """ test get_ip_for_sid with static IP """ apn = 'magma' imsi = 'IMSI110' sid = imsi + '.' + apn assigned_ip = '1.2.3.24' gw_ip = "1.2.3.55" gw_mac = None vlan = "200" MockedSubscriberDBStub.add_sub(sid=imsi, apn=apn, ip=assigned_ip, gw_ip=gw_ip, gw_mac=gw_mac, vlan=vlan) ip0, _ = self._allocator.alloc_ip_address(sid) ip0_returned = self._allocator.get_ip_for_sid(sid) # check if retrieved ip is the same as the one allocated self.assertEqual(ip0, ip0_returned) self.assertEqual(ip0, ipaddress.ip_address(assigned_ip)) self.check_type(sid, IPType.STATIC) self.check_gw_info(vlan, gw_ip, "")
def test_get_ipv6_for_subscriber_with_only_wildcard_apn_gw(self): """ test wildcard apn""" apn = 'magma' imsi = 'IMSI110' sid = imsi + '.' + apn + ",ipv6" assigned_ip = '2022::1' gw_ip = "2022::1:1:1" gw_mac = "11:22:33:11:77:81" MockedSubscriberDBStub.add_sub( sid=imsi, apn="*", ip=assigned_ip, gw_ip=gw_ip, gw_mac=gw_mac, ) ip0, _ = self._allocator.alloc_ip_address(sid, version=IPAddress.IPV6) ip0_returned = self._allocator.get_ip_for_sid(sid) # check if retrieved ip is the same as the one allocated self.assertEqual(ip0, ip0_returned) self.assertEqual(ip0, ipaddress.ip_address(assigned_ip)) self.check_type(sid, IPType.STATIC)
def test_get_ip_for_subscriber_with_apn_dup_assignment(self): """ test duplicate static IPs """ apn = 'magma' imsi = 'IMSI110' sid = imsi + '.' + apn + ",ipv4" assigned_ip = '1.2.3.4' MockedSubscriberDBStub.add_sub(sid=imsi, apn=apn, ip=assigned_ip) ip0, _ = self._allocator.alloc_ip_address(sid) ip0_returned = self._allocator.get_ip_for_sid(sid) # check if retrieved ip is the same as the one allocated self.assertEqual(ip0, ip0_returned) self.assertEqual(ip0, ipaddress.ip_address(assigned_ip)) self.check_type(sid, IPType.STATIC) apn = 'magma' imsi = 'IMSI999' sid = imsi + '.' + apn + ",ipv4" MockedSubscriberDBStub.add_sub(sid=imsi, apn=apn, ip=assigned_ip) with self.assertRaises(DuplicateIPAssignmentError): ip0, _ = self._allocator.alloc_ip_address(sid)
def test_get_ip_for_subscriber_with_apn(self): """ test get_ip_for_sid with static IP """ apn = 'magma' imsi = 'IMSI110' sid = imsi + '.' + apn + ",ipv4" assigned_ip = '1.2.3.4' MockedSubscriberDBStub.add_sub(sid=imsi, apn=apn, ip=assigned_ip) ip0, _ = self._allocator.alloc_ip_address(sid) ip0_returned = self._allocator.get_ip_for_sid(sid) # check if retrieved ip is the same as the one allocated ip0_ipaddr = ipaddress.ip_address(assigned_ip) self.assertEqual(ip0, ip0_returned) self.assertEqual(ip0, ip0_ipaddr) self.check_type(sid, IPType.STATIC) ip_block = ipaddress.ip_network(ip0_ipaddr) self.assertIn(ip_block, self._allocator._store.assigned_ip_blocks) self._allocator.release_ip_address(sid, ip0_ipaddr) time.sleep(2) self.assertNotIn(ip_block, self._allocator._store.assigned_ip_blocks)
def test_get_ipv6_for_subscriber_with_apn_with_gw_nul_ip(self): """ test get_ip_for_sid with static IP """ apn = 'magma' imsi = 'IMSI110' sid = imsi + '.' + apn + ",ipv6" assigned_ip = '2022::1' gw_ip = "" gw_mac = "11:22:33:11:77:45" vlan = "200" MockedSubscriberDBStub.add_sub( sid=imsi, apn=apn, ip=assigned_ip, gw_ip=gw_ip, gw_mac=gw_mac, vlan=vlan, ) ip0, _ = self._allocator.alloc_ip_address(sid, version=IPAddress.IPV6) ip0_returned = self._allocator.get_ip_for_sid(sid) # check if retrieved ip is the same as the one allocated self.assertEqual(ip0, ip0_returned) self.assertEqual(ip0, ipaddress.ip_address(assigned_ip)) self.check_type(sid, IPType.STATIC) self.check_gw_info(vlan, None, None)
def _new_ip_allocator(self, recycling_interval): """ Creates and sets up an IPAllocator with the given recycling interval. """ store = MobilityStore(get_default_client(), False, 3980) ip_allocator = IpAllocatorPool(store) ipv4_allocator = IPAllocatorStaticWrapper( store, subscriberdb_rpc_stub=MockedSubscriberDBStub(), ip_allocator=ip_allocator) ipv6_allocator = IPv6AllocatorPool(store, session_prefix_alloc_mode='RANDOM') self._allocator = IPAddressManager(ipv4_allocator, ipv6_allocator, store, recycling_interval) self._allocator.add_ip_block(self._block)
def _new_ip_allocator(self, recycling_interval): """ Creates and sets up an IPAllocator with the given recycling interval. """ config = { 'recycling_interval': recycling_interval, 'persist_to_redis': False, 'redis_port': 6379, } mconfig = MobilityD(ip_allocator_type=MobilityD.IP_POOL, static_ip_enabled=True) self._allocator = IPAddressManager(recycling_interval=recycling_interval, subscriberdb_rpc_stub=MockedSubscriberDBStub(), config=config, mconfig=mconfig) self._allocator.add_ip_block(self._block)
def _new_ip_allocator(self, recycling_interval): """ Creates and sets up an IPAllocator with the given recycling interval. """ store = MobilityStore(fakeredis.FakeStrictRedis()) ip_allocator = IpAllocatorPool(store) ipv4_allocator = IPAllocatorStaticWrapper( store, subscriberdb_rpc_stub=MockedSubscriberDBStub(), ip_allocator=ip_allocator, ) ipv6_allocator = IPv6AllocatorPool( store, session_prefix_alloc_mode='RANDOM', ) self._allocator = IPAddressManager( ipv4_allocator, ipv6_allocator, store, recycling_interval, ) self._allocator.add_ip_block(self._block)
def tearDown(self): MockedSubscriberDBStub.clear_subs()