Beispiel #1
0
    def test_url_resolvable(self):
        """test_url_resolvable - Test resolving urls"""

        with mock.patch.object(util, 'is_resolvable') as mockresolve:
            util.is_resolvable_url("http://1.2.3.4/ubuntu")
        mockresolve.assert_called_with("1.2.3.4")

        with mock.patch.object(util, 'is_resolvable') as mockresolve:
            util.is_resolvable_url("http://us.archive.ubuntu.com/ubuntu")
        mockresolve.assert_called_with("us.archive.ubuntu.com")

        bad = [(None, None, None, "badname", ["10.3.2.1"])]
        good = [(None, None, None, "goodname", ["10.2.3.4"])]
        with mock.patch.object(socket, 'getaddrinfo',
                               side_effect=[bad, bad, good,
                                            good]) as mocksock:
            ret = util.is_resolvable_url("http://us.archive.ubuntu.com/ubuntu")
            ret2 = util.is_resolvable_url("http://1.2.3.4/ubuntu")
        calls = [call('does-not-exist.example.com.', None, 0, 0, 1, 2),
                 call('example.invalid.', None, 0, 0, 1, 2),
                 call('us.archive.ubuntu.com', None),
                 call('1.2.3.4', None)]
        mocksock.assert_has_calls(calls)
        self.assertTrue(ret)
        self.assertTrue(ret2)

        # side effect need only bad ret after initial call
        with mock.patch.object(socket, 'getaddrinfo',
                               side_effect=[bad]) as mocksock:
            ret3 = util.is_resolvable_url("http://failme.com/ubuntu")
        calls = [call('failme.com', None)]
        mocksock.assert_has_calls(calls)
        self.assertFalse(ret3)
Beispiel #2
0
    def test_mirror_search(self):
        """test_mirror_search
           Check searching through a mirror list
           This is checked in the test (late) intentionally.
           No matter if resolution worked or failed it shouldn't fail
           fatally (python error and trace).
           We just can't rely on the content to be found in that case
           so we skip the check then."""
        res1 = util.is_resolvable_url("http://does.not.exist/ubuntu")
        res2 = util.is_resolvable_url("http://does.also.not.exist/ubuntu")
        res3 = util.is_resolvable_url("http://us.archive.ubuntu.com/ubuntu")
        res4 = util.is_resolvable_url("http://security.ubuntu.com/ubuntu")
        if res1 or res2 or not res3 or not res4:
            raise SkipTest(("Name resolution not as required"
                            "(%s, %s, %s, %s)" % (res1, res2, res3, res4)))

        self.check_file_regex("sources.list", r"us.archive.ubuntu.com")
        self.check_file_regex("sources.list", r"security.ubuntu.com")
Beispiel #3
0
def search_for_mirror(candidates):
    """
    Search through a list of mirror urls for one that works
    This needs to return quickly.
    """
    if candidates is None:
        return None

    LOG.debug("search for mirror in candidates: '%s'", candidates)
    for cand in candidates:
        try:
            if util.is_resolvable_url(cand):
                LOG.debug("found working mirror: '%s'", cand)
                return cand
        except Exception:
            pass
    return None