| 1 |
import json |
| 2 |
import shutil |
| 3 |
import os |
| 4 |
import tempfile |
| 5 |
import logging |
| 6 |
import copy |
| 7 |
import stat |
| 8 |
import time |
| 9 |
import pytest |
| 10 |
|
| •11 |
from unittest.mock import patch |
| 12 |
|
| 13 |
from optparse import OptionParser |
| 14 |
|
| 15 |
|
| 16 |
from biomaj.bank import Bank |
| 17 |
from biomaj.session import Session |
| 18 |
from biomaj.workflow import Workflow |
| 19 |
from biomaj.workflow import UpdateWorkflow |
| 20 |
from biomaj.workflow import ReleaseCheckWorkflow |
| 21 |
from biomaj_core.utils import Utils |
| 22 |
from biomaj_core.config import BiomajConfig |
| 23 |
from biomaj.process.processfactory import PostProcessFactory |
| 24 |
from biomaj.process.processfactory import PreProcessFactory |
| 25 |
from biomaj.process.processfactory import RemoveProcessFactory |
| 26 |
from biomaj_user.user import BmajUser |
| 27 |
from biomaj_core.bmajindex import BmajIndex |
| 28 |
|
| 29 |
|
| 30 |
class UtilsForTest(): |
| 31 |
""" |
| 32 |
Copy properties files to a temp directory and update properties to |
| 33 |
use a temp directory |
| 34 |
""" |
| 35 |
|
| 36 |
def __init__(self): |
| 37 |
""" |
| 38 |
Setup the temp dirs and files. |
| 39 |
""" |
| 40 |
self.global_properties = None |
| 41 |
self.bank_properties = None |
| 42 |
|
| 43 |
self.test_dir = tempfile.mkdtemp('biomaj') |
| 44 |
|
| 45 |
self.conf_dir =os.path.join(self.test_dir,'conf') |
| 46 |
if not os.path.exists(self.conf_dir): |
| 47 |
os.makedirs(self.conf_dir) |
| 48 |
self.data_dir =os.path.join(self.test_dir,'data') |
| 49 |
if not os.path.exists(self.data_dir): |
| 50 |
os.makedirs(self.data_dir) |
| 51 |
self.log_dir =os.path.join(self.test_dir,'log') |
| 52 |
if not os.path.exists(self.log_dir): |
| 53 |
os.makedirs(self.log_dir) |
| 54 |
self.process_dir =os.path.join(self.test_dir,'process') |
| 55 |
if not os.path.exists(self.process_dir): |
| 56 |
os.makedirs(self.process_dir) |
| 57 |
self.lock_dir =os.path.join(self.test_dir,'lock') |
| 58 |
if not os.path.exists(self.lock_dir): |
| 59 |
os.makedirs(self.lock_dir) |
| 60 |
self.cache_dir =os.path.join(self.test_dir,'cache') |
| 61 |
if not os.path.exists(self.cache_dir): |
| 62 |
os.makedirs(self.cache_dir) |
| 63 |
|
| 64 |
|
| 65 |
if self.global_properties is None: |
| 66 |
self.__copy_global_properties() |
| 67 |
|
| 68 |
if self.bank_properties is None: |
| 69 |
self.__copy_test_bank_properties() |
| 70 |
|
| 71 |
def clean(self): |
| 72 |
""" |
| 73 |
Deletes temp directory |
| 74 |
""" |
| 75 |
shutil.rmtree(self.test_dir) |
| 76 |
|
| 77 |
def __copy_test_bank_properties(self): |
| 78 |
if self.bank_properties is not None: |
| 79 |
return |
| 80 |
self.bank_properties = ['pdbaa', 'local', 'testhttp','directhttp', |
| 81 |
'alu_list_error'] |
| 82 |
curdir = os.path.dirname(os.path.realpath(__file__)) |
| 83 |
for b in self.bank_properties: |
| 84 |
from_file = os.path.join(curdir, b+'.properties') |
| 85 |
to_file = os.path.join(self.conf_dir, b+'.properties') |
| 86 |
shutil.copyfile(from_file, to_file) |
| 87 |
|
| 88 |
self.bank_process = ['test.sh'] |
| 89 |
curdir = os.path.dirname(os.path.realpath(__file__)) |
| 90 |
procdir = os.path.join(curdir, 'bank/process') |
| 91 |
for proc in self.bank_process: |
| 92 |
from_file = os.path.join(procdir, proc) |
| 93 |
to_file = os.path.join(self.process_dir, proc) |
| 94 |
shutil.copyfile(from_file, to_file) |
| 95 |
os.chmod(to_file, stat.S_IRWXU) |
| 96 |
|
| 97 |
# Manage local bank test, use bank test subdir as remote |
| 98 |
properties = ['multi.properties', 'computederror.properties', |
| 99 |
'error.properties', 'local.properties', |
| 100 |
'localprocess.properties', 'testhttp.properties', |
| 101 |
'computed.properties', 'computed2.properties', |
| 102 |
'sub1.properties', 'sub2.properties'] |
| 103 |
for prop in properties: |
| 104 |
from_file = os.path.join(curdir, prop) |
| 105 |
to_file = os.path.join(self.conf_dir, prop) |
| 106 |
fout = open(to_file,'w') |
| 107 |
with open(from_file,'r') as fin: |
| 108 |
for line in fin: |
| 109 |
if line.startswith('remote.dir'): |
| 110 |
fout.write("remote.dir="+os.path.join(curdir,'bank')+"\n") |
| 111 |
elif line.startswith('remote.files'): |
| 112 |
fout.write(line.replace('/tmp', os.path.join(curdir,'bank'))) |
| 113 |
else: |
| 114 |
fout.write(line) |
| 115 |
fout.close() |
| 116 |
|
| 117 |
def __copy_global_properties(self): |
| 118 |
if self.global_properties is not None: |
| 119 |
return |
| 120 |
self.global_properties = os.path.join(self.conf_dir,'global.properties') |
| 121 |
curdir = os.path.dirname(os.path.realpath(__file__)) |
| 122 |
global_template = os.path.join(curdir,'global.properties') |
| 123 |
fout = open(self.global_properties,'w') |
| 124 |
with open(global_template,'r') as fin: |
| 125 |
for line in fin: |
| 126 |
if line.startswith('conf.dir'): |
| 127 |
fout.write("conf.dir="+self.conf_dir+"\n") |
| 128 |
elif line.startswith('log.dir'): |
| 129 |
fout.write("log.dir="+self.log_dir+"\n") |
| 130 |
elif line.startswith('data.dir'): |
| 131 |
fout.write("data.dir="+self.data_dir+"\n") |
| 132 |
elif line.startswith('process.dir'): |
| 133 |
fout.write("process.dir="+self.process_dir+"\n") |
| 134 |
elif line.startswith('lock.dir'): |
| 135 |
fout.write("lock.dir="+self.lock_dir+"\n") |
| 136 |
else: |
| 137 |
fout.write(line) |
| 138 |
fout.close() |
| 139 |
|
| 140 |
|
| 141 |
class TestBiomajSetup(): |
| 142 |
|
| 143 |
def setup_method(self, m): |
| 144 |
self.utils = UtilsForTest() |
| 145 |
curdir = os.path.dirname(os.path.realpath(__file__)) |
| 146 |
BiomajConfig.load_config(self.utils.global_properties, allow_user_config=False) |
| 147 |
|
| 148 |
# Delete all banks |
| 149 |
b = Bank('pdbaa') |
| 150 |
b.banks.remove({}) |
| 151 |
|
| 152 |
self.config = BiomajConfig('pdbaa') |
| 153 |
data_dir = self.config.get('data.dir') |
| 154 |
lock_file = os.path.join(data_dir,'alu.lock') |
| 155 |
if os.path.exists(lock_file): |
| 156 |
os.remove(lock_file) |
| 157 |
|
| 158 |
def teardown_method(self, m): |
| 159 |
data_dir = self.config.get('data.dir') |
| 160 |
lock_file = os.path.join(data_dir,'alu.lock') |
| 161 |
if os.path.exists(lock_file): |
| 162 |
os.remove(lock_file) |
| 163 |
self.utils.clean() |
| 164 |
|
| 165 |
def test_new_bank(self): |
| 166 |
""" |
| 167 |
Checks bank init |
| 168 |
""" |
| 169 |
b = Bank('pdbaa') |
| 170 |
|
| 171 |
def test_new_session(self): |
| 172 |
""" |
| 173 |
Checks an empty session is created |
| 174 |
""" |
| 175 |
b = Bank('pdbaa') |
| 176 |
b.load_session(UpdateWorkflow.FLOW) |
| 177 |
for key in b.session._session['status'].keys(): |
| 178 |
assert not(b.session.get_status(key)) |
| 179 |
|
| 180 |
def test_session_reload_notover(self): |
| 181 |
""" |
| 182 |
Checks a session is used if present |
| 183 |
""" |
| 184 |
b = Bank('pdbaa') |
| 185 |
for i in range(1, 5): |
| 186 |
s = Session('pdbaa', self.config, UpdateWorkflow.FLOW) |
| 187 |
s._session['status'][Workflow.FLOW_INIT] = True |
| 188 |
b.session = s |
| 189 |
b.save_session() |
| 190 |
|
| 191 |
b = Bank('pdbaa') |
| 192 |
b.load_session(UpdateWorkflow.FLOW) |
| 193 |
assert (b.session.get_status(Workflow.FLOW_INIT)) |
| 194 |
|
| 195 |
def test_clean_old_sessions(self): |
| 196 |
""" |
| 197 |
Checks a session is used if present |
| 198 |
""" |
| 199 |
b = Bank('local') |
| 200 |
for i in range(1,5): |
| 201 |
s = Session('pdbaa', self.config, UpdateWorkflow.FLOW) |
| 202 |
s._session['status'][Workflow.FLOW_INIT] = True |
| 203 |
b.session = s |
| 204 |
b.save_session() |
| 205 |
b2 = Bank('local') |
| 206 |
b2.update() |
| 207 |
b2.clean_old_sessions() |
| 208 |
assert (len(b2.bank['sessions']) == 1) |
| 209 |
|
| 210 |
def test_session_reload_over(self): |
| 211 |
""" |
| 212 |
Checks a session if is not over |
| 213 |
""" |
| 214 |
b = Bank('pdbaa') |
| 215 |
for i in range(1,5): |
| 216 |
s = Session('pdbaa', self.config, UpdateWorkflow.FLOW) |
| 217 |
s._session['status'][Workflow.FLOW_INIT] = True |
| 218 |
s._session['status'][Workflow.FLOW_OVER] = True |
| 219 |
b.session = s |
| 220 |
b.save_session() |
| 221 |
|
| 222 |
b = Bank('pdbaa') |
| 223 |
b.load_session(UpdateWorkflow.FLOW) |
| 224 |
assert not (b.session.get_status(Workflow.FLOW_INIT)) |
| 225 |
|
| 226 |
def test_bank_list(self): |
| 227 |
b1 = Bank('pdbaa') |
| 228 |
b2 = Bank('local') |
| 229 |
banks = Bank.list() |
| 230 |
assert (len(banks) == 2) |
| 231 |
|
| 232 |
@pytest.mark.skipif( |
| 233 |
os.environ.get('NETWORK', 1) == '0', |
| 234 |
reason='network tests disabled' |
| 235 |
) |
| 236 |
def test_get_release(self): |
| 237 |
""" |
| 238 |
Get release |
| 239 |
""" |
| 240 |
b = Bank('pdbaa') |
| 241 |
b.load_session(UpdateWorkflow.FLOW) |
| 242 |
res = b.update() |
| 243 |
assert (b.session.get('update')) |
| 244 |
assert (res) |
| 245 |
assert (b.session._session['release'] is not None) |
| 246 |
|
| 247 |
@pytest.mark.skipif( |
| 248 |
os.environ.get('NETWORK', 1) == '0', |
| 249 |
reason='network tests disabled' |
| 250 |
) |
| 251 |
def test_remove_session(self): |
| 252 |
b = Bank('pdbaa') |
| 253 |
for i in range(1,5): |
| 254 |
s = Session('pdbaa', self.config, UpdateWorkflow.FLOW) |
| 255 |
s._session['status'][Workflow.FLOW_INIT] = True |
| 256 |
b.session = s |
| 257 |
b.save_session() |
| 258 |
assert (len(b.bank['sessions'])==4) |
| 259 |
b.remove_session(b.session.get('id')) |
| 260 |
assert (len(b.bank['sessions'])==3) |
| 261 |
|
| 262 |
def test_postprocesses_setup(self): |
| 263 |
b = Bank('localprocess') |
| 264 |
pfactory = PostProcessFactory(b) |
| 265 |
pfactory.run(True) |
| 266 |
assert (len(pfactory.threads_tasks[0])==2) |
| 267 |
assert (len(pfactory.threads_tasks[1])==1) |
| 268 |
|
| 269 |
def test_postprocesses_exec_again(self): |
| 270 |
""" |
| 271 |
Execute once, set a status to false, check that False processes are executed |
| 272 |
""" |
| 273 |
b = Bank('localprocess') |
| 274 |
pfactory = PostProcessFactory(b) |
| 275 |
pfactory.run() |
| 276 |
assert (pfactory.blocks['BLOCK1']['META0']['PROC0']) |
| 277 |
assert (pfactory.blocks['BLOCK2']['META1']['PROC1']) |
| 278 |
assert (pfactory.blocks['BLOCK2']['META1']['PROC2']) |
| 279 |
blocks = copy.deepcopy(pfactory.blocks) |
| 280 |
blocks['BLOCK2']['META1']['PROC2'] = False |
| 281 |
pfactory2 = PostProcessFactory(b, blocks) |
| 282 |
pfactory2.run() |
| 283 |
assert (pfactory2.blocks['BLOCK2']['META1']['PROC2']) |
| 284 |
|
| 285 |
def test_preprocesses(self): |
| 286 |
b = Bank('localprocess') |
| 287 |
pfactory = PreProcessFactory(b) |
| 288 |
pfactory.run() |
| 289 |
assert (pfactory.meta_status['META0']['PROC0']) |
| 290 |
|
| 291 |
def test_removeprocesses(self): |
| 292 |
b = Bank('localprocess') |
| 293 |
pfactory = RemoveProcessFactory(b) |
| 294 |
pfactory.run() |
| 295 |
assert (pfactory.meta_status['META0']['PROC0']) |
| 296 |
|
| 297 |
def test_dependencies_list(self): |
| 298 |
b = Bank('computed') |
| 299 |
deps = b.get_dependencies() |
| 300 |
assert (len(deps)==2) |
| 301 |
|
| 302 |
class TestBiomajFunctional(): |
| 303 |
|
| 304 |
# Banks used in tests |
| 305 |
BANKS = ['local', 'alu_list_error'] |
| 306 |
|
| 307 |
def setup_method(self, m): |
| 308 |
self.utils = UtilsForTest() |
| 309 |
BiomajConfig.load_config(self.utils.global_properties, allow_user_config=False) |
| 310 |
|
| 311 |
# Clean banks used in tests |
| 312 |
for bank_name in self.BANKS: |
| 313 |
# Delete all releases |
| 314 |
b = Bank(bank_name) |
| 315 |
b.banks.remove({}) |
| 316 |
# Delete lock files |
| 317 |
config = BiomajConfig(bank_name) |
| 318 |
data_dir = config.get('data.dir') |
| 319 |
lock_file = os.path.join(data_dir, 'local.lock') |
| 320 |
if os.path.exists(lock_file): |
| 321 |
os.remove(lock_file) |
| 322 |
|
| 323 |
def teardown_method(self, m): |
| 324 |
# Delete lock files |
| 325 |
for bank_name in self.BANKS: |
| 326 |
config = BiomajConfig(bank_name) |
| 327 |
data_dir = config.get('data.dir') |
| 328 |
lock_file = os.path.join(data_dir,'local.lock') |
| 329 |
if os.path.exists(lock_file): |
| 330 |
os.remove(lock_file) |
| 331 |
self.utils.clean() |
| 332 |
|
| 333 |
def test_extract_release_from_file_name(self): |
| 334 |
b = Bank('local') |
| 335 |
b.load_session(UpdateWorkflow.FLOW) |
| 336 |
b.session.config.set('release.file', 'test_(\d+)\.txt') |
| 337 |
b.session.config.set('release.regexp', '') |
| 338 |
w = UpdateWorkflow(b) |
| 339 |
w.wf_release() |
| 340 |
assert (b.session.get('release') == '100') |
| 341 |
|
| 342 |
def test_remoterelease_check(self): |
| 343 |
b = Bank('local') |
| 344 |
b.load_session(ReleaseCheckWorkflow.FLOW) |
| 345 |
b.session.config.set('release.file', 'test_(\d+)\.txt') |
| 346 |
b.session.config.set('release.regexp', '') |
| 347 |
workflow = ReleaseCheckWorkflow(b) |
| 348 |
res = workflow.start() |
| 349 |
remoterelease = b.session.get('remoterelease') |
| 350 |
assert (remoterelease == '100') |
| 351 |
|
| 352 |
def test_extract_release_from_file_content(self): |
| 353 |
b = Bank('local') |
| 354 |
b.load_session(UpdateWorkflow.FLOW) |
| 355 |
b.session.config.set('release.file', 'test_100\.txt') |
| 356 |
b.session.config.set('release.regexp', 'Release\s*(\d+)') |
| 357 |
w = UpdateWorkflow(b) |
| 358 |
w.wf_release() |
| 359 |
assert (b.session.get('release') == '103') |
| 360 |
|
| 361 |
def test_publish(self): |
| 362 |
""" |
| 363 |
Update a bank, then publish it |
| 364 |
""" |
| 365 |
b = Bank('local') |
| 366 |
b.update() |
| 367 |
current_link = os.path.join(b.config.get('data.dir'), |
| 368 |
b.config.get('dir.version'), |
| 369 |
'current') |
| 370 |
assert not (os.path.exists(current_link)) |
| 371 |
assert (b.bank['current'] is None) |
| 372 |
b.publish() |
| 373 |
assert (os.path.exists(current_link)) |
| 374 |
assert (b.bank['current'] == b.session._session['id']) |
| 375 |
|
| 376 |
# Should test this on local downloader, changing 1 file to force update, |
| 377 |
# else we would get same bank and there would be no update |
| 378 |
def test_no_update(self): |
| 379 |
""" |
| 380 |
Try updating twice, at second time, bank should not be updated |
| 381 |
""" |
| 382 |
b = Bank('local') |
| 383 |
b.update() |
| 384 |
assert (b.session.get('update')) |
| 385 |
b.update() |
| 386 |
assert not (b.session.get('update')) |
| 387 |
assert not (b.session.get_status(Workflow.FLOW_POSTPROCESS)) |
| 388 |
|
| 389 |
def test_download_from_list(self): |
| 390 |
""" |
| 391 |
Use remote.list to define a list of files to download |
| 392 |
""" |
| 393 |
b = Bank('local') |
| 394 |
fd, file_path = tempfile.mkstemp() |
| 395 |
try: |
| 396 |
b.config.set('remote.list', file_path) |
| 397 |
with os.fdopen(fd, 'w') as tmp: |
| 398 |
tmp.write('[{"name": "test_100.txt", "root": "' + b.config.get('remote.dir') + '"}]') |
| 399 |
b.update() |
| 400 |
assert (b.session.get('update')) |
| 401 |
finally: |
| 402 |
#os.remove(file_path) |
| 403 |
print(file_path) |
| 404 |
|
| 405 |
def test_release_control(self): |
| 406 |
""" |
| 407 |
Try updating twice, at second time, modify one file (same date), |
| 408 |
bank should update |
| 409 |
""" |
| 410 |
b = Bank('local') |
| 411 |
b.update() |
| 412 |
b.session.config.set('keep.old.version', '3') |
| 413 |
assert (b.session.get('update')) |
| 414 |
remote_file = b.session.config.get('remote.dir') + 'test2.fasta' |
| 415 |
os.utime(remote_file, None) |
| 416 |
# Update test2.fasta and set release.control |
| 417 |
b.session.config.set('release.control', 'true') |
| 418 |
b.update() |
| 419 |
assert (b.session.get('update')) |
| 420 |
b.update() |
| 421 |
assert not (b.session.get('update')) |
| 422 |
b.session.config.set('copy.skip', '1') |
| 423 |
b.session.config.set('remote.files', '^test2.fasta') |
| 424 |
b.update() |
| 425 |
assert (b.session.get('update')) |
| 426 |
|
| 427 |
def test_update_hardlinks(self): |
| 428 |
""" |
| 429 |
Update a bank twice with hard links. Files copied from previous release |
| 430 |
must be links. |
| 431 |
""" |
| 432 |
b = Bank('local') |
| 433 |
b.config.set('keep.old.version', '3') |
| 434 |
b.config.set('use_hardlinks', '1') |
| 435 |
# Create a file in bank dir (which is the source dir) so we can manipulate |
| 436 |
# it. The pattern is taken into account by the bank configuration. |
| 437 |
# Note that this file is created in the source tree so we remove it after |
| 438 |
# or if this test fails in between. |
| 439 |
tmp_remote_file = b.config.get('remote.dir') + 'test.safe_to_del' |
| 440 |
if os.path.exists(tmp_remote_file): |
| 441 |
os.remove(tmp_remote_file) |
| 442 |
open(tmp_remote_file, "w") |
| 443 |
# First update |
| 444 |
b.update() |
| 445 |
assert (b.session.get('update')) |
| 446 |
old_release = b.session.get_full_release_directory() |
| 447 |
# Touch tmp_remote_file to force update. We set the date to tomorrow so we |
| 448 |
# are sure that a new release will be detected. |
| 449 |
tomorrow = time.time() + 3660 * 24 # 3660s for safety (leap second, etc.) |
| 450 |
os.utime(tmp_remote_file, (tomorrow, tomorrow)) |
| 451 |
# Second update |
| 452 |
try: |
| 453 |
b.update() |
| 454 |
assert (b.session.get('update')) |
| 455 |
new_release = b.session.get_full_release_directory() |
| 456 |
# Test that files in both releases are links to the the same file. |
| 457 |
# We can't use tmp_remote_file because it's the source of update and we |
| 458 |
# can't use test.fasta.gz because it is uncompressed and then not the |
| 459 |
# same file. |
| 460 |
for f in ['test2.fasta', 'test_100.txt']: |
| 461 |
file_old_release = os.path.join(old_release, 'flat', f) |
| 462 |
file_new_release = os.path.join(new_release, 'flat', f) |
| 463 |
try: |
| 464 |
assert (os.path.samefile(file_old_release, file_new_release)) |
| 465 |
except AssertionError: |
| 466 |
msg = "In %s: copy worked but hardlinks were not used." % self.id() |
| 467 |
logging.info(msg) |
| 468 |
# Test that no links are done for tmp_remote_file |
| 469 |
file_old_release = os.path.join(old_release, 'flat', 'test.safe_to_del') |
| 470 |
file_new_release = os.path.join(new_release, 'flat', 'test.safe_to_del') |
| 471 |
assert not (os.path.samefile(file_old_release, file_new_release)) |
| 472 |
except Exception: |
| 473 |
raise |
| 474 |
finally: |
| 475 |
# Remove file |
| 476 |
if os.path.exists(tmp_remote_file): |
| 477 |
os.remove(tmp_remote_file) |
| 478 |
|
| 479 |
def test_fromscratch_update(self): |
| 480 |
""" |
| 481 |
Try updating twice, at second time, bank should be updated (force with fromscratc) |
| 482 |
""" |
| 483 |
b = Bank('local') |
| 484 |
b.update() |
| 485 |
assert (b.session.get('update')) |
| 486 |
sess = b.session.get('release') |
| 487 |
b.options.fromscratch = True |
| 488 |
b.update() |
| 489 |
assert (b.session.get('update')) |
| 490 |
assert (b.session.get('release') == sess+'__1') |
| 491 |
|
| 492 |
|
| 493 |
def test_fromscratch_update_with_release(self): |
| 494 |
""" |
| 495 |
Try updating twice, at second time, bank should be updated (force with fromscratch) |
| 496 |
|
| 497 |
Use case with release defined in release file |
| 498 |
""" |
| 499 |
b = Bank('local') |
| 500 |
b.load_session(UpdateWorkflow.FLOW) |
| 501 |
b.session.config.set('release.file', 'test_(\d+)\.txt') |
| 502 |
b.session.config.set('release.regexp', '') |
| 503 |
w = UpdateWorkflow(b) |
| 504 |
w.wf_release() |
| 505 |
assert (b.session.get('release') == '100') |
| 506 |
os.makedirs(b.session.get_full_release_directory()) |
| 507 |
w = UpdateWorkflow(b) |
| 508 |
# Reset release |
| 509 |
b.session.set('release', None) |
| 510 |
w.options.fromscratch = True |
| 511 |
w.wf_release() |
| 512 |
assert (b.session.get('release') == '100__1') |
| 513 |
|
| 514 |
|
| 515 |
def test_mix_stop_from_task(self): |
| 516 |
""" |
| 517 |
Get a first release, then fromscratch --stop-after, then restart from-task |
| 518 |
""" |
| 519 |
b = Bank('local') |
| 520 |
b.update() |
| 521 |
rel = b.session.get('release') |
| 522 |
b2 = Bank('local') |
| 523 |
b2.options.stop_after = 'download' |
| 524 |
b2.options.fromscratch = True |
| 525 |
res = b2.update() |
| 526 |
assert (b2.session.get('release') == rel+'__1') |
| 527 |
b3 = Bank('local') |
| 528 |
res = b3.update() |
| 529 |
assert (b3.session.get('release') == rel+'__1') |
| 530 |
assert (res) |
| 531 |
|
| 532 |
def test_mix_stop_from_task2(self): |
| 533 |
""" |
| 534 |
Get a first release, then fromscratch --stop-after, then restart from-task |
| 535 |
""" |
| 536 |
b = Bank('local') |
| 537 |
b.update() |
| 538 |
rel = b.session.get('release') |
| 539 |
b2 = Bank('local') |
| 540 |
b2.options.stop_after = 'download' |
| 541 |
b2.options.fromscratch = True |
| 542 |
res = b2.update() |
| 543 |
assert (b2.session.get('release') == rel+'__1') |
| 544 |
b3 = Bank('local') |
| 545 |
res = b3.update() |
| 546 |
b2.options.from_task = 'download' |
| 547 |
assert (b3.session.get('release') == rel+'__1') |
| 548 |
assert (res) |
| 549 |
|
| 550 |
def test_mix_stop_from_task3(self): |
| 551 |
""" |
| 552 |
Get a first release, then fromscratch --stop-after, then restart from-task |
| 553 |
""" |
| 554 |
b = Bank('local') |
| 555 |
b.update() |
| 556 |
rel = b.session.get('release') |
| 557 |
b2 = Bank('local') |
| 558 |
b2.options.stop_after = 'download' |
| 559 |
b2.options.fromscratch = True |
| 560 |
res = b2.update() |
| 561 |
assert (b2.session.get('release') == rel+'__1') |
| 562 |
b3 = Bank('local') |
| 563 |
res = b3.update() |
| 564 |
b2.options.from_task = 'postprocess' |
| 565 |
assert (b3.session.get('release') == rel+'__1') |
| 566 |
assert (res) |
| 567 |
|
| 568 |
def test_mix_stop_from_task4(self): |
| 569 |
""" |
| 570 |
Get a first release, then fromscratch --stop-after, then restart from-task |
| 571 |
""" |
| 572 |
b = Bank('local') |
| 573 |
b.update() |
| 574 |
rel = b.session.get('release') |
| 575 |
b2 = Bank('local') |
| 576 |
b2.options.stop_before = 'download' |
| 577 |
b2.options.fromscratch = True |
| 578 |
res = b2.update() |
| 579 |
b3 = Bank('local') |
| 580 |
b3.options.from_task = 'postprocess' |
| 581 |
res = b3.update() |
| 582 |
assert not (res) |
| 583 |
|
| 584 |
def test_delete_old_dirs(self): |
| 585 |
""" |
| 586 |
Try updating 3 times, oldest dir should be removed |
| 587 |
""" |
| 588 |
b = Bank('local') |
| 589 |
b.removeAll(True) |
| 590 |
b = Bank('local') |
| 591 |
b.update() |
| 592 |
assert (b.session.get('update')) |
| 593 |
b.options.fromscratch = True |
| 594 |
b.update() |
| 595 |
assert (b.session.get('update')) |
| 596 |
assert (len(b.bank['production']) == 2) |
| 597 |
b.update() |
| 598 |
assert (b.session.get('update')) |
| 599 |
# one new dir, but olders must be deleted |
| 600 |
assert (len(b.bank['production']) == 2) |
| 601 |
|
| 602 |
def test_delete_old_dirs_with_freeze(self): |
| 603 |
""" |
| 604 |
Try updating 3 times, oldest dir should be removed but not freezed releases |
| 605 |
""" |
| 606 |
b = Bank('local') |
| 607 |
b.removeAll(True) |
| 608 |
b = Bank('local') |
| 609 |
b.update() |
| 610 |
b.freeze(b.session.get('release')) |
| 611 |
assert (b.session.get('update')) |
| 612 |
b.options.fromscratch = True |
| 613 |
b.update() |
| 614 |
b.freeze(b.session.get('release')) |
| 615 |
assert (b.session.get('update')) |
| 616 |
assert (len(b.bank['production']) == 2) |
| 617 |
b.update() |
| 618 |
assert (b.session.get('update')) |
| 619 |
# one new dir, but olders must be deleted |
| 620 |
assert (len(b.bank['production']) == 3) |
| 621 |
|
| 622 |
def test_removeAll(self): |
| 623 |
b = Bank('local') |
| 624 |
b.update() |
| 625 |
b.removeAll() |
| 626 |
assert not (os.path.exists(b.get_data_dir())) |
| 627 |
bdb = b.banks.find_one({'name': b.name}) |
| 628 |
assert (bdb is None) |
| 629 |
|
| 630 |
def test_remove(self): |
| 631 |
""" |
| 632 |
test removal of a production dir |
| 633 |
""" |
| 634 |
b = Bank('local') |
| 635 |
b.update() |
| 636 |
assert (os.path.exists(b.session.get_full_release_directory())) |
| 637 |
assert (len(b.bank['production'])==1) |
| 638 |
b.remove(b.session.get('release')) |
| 639 |
assert not (os.path.exists(b.session.get_full_release_directory())) |
| 640 |
b = Bank('local') |
| 641 |
assert (len(b.bank['production'])==0) |
| 642 |
|
| 643 |
def test_update_stop_after(self): |
| 644 |
b = Bank('local') |
| 645 |
b.options.stop_after = 'download' |
| 646 |
b.update() |
| 647 |
assert (b.session.get_status('download')) |
| 648 |
assert not (b.session.get_status('postprocess')) |
| 649 |
|
| 650 |
def test_update_stop_before(self): |
| 651 |
b = Bank('local') |
| 652 |
b.options.stop_before = 'postprocess' |
| 653 |
b.update() |
| 654 |
assert (b.session.get_status('download')) |
| 655 |
assert not (b.session.get_status('postprocess')) |
| 656 |
|
| 657 |
def test_reupdate_from_task(self): |
| 658 |
b = Bank('local') |
| 659 |
b.options.stop_after = 'download' |
| 660 |
b.update() |
| 661 |
assert not (b.session.get_status('postprocess')) |
| 662 |
b2 = Bank('local') |
| 663 |
b2.options.from_task = 'postprocess' |
| 664 |
b2.options.release = b.session.get('release') |
| 665 |
b2.update() |
| 666 |
assert (b2.session.get_status('postprocess')) |
| 667 |
assert (b.session.get_full_release_directory() == b2.session.get_full_release_directory()) |
| 668 |
|
| 669 |
def test_reupdate_from_task_error(self): |
| 670 |
b = Bank('local') |
| 671 |
b.options.stop_after = 'check' |
| 672 |
b.update() |
| 673 |
assert not (b.session.get_status('postprocess')) |
| 674 |
b2 = Bank('local') |
| 675 |
b2.options.from_task = 'postprocess' |
| 676 |
b2.options.release = b.session.get('release') |
| 677 |
res = b2.update() |
| 678 |
assert not (res) |
| 679 |
|
| 680 |
def test_reupdate_from_task_wrong_release(self): |
| 681 |
b = Bank('local') |
| 682 |
b.options.stop_after = 'download' |
| 683 |
b.update() |
| 684 |
assert not (b.session.get_status('postprocess')) |
| 685 |
b2 = Bank('local') |
| 686 |
b2.options.from_task = 'postprocess' |
| 687 |
b2.options.release = 'wrongrelease' |
| 688 |
res = b2.update() |
| 689 |
assert not (res) |
| 690 |
|
| 691 |
def test_postprocesses_restart_from_proc(self): |
| 692 |
b = Bank('localprocess') |
| 693 |
b.update() |
| 694 |
proc1file = os.path.join(b.session.get_full_release_directory(),'proc1.txt') |
| 695 |
proc2file = os.path.join(b.session.get_full_release_directory(),'proc2.txt') |
| 696 |
assert (os.path.exists(proc1file)) |
| 697 |
assert (os.path.exists(proc2file)) |
| 698 |
os.remove(proc1file) |
| 699 |
os.remove(proc2file) |
| 700 |
# Restart from postprocess, reexecute all processes |
| 701 |
b2 = Bank('localprocess') |
| 702 |
b2.options.from_task = 'postprocess' |
| 703 |
b2.options.release = b.session.get('release') |
| 704 |
b2.update() |
| 705 |
assert (os.path.exists(proc1file)) |
| 706 |
assert (os.path.exists(proc2file)) |
| 707 |
os.remove(proc1file) |
| 708 |
os.remove(proc2file) |
| 709 |
# Restart from postprocess, but at process PROC2 and following |
| 710 |
b3 = Bank('localprocess') |
| 711 |
b3.options.from_task = 'postprocess' |
| 712 |
b3.options.process = 'PROC2' |
| 713 |
b3.options.release = b.session.get('release') |
| 714 |
b3.update() |
| 715 |
#assert not (os.path.exists(proc1file)) |
| 716 |
assert (os.path.exists(proc2file)) |
| 717 |
|
| 718 |
def test_postprocess_wrong_process_name(self): |
| 719 |
"""If a wrong process name is given, update returns False and prints an error message""" |
| 720 |
b = Bank('local') |
| 721 |
b.options.stop_after = 'download' |
| 722 |
b.update() |
| 723 |
assert not (b.session.get_status('postprocess')) |
| 724 |
b2 = Bank('local') |
| 725 |
b2.options.from_task = 'postprocess' |
| 726 |
b2.options.release = b.session.get('release') |
| 727 |
b2.options.process = 'fake' |
| 728 |
assert not (b2.update()) |
| 729 |
assert not (b2.session.get_status('postprocess')) |
| 730 |
assert (b.session.get_full_release_directory() == b2.session.get_full_release_directory()) |
| 731 |
|
| 732 |
def test_computed(self): |
| 733 |
b = Bank('computed') |
| 734 |
res = b.update(True) |
| 735 |
assert (res) |
| 736 |
assert (os.path.exists(b.session.get_full_release_directory()+'/sub1/flat/test_100.txt')) |
| 737 |
assert (b.session.get('update')) |
| 738 |
# Check that, with depends non updated, bank is not updated itself |
| 739 |
nextb = Bank('computed') |
| 740 |
res = nextb.update(True) |
| 741 |
assert not (nextb.session.get('update')) |
| 742 |
|
| 743 |
def test_computed_nofile(self): |
| 744 |
b = Bank('computed2') |
| 745 |
b.load_session(UpdateWorkflow.FLOW) |
| 746 |
b.session.config.set('protocol', 'none') |
| 747 |
b.session.config.set('sub1.files.move', 'flat/test_.*') |
| 748 |
res = b.update(True) |
| 749 |
assert (res) |
| 750 |
assert (os.path.exists(b.session.get_full_release_directory()+'/sub1/flat/test_100.txt')) |
| 751 |
|
| 752 |
def test_computed_ref_release(self): |
| 753 |
b = Bank('computed2') |
| 754 |
res = b.update(True) |
| 755 |
b2 = Bank('sub1') |
| 756 |
b2release = b2.bank['production'][len(b2.bank['production'])-1]['release'] |
| 757 |
brelease = b.bank['production'][len(b.bank['production'])-1]['release'] |
| 758 |
assert (res) |
| 759 |
assert (brelease == b2release) |
| 760 |
|
| 761 |
def test_computed_ref_release(self): |
| 762 |
b = Bank('computed2') |
| 763 |
res = b.update(True) |
| 764 |
assert (b.session.get('update')) |
| 765 |
b2 = Bank('computed2') |
| 766 |
res = b2.update(True) |
| 767 |
assert not (b2.session.get('update')) |
| 768 |
|
| 769 |
def test_computederror(self): |
| 770 |
b = Bank('computederror') |
| 771 |
res = b.update(True) |
| 772 |
assert not (res) |
| 773 |
assert (b.session._session['depends']['sub2']) |
| 774 |
assert not (b.session._session['depends']['error']) |
| 775 |
|
| 776 |
@pytest.mark.skipif( |
| 777 |
os.environ.get('NETWORK', 1) == '0', |
| 778 |
reason='network tests disabled' |
| 779 |
) |
| 780 |
def test_directhttp_release(self): |
| 781 |
b = Bank('directhttp') |
| 782 |
res = b.update() |
| 783 |
assert (b.session.get('update')) |
| 784 |
assert (os.path.exists(b.session.get_full_release_directory()+'/flat/debian/README.html')) |
| 785 |
# print str(b.session.get('release')) |
| 786 |
# print str(b.session.get('remoterelease')) |
| 787 |
|
| 788 |
@pytest.mark.skipif( |
| 789 |
os.environ.get('NETWORK', 1) == '0', |
| 790 |
reason='network tests disabled' |
| 791 |
) |
| 792 |
def test_multi(self): |
| 793 |
b = Bank('multi') |
| 794 |
res = b.update() |
| 795 |
assert (res) |
| 796 |
with open(os.path.join(b.session.get_full_release_directory(),'flat/test1.json'), 'r') as content_file: |
| 797 |
content = content_file.read() |
| 798 |
my_json = json.loads(content) |
| 799 |
assert (my_json['args']['key1'] == 'value1') |
| 800 |
with open(os.path.join(b.session.get_full_release_directory(),'flat/test2.json'), 'r') as content_file: |
| 801 |
content = content_file.read() |
| 802 |
my_json = json.loads(content) |
| 803 |
assert (my_json['form']['key1'] == 'value1') |
| 804 |
|
| 805 |
def test_freeze(self): |
| 806 |
b = Bank('local') |
| 807 |
b.update() |
| 808 |
rel = b.session.get('release') |
| 809 |
b.freeze(rel) |
| 810 |
prod = b.get_production(rel) |
| 811 |
assert (prod['freeze'] == True) |
| 812 |
res = b.remove(rel) |
| 813 |
assert (res == False) |
| 814 |
b.unfreeze(rel) |
| 815 |
prod = b.get_production(rel) |
| 816 |
assert (prod['freeze'] == False) |
| 817 |
res = b.remove(rel) |
| 818 |
assert (res == True) |
| 819 |
|
| 820 |
def test_stats(self): |
| 821 |
b = Bank('local') |
| 822 |
b.update() |
| 823 |
rel = b.session.get('release') |
| 824 |
stats = Bank.get_banks_disk_usage() |
| 825 |
assert (stats[0]['size']>0) |
| 826 |
for release in stats[0]['releases']: |
| 827 |
if release['name'] == rel: |
| 828 |
assert (release['size']>0) |
| 829 |
|
| 830 |
def test_processes_meta_data(self): |
| 831 |
b = Bank('localprocess') |
| 832 |
b.update() |
| 833 |
formats = b.session.get('formats') |
| 834 |
assert (len(formats['blast'])==2) |
| 835 |
assert (len(formats['test'][0]['files'])==3) |
| 836 |
|
| 837 |
def test_search(self): |
| 838 |
b = Bank('localprocess') |
| 839 |
b.update() |
| 840 |
search_res = Bank.search(['blast'],[]) |
| 841 |
assert (len(search_res)==1) |
| 842 |
search_res = Bank.search([],['nucleic']) |
| 843 |
assert (len(search_res)==1) |
| 844 |
search_res = Bank.search(['blast'],['nucleic']) |
| 845 |
assert (len(search_res)==1) |
| 846 |
search_res = Bank.search(['blast'],['proteic']) |
| 847 |
assert (len(search_res)==0) |
| 848 |
|
| 849 |
def test_owner(self): |
| 850 |
""" |
| 851 |
test ACL with owner |
| 852 |
""" |
| 853 |
b = Bank('local') |
| 854 |
res = b.update() |
| 855 |
assert (res) |
| 856 |
b.set_owner('sample') |
| 857 |
b2 = Bank('local') |
| 858 |
try: |
| 859 |
res = b2.update() |
| 860 |
self.fail('not owner, should not be allowed') |
| 861 |
except Exception as e: |
| 862 |
pass |
| 863 |
|
| 864 |
@pytest.mark.skipif( |
| 865 |
os.environ.get('NETWORK', 1) == '0', |
| 866 |
reason='network tests disabled' |
| 867 |
) |
| 868 |
def test_bank_list_error(self): |
| 869 |
b = Bank('alu_list_error') |
| 870 |
res = b.update() |
| 871 |
assert not (res) |