Allow proxies for module tests

This should make it easier to include geo-blocked comics.
This commit is contained in:
Tobias Gruetzmacher 2020-09-29 01:50:02 +02:00
parent b6631f5715
commit 4d8c4e8d2e
2 changed files with 30 additions and 3 deletions

View file

@ -6,7 +6,7 @@ def prepareDocker () {
def uid = sh returnStdout: true, script: 'id -u'
writeFile file: 'Dockerfile', text: """
FROM python:3.8-buster
RUN pip install pytest-cov && \\
RUN pip install pytest-cov PySocks && \\
useradd -m -u ${uid.trim()} jenkins
"""
img = docker.build('local:test-all-comics')
@ -22,8 +22,10 @@ def runTests() {
sh "pip install --user -e .[css,dev]"
}
stage ('Run tests') {
withCredentials([string(credentialsId: 'proxymap', variable: 'PROXYMAP')]) {
sh "TESTALL=1 py.test -v --cov=dosagelib --cov-report xml --tb=short -n10 --junitxml=junit.xml tests/modules/check_comics.py || true"
}
}
stage('Report') {
junit 'junit.xml'
publishCoverage calculateDiffForChangeRequests: true,

View file

@ -2,8 +2,10 @@
# Copyright (C) 2004-2008 Tristan Seligmann and Jonathan Jacobs
# Copyright (C) 2012-2014 Bastian Kleineidam
# Copyright (C) 2015-2020 Tobias Gruetzmacher
import re
import json
import multiprocessing
import os
import re
import warnings
from urllib.parse import urlsplit
@ -40,6 +42,7 @@ def _test_comic(outdir, scraperobj):
num_strips = 0
strip = None
files = []
PROXYMAP.apply(scraperobj.name)
for strip in scraperobj.getStrips(MaxStrips):
files.append(_check_strip(outdir, strip,
scraperobj.multipleImagesPerStrip))
@ -110,3 +113,25 @@ def _check_stripurl(strip, scraperobj):
if not mo:
warnings.warn('strip URL {!r} does not match stripUrl pattern {}'.format(
strip.strip_url, urlmatch))
class ProxyConfig:
"""Loads proxy config from an environment variable and applies it for each test."""
def __init__(self):
self.config = {}
if 'PROXYMAP' in os.environ:
for regex, server in json.loads(os.environ['PROXYMAP']).items():
self.config[re.compile(regex)] = server
def apply(self, name):
useserver = ''
for regex, server in self.config.items():
if regex.match(name):
useserver = server
break
os.environ['http_proxy'] = useserver
os.environ['https_proxy'] = useserver
# External proxy config to fetch some modules via proxies
PROXYMAP = ProxyConfig()