Skip to content

Commit

Permalink
Skip tests that use a site which is failing
Browse files Browse the repository at this point in the history
Add 'hostname' to TestCase.sites dict, and skip class when
failed to receive a useful response for the hosts homepage.

Bug: T58963
Change-Id: I8fdcdaa0fab3b680d35b81b20a12ff5b786f779d
  • Loading branch information
jayvdb committed Dec 6, 2014
1 parent ec7bd72 commit d7d2321
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 22 deletions.
83 changes: 76 additions & 7 deletions tests/aspects.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from pywikibot import config, log, Site
from pywikibot.site import BaseSite
from pywikibot.family import WikimediaFamily
from pywikibot.comms import threadedhttp
from pywikibot.data.api import Request as _original_Request

import tests
Expand Down Expand Up @@ -382,6 +383,65 @@ def tearDown(self):
super(CacheInfoMixin, self).tearDown()


class CheckHostnameMixin(TestCaseBase):

"""Check the hostname is online before running tests."""

_checked_hostnames = {}

@classmethod
def setUpClass(cls):
"""
Set up the test class.
Prevent tests running if the host is down.
"""
super(CheckHostnameMixin, cls).setUpClass()

if not hasattr(cls, 'sites'):
return

for key, data in cls.sites.items():
if 'hostname' not in data:
raise Exception('%s: hostname not defined for %s'
% (cls.__name__, key))
hostname = data['hostname']

if hostname in cls._checked_hostnames:
if isinstance(cls._checked_hostnames[hostname], Exception):
raise unittest.SkipTest(
'%s: hostname %s failed (cached): %s'
% (cls.__name__, hostname,
cls._checked_hostnames[hostname]))
elif cls._checked_hostnames[hostname] is False:
raise unittest.SkipTest('%s: hostname %s failed (cached)'
% (cls.__name__, hostname))
else:
continue

protocol = 'http'
try:
r = threadedhttp.Http()
rv = r.request(uri=protocol + '://' + hostname)
if isinstance(rv, Exception):
cls._checked_hostnames[hostname] = rv
raise unittest.SkipTest(
'%s: contacting hostname %s failed: %s'
% (cls.__name__, hostname, rv))
except Exception as e:
cls._checked_hostnames[hostname] = e
raise unittest.SkipTest(
'%s: failed contacting hostname %s: %s'
% (cls.__name__, hostname, e))
if not rv[1] or not isinstance(rv[1], bytes):
# This is not an error, as the root of the webserver is
# probably not the same page accessed by the test, but
# it may help identify a problem.
print('%s: hostname %s returned %s instead of unicode'
% (cls.__name__, type(rv[1]), hostname))
cls._checked_hostnames[hostname] = True


class SiteWriteMixin(TestCaseBase):

"""
Expand Down Expand Up @@ -591,6 +651,8 @@ def wrapped_method(self):
if 'cached' in dct and dct['cached']:
bases = tuple([ForceCacheMixin] + list(bases))

bases = tuple([CheckHostnameMixin] + list(bases))

if 'write' in dct and dct['write']:
bases = tuple([SiteWriteMixin] + list(bases))

Expand Down Expand Up @@ -665,16 +727,19 @@ class has declared are needed.
interface = DrySite

for data in cls.sites.values():
if 'site' not in data:
if 'site' not in data and 'code' in data and 'family' in data:
data['site'] = Site(data['code'], data['family'],
interface=interface)
if 'hostname' not in data and 'site' in data:
data['hostname'] = data['site'].hostname()

if not hasattr(cls, 'cached') or not cls.cached:
pywikibot._sites = orig_sites

if len(cls.sites) == 1:
key = next(iter(cls.sites.keys()))
cls.site = cls.sites[key]['site']
if 'site' in cls.sites[key]:
cls.site = cls.sites[key]['site']

@classmethod
def get_site(cls, name=None):
Expand Down Expand Up @@ -830,19 +895,23 @@ def setUpClass(cls):
"""
super(WikibaseTestCase, cls).setUpClass()

for site in cls.sites.values():
if not site['site'].has_data_repository:
for data in cls.sites.values():
if 'site' not in data:
continue

site = data['site']
if not site.has_data_repository:
raise unittest.SkipTest(
u'%s: %r does not have data repository'
% (cls.__name__, site['site']))
% (cls.__name__, site))

if (hasattr(cls, 'repo') and
cls.repo != site['site'].data_repository()):
cls.repo != site.data_repository()):
raise Exception(
'%s: sites do not all have the same data repository'
% cls.__name__)

cls.repo = site['site'].data_repository()
cls.repo = site.data_repository()

@classmethod
def get_repo(cls):
Expand Down
12 changes: 10 additions & 2 deletions tests/data_ingestion_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ class TestPhoto(TestCase):

"""Test Photo class."""

net = True
sites = {
'wm-upload': {
'hostname': 'upload.wikimedia.org',
},
}

def setUp(self):
super(TestPhoto, self).setUp()
Expand Down Expand Up @@ -54,7 +58,11 @@ class TestCSVReader(TestCase):

"""Test CSVReader class."""

net = False
sites = {
'wm-upload': {
'hostname': 'upload.wikimedia.org',
},
}

def setUp(self):
super(TestCSVReader, self).setUp()
Expand Down
24 changes: 22 additions & 2 deletions tests/http_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,20 @@ class HttpTestCase(TestCase):

"""Tests for http module."""

net = True
sites = {
'www-wp': {
'hostname': 'www.wikipedia.org',
},
'www-wq': {
'hostname': 'www.wikiquote.org',
},
'omegawiki': {
'hostname': 'www.omegawiki.org',
},
'vikidia': {
'hostname': 'en.vikidia.org',
},
}

def test_http(self):
"""Test http request function."""
Expand Down Expand Up @@ -90,7 +103,14 @@ class ThreadedHttpTestCase(TestCase):

"""Tests for threadedhttp module."""

net = True
sites = {
'www-wp': {
'hostname': 'www.wikipedia.org',
},
'wikidata': {
'hostname': 'test.wikidata.org',
},
}

def test_http(self):
o = threadedhttp.Http()
Expand Down
15 changes: 8 additions & 7 deletions tests/weblib_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ class TestArchiveSites(TestCase):

"""Test weblib methods to access archive websites."""

net = True

@classmethod
def setUpClass(cls):
if os.environ.get('TRAVIS', 'false') == 'true':
raise unittest.SkipTest('Weblib tests are disabled on Travis-CI')
super(TestArchiveSites, cls).setUpClass()
sites = {
'archive.org': {
'hostname': 'web.archive.org',
},
'webcite': {
'hostname': 'www.webcitation.org',
}
}

def testInternetArchiveNewest(self):
archivedversion = weblib.getInternetArchiveURL('https://google.com')
Expand Down
22 changes: 19 additions & 3 deletions tests/wikidataquery_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


import pywikibot.data.wikidataquery as query
from tests.aspects import unittest, WikidataTestCase, TestCase
from tests.aspects import unittest, WikibaseTestCase, TestCase

import pywikibot
from pywikibot.page import ItemPage, PropertyPage, Claim
Expand All @@ -19,10 +19,22 @@
import time


class TestApiFunctions(WikidataTestCase):
class TestApiFunctions(WikibaseTestCase):

"""Test WikiDataQuery API functions."""

sites = {
'wikidata': {
'family': 'wikidata',
'code': 'wikidata',
},
'wdq': {
'hostname': 'wdq.wmflabs.org',
},
}

cached = True

def testQueries(self):
"""
Test Queries and check whether they're behaving correctly.
Expand Down Expand Up @@ -217,7 +229,11 @@ class TestApiSlowFunctions(TestCase):

"""Test slow WikiDataQuery API functions."""

net = True
sites = {
'wdq': {
'hostname': 'wdq.wmflabs.org',
},
}

def testQueryApiGetter(self):
"""Test that we can actually retreive data and that caching works."""
Expand Down
6 changes: 5 additions & 1 deletion tests/wikistats_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ class WikiStatsTestCase(TestCase):

"""Test WikiStats dump."""

net = True
sites = {
'wikistats': {
'hostname': 'wikistats.wmflabs.org',
},
}

def test_sort(self):
ws = WikiStats()
Expand Down

0 comments on commit d7d2321

Please sign in to comment.