2016-11-16 11:41:34 -08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
'''
|
|
|
|
test_units.py - some unit tests for parts of brozzler amenable to that
|
|
|
|
|
2017-02-15 16:46:45 -08:00
|
|
|
Copyright (C) 2016-2017 Internet Archive
|
2016-11-16 11:41:34 -08:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
'''
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import http.server
|
|
|
|
import threading
|
|
|
|
import os
|
|
|
|
import brozzler
|
2016-12-06 17:12:20 -08:00
|
|
|
import brozzler.chrome
|
|
|
|
import logging
|
2017-02-15 16:46:45 -08:00
|
|
|
import yaml
|
2017-03-02 16:53:24 -08:00
|
|
|
import datetime
|
2017-04-17 16:47:05 -07:00
|
|
|
import requests
|
2017-04-17 18:15:22 -07:00
|
|
|
import tempfile
|
2017-04-18 12:00:23 -07:00
|
|
|
import uuid
|
|
|
|
import socket
|
2017-04-20 17:08:16 -07:00
|
|
|
import time
|
|
|
|
import sys
|
|
|
|
|
|
|
|
logging.basicConfig(
|
|
|
|
stream=sys.stderr, level=logging.INFO, format=(
|
|
|
|
'%(asctime)s %(process)d %(levelname)s %(threadName)s '
|
|
|
|
'%(name)s.%(funcName)s(%(filename)s:%(lineno)d) %(message)s'))
|
2016-11-16 11:41:34 -08:00
|
|
|
|
|
|
|
@pytest.fixture(scope='module')
|
|
|
|
def httpd(request):
|
|
|
|
# SimpleHTTPRequestHandler always uses CWD so we have to chdir
|
|
|
|
os.chdir(os.path.join(os.path.dirname(__file__), 'htdocs'))
|
|
|
|
|
|
|
|
httpd = http.server.HTTPServer(
|
|
|
|
('localhost', 0), http.server.SimpleHTTPRequestHandler)
|
|
|
|
httpd_thread = threading.Thread(name='httpd', target=httpd.serve_forever)
|
|
|
|
httpd_thread.start()
|
|
|
|
|
|
|
|
def fin():
|
|
|
|
httpd.shutdown()
|
|
|
|
httpd.server_close()
|
|
|
|
httpd_thread.join()
|
|
|
|
request.addfinalizer(fin)
|
|
|
|
|
|
|
|
return httpd
|
|
|
|
|
|
|
|
def test_robots(httpd):
|
|
|
|
'''
|
|
|
|
Basic test of robots.txt user-agent substring matching.
|
|
|
|
'''
|
|
|
|
url = 'http://localhost:%s/' % httpd.server_port
|
2017-03-02 12:48:45 -08:00
|
|
|
site = brozzler.Site(None, {'seed':url,'user_agent':'im/a/GoOdbot/yep'})
|
2016-11-16 11:41:34 -08:00
|
|
|
assert brozzler.is_permitted_by_robots(site, url)
|
|
|
|
|
2017-03-02 12:48:45 -08:00
|
|
|
site = brozzler.Site(None, {'seed':url,'user_agent':'im/a bAdBOt/uh huh'})
|
2016-11-16 11:41:34 -08:00
|
|
|
assert not brozzler.is_permitted_by_robots(site, url)
|
|
|
|
|
2017-02-15 16:46:45 -08:00
|
|
|
def test_scoping():
|
|
|
|
test_scope = yaml.load('''
|
|
|
|
max_hops: 100
|
|
|
|
accepts:
|
|
|
|
- url_match: REGEX_MATCH
|
|
|
|
value: ^.*/audio_file/.*\.mp3$
|
|
|
|
- url_match: SURT_MATCH
|
|
|
|
value: http://(com,vimeocdn,
|
|
|
|
- url_match: STRING_MATCH
|
|
|
|
value: ec-media.soundcloud.com
|
|
|
|
- regex: ^https?://twitter\.com.*$
|
|
|
|
- substring: facebook.com
|
|
|
|
- regex: ^https?://(www.)?youtube.com/watch?.*$
|
|
|
|
parent_url_regex: ^https?://(www.)?youtube.com/user/.*$
|
|
|
|
blocks:
|
|
|
|
- domain: twitter.com
|
|
|
|
url_match: REGEX_MATCH
|
|
|
|
value: ^.*lang=(?!en).*$
|
|
|
|
''')
|
|
|
|
|
2017-03-02 12:48:45 -08:00
|
|
|
site = brozzler.Site(None, {
|
|
|
|
'id': 1, 'seed': 'http://example.com/foo/bar?baz=quux#monkey',
|
|
|
|
'scope': test_scope})
|
|
|
|
page = brozzler.Page(None, {
|
|
|
|
'url': 'http://example.com/foo/bar?baz=quux#monkey',
|
|
|
|
'site_id': site.id})
|
2017-02-15 16:46:45 -08:00
|
|
|
|
|
|
|
assert site.is_in_scope('http://example.com/foo/bar', page)
|
|
|
|
assert not site.is_in_scope('http://example.com/foo/baz', page)
|
|
|
|
|
|
|
|
assert not site.is_in_scope('http://foo.com/some.mp3', page)
|
|
|
|
assert site.is_in_scope('http://foo.com/blah/audio_file/some.mp3', page)
|
|
|
|
|
|
|
|
assert site.is_in_scope('http://a.b.vimeocdn.com/blahblah', page)
|
|
|
|
assert not site.is_in_scope('https://a.b.vimeocdn.com/blahblah', page)
|
|
|
|
|
|
|
|
assert site.is_in_scope('https://twitter.com/twit', page)
|
|
|
|
assert site.is_in_scope('https://twitter.com/twit?lang=en', page)
|
|
|
|
assert not site.is_in_scope('https://twitter.com/twit?lang=es', page)
|
|
|
|
|
|
|
|
assert site.is_in_scope('https://www.facebook.com/whatevz', page)
|
|
|
|
|
|
|
|
assert not site.is_in_scope(
|
|
|
|
'https://www.youtube.com/watch?v=dUIn5OAPS5s', page)
|
2017-03-02 12:48:45 -08:00
|
|
|
yt_user_page = brozzler.Page(None, {
|
|
|
|
'url': 'https://www.youtube.com/user/SonoraSantaneraVEVO',
|
|
|
|
'site_id': site.id, 'hops_from_seed': 10})
|
2017-02-15 16:46:45 -08:00
|
|
|
assert site.is_in_scope(
|
|
|
|
'https://www.youtube.com/watch?v=dUIn5OAPS5s', yt_user_page)
|
|
|
|
|
2017-04-17 18:15:22 -07:00
|
|
|
def test_proxy_down():
|
2017-04-17 16:47:05 -07:00
|
|
|
'''
|
2017-04-17 18:15:22 -07:00
|
|
|
Test all fetching scenarios raise `brozzler.ProxyError` when proxy is down.
|
2017-04-17 16:47:05 -07:00
|
|
|
|
2017-04-17 18:15:22 -07:00
|
|
|
This test needs to cover every possible fetch through the proxy other than
|
|
|
|
fetches from the browser. For that, see test_brozzling.py.
|
|
|
|
|
2017-04-18 12:00:23 -07:00
|
|
|
Tests two different kinds of connection error:
|
|
|
|
- nothing listening the port (nobody listens on on port 4 :))
|
|
|
|
- port bound but not accepting connections
|
|
|
|
'''
|
|
|
|
sock = socket.socket()
|
|
|
|
sock.bind(('127.0.0.1', 0))
|
|
|
|
for not_listening_proxy in (
|
|
|
|
'127.0.0.1:4', '127.0.0.1:%s' % sock.getsockname()[1]):
|
|
|
|
worker = brozzler.BrozzlerWorker(
|
|
|
|
frontier=None, proxy=not_listening_proxy)
|
|
|
|
site = brozzler.Site(None, {
|
|
|
|
'id': str(uuid.uuid4()), 'seed': 'http://example.com/'})
|
|
|
|
page = brozzler.Page(None, {'url': 'http://example.com/'})
|
|
|
|
|
|
|
|
# robots.txt fetch
|
|
|
|
with pytest.raises(brozzler.ProxyError):
|
|
|
|
brozzler.is_permitted_by_robots(
|
|
|
|
site, 'http://example.com/', proxy=not_listening_proxy)
|
2017-04-17 18:15:22 -07:00
|
|
|
|
2017-04-18 12:00:23 -07:00
|
|
|
# youtube-dl fetch
|
|
|
|
with tempfile.TemporaryDirectory(prefix='brzl-ydl-') as tempdir:
|
|
|
|
ydl = worker._youtube_dl(tempdir, site)
|
|
|
|
with pytest.raises(brozzler.ProxyError):
|
|
|
|
worker._try_youtube_dl(ydl, site, page)
|
2017-04-17 18:15:22 -07:00
|
|
|
|
2017-04-18 12:00:23 -07:00
|
|
|
# raw fetch
|
2017-04-17 18:15:22 -07:00
|
|
|
with pytest.raises(brozzler.ProxyError):
|
2017-04-18 12:00:23 -07:00
|
|
|
worker._fetch_url(site, page)
|
2017-04-17 16:47:05 -07:00
|
|
|
|
2017-04-18 16:58:51 -07:00
|
|
|
# WARCPROX_WRITE_RECORD
|
|
|
|
with pytest.raises(brozzler.ProxyError):
|
|
|
|
worker._warcprox_write_record(
|
|
|
|
warcprox_address=not_listening_proxy,
|
|
|
|
url='test://proxy_down/warcprox_write_record',
|
|
|
|
warc_type='metadata',
|
|
|
|
content_type='text/plain',
|
|
|
|
payload=b'''payload doesn't matter here''')
|
|
|
|
|
2017-03-02 16:53:24 -08:00
|
|
|
def test_start_stop_backwards_compat():
|
|
|
|
site = brozzler.Site(None, {'seed': 'http://example.com/'})
|
|
|
|
assert len(site.starts_and_stops) == 1
|
|
|
|
assert site.starts_and_stops[0]['start']
|
|
|
|
assert site.starts_and_stops[0]['stop'] is None
|
|
|
|
assert not 'start_time' in site
|
|
|
|
|
|
|
|
site = brozzler.Site(None, {
|
|
|
|
'seed': 'http://example.com/',
|
|
|
|
'start_time': datetime.datetime(2017,1,1)})
|
|
|
|
assert len(site.starts_and_stops) == 1
|
|
|
|
assert site.starts_and_stops[0]['start'] == datetime.datetime(2017, 1, 1)
|
|
|
|
assert site.starts_and_stops[0]['stop'] is None
|
|
|
|
assert not 'start_time' in site
|
|
|
|
|
|
|
|
job = brozzler.Job(None, {'seeds': [{'url':'https://example.com/'}]})
|
|
|
|
assert job.starts_and_stops[0]['start']
|
|
|
|
assert job.starts_and_stops[0]['stop'] is None
|
|
|
|
assert not 'started' in job
|
|
|
|
assert not 'finished' in job
|
|
|
|
|
|
|
|
job = brozzler.Job(None, {
|
|
|
|
'seeds': [{'url':'https://example.com/'}],
|
|
|
|
'started': datetime.datetime(2017, 1, 1),
|
|
|
|
'finished': datetime.datetime(2017, 1, 2)})
|
|
|
|
assert job.starts_and_stops[0]['start'] == datetime.datetime(2017, 1, 1)
|
|
|
|
assert job.starts_and_stops[0]['stop'] == datetime.datetime(2017, 1, 2)
|
|
|
|
assert not 'started' in job
|
|
|
|
assert not 'finished' in job
|
|
|
|
|
2017-04-20 17:08:16 -07:00
|
|
|
def test_thread_raise():
|
|
|
|
let_thread_finish = threading.Event()
|
|
|
|
thread_preamble_done = threading.Event()
|
|
|
|
thread_caught_exception = None
|
|
|
|
|
|
|
|
def thread_target(accept_exceptions=False, block_exceptions=False):
|
|
|
|
if accept_exceptions:
|
|
|
|
brozzler.thread_accept_exceptions()
|
|
|
|
if block_exceptions:
|
|
|
|
brozzler.thread_block_exceptions()
|
|
|
|
thread_preamble_done.set()
|
|
|
|
|
|
|
|
try:
|
|
|
|
logging.info('waiting')
|
|
|
|
let_thread_finish.wait()
|
|
|
|
except Exception as e:
|
|
|
|
logging.info('caught exception %s', e)
|
|
|
|
nonlocal thread_caught_exception
|
|
|
|
thread_caught_exception = e
|
|
|
|
finally:
|
|
|
|
logging.info('finishing')
|
|
|
|
let_thread_finish.clear()
|
|
|
|
thread_preamble_done.clear()
|
|
|
|
|
|
|
|
# test that thread_raise does not raise exception in a thread that has not
|
|
|
|
# called thread_accept_exceptions
|
|
|
|
thread_caught_exception = None
|
|
|
|
th = threading.Thread(target=lambda: thread_target())
|
|
|
|
th.start()
|
|
|
|
thread_preamble_done.wait()
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
brozzler.thread_raise(
|
|
|
|
th, Exception("i'm an instance, which is not allowed"))
|
|
|
|
assert brozzler.thread_raise(th, Exception) is False
|
|
|
|
assert thread_caught_exception is None
|
|
|
|
let_thread_finish.set()
|
|
|
|
th.join()
|
|
|
|
assert thread_caught_exception is None
|
|
|
|
|
|
|
|
# test that thread_raise raises exception in a thread that has called
|
|
|
|
# thread_accept_exceptions
|
|
|
|
thread_caught_exception = None
|
|
|
|
th = threading.Thread(target=lambda: thread_target(accept_exceptions=True))
|
|
|
|
th.start()
|
|
|
|
thread_preamble_done.wait()
|
|
|
|
assert brozzler.thread_raise(th, Exception) is True
|
|
|
|
let_thread_finish.set()
|
|
|
|
th.join()
|
|
|
|
assert thread_caught_exception
|
|
|
|
with pytest.raises(threading.ThreadError): # thread is not running
|
|
|
|
brozzler.thread_raise(th, Exception)
|
|
|
|
|
|
|
|
# test that thread_raise does not raise exception in a thread that has
|
|
|
|
# called thread_block_exceptions
|
|
|
|
thread_caught_exception = None
|
|
|
|
th = threading.Thread(target=lambda: thread_target(block_exceptions=True))
|
|
|
|
th.start()
|
|
|
|
thread_preamble_done.wait()
|
|
|
|
assert brozzler.thread_raise(th, Exception) is False
|
|
|
|
let_thread_finish.set()
|
|
|
|
th.join()
|
|
|
|
assert thread_caught_exception is None
|
|
|
|
|
|
|
|
|