def test_run_out_of_ip(self): """ should raise RESOURCE_EXHAUSTED when running out of IP """ # The subnet is provisioned with 16 addresses # Inside ip_address_man.py 11 addresses are reserved, # 2 addresses are not usable (all zeros and all ones) # Thus, we have a usable pool of 3 IP addresses; # first three allocations should succeed, while the fourth # request should raise RESOURCE_EXHAUSTED error self._stub.AddIPBlock(self._block_msg) request = AllocateIPRequest(sid=self._sid0, version=AllocateIPRequest.IPV4, apn=self._apn0) self._stub.AllocateIPAddress(request) request.apn = self._apn1 self._stub.AllocateIPAddress(request) request.sid.CopyFrom(self._sid1) self._stub.AllocateIPAddress(request) request.sid.CopyFrom(self._sid2) with self.assertRaises(grpc.RpcError) as err: self._stub.AllocateIPAddress(request) self.assertEqual(err.exception.code(), grpc.StatusCode.RESOURCE_EXHAUSTED)
def allocate_ip_handler(client, args): try: sid_msg = SIDUtils.to_pb(args.sid) except ValueError: print("Invalid SubscriberID format: %s" % args.sid) return request = AllocateIPRequest() if int(args.version) == 4: request.version = AllocateIPRequest.IPV4 elif int(args.version) == 6: request.version = AllocateIPRequest.IPV6 else: print("Error: IP version %d is not supported yet" % args.version) return request.sid.CopyFrom(sid_msg) request.apn = args.apn response = client.AllocateIPAddress(request) ip_list_msg = response.ip_list for ip_msg in ip_list_msg: if ip_msg.version == IPAddress.IPV4: ip = ipaddress.IPv4Address(ip_msg.address) print("IPv4 address allocated: %s" % ip) elif ip_msg.version == IPAddress.IPV6: ip = ipaddress.IPv6Address(ip_msg.address) print("IPv6 address allocated: %s" % ip) else: print("Error: unknown IP version")
def test_multiple_apn_ipallocation(self): """ test AllocateIPAddress for multiple APNs """ self._stub.AddIPBlock(self._block_msg) # allocate 1st IP request = AllocateIPRequest(sid=self._sid0, version=AllocateIPRequest.IPV4, apn=self._apn0) ip_msg0 = self._stub.AllocateIPAddress(request) self.assertEqual(ip_msg0.ip_list[0].version, AllocateIPRequest.IPV4) ip0 = ipaddress.ip_address(ip_msg0.ip_list[0].address) self.assertTrue(ip0 in self._block) # allocate 2nd IP from another APN to the same user request.apn = self._apn1 ip_msg1 = self._stub.AllocateIPAddress(request) self.assertEqual(ip_msg1.ip_list[0].version, AllocateIPRequest.IPV4) ip1 = ipaddress.ip_address(ip_msg1.ip_list[0].address) self.assertTrue(ip1 in self._block)