Skip to content

Commit 85ac115

Browse files
committed
Initial tests for sh.hs.jobq
1 parent 849f9f2 commit 85ac115

File tree

3 files changed

+441
-446
lines changed

3 files changed

+441
-446
lines changed

tests/hubstorage/conftest.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import os
2+
import vcr
3+
import pytest
4+
import requests
5+
from scrapinghub import HubstorageClient
6+
from scrapinghub.hubstorage.utils import urlpathjoin
7+
8+
9+
TEST_PROJECT_ID = "2222222"
10+
TEST_SPIDER_NAME = 'hs-test-spider'
11+
TEST_FRONTIER_NAME = 'test'
12+
TEST_FRONTIER_SLOT = 'site.com'
13+
TEST_BOTGROUPS = ['python-hubstorage-test', 'g1']
14+
15+
VCR_CASSETES_DIR = 'tests/hubstorage/cassetes'
16+
17+
TEST_AUTH = os.getenv('HS_AUTH', 'f' * 32)
18+
TEST_ENDPOINT = os.getenv('HS_ENDPOINT', 'http://storage.vm.scrapinghub.com')
19+
20+
21+
@pytest.fixture
22+
def vcr_instance(scope='session'):
23+
return vcr.VCR(
24+
cassette_library_dir=VCR_CASSETES_DIR,
25+
record_mode='once',
26+
match_on=['uri', 'method'],
27+
)
28+
29+
@pytest.fixture(scope='session')
30+
def hsclient():
31+
return HubstorageClient(auth=TEST_AUTH, endpoint=TEST_ENDPOINT)
32+
33+
34+
@pytest.fixture(scope='session')
35+
def hsproject(hsclient):
36+
return hsclient.get_project(TEST_PROJECT_ID)
37+
38+
39+
@pytest.fixture
40+
def hsspiderid(hsproject):
41+
return str(hsproject.ids.spider(TEST_SPIDER_NAME, create=1))
42+
43+
44+
@pytest.fixture(autouse=True, scope='session')
45+
def setup_session(hsclient, hsproject):
46+
yield
47+
hsclient.close()
48+
49+
50+
@pytest.fixture(autouse=True)
51+
def setup_test(hsclient, hsproject, request, vcr_instance):
52+
cassette_name = '{}/{}.yaml'.format(
53+
request.function.__module__.split('.')[-1],
54+
request.function.__name__
55+
)
56+
with vcr_instance.use_cassette(cassette_name):
57+
_set_testbotgroup(hsproject)
58+
_remove_all_jobs(hsproject)
59+
yield
60+
_remove_all_jobs(hsproject)
61+
_unset_testbotgroup(hsproject)
62+
63+
# ----------------------------------------------------------------------------
64+
65+
66+
def _set_testbotgroup(hsproject):
67+
hsproject.settings.apipost(jl={'botgroups': [TEST_BOTGROUPS[0]]})
68+
# Additional step to populate JobQ's botgroups table
69+
for botgroup in TEST_BOTGROUPS:
70+
url = urlpathjoin(TEST_ENDPOINT, 'botgroups', botgroup, 'max_running')
71+
requests.post(url, auth=hsproject.auth, data='null')
72+
hsproject.settings.expire()
73+
74+
75+
def _unset_testbotgroup(hsproject):
76+
hsproject.settings.apidelete('botgroups')
77+
hsproject.settings.expire()
78+
# Additional step to delete botgroups in JobQ
79+
for botgroup in TEST_BOTGROUPS:
80+
url = urlpathjoin(TEST_ENDPOINT, 'botgroups', botgroup)
81+
requests.delete(url, auth=hsproject.auth)
82+
83+
84+
85+
def _remove_all_jobs(hsproject):
86+
for k in list(hsproject.settings.keys()):
87+
if k != 'botgroups':
88+
del hsproject.settings[k]
89+
hsproject.settings.save()
90+
91+
# Cleanup JobQ
92+
jobq = hsproject.jobq
93+
for queuename in ('pending', 'running', 'finished'):
94+
info = {'summary': [None]} # poor-guy do...while
95+
while info['summary']:
96+
info = jobq.summary(queuename)
97+
for summary in info['summary']:
98+
_remove_job(hsproject, summary['key'])
99+
100+
101+
def _remove_job(hsproject, jobkey):
102+
hsproject.jobq.finish(jobkey)
103+
hsproject.jobq.delete(jobkey)
104+
_delete_job(hsproject, jobkey)
105+
106+
107+
def _delete_job(hsproject, jobkey):
108+
assert jobkey.startswith(TEST_PROJECT_ID), jobkey
109+
hsproject.jobs.apidelete(jobkey.partition('/')[2])
110+
111+
112+
def start_job(hsproject, **startparams):
113+
jobdata = hsproject.jobq.start(**startparams)
114+
if jobdata:
115+
jobkey = jobdata.pop('key')
116+
jobauth = (jobkey, jobdata['auth'])
117+
return hsproject.get_job(jobkey, jobauth=jobauth, metadata=jobdata)

tests/hubstorage/hstestcase.py

Lines changed: 0 additions & 102 deletions
This file was deleted.

0 commit comments

Comments
 (0)