def test_get_dns_name_and_fall_back_ip_address_cannot_be_resolved(
        mocker, capsys):
    """
    When the fallback to mount target ip address is enabled, the mount target ip address is retrieved but cannot be connected
    """
    config = _get_mock_config()
    mount_efs.BOTOCORE_PRESENT = True
    check_fallback_enabled_mock = mocker.patch(
        "mount_efs.check_if_fall_back_to_mount_target_ip_address_is_enabled",
        return_value=True,
    )
    get_fallback_mount_target_ip_mock = mocker.patch(
        "mount_efs.get_fallback_mount_target_ip_address_helper",
        return_value=FALLBACK_IP_ADDRESS,
    )
    check_ip_resolve_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_fallback_mount_target_ip_address(config, FS_ID,
                                                       DEFAULT_NFS_OPTIONS,
                                                       DNS_NAME)

        assert 0 != ex.value.code

        out, err = capsys.readouterr()
        assert "Failed to resolve" in err
        assert "cannot be found" in err

    utils.assert_called(check_fallback_enabled_mock)
    utils.assert_called(get_fallback_mount_target_ip_mock)
    utils.assert_called(check_ip_resolve_mock)
Beispiel #2
0
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_fallback_mount_target_ip_address_throw_fallback_exception(mocker):
    """
    This tests make sure that all exception is not handled and thrown by default to upper level
    """
    config = _get_mock_config()

    get_target_az_mock = mocker.patch('mount_efs.get_target_az',
                                      return_value=DEFAULT_AZ)
    get_botocore_client_mock = mocker.patch(
        'mount_efs.get_botocore_client',
        side_effect=[MOCK_EFS_AGENT, MOCK_EC2_AGENT])
    get_mount_target_az_mock = mocker.patch(
        'mount_efs.get_mount_target_in_az',
        side_effect=mount_efs.FallbackException('No mount target'))

    with pytest.raises(mount_efs.FallbackException) as excinfo:
        mount_efs.get_fallback_mount_target_ip_address_helper(
            config, {}, FS_ID)
    assert 'No mount target' in str(excinfo)

    utils.assert_called_once(get_target_az_mock)
    utils.assert_called_n_times(get_botocore_client_mock, 2)
    utils.assert_called_once(get_mount_target_az_mock)
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)