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_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_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_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_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(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_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_parse_ipv4_service_ranges(json_file): services = parse_ipv4_service_ranges(json_file) assert len(services) == 1886 assert services[0].region == 'eu-west-1' assert services[0].service == 'AMAZON'
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 ranges(json_file): return parse_ipv4_service_ranges(json_file)
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_basic_test_file(json_file): ranges = parse_ipv4_service_ranges(json_file) assert ranges != None, "Test file should parse to something" assert len(ranges) == 1886, "Test file should have 1886 IPv4 addresses"
def test_parse_ip4_service_ranges(json_file): service_ranges = parse_ipv4_service_ranges(json_file) assert len(service_ranges) == 1886 assert isinstance(service_ranges, list) assert isinstance(service_ranges[0], ServiceIPRange)
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"
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))