def test_get_dns_name_and_mount_target_ip_address_via_option_failure( mocker, capsys): """ When the mount target ip address is passed through mount options and cannot be connected """ config = _get_mock_config() dns_mock = mocker.patch("socket.gethostbyname") get_fallback_mount_target_ip_mock = mocker.patch( "mount_efs.get_fallback_mount_target_ip_address") ip_address_connect_mock = mocker.patch( "mount_efs.mount_target_ip_address_can_be_resolved", side_effect=mount_efs.FallbackException("Timeout"), ) with pytest.raises(SystemExit) as ex: mount_efs.get_dns_name_and_fallback_mount_target_ip_address( config, FS_ID, OPTIONS_WITH_IP) assert 0 != ex.value.code out, err = capsys.readouterr() assert "Failed to resolve" not in err assert IP_ADDRESS in err assert "Cannot connect" in err assert "Timeout" in err utils.assert_not_called(dns_mock) utils.assert_not_called(get_fallback_mount_target_ip_mock) utils.assert_called(ip_address_connect_mock)
def test_get_dns_name_bad_format_wrong_specifiers(): config = _get_mock_config("{foo}.efs.{bar}") with pytest.raises(ValueError) as ex: mount_efs.get_dns_name_and_fallback_mount_target_ip_address( config, FS_ID, DEFAULT_NFS_OPTIONS) assert "must include" in str(ex.value)
def test_get_dns_name_bad_format_too_many_specifiers_2(): config = _get_mock_config("{fs_id}.efs.{region}.{foo}") with pytest.raises(ValueError) as ex: mount_efs.get_dns_name_and_fallback_mount_target_ip_address( config, FS_ID, DEFAULT_NFS_OPTIONS) assert "incorrect number" in str(ex.value)
def test_get_dns_name_unresolvable(mocker, capsys): config = _get_mock_config() mocker.patch("socket.gethostbyname", side_effect=socket.gaierror) with pytest.raises(SystemExit) as ex: mount_efs.get_dns_name_and_fallback_mount_target_ip_address( config, FS_ID, DEFAULT_NFS_OPTIONS) assert 0 != ex.value.code out, err = capsys.readouterr() assert "Failed to resolve" in err
def test_get_dns_name_suffix_hardcoded(): config = _get_mock_config('{az}.{fs_id}.efs.{region}.amazonaws.com') dns_name, ip_address = mount_efs.get_dns_name_and_fallback_mount_target_ip_address( config, FS_ID, DEFAULT_NFS_OPTIONS) assert '%s.efs.%s.amazonaws.com' % (FS_ID, DEFAULT_REGION) == dns_name assert None == ip_address
def test_get_dns_name_and_fallback_mount_target_ip_address(): config = _get_mock_config() dns_name, ip_address = mount_efs.get_dns_name_and_fallback_mount_target_ip_address( config, FS_ID, DEFAULT_NFS_OPTIONS) assert "%s.efs.%s.amazonaws.com" % (FS_ID, DEFAULT_REGION) == dns_name assert None == ip_address
def test_get_dns_name_without_az_in_options(): config = _get_mock_config("{az}.{fs_id}.efs.{region}.amazonaws.com") dns_name, ip_address = mount_efs.get_dns_name_and_fallback_mount_target_ip_address( config, FS_ID, DEFAULT_NFS_OPTIONS) assert "%s.efs.%s.amazonaws.com" % (FS_ID, DEFAULT_REGION) == dns_name assert None == ip_address
def test_get_dns_name_with_az_in_options(): config = _get_mock_config('{az}.{fs_id}.efs.{region}.amazonaws.com') dns_name, ip_address = mount_efs.get_dns_name_and_fallback_mount_target_ip_address( config, FS_ID, OPTIONS_WITH_AZ) assert '%s.%s.efs.%s.amazonaws.com' % (DEFAULT_AZ, FS_ID, DEFAULT_REGION) == dns_name assert None == ip_address
def test_get_dns_name_with_ip_in_options(mocker): config = _get_mock_config() ip_address_connect_mock = mocker.patch( "mount_efs.mount_target_ip_address_can_be_resolved", return_value=True) dns_name, ip_address = mount_efs.get_dns_name_and_fallback_mount_target_ip_address( config, FS_ID, OPTIONS_WITH_IP) assert "%s.efs.%s.amazonaws.com" % (FS_ID, DEFAULT_REGION) == dns_name assert IP_ADDRESS == ip_address utils.assert_called(ip_address_connect_mock)
def test_get_dns_name_region_and_suffix_hardcoded(mocker): get_target_region_mock = mocker.patch("mount_efs.get_target_region") config = _get_mock_config("{az}.{fs_id}.efs.us-west-2.amazonaws.com") dns_name, ip_address = mount_efs.get_dns_name_and_fallback_mount_target_ip_address( config, FS_ID, DEFAULT_NFS_OPTIONS) utils.assert_not_called(get_target_region_mock) assert "%s.efs.us-west-2.amazonaws.com" % FS_ID == dns_name assert None == ip_address
def test_get_dns_name_region_hardcoded(mocker): get_target_region_mock = mocker.patch('mount_efs.get_target_region') config = _get_mock_config('{az}.{fs_id}.efs.%s.{dns_name_suffix}' % DEFAULT_REGION) dns_name, ip_address = mount_efs.get_dns_name_and_fallback_mount_target_ip_address( config, FS_ID, DEFAULT_NFS_OPTIONS) utils.assert_not_called(get_target_region_mock) assert '%s.efs.%s.amazonaws.com' % (FS_ID, DEFAULT_REGION) == dns_name assert None == ip_address
def test_get_dns_name_and_fall_back_ip_address_failure(mocker, capsys): ''' When the dns name cannot be resolved, and the fallback to mount target ip address throw FallbackException ''' config = _get_mock_config() dns_mock = mocker.patch('socket.gethostbyname', side_effect=socket.gaierror) get_fallback_mount_target_ip_mock = mocker.patch( 'mount_efs.get_fallback_mount_target_ip_address', side_effect=mount_efs.FallbackException('timeout')) with pytest.raises(SystemExit) as ex: mount_efs.get_dns_name_and_fallback_mount_target_ip_address( config, FS_ID, DEFAULT_NFS_OPTIONS) assert 0 != ex.value.code out, err = capsys.readouterr() assert 'cannot be retrieved' in err utils.assert_called(dns_mock) utils.assert_called(get_fallback_mount_target_ip_mock)
def test_get_dns_name_special_region(mocker): for special_region in SPECIAL_REGIONS: mocker.patch('mount_efs.get_target_region', return_value=special_region) config_section = 'mount.%s' % special_region special_dns_name_suffix = SPECIAL_REGION_DNS_DICT[special_region] config = _get_mock_config(dns_name_suffix=special_dns_name_suffix, config_section=config_section) dns_name, ip_address = mount_efs.get_dns_name_and_fallback_mount_target_ip_address( config, FS_ID, DEFAULT_NFS_OPTIONS) assert '%s.efs.%s.%s' % (FS_ID, special_region, special_dns_name_suffix) == dns_name assert ip_address == None
def test_match_device_fqdn_same_as_dns_name_with_az(mocker, capsys): dns_name = '%s.%s.efs.us-east-1.amazonaws.com' % (DEFAULT_AZ, FS_ID) gethostbyname_ex_mock = mocker.patch('socket.gethostbyname_ex', return_value=(dns_name, [], None)) efs_fqdn_match = mount_efs.EFS_FQDN_RE.match(dns_name) assert efs_fqdn_match assert FS_ID == efs_fqdn_match.group('fs_id') config = _get_mock_config() expected_dns_name, ip_address = mount_efs.get_dns_name_and_fallback_mount_target_ip_address( config, FS_ID, OPTIONS_WITH_AZ) assert dns_name == expected_dns_name assert None == ip_address for device, (fs_id, path, az) in CORRECT_DEVICE_DESCRIPTORS_CNAME_DNS_WITH_AZ: assert (fs_id, path, az) == mount_efs.match_device(config, device, OPTIONS_WITH_AZ) utils.assert_called(gethostbyname_ex_mock)
def test_get_dns_name_region_in_suffix(mocker): get_target_region_mock = mocker.patch('mount_efs.get_target_region') for special_region in SPECIAL_REGIONS: special_dns_name_suffix = SPECIAL_REGION_DNS_DICT[special_region] dns_name_suffix = '%s.%s' % (special_region, special_dns_name_suffix) config = _get_mock_config('{fs_id}.efs.{dns_name_suffix}', dns_name_suffix=dns_name_suffix) dns_name, ip_address = mount_efs.get_dns_name_and_fallback_mount_target_ip_address( config, FS_ID, DEFAULT_NFS_OPTIONS) utils.assert_not_called(get_target_region_mock) assert '%s.efs.%s.%s' % (FS_ID, special_region, special_dns_name_suffix) == dns_name assert None == ip_address
def test_get_dns_name_and_fall_back_ip_address_success(mocker): """ When the dns name cannot be resolved, and the fallback to mount target ip address is retrieved """ config = _get_mock_config() dns_mock = mocker.patch("socket.gethostbyname", side_effect=socket.gaierror) get_fallback_mount_target_ip_mock = mocker.patch( "mount_efs.get_fallback_mount_target_ip_address", return_value=IP_ADDRESS) dns_name, ip_address = mount_efs.get_dns_name_and_fallback_mount_target_ip_address( config, FS_ID, DEFAULT_NFS_OPTIONS) assert "%s.efs.%s.amazonaws.com" % (FS_ID, DEFAULT_REGION) == dns_name assert IP_ADDRESS == ip_address utils.assert_called(dns_mock) utils.assert_called(get_fallback_mount_target_ip_mock)
def test_get_dns_name_and_mount_target_ip_address_via_option_success(mocker): """ When the mount target ip address is passed through mount options and can be connected """ config = _get_mock_config() dns_mock = mocker.patch("socket.gethostbyname") get_fallback_mount_target_ip_mock = mocker.patch( "mount_efs.get_fallback_mount_target_ip_address") ip_address_connect_mock = mocker.patch( "mount_efs.mount_target_ip_address_can_be_resolved", return_value=True) dns_name, ip_address = mount_efs.get_dns_name_and_fallback_mount_target_ip_address( config, FS_ID, OPTIONS_WITH_IP) assert "%s.efs.%s.amazonaws.com" % (FS_ID, DEFAULT_REGION) == dns_name assert IP_ADDRESS == ip_address utils.assert_not_called(dns_mock) utils.assert_not_called(get_fallback_mount_target_ip_mock) utils.assert_called(ip_address_connect_mock)
def test_match_device_fqdn_same_as_dns_name(mocker, capsys): dns_name = "%s.efs.us-east-1.amazonaws.com" % FS_ID gethostbyname_ex_mock = mocker.patch("socket.gethostbyname_ex", return_value=(dns_name, [], None)) efs_fqdn_match = mount_efs.EFS_FQDN_RE.match(dns_name) assert efs_fqdn_match assert FS_ID == efs_fqdn_match.group("fs_id") config = _get_mock_config() ( expected_dns_name, ip_address, ) = mount_efs.get_dns_name_and_fallback_mount_target_ip_address( config, FS_ID, DEFAULT_NFS_OPTIONS) assert dns_name == expected_dns_name assert None == ip_address for device, (fs_id, path, az) in CORRECT_DEVICE_DESCRIPTORS_CNAME_DNS: assert (fs_id, path, az) == mount_efs.match_device(config, device, DEFAULT_NFS_OPTIONS) utils.assert_called(gethostbyname_ex_mock)