def test_get_aws_service_range_address_error(json_file): service_ranges = parse_ipv4_service_ranges(json_file) with pytest.raises(ValueError) as excinfo: get_aws_service_range('7', service_ranges) assert "Address must be a valid IPv4 address" in str(excinfo.value)
def test_ip4_address_not_in_service_ranges(service_ranges): # 192.0.2.16 is outside 192.0.2.8/29 # 192.0.2.8/29 : 192.0.2.8 - 192.0.2.7 assert not get_aws_service_range("192.0.2.16", service_ranges) # 192.168.1.33/27 is outside 192.168.1.0/27 # 192.168.1.0/27 : 192.168.1.0 - 192.168.1.31 assert not get_aws_service_range("192.168.2.33", service_ranges)
def test_ipv4_address_in_service_ranges(service_ranges): actual = get_aws_service_range("10.1.18.15", service_ranges) expected = [service_ranges[0]] assert actual == expected # 192.168.1.16 is in the subnect of both # 192.168.1.0/27 and 192.168.1.0/24 actual = get_aws_service_range("192.168.1.16", service_ranges) expected = [service_ranges[1], service_ranges[3]] assert actual == expected
def test_get_aws_service_range(json_file): services = parse_ipv4_service_ranges(json_file) service_range = get_aws_service_range('13.248.118.1', services) assert len(service_range) == 2 assert set(s.region for s in service_range) == {'eu-west-1'} assert set(s.service for s in service_range) == {'AMAZON', 'GLOBALACCELERATOR'} assert get_aws_service_range('158.152.1.65', services) == [] with pytest.raises(ValueError) as exc: get_aws_service_range('0.0.0.256', services) assert 'Address must be a valid IPv4 address' in str(exc.value)
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 test_edge_conditions(json_file): ranges = parse_ipv4_service_ranges(json_file) with pytest.raises(ValueError, match='Address must be a valid IPv4 address') as ve: get_aws_service_range('not.a.gootd.address', ranges) aws_ranges = get_aws_service_range('54.238.0.0', []) assert len(aws_ranges) == 0 aws_ranges = get_aws_service_range('192.168.0.0', ranges) assert len(aws_ranges) == 0
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_get_aws_service_range_success(json_file): service_ranges = parse_ipv4_service_ranges(json_file) sipr = get_aws_service_range('52.93.120.178', service_ranges)[0] assert sipr.service == 'AMAZON' assert sipr.region == 'us-west-1' assert str(sipr.cidr) == '52.93.120.178/32'
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_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_get_aws_service_range(json_file): ranges = parse_ipv4_service_ranges(json_file) aws_ranges = get_aws_service_range('54.238.0.0', ranges) assert aws_ranges != None, "Should find 54.238.0.0 in test file" assert len( aws_ranges) == 2, "Should find 2 instances of 54.238.0.0 in test file" services = {range.service for range in aws_ranges} assert services == {'AMAZON', 'EC2'} regions = {range.region for range in aws_ranges} assert regions == {'ap-northeast-1'} cidrs = {str(range.cidr.network_address) for range in aws_ranges} assert cidrs == {'54.238.0.0'} range_strs = [str(range) for range in aws_ranges if range.service == 'EC2'] assert range_strs[ 0] == '54.238.0.0/16 is allocated to the EC2 service in the ap-northeast-1 region'
def test_ValueError(json_file): with pytest.raises(ValueError) as excinfo: service_ranges = parse_ipv4_service_ranges(json_file) get_aws_service_range("256.0.0.0", service_ranges) assert 'Address must be a valid IPv4 address' in str(excinfo.value)
def test_value_error(json_file): with pytest.raises(ValueError) as e: service_ranges = parse_ipv4_service_ranges(json_file) assert get_aws_service_range("260.260.260.260", service_ranges) assert str(e.value) == "Address must be a valid IPv4 address"
def test_empty_return(json_file, arg, expected): service_ranges = parse_ipv4_service_ranges(json_file) results = get_aws_service_range(arg, service_ranges) assert results == expected
def test_valid_ip(json_file, arg, expected): service_ranges = parse_ipv4_service_ranges(json_file) results = get_aws_service_range(arg, service_ranges) assert str(results[0]) == expected
def test_raises(ranges): with pytest.raises(ValueError) as excinfo: get_aws_service_range(ranges, '256.256.256.256') assert "maximum recursion" in str(excinfo.value)
def test_correct(json_file, arg, expected): actual = str( get_aws_service_range(arg, parse_ipv4_service_ranges(json_file))[0]) assert actual == expected
def test_err(json_file, err_arg): with pytest.raises(ValueError, match="Address must be a valid IPv4 address"): get_aws_service_range(err_arg, parse_ipv4_service_ranges(json_file))
def test_error_get_aws_service_range(json_file): iplist = parse_ipv4_service_ranges(json_file) with pytest.raises(ValueError) as valerr: get_aws_service_range('invalid string', iplist) assert valerr.value.args[0] == "Address must be a valid IPv4 address"