Exemplo n.º 1
0
    def setUp(self):
        yield super(TestTorrentHealthEndpoint, self).setUp()

        min_base_port, max_base_port = self.get_bucket_range_port()

        self.udp_port = get_random_port(min_port=min_base_port,
                                        max_port=max_base_port)
        self.udp_tracker = UDPTracker(self.udp_port)

        self.http_port = get_random_port(min_port=min_base_port,
                                         max_port=max_base_port)
        self.http_tracker = HTTPTracker(self.http_port)
Exemplo n.º 2
0
    def setUp(self):
        yield super(TestTorrentHealthEndpoint, self).setUp()

        min_base_port, max_base_port = self.get_bucket_range_port()

        self.udp_port = get_random_port(min_port=min_base_port, max_port=max_base_port)
        self.udp_tracker = UDPTracker(self.udp_port)

        self.http_port = get_random_port(min_port=min_base_port, max_port=max_base_port)
        self.http_tracker = HTTPTracker(self.http_port)
Exemplo n.º 3
0
class TestTorrentHealthEndpoint(AbstractApiTest):

    def setUpPreSession(self):
        super(TestTorrentHealthEndpoint, self).setUpPreSession()
        self.config.set_chant_enabled(True)

    @inlineCallbacks
    def setUp(self):
        yield super(TestTorrentHealthEndpoint, self).setUp()

        min_base_port, max_base_port = self.get_bucket_range_port()

        self.udp_port = get_random_port(min_port=min_base_port, max_port=max_base_port)
        self.udp_tracker = UDPTracker(self.udp_port)

        self.http_port = get_random_port(min_port=min_base_port, max_port=max_base_port)
        self.http_tracker = HTTPTracker(self.http_port)

    @inlineCallbacks
    def tearDown(self):
        self.session.lm.ltmgr = None
        if self.udp_tracker:
            yield self.udp_tracker.stop()
        if self.http_tracker:
            yield self.http_tracker.stop()
        yield super(TestTorrentHealthEndpoint, self).tearDown()

    @trial_timeout(20)
    @inlineCallbacks
    def test_check_torrent_health(self):
        """
        Test the endpoint to fetch the health of a chant-managed, infohash-only torrent
        """
        infohash = 'a' * 20
        tracker_url = 'udp://localhost:%s/announce' % self.udp_port
        self.udp_tracker.tracker_info.add_info_about_infohash(infohash, 12, 11, 1)

        with db_session:
            tracker_state = self.session.lm.mds.TrackerState(url=tracker_url)
            torrent_state = self.session.lm.mds.TorrentState(trackers=tracker_state, infohash=infohash)
            self.session.lm.mds.TorrentMetadata(infohash=infohash,
                                                title='ubuntu-torrent.iso',
                                                size=42,
                                                tracker_info=tracker_url,
                                                health=torrent_state)
        url = 'metadata/torrents/%s/health?timeout=10&refresh=1' % hexlify(infohash)
        self.should_check_equality = False

        # Initialize the torrent checker
        self.session.lm.torrent_checker = TorrentChecker(self.session)
        self.session.lm.torrent_checker.initialize()

        def verify_response_no_trackers(response):
            json_response = json.loads(response)
            self.assertIn("health", json_response)
            self.assertIn("udp://localhost:%s" % self.udp_port, json_response['health'])
            if has_bep33_support():
                self.assertIn("DHT", json_response['health'])

        # Add mock DHT response
        self.session.lm.ltmgr = MockObject()
        self.session.lm.ltmgr.dht_health_manager = MockObject()
        dht_health_dict = {
            "infohash": hexlify(infohash),
            "seeders": 1,
            "leechers": 2
        }
        self.session.lm.ltmgr.dht_health_manager.get_health = lambda *_, **__: succeed({"DHT": [dht_health_dict]})

        # Left for compatibility with other tests in this object
        self.udp_tracker.start()
        self.http_tracker.start()
        yield self.do_request(url, expected_code=200, request_type='GET').addCallback(verify_response_no_trackers)

        def verify_response_nowait(response):
            json_response = json.loads(response)
            self.assertDictEqual(json_response, {u'checking': u'1'})

        yield self.do_request(url + '&nowait=1', expected_code=200, request_type='GET').addCallback(
            verify_response_nowait)
Exemplo n.º 4
0
class TestTorrentHealthEndpoint(AbstractApiTest):
    def setUpPreSession(self):
        super(TestTorrentHealthEndpoint, self).setUpPreSession()
        self.config.set_chant_enabled(True)

    @inlineCallbacks
    def setUp(self):
        yield super(TestTorrentHealthEndpoint, self).setUp()

        min_base_port, max_base_port = self.get_bucket_range_port()

        self.udp_port = get_random_port(min_port=min_base_port,
                                        max_port=max_base_port)
        self.udp_tracker = UDPTracker(self.udp_port)

        self.http_port = get_random_port(min_port=min_base_port,
                                         max_port=max_base_port)
        self.http_tracker = HTTPTracker(self.http_port)

    @inlineCallbacks
    def tearDown(self):
        self.session.lm.ltmgr = None
        if self.udp_tracker:
            yield self.udp_tracker.stop()
        if self.http_tracker:
            yield self.http_tracker.stop()
        yield super(TestTorrentHealthEndpoint, self).tearDown()

    @trial_timeout(20)
    @inlineCallbacks
    def test_check_torrent_health(self):
        """
        Test the endpoint to fetch the health of a chant-managed, infohash-only torrent
        """
        infohash = 'a' * 20
        tracker_url = 'udp://localhost:%s/announce' % self.udp_port
        self.udp_tracker.tracker_info.add_info_about_infohash(
            infohash, 12, 11, 1)

        with db_session:
            tracker_state = self.session.lm.mds.TrackerState(url=tracker_url)
            torrent_state = self.session.lm.mds.TorrentState(
                trackers=tracker_state, infohash=infohash)
            self.session.lm.mds.TorrentMetadata(infohash=infohash,
                                                title='ubuntu-torrent.iso',
                                                size=42,
                                                tracker_info=tracker_url,
                                                health=torrent_state)
        url = 'metadata/torrents/%s/health?timeout=10&refresh=1' % hexlify(
            infohash)
        self.should_check_equality = False

        # Initialize the torrent checker
        self.session.lm.torrent_checker = TorrentChecker(self.session)
        self.session.lm.torrent_checker.initialize()

        def verify_response_no_trackers(response):
            json_response = json.loads(response)
            self.assertIn("health", json_response)
            self.assertIn("udp://localhost:%s" % self.udp_port,
                          json_response['health'])
            if has_bep33_support():
                self.assertIn("DHT", json_response['health'])

        # Add mock DHT response
        self.session.lm.ltmgr = MockObject()
        self.session.lm.ltmgr.dht_health_manager = MockObject()
        dht_health_dict = {
            "infohash": hexlify(infohash),
            "seeders": 1,
            "leechers": 2
        }
        self.session.lm.ltmgr.dht_health_manager.get_health = lambda *_, **__: succeed(
            {"DHT": [dht_health_dict]})

        # Left for compatibility with other tests in this object
        self.udp_tracker.start()
        self.http_tracker.start()
        yield self.do_request(
            url, expected_code=200,
            request_type='GET').addCallback(verify_response_no_trackers)

        def verify_response_nowait(response):
            json_response = json.loads(response)
            self.assertDictEqual(json_response, {u'checking': u'1'})

        yield self.do_request(
            url + '&nowait=1', expected_code=200,
            request_type='GET').addCallback(verify_response_nowait)
Exemplo n.º 5
0
class TestTorrentHealthEndpoint(AbstractApiTest):
    @blocking_call_on_reactor_thread
    @inlineCallbacks
    def setUp(self, autoload_discovery=True):
        yield super(TestTorrentHealthEndpoint,
                    self).setUp(autoload_discovery=autoload_discovery)

        min_base_port, max_base_port = self.get_bucket_range_port()

        self.udp_port = get_random_port(min_port=min_base_port,
                                        max_port=max_base_port)
        self.udp_tracker = UDPTracker(self.udp_port)

        self.http_port = get_random_port(min_port=min_base_port,
                                         max_port=max_base_port)
        self.http_tracker = HTTPTracker(self.http_port)

    @blocking_call_on_reactor_thread
    @inlineCallbacks
    def tearDown(self, annotate=True):
        self.session.lm.ltmgr = None
        yield self.udp_tracker.stop()
        yield self.http_tracker.stop()
        yield super(TestTorrentHealthEndpoint,
                    self).tearDown(annotate=annotate)

    @deferred(timeout=20)
    @inlineCallbacks
    def test_check_torrent_health(self):
        """
        Test the endpoint to fetch the health of a torrent
        """
        torrent_db = self.session.open_dbhandler(NTFY_TORRENTS)
        torrent_db.addExternalTorrentNoDef(
            'a' * 20, 'ubuntu-torrent.iso', [['file1.txt', 42]],
            ('udp://localhost:%s/announce' % self.udp_port,
             'http://localhost:%s/announce' % self.http_port), time.time())

        url = 'torrents/%s/health?timeout=10&refresh=1' % ('a' *
                                                           20).encode('hex')

        self.should_check_equality = False
        yield self.do_request(url, expected_code=400,
                              request_type='GET')  # No torrent checker

        def call_cb(infohash, callback, **_):
            callback({"seeders": 1, "leechers": 2})

        # Initialize the torrent checker
        self.session.lm.torrent_checker = TorrentChecker(self.session)
        self.session.lm.torrent_checker.initialize()
        self.session.lm.ltmgr = MockObject()
        self.session.lm.ltmgr.get_metainfo = call_cb

        yield self.do_request('torrents/%s/health' % ('f' * 40),
                              expected_code=404,
                              request_type='GET')

        def verify_response_no_trackers(response):
            json_response = json.loads(response)
            self.assertTrue('DHT' in json_response['health'])

        def verify_response_with_trackers(response):
            json_response = json.loads(response)
            expected_dict = {
                u"health": {
                    u"DHT": {
                        u"leechers": 2,
                        u"seeders": 1,
                        u"infohash": (u'a' * 20).encode('hex')
                    },
                    u"udp://localhost:%s" % self.udp_port: {
                        u"leechers": 20,
                        u"seeders": 10,
                        u"infohash": (u'a' * 20).encode('hex')
                    },
                    u"http://localhost:%s/announce" % self.http_port: {
                        u"leechers": 30,
                        u"seeders": 20,
                        u"infohash": (u'a' * 20).encode('hex')
                    }
                }
            }
            self.assertDictEqual(json_response, expected_dict)

        yield self.do_request(
            url, expected_code=200,
            request_type='GET').addCallback(verify_response_no_trackers)

        self.udp_tracker.start()
        self.udp_tracker.tracker_info.add_info_about_infohash('a' * 20, 10, 20)

        self.http_tracker.start()
        self.http_tracker.tracker_info.add_info_about_infohash(
            'a' * 20, 20, 30)

        yield self.do_request(
            url, expected_code=200,
            request_type='GET').addCallback(verify_response_with_trackers)
Exemplo n.º 6
0
class TestTorrentHealthEndpoint(AbstractApiTest):
    def setUpPreSession(self):
        super(TestTorrentHealthEndpoint, self).setUpPreSession()
        self.config.set_chant_enabled(True)

    @inlineCallbacks
    def setUp(self):
        yield super(TestTorrentHealthEndpoint, self).setUp()

        min_base_port, max_base_port = self.get_bucket_range_port()

        self.udp_port = get_random_port(min_port=min_base_port,
                                        max_port=max_base_port)
        self.udp_tracker = UDPTracker(self.udp_port)

        self.http_port = get_random_port(min_port=min_base_port,
                                         max_port=max_base_port)
        self.http_tracker = HTTPTracker(self.http_port)

    @inlineCallbacks
    def tearDown(self):
        self.session.lm.ltmgr = None
        if self.udp_tracker:
            yield self.udp_tracker.stop()
        if self.http_tracker:
            yield self.http_tracker.stop()
        yield super(TestTorrentHealthEndpoint, self).tearDown()

    @trial_timeout(20)
    @inlineCallbacks
    def test_check_torrent_health(self):
        """
        Test the endpoint to fetch the health of a torrent
        """
        torrent_db = self.session.open_dbhandler(NTFY_TORRENTS)
        torrent_db.addExternalTorrentNoDef(
            'a' * 20, 'ubuntu-torrent.iso', [['file1.txt', 42]],
            ('udp://localhost:%s/announce' % self.udp_port,
             'http://localhost:%s/announce' % self.http_port), time.time())

        url = 'torrents/%s/health?timeout=10&refresh=1' % ('a' *
                                                           20).encode('hex')

        self.should_check_equality = False
        yield self.do_request(url, expected_code=400,
                              request_type='GET')  # No torrent checker

        def call_cb(infohash, callback, **_):
            callback({"seeders": 1, "leechers": 2})

        # Initialize the torrent checker
        self.session.lm.torrent_checker = TorrentChecker(self.session)
        self.session.lm.torrent_checker.initialize()
        self.session.lm.ltmgr = MockObject()
        self.session.lm.ltmgr.get_metainfo = call_cb

        yield self.do_request('torrents/%s/health' % ('f' * 40),
                              expected_code=404,
                              request_type='GET')

        def verify_response_no_trackers(response):
            json_response = json.loads(response)
            self.assertTrue('DHT' in json_response['health'])

        def verify_response_with_trackers(response):
            json_response = json.loads(response)
            expected_dict = {
                u"health": {
                    u"DHT": {
                        u"leechers": 2,
                        u"seeders": 1,
                        u"infohash": (u'a' * 20).encode('hex')
                    },
                    u"udp://localhost:%s" % self.udp_port: {
                        u"leechers": 20,
                        u"seeders": 10,
                        u"infohash": (u'a' * 20).encode('hex')
                    },
                    u"http://localhost:%s/announce" % self.http_port: {
                        u"leechers": 30,
                        u"seeders": 20,
                        u"infohash": (u'a' * 20).encode('hex')
                    }
                }
            }
            self.assertDictEqual(json_response, expected_dict)

        yield self.do_request(
            url, expected_code=200,
            request_type='GET').addCallback(verify_response_no_trackers)

        self.udp_tracker.start()
        self.udp_tracker.tracker_info.add_info_about_infohash('a' * 20, 10, 20)

        self.http_tracker.start()
        self.http_tracker.tracker_info.add_info_about_infohash(
            'a' * 20, 20, 30)

        yield self.do_request(
            url, expected_code=200,
            request_type='GET').addCallback(verify_response_with_trackers)

    @trial_timeout(20)
    @inlineCallbacks
    def test_check_torrent_health_chant(self):
        """
        Test the endpoint to fetch the health of a chant-managed, infohash-only torrent
        """
        infohash = 'a' * 20
        tracker_url = 'udp://localhost:%s/announce' % self.udp_port

        meta_info = {
            "info": {
                "name": "my_torrent",
                "piece length": 42,
                "root hash": infohash,
                "files": [],
                "url-list": tracker_url
            }
        }
        tdef = TorrentDef.load_from_dict(meta_info)

        with db_session:
            self.session.lm.mds.TorrentMetadata(infohash=tdef.infohash,
                                                title='ubuntu-torrent.iso',
                                                size=42,
                                                tracker_info=tracker_url)
        url = 'torrents/%s/health?timeout=10&refresh=1' % tdef.infohash.encode(
            'hex')
        self.should_check_equality = False

        def fake_get_metainfo(_,
                              callback,
                              timeout=10,
                              timeout_callback=None,
                              notify=True):
            meta_info_extended = meta_info.copy()
            meta_info_extended['seeders'] = 12
            meta_info_extended['leechers'] = 11
            callback(meta_info_extended)

        # Initialize the torrent checker
        self.session.lm.torrent_checker = TorrentChecker(self.session)
        self.session.lm.torrent_checker.initialize()
        self.session.lm.ltmgr = MockObject()
        self.session.lm.ltmgr.get_metainfo = fake_get_metainfo

        def verify_response_no_trackers(response):
            json_response = json.loads(response)
            expected_dict = {
                u"health": {
                    u"DHT": {
                        u"leechers": 11,
                        u"seeders": 12,
                        u"infohash": unicode(tdef.infohash.encode('hex'))
                    }
                }
            }
            self.assertDictEqual(json_response, expected_dict)

        # Left for compatibility with other tests in this object
        self.udp_tracker.start()
        self.http_tracker.start()
        # TODO: add test for DHT timeout
        yield self.do_request(
            url, expected_code=200,
            request_type='GET').addCallback(verify_response_no_trackers)
Exemplo n.º 7
0
class TestTorrentHealthEndpoint(AbstractApiTest):

    def setUpPreSession(self):
        super(TestTorrentHealthEndpoint, self).setUpPreSession()
        self.config.set_chant_enabled(True)

    @inlineCallbacks
    def setUp(self):
        yield super(TestTorrentHealthEndpoint, self).setUp()

        min_base_port, max_base_port = self.get_bucket_range_port()

        self.udp_port = get_random_port(min_port=min_base_port, max_port=max_base_port)
        self.udp_tracker = UDPTracker(self.udp_port)

        self.http_port = get_random_port(min_port=min_base_port, max_port=max_base_port)
        self.http_tracker = HTTPTracker(self.http_port)

    @inlineCallbacks
    def tearDown(self):
        self.session.lm.ltmgr = None
        if self.udp_tracker:
            yield self.udp_tracker.stop()
        if self.http_tracker:
            yield self.http_tracker.stop()
        yield super(TestTorrentHealthEndpoint, self).tearDown()

    @trial_timeout(20)
    @inlineCallbacks
    def test_check_torrent_health(self):
        """
        Test the endpoint to fetch the health of a torrent
        """
        torrent_db = self.session.open_dbhandler(NTFY_TORRENTS)
        torrent_db.addExternalTorrentNoDef('a' * 20, 'ubuntu-torrent.iso', [['file1.txt', 42]],
                                           ('udp://localhost:%s/announce' % self.udp_port,
                                            'http://localhost:%s/announce' % self.http_port), time.time())

        url = 'torrents/%s/health?timeout=10&refresh=1' % ('a' * 20).encode('hex')

        self.should_check_equality = False
        yield self.do_request(url, expected_code=400, request_type='GET')  # No torrent checker

        def call_cb(infohash, callback, **_):
            callback({"seeders": 1, "leechers": 2})

        # Initialize the torrent checker
        self.session.lm.torrent_checker = TorrentChecker(self.session)
        self.session.lm.torrent_checker.initialize()
        self.session.lm.ltmgr = MockObject()
        self.session.lm.ltmgr.get_metainfo = call_cb

        yield self.do_request('torrents/%s/health' % ('f' * 40), expected_code=404, request_type='GET')

        def verify_response_no_trackers(response):
            json_response = json.loads(response)
            self.assertTrue('DHT' in json_response['health'])

        def verify_response_with_trackers(response):
            json_response = json.loads(response)
            expected_dict = {u"health":
                                 {u"DHT":
                                      {u"leechers": 2, u"seeders": 1, u"infohash": (u'a' * 20).encode('hex')},
                                  u"udp://localhost:%s" % self.udp_port:
                                      {u"leechers": 20, u"seeders": 10, u"infohash": (u'a' * 20).encode('hex')},
                                  u"http://localhost:%s/announce" % self.http_port:
                                      {u"leechers": 30, u"seeders": 20, u"infohash": (u'a' * 20).encode('hex')}}}
            self.assertDictEqual(json_response, expected_dict)

        yield self.do_request(url, expected_code=200, request_type='GET').addCallback(verify_response_no_trackers)

        self.udp_tracker.start()
        self.udp_tracker.tracker_info.add_info_about_infohash('a' * 20, 10, 20)

        self.http_tracker.start()
        self.http_tracker.tracker_info.add_info_about_infohash('a' * 20, 20, 30)

        yield self.do_request(url, expected_code=200, request_type='GET').addCallback(verify_response_with_trackers)

    @trial_timeout(20)
    @inlineCallbacks
    def test_check_torrent_health_chant(self):
        """
        Test the endpoint to fetch the health of a chant-managed, infohash-only torrent
        """
        infohash = 'a' * 20
        tracker_url = 'udp://localhost:%s/announce' % self.udp_port

        meta_info = {"info": {"name": "my_torrent", "piece length": 42,
                              "root hash": infohash, "files": [],
                              "url-list": tracker_url}}
        tdef = TorrentDef.load_from_dict(meta_info)

        with db_session:
            self.session.lm.mds.TorrentMetadata(infohash=tdef.infohash,
                                                title='ubuntu-torrent.iso',
                                                size=42,
                                                tracker_info=tracker_url)
        url = 'torrents/%s/health?timeout=10&refresh=1' % tdef.infohash.encode('hex')
        self.should_check_equality = False

        def fake_get_metainfo(_, callback, timeout=10, timeout_callback=None, notify=True):
            meta_info_extended = meta_info.copy()
            meta_info_extended['seeders'] = 12
            meta_info_extended['leechers'] = 11
            callback(meta_info_extended)

        # Initialize the torrent checker
        self.session.lm.torrent_checker = TorrentChecker(self.session)
        self.session.lm.torrent_checker.initialize()
        self.session.lm.ltmgr = MockObject()
        self.session.lm.ltmgr.get_metainfo = fake_get_metainfo

        def verify_response_no_trackers(response):
            json_response = json.loads(response)
            expected_dict = {u"health":
                                 {u"DHT":
                                      {u"leechers": 11, u"seeders": 12,
                                       u"infohash": unicode(tdef.infohash.encode('hex'))}}}
            self.assertDictEqual(json_response, expected_dict)

        # Left for compatibility with other tests in this object
        self.udp_tracker.start()
        self.http_tracker.start()
        # TODO: add test for DHT timeout
        yield self.do_request(url, expected_code=200, request_type='GET').addCallback(verify_response_no_trackers)