def test_working_get_aws_service_range(json_file): iplist = parse_ipv4_service_ranges(json_file) assert ServiceIPRange( service='AMAZON', region='us-east-1', cidr=IPv4Network('52.95.245.0/24')) in get_aws_service_range( '52.95.245.0', iplist) assert ServiceIPRange( service='WORKSPACES_GATEWAYS', region='ap-northeast-1', cidr=IPv4Network('54.250.251.0/24')) in get_aws_service_range( '54.250.251.0', iplist)
def test_valid(json_file): service_ranges = parse_ipv4_service_ranges(json_file) x = get_aws_service_range("35.180.0.0", service_ranges) assert x == [ ServiceIPRange(service='AMAZON', region='eu-west-3', cidr=IPv4Network('35.180.0.0/16')), ServiceIPRange(service='EC2', region='eu-west-3', cidr=IPv4Network('35.180.0.0/16')) ] assert str( x[0] ) == '35.180.0.0/16 is allocated to the AMAZON service in the eu-west-3 region'
def test_ServiceIPRange(): serv = ServiceIPRange(service='Tester', region='Bolton', cidr=IPv4Network('158.152.1.0/24')) assert str( serv ) == '158.152.1.0/24 is allocated to the Tester service in the Bolton region'
def test_ServiceIPRange_string_representaiton(): service = "CLOUD9" region = "eu-west-1" cidr = IP expected = f"{cidr} is allocated to the {service} service in the {region} region" assert str(ServiceIPRange(service, region, cidr)) == expected
def test_get_aws_service_range(ranges): assert [] == get_aws_service_range('67.10.12.2', ranges) assert [[ ServiceIPRange(service='AMAZON', region='ap-east-1', cidr=IPv4Network('54.240.17.0/24')), ServiceIPRange(service='EC2', region='ap-east-1', cidr=IPv4Network('54.240.17.0/24')) ]] == (get_aws_service_range('54.240.17.255', ranges)) network = "IPv4Network('54.240.17.0/24')" service = "AMAZON" region = "ap-east-1" exp_out = f"{network} is allocated to the {service} " \ f"service in the {region} region" assert exp_out == str(get_aws_service_range('54.240.17.255', ranges)[0])
def test_invalid_ip_address(): service = "CLOUD9" region = "eu-west-1" cidr = IP invalid_ip = "192.0.2.9/24" # Invalid IP. This is a valid CIDR service_ranges = [ServiceIPRange(service, region, cidr)] with pytest.raises(ValueError) as exc: get_aws_service_range(invalid_ip, service_ranges) assert str(exc.value) == "Address must be a valid IPv4 address"
def service_ranges(): service = "CLOUD9" region = "eu-west-1" CIDRS = [ "10.1.18.0/24", "192.168.1.0/27", "192.0.2.8/29", "192.168.1.0/24" ] return [ ServiceIPRange(service=service, region=region, cidr=IPv4Network(cidr)) for cidr in CIDRS ]
def test_serviceIPrange(json_file): iplist = parse_ipv4_service_ranges(json_file) assert str(iplist[0]) == (f"13.248.118.0/24 is allocated to the AMAZON " f"service in the eu-west-1 region") assert str(iplist[-1]) == ( f"54.250.251.0/24 is allocated to the WORKSPACES_GATEWAYS " f"service in the ap-northeast-1 region") assert str(iplist[1]) == (f"18.208.0.0/13 is allocated to the AMAZON " f"service in the us-east-1 region") assert len(iplist) == 1886 assert type(iplist[0]) == ServiceIPRange assert type(iplist) == list assert type(iplist[-1]) == ServiceIPRange assert iplist[0] == ServiceIPRange(service='AMAZON', region='eu-west-1', cidr=IPv4Network('13.248.118.0/24'))
def test_dataclass(): sipr = ServiceIPRange("TestService", "TestRegion", IP) assert str( sipr ) == f"{IP} is allocated to the TestService service in the TestRegion region"