Exemple #1
0
def test_download_timeout():

    with requests_mock.Mocker() as m:
        m.register_uri("GET", TEST_URL, exc=requests.exceptions.ConnectTimeout)

        with pytest.raises(MoulinetteError):
            download_text(TEST_URL)
Exemple #2
0
def test_download_404():

    with requests_mock.Mocker() as m:
        m.register_uri("GET", TEST_URL, status_code=404)

        with pytest.raises(MoulinetteError):
            download_text(TEST_URL)
Exemple #3
0
def test_download_sslerror():

    with requests_mock.Mocker() as m:
        m.register_uri("GET", TEST_URL, exc=requests.exceptions.SSLError)

        with pytest.raises(MoulinetteError):
            download_text(TEST_URL)
Exemple #4
0
def test_download_timeout():

    with requests_mock.Mocker() as m:
        m.register_uri("GET", TEST_URL, exc=requests.exceptions.ConnectTimeout)

        with pytest.raises(MoulinetteError):
            download_text(TEST_URL)
Exemple #5
0
def test_download_sslerror():

    with requests_mock.Mocker() as m:
        m.register_uri("GET", TEST_URL, exc=requests.exceptions.SSLError)

        with pytest.raises(MoulinetteError):
            download_text(TEST_URL)
Exemple #6
0
def test_download_404():

    with requests_mock.Mocker() as m:
        m.register_uri("GET", TEST_URL, status_code=404)

        with pytest.raises(MoulinetteError):
            download_text(TEST_URL)
Exemple #7
0
def get_public_ip_from_remote_server(protocol=4):
    """Retrieve the public IP address from ip.yunohost.org"""

    # We can know that ipv6 is not available directly if this file does not exists
    if protocol == 6 and not os.path.exists("/proc/net/if_inet6"):
        logger.debug("IPv6 appears not at all available on the system, so assuming there's no IP address for that version")
        return None

    # If we are indeed connected in ipv4 or ipv6, we should find a default route
    routes = check_output("ip -%s route show table all" % protocol).split("\n")

    def is_default_route(r):
        # Typically the default route starts with "default"
        # But of course IPv6 is more complex ... e.g. on internet cube there's
        # no default route but a /3 which acts as a default-like route...
        # e.g. 2000:/3 dev tun0 ...
        return r.startswith("default") or (":" in r and re.match(r".*/[0-3]$", r.split()[0]))
    if not any(is_default_route(r) for r in routes):
        logger.debug("No default route for IPv%s, so assuming there's no IP address for that version" % protocol)
        return None

    url = 'https://ip%s.yunohost.org' % (protocol if protocol != 4 else '')
    logger.debug("Fetching IP from %s " % url)

    try:
        return download_text(url, timeout=30).strip()
    except Exception as e:
        logger.debug("Could not get public IPv%s : %s" % (str(protocol), str(e)))
        return None
Exemple #8
0
def test_download():

    with requests_mock.Mocker() as m:
        m.register_uri("GET", TEST_URL, text='some text')

        fetched_text = download_text(TEST_URL)

    assert fetched_text == "some text"
Exemple #9
0
def test_download():

    with requests_mock.Mocker() as m:
        m.register_uri("GET", TEST_URL, text='some text')

        fetched_text = download_text(TEST_URL)

    assert fetched_text == "some text"
Exemple #10
0
    def get_public_ip(self, protocol=4):

        # FIXME - TODO : here we assume that DNS resolution for ip.yunohost.org is working
        # but if we want to be able to diagnose DNS resolution issues independently from
        # internet connectivity, we gotta rely on fixed IPs first....

        assert protocol in [
            4, 6
        ], "Invalid protocol version, it should be either 4 or 6 and was '%s'" % repr(
            protocol)

        url = 'https://ip%s.yunohost.org' % ('6' if protocol == 6 else '')

        try:
            return download_text(url, timeout=30).strip()
        except Exception as e:
            self.logger_debug("Could not get public IPv%s : %s" %
                              (str(protocol), str(e)))
            return None
Exemple #11
0
def test_download_badurl():

    with pytest.raises(MoulinetteError):
        download_text(TEST_URL)
Exemple #12
0
def test_download(test_url):
    with requests_mock.Mocker() as mock:
        mock.register_uri("GET", test_url, text="some text")
        fetched_text = download_text(test_url)
    assert fetched_text == "some text"
Exemple #13
0
def test_download_timeout(test_url):
    with requests_mock.Mocker() as mock:
        exception = requests.exceptions.Timeout
        mock.register_uri("GET", test_url, exc=exception)
        with pytest.raises(MoulinetteError):
            download_text(test_url)
Exemple #14
0
def test_download_404(test_url):
    with requests_mock.Mocker() as mock:
        mock.register_uri("GET", test_url, status_code=404)
        with pytest.raises(MoulinetteError):
            download_text(test_url)
Exemple #15
0
def test_download_bad_url():
    with pytest.raises(MoulinetteError):
        download_text("Nowhere")
Exemple #16
0
def test_download_badurl():

    with pytest.raises(MoulinetteError):
        download_text(TEST_URL)