John Vandenberg has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/177985

Change subject: Skip tests that use a site which is failing
......................................................................

Skip tests that use a site which is failing

Add 'hostname' to TestCase.sites dict, and skip class when
failed to receive a useful response for the hosts homepage.

Re-enable weblib tests on travis-ci, instead using the new
automatic skip functionality.

Bug: T58963
Change-Id: I8fdcdaa0fab3b680d35b81b20a12ff5b786f779d
---
M tests/aspects.py
M tests/data_ingestion_tests.py
M tests/http_tests.py
M tests/weblib_tests.py
M tests/wikidataquery_tests.py
M tests/wikistats_tests.py
6 files changed, 139 insertions(+), 22 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/85/177985/1

diff --git a/tests/aspects.py b/tests/aspects.py
index 6466018..f375317 100644
--- a/tests/aspects.py
+++ b/tests/aspects.py
@@ -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
@@ -382,6 +383,64 @@
         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: hostname %s failed: %s'
+                        % (cls.__name__, hostname, rv))
+            except Exception as e:
+                cls._checked_hostnames[hostname] = e
+                raise unittest.SkipTest(
+                    '%s: hostname %s failed: %s'
+                    % (cls.__name__, hostname, e))
+            if not rv[1] or not isinstance(rv[1], bytes):
+                # This is not an error, as the tests may use a webpage other
+                # than the root of the webserver, but it may be useful.
+                print('%s: hostname %s returned %s instead of unicode'
+                      % (cls.__name__, hostname, type(rv[1])))
+            cls._checked_hostnames[hostname] = True
+
+
 class SiteWriteMixin(TestCaseBase):
 
     """
@@ -591,6 +650,8 @@
         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))
 
@@ -665,16 +726,19 @@
             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):
@@ -830,19 +894,23 @@
         """
         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):
diff --git a/tests/data_ingestion_tests.py b/tests/data_ingestion_tests.py
index deec8a1..fc9a1b4 100644
--- a/tests/data_ingestion_tests.py
+++ b/tests/data_ingestion_tests.py
@@ -14,7 +14,11 @@
 
     """Test Photo class."""
 
-    net = True
+    sites = {
+        'wm-upload': {
+            'hostname': 'upload.wikimedia.org',
+        },
+    }
 
     def setUp(self):
         super(TestPhoto, self).setUp()
@@ -54,7 +58,11 @@
 
     """Test CSVReader class."""
 
-    net = False
+    sites = {
+        'wm-upload': {
+            'hostname': 'upload.wikimedia.org',
+        },
+    }
 
     def setUp(self):
         super(TestCSVReader, self).setUp()
diff --git a/tests/http_tests.py b/tests/http_tests.py
index 28bb87a..a37e4b4 100644
--- a/tests/http_tests.py
+++ b/tests/http_tests.py
@@ -22,7 +22,20 @@
 
     """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."""
@@ -90,7 +103,14 @@
 
     """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()
diff --git a/tests/weblib_tests.py b/tests/weblib_tests.py
index f4916e7..1b64880 100644
--- a/tests/weblib_tests.py
+++ b/tests/weblib_tests.py
@@ -22,13 +22,14 @@
 
     """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')
diff --git a/tests/wikidataquery_tests.py b/tests/wikidataquery_tests.py
index 4c3b018..c14646e 100644
--- a/tests/wikidataquery_tests.py
+++ b/tests/wikidataquery_tests.py
@@ -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
@@ -19,9 +19,21 @@
 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):
         """
@@ -217,7 +229,11 @@
 
     """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."""
diff --git a/tests/wikistats_tests.py b/tests/wikistats_tests.py
index c9e8532..deda834 100644
--- a/tests/wikistats_tests.py
+++ b/tests/wikistats_tests.py
@@ -22,7 +22,11 @@
 
     """Test WikiStats dump."""
 
-    net = True
+    sites = {
+        'wikistats': {
+            'hostname': 'wikistats.wmflabs.org',
+        },
+    }
 
     def test_sort(self):
         ws = WikiStats()

-- 
To view, visit https://gerrit.wikimedia.org/r/177985
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8fdcdaa0fab3b680d35b81b20a12ff5b786f779d
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <jay...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to