Improved doc, refactored names and added test case for segmentation_utils

CR
This commit is contained in:
Shay Nehmad 2019-09-02 10:20:52 +03:00
parent 36ad6fc441
commit faf6da15bb
2 changed files with 21 additions and 5 deletions

View File

@ -11,8 +11,13 @@ def get_ip_in_src_and_not_in_dst(ip_addresses, source_subnet, target_subnet):
return get_ip_if_in_subnet(ip_addresses, source_subnet) return get_ip_if_in_subnet(ip_addresses, source_subnet)
def get_ip_if_in_subnet(ip_addresses, source_subnet): def get_ip_if_in_subnet(ip_addresses, subnet):
"""
:param ip_addresses: IP address list.
:param subnet: Subnet to check if one of ip_addresses is in there. This is common.network.network_range.NetworkRange
:return: The first IP in ip_addresses which is in the subnet.
"""
for ip_address in ip_addresses: for ip_address in ip_addresses:
if source_subnet.is_in_range(ip_address): if subnet.is_in_range(ip_address):
return ip_address return ip_address
return None return None

View File

@ -8,12 +8,23 @@ class TestSegmentationUtils(IslandTestCase):
self.fail_if_not_testing_env() self.fail_if_not_testing_env()
source = CidrRange("1.1.1.0/24") source = CidrRange("1.1.1.0/24")
target = CidrRange("2.2.2.0/24") target = CidrRange("2.2.2.0/24")
self.assertIsNone(get_ip_in_src_and_not_in_dst(
[text_type("2.2.2.2")], source, target # IP not in both
))
self.assertIsNone(get_ip_in_src_and_not_in_dst( self.assertIsNone(get_ip_in_src_and_not_in_dst(
[text_type("3.3.3.3"), text_type("4.4.4.4")], source, target [text_type("3.3.3.3"), text_type("4.4.4.4")], source, target
)) ))
# IP not in source, in target
self.assertIsNone(get_ip_in_src_and_not_in_dst(
[text_type("2.2.2.2")], source, target
))
# IP in source, not in target
self.assertIsNotNone(get_ip_in_src_and_not_in_dst( self.assertIsNotNone(get_ip_in_src_and_not_in_dst(
[text_type("8.8.8.8"), text_type("1.1.1.1")], source, target [text_type("8.8.8.8"), text_type("1.1.1.1")], source, target
)) ))
# IP in both subnets
self.assertIsNone(get_ip_in_src_and_not_in_dst(
[text_type("8.8.8.8"), text_type("1.1.1.1")], source, source
))