| 1 |
import shutil |
| 2 |
import os |
| 3 |
import sys |
| 4 |
import tempfile |
| 5 |
import stat |
| 6 |
import pytest |
| 7 |
|
| 8 |
from biomaj_core.config import BiomajConfig |
| 9 |
from biomaj_core.utils import Utils |
| 10 |
from biomaj_core.bmajindex import BmajIndex |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class UtilsForTest: |
| 15 |
""" |
| 16 |
Copy properties files to a temp directory and update properties to |
| 17 |
use a temp directory |
| 18 |
""" |
| 19 |
|
| 20 |
def __init__(self): |
| 21 |
""" |
| 22 |
Setup the temp dirs and files. |
| 23 |
""" |
| 24 |
self.global_properties = None |
| 25 |
self.global_properties_hl = None |
| 26 |
self.bank_properties = None |
| 27 |
|
| 28 |
self.test_dir = tempfile.mkdtemp('biomaj') |
| 29 |
|
| 30 |
self.conf_dir = os.path.join(self.test_dir, 'conf') |
| 31 |
if not os.path.exists(self.conf_dir): |
| 32 |
os.makedirs(self.conf_dir) |
| 33 |
self.data_dir = os.path.join(self.test_dir, 'data') |
| 34 |
if not os.path.exists(self.data_dir): |
| 35 |
os.makedirs(self.data_dir) |
| 36 |
self.log_dir = os.path.join(self.test_dir, 'log') |
| 37 |
if not os.path.exists(self.log_dir): |
| 38 |
os.makedirs(self.log_dir) |
| 39 |
self.process_dir = os.path.join(self.test_dir, 'process') |
| 40 |
if not os.path.exists(self.process_dir): |
| 41 |
os.makedirs(self.process_dir) |
| 42 |
self.lock_dir = os.path.join(self.test_dir, 'lock') |
| 43 |
if not os.path.exists(self.lock_dir): |
| 44 |
os.makedirs(self.lock_dir) |
| 45 |
self.cache_dir = os.path.join(self.test_dir, 'cache') |
| 46 |
if not os.path.exists(self.cache_dir): |
| 47 |
os.makedirs(self.cache_dir) |
| 48 |
|
| 49 |
if self.global_properties is None: |
| 50 |
self.__copy_global_properties() |
| 51 |
|
| 52 |
if self.global_properties_hl is None: |
| 53 |
self.__copy_global_properties_hl() |
| 54 |
|
| 55 |
if self.bank_properties is None: |
| 56 |
self.__copy_test_bank_properties() |
| 57 |
|
| 58 |
def clean(self): |
| 59 |
""" |
| 60 |
Deletes temp directory |
| 61 |
""" |
| 62 |
shutil.rmtree(self.test_dir) |
| 63 |
|
| 64 |
def __copy_test_bank_properties(self): |
| 65 |
if self.bank_properties is not None: |
| 66 |
return |
| 67 |
self.bank_properties = ['alu', 'local', 'testhttp', 'directhttp', |
| 68 |
'noname', 'noexe', 'noargs', 'prenoname', |
| 69 |
'prenoexe', 'prenoargs', 'rmnoname', 'rmnoexe', |
| 70 |
'rmnoargs'] |
| 71 |
curdir = os.path.dirname(os.path.realpath(__file__)) |
| 72 |
for b in self.bank_properties: |
| 73 |
from_file = os.path.join(curdir, b+'.properties') |
| 74 |
to_file = os.path.join(self.conf_dir, b+'.properties') |
| 75 |
shutil.copyfile(from_file, to_file) |
| 76 |
|
| 77 |
self.bank_process = ['test.sh'] |
| 78 |
curdir = os.path.dirname(os.path.realpath(__file__)) |
| 79 |
procdir = os.path.join(curdir, 'bank/process') |
| 80 |
for proc in self.bank_process: |
| 81 |
from_file = os.path.join(procdir, proc) |
| 82 |
to_file = os.path.join(self.process_dir, proc) |
| 83 |
shutil.copyfile(from_file, to_file) |
| 84 |
os.chmod(to_file, stat.S_IRWXU) |
| 85 |
|
| 86 |
|
| 87 |
properties = ['multi.properties', 'computederror.properties', |
| 88 |
'error.properties', 'local.properties', |
| 89 |
'localprocess.properties', 'testhttp.properties', |
| 90 |
'computed.properties', 'computed2.properties', |
| 91 |
'sub1.properties', 'sub2.properties', |
| 92 |
'hardlinks.properties'] |
| 93 |
for prop in properties: |
| 94 |
from_file = os.path.join(curdir, prop) |
| 95 |
to_file = os.path.join(self.conf_dir, prop) |
| 96 |
fout = open(to_file,'w', encoding='UTF-8') |
| 97 |
with open(from_file,'r', encoding='UTF-8') as fin: |
| 98 |
for line in fin: |
| 99 |
if line.startswith('remote.dir'): |
| 100 |
fout.write("remote.dir="+os.path.join(curdir, |
| 101 |
'bank') + "\n") |
| 102 |
elif line.startswith('remote.files'): |
| 103 |
fout.write(line.replace('/tmp', os.path.join(curdir, |
| 104 |
'bank'))) |
| 105 |
else: |
| 106 |
fout.write(line) |
| 107 |
fout.close() |
| 108 |
|
| 109 |
def __copy_global_properties(self): |
| 110 |
if self.global_properties is not None: |
| 111 |
return |
| 112 |
self.global_properties = os.path.join(self.conf_dir, 'global.properties') |
| 113 |
curdir = os.path.dirname(os.path.realpath(__file__)) |
| 114 |
global_template = os.path.join(curdir, 'global.properties') |
| 115 |
fout = open(self.global_properties, 'w', encoding='UTF-8') |
| 116 |
with open(global_template,'r', encoding='UTF-8') as fin: |
| 117 |
for line in fin: |
| 118 |
if line.startswith('conf.dir'): |
| 119 |
fout.write("conf.dir="+self.conf_dir+"\n") |
| 120 |
elif line.startswith('log.dir'): |
| 121 |
fout.write("log.dir="+self.log_dir+"\n") |
| 122 |
elif line.startswith('data.dir'): |
| 123 |
fout.write("data.dir="+self.data_dir+"\n") |
| 124 |
elif line.startswith('process.dir'): |
| 125 |
fout.write("process.dir="+self.process_dir+"\n") |
| 126 |
elif line.startswith('lock.dir'): |
| 127 |
fout.write("lock.dir="+self.lock_dir+"\n") |
| 128 |
else: |
| 129 |
fout.write(line) |
| 130 |
fout.close() |
| 131 |
|
| 132 |
def __copy_global_properties_hl(self): |
| 133 |
if self.global_properties_hl is not None: |
| 134 |
return |
| 135 |
self.global_properties_hl = os.path.join(self.conf_dir, 'global_hardlinks.properties') |
| 136 |
curdir = os.path.dirname(os.path.realpath(__file__)) |
| 137 |
global_template = os.path.join(curdir, 'global_hardlinks.properties') |
| 138 |
fout = open(self.global_properties_hl, 'w', encoding='UTF-8') |
| 139 |
with open(global_template,'r', encoding='UTF-8') as fin: |
| 140 |
for line in fin: |
| 141 |
if line.startswith('conf.dir'): |
| 142 |
fout.write("conf.dir="+self.conf_dir+"\n") |
| 143 |
elif line.startswith('log.dir'): |
| 144 |
fout.write("log.dir="+self.log_dir+"\n") |
| 145 |
elif line.startswith('data.dir'): |
| 146 |
fout.write("data.dir="+self.data_dir+"\n") |
| 147 |
elif line.startswith('process.dir'): |
| 148 |
fout.write("process.dir="+self.process_dir+"\n") |
| 149 |
elif line.startswith('lock.dir'): |
| 150 |
fout.write("lock.dir="+self.lock_dir+"\n") |
| 151 |
else: |
| 152 |
fout.write(line) |
| 153 |
fout.close() |
| 154 |
|
| 155 |
|
| 156 |
class TestBiomajUtils(): |
| 157 |
|
| 158 |
def setup_method(self, m): |
| 159 |
self.utils = UtilsForTest() |
| 160 |
|
| 161 |
def teardown_method(self, m): |
| 162 |
self.utils.clean() |
| 163 |
|
| 164 |
def test_properties_override(self): |
| 165 |
BiomajConfig.load_config(self.utils.global_properties, |
| 166 |
allow_user_config=False) |
| 167 |
config = BiomajConfig('local') |
| 168 |
ldap_host = config.get('ldap.host') |
| 169 |
assert (ldap_host == 'localhost') |
| 170 |
os.environ['BIOMAJ_LDAP_HOST'] = 'someserver' |
| 171 |
ldap_host = config.get('ldap.host') |
| 172 |
assert (ldap_host == 'someserver') |
| 173 |
|
| 174 |
def test_service_config_override(self): |
| 175 |
config = { |
| 176 |
'rabbitmq': { 'host': '1.2.3.4'}, |
| 177 |
'web': {'local_endpoint': 'http://localhost'} |
| 178 |
} |
| 179 |
Utils.service_config_override(config) |
| 180 |
assert (config['rabbitmq']['host'] == '1.2.3.4') |
| 181 |
os.environ['RABBITMQ_HOST'] = '4.3.2.1' |
| 182 |
Utils.service_config_override(config) |
| 183 |
assert (config['rabbitmq']['host'] == '4.3.2.1') |
| 184 |
os.environ['WEB_LOCAL_ENDPOINT_DOWNLOAD'] = 'http://download' |
| 185 |
Utils.service_config_override(config) |
| 186 |
assert (config['web']['local_endpoint_download'] == 'http://download') |
| 187 |
endpoint = Utils.get_service_endpoint(config, 'download') |
| 188 |
assert (endpoint == 'http://download') |
| 189 |
endpoint = Utils.get_service_endpoint(config, 'process') |
| 190 |
assert (endpoint == 'http://localhost') |
| 191 |
|
| 192 |
def test_use_hardlinks_config(self): |
| 193 |
""" |
| 194 |
Test that hardlinks are disabled by default and can be overridden. |
| 195 |
""" |
| 196 |
BiomajConfig.load_config(self.utils.global_properties, |
| 197 |
allow_user_config=False) |
| 198 |
|
| 199 |
config = BiomajConfig('local') |
| 200 |
assert not config.get_bool("use_hardlinks") |
| 201 |
|
| 202 |
config = BiomajConfig('hardlinks') |
| 203 |
assert config.get_bool("use_hardlinks") |
| 204 |
|
| 205 |
BiomajConfig.load_config(self.utils.global_properties_hl, |
| 206 |
allow_user_config=False) |
| 207 |
config = BiomajConfig('local') |
| 208 |
assert config.get_bool("use_hardlinks") |
| 209 |
|
| 210 |
def test_mimes(self): |
| 211 |
fasta_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), |
| 212 |
'bank/test2.fasta') |
| 213 |
(mime, _) = Utils.detect_format(fasta_file) |
| 214 |
assert 'application/fasta' == mime |
| 215 |
|
| 216 |
def test_uncompress(self): |
| 217 |
from_file = { 'root': os.path.dirname(os.path.realpath(__file__)), |
| 218 |
'name': 'bank/test.fasta.gz' |
| 219 |
} |
| 220 |
|
| 221 |
to_dir = self.utils.data_dir |
| 222 |
Utils.copy_files([from_file], to_dir) |
| 223 |
Utils.uncompress(os.path.join(to_dir, from_file['name'])) |
| 224 |
assert os.path.exists(to_dir+'/bank/test.fasta') |
| 225 |
|
| 226 |
def test_copy_with_regexp(self): |
| 227 |
from_dir = os.path.dirname(os.path.realpath(__file__)) |
| 228 |
to_dir = self.utils.data_dir |
| 229 |
Utils.copy_files_with_regexp(from_dir, to_dir, [r'.*\.py']) |
| 230 |
assert os.path.exists(to_dir+'/test_biomaj.py') |
| 231 |
|
| 232 |
def test_copy_with_regexp_hardlink(self): |
| 233 |
""" |
| 234 |
Test copy with hardlinks: we create files in data_dir and try to |
| 235 |
link them. This should work unless /tmp don't accept hardlinks. |
| 236 |
""" |
| 237 |
|
| 238 |
|
| 239 |
suffix = ".dat" |
| 240 |
regexp = ".*\\" + suffix |
| 241 |
orig_file_full = [ |
| 242 |
tempfile.mkstemp(dir=self.utils.data_dir, suffix=suffix)[1] |
| 243 |
for i in range(5) |
| 244 |
] |
| 245 |
to_dir = tempfile.mkdtemp(dir=self.utils.data_dir) |
| 246 |
new_file_full = [os.path.join(to_dir, os.path.basename(f)) |
| 247 |
for f in orig_file_full] |
| 248 |
|
| 249 |
from_dir = self.utils.data_dir |
| 250 |
Utils.copy_files_with_regexp(from_dir, to_dir, [regexp], |
| 251 |
use_hardlinks=True) |
| 252 |
|
| 253 |
for orig, new in zip(orig_file_full, new_file_full): |
| 254 |
assert (os.path.exists(new)) |
| 255 |
|
| 256 |
|
| 257 |
orig_file_stat = os.stat(orig) |
| 258 |
new_file_stat = os.stat(new) |
| 259 |
try: |
| 260 |
assert (orig_file_stat.st_ino == new_file_stat.st_ino) |
| 261 |
except Exception: |
| 262 |
msg = "In %s: copy worked but hardlinks were not used." % orig_file_full |
| 263 |
print(msg, file=sys.stderr) |
| 264 |
|
| 265 |
def test_copy(self): |
| 266 |
from_dir = os.path.dirname(os.path.realpath(__file__)) |
| 267 |
local_file = 'test_biomaj.py' |
| 268 |
files_to_copy = [ {'root': from_dir, 'name': local_file}] |
| 269 |
to_dir = self.utils.data_dir |
| 270 |
Utils.copy_files(files_to_copy, to_dir) |
| 271 |
assert os.path.exists(to_dir+'/test_biomaj.py') |
| 272 |
|
| 273 |
def test_copy_hardlink(self): |
| 274 |
""" |
| 275 |
Test copy with hardlinks: we create a file in data_dir and try to |
| 276 |
link it. This should work unless /tmp don't accept hardlinks. |
| 277 |
""" |
| 278 |
|
| 279 |
|
| 280 |
_, orig_file_full = tempfile.mkstemp(dir=self.utils.data_dir) |
| 281 |
orig_file = os.path.basename(orig_file_full) |
| 282 |
to_dir = tempfile.mkdtemp(dir=self.utils.data_dir) |
| 283 |
new_file_full = os.path.join(to_dir, orig_file) |
| 284 |
|
| 285 |
from_dir = self.utils.data_dir |
| 286 |
files_to_copy = [{'root': from_dir, 'name': orig_file}] |
| 287 |
Utils.copy_files(files_to_copy, to_dir, use_hardlinks=True) |
| 288 |
|
| 289 |
assert (os.path.exists(new_file_full)) |
| 290 |
|
| 291 |
|
| 292 |
orig_file_stat = os.stat(orig_file_full) |
| 293 |
new_file_stat = os.stat(new_file_full) |
| 294 |
try: |
| 295 |
assert orig_file_stat.st_ino == new_file_stat.st_ino |
| 296 |
except Exception: |
| 297 |
msg = "In %s: copy worked but hardlinks were not used." % orig_file_full |
| 298 |
print(msg, file=sys.stderr) |
| 299 |
|
| 300 |
def test_check_method(self): |
| 301 |
"""Check .name, .exe and .args are well check during bank configuration |
| 302 |
checking""" |
| 303 |
BiomajConfig.load_config(self.utils.global_properties) |
| 304 |
for conf in ['noname', 'noexe', 'noargs', 'prenoname', 'prenoexe', |
| 305 |
'prenoargs', 'rmnoname', 'rmnoexe', 'rmnoargs']: |
| 306 |
config = BiomajConfig(conf) |
| 307 |
assert not config.check() |
| 308 |
|
| 309 |
|
| 310 |
class TestElastic(): |
| 311 |
""" |
| 312 |
test indexing and search |
| 313 |
""" |
| 314 |
|
| 315 |
def setup_method(self, m): |
| 316 |
BmajIndex.es = None |
| 317 |
self.utils = UtilsForTest() |
| 318 |
) |
| 319 |
BiomajConfig.load_config(self.utils.global_properties, |
| 320 |
allow_user_config=False) |
| 321 |
if BmajIndex.do_index == False: |
| 322 |
pytest.skip("Skipping indexing tests due to elasticsearch not available") |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
BmajIndex.delete_all_bank('local') |
| 327 |
|
| 328 |
self.config = BiomajConfig('local') |
| 329 |
data_dir = self.config.get('data.dir') |
| 330 |
lock_file = os.path.join(data_dir,'local.lock') |
| 331 |
if os.path.exists(lock_file): |
| 332 |
os.remove(lock_file) |
| 333 |
|
| 334 |
def teardown_method(self, m): |
| 335 |
data_dir = self.config.get('data.dir') |
| 336 |
lock_file = os.path.join(data_dir,'local.lock') |
| 337 |
if os.path.exists(lock_file): |
| 338 |
os.remove(lock_file) |
| 339 |
self.utils.clean() |
| 340 |
BmajIndex.delete_all_bank('test') |
| 341 |
|
| 342 |
def test_index(self): |
| 343 |
BmajIndex.do_index = True |
| 344 |
prod = { |
| 345 |
"data_dir" : "/tmp/test/data", |
| 346 |
"formats" : { |
| 347 |
"fasta" : [ |
| 348 |
{ |
| 349 |
"files" : [ |
| 350 |
"fasta/chr1.fa", |
| 351 |
"fasta/chr2.fa" |
| 352 |
], |
| 353 |
"types" : [ |
| 354 |
"nucleic" |
| 355 |
], |
| 356 |
"tags" : { |
| 357 |
"organism" : "hg19" |
| 358 |
} |
| 359 |
} |
| 360 |
], |
| 361 |
"blast": [ |
| 362 |
{ |
| 363 |
"files" : [ |
| 364 |
"blast/chr1/chr1db" |
| 365 |
], |
| 366 |
"types" : [ |
| 367 |
"nucleic" |
| 368 |
], |
| 369 |
"tags" : { |
| 370 |
"chr" : "chr1", |
| 371 |
"organism" : "hg19" |
| 372 |
} |
| 373 |
} |
| 374 |
] |
| 375 |
|
| 376 |
}, |
| 377 |
"freeze" : False, |
| 378 |
"session" : 1416229253.930908, |
| 379 |
"prod_dir" : "alu-2003-11-26", |
| 380 |
"release" : "2003-11-26", |
| 381 |
"types" : [ |
| 382 |
"nucleic" |
| 383 |
] |
| 384 |
} |
| 385 |
|
| 386 |
BmajIndex.add('test',prod, True) |
| 387 |
|
| 388 |
query = { |
| 389 |
'query' : { |
| 390 |
'match' : {'bank': 'test'} |
| 391 |
} |
| 392 |
} |
| 393 |
res = BmajIndex.search(query) |
| 394 |
assert len(res)==2 |
| 395 |
|
| 396 |
|
| 397 |
def test_remove_all(self): |
| 398 |
self.test_index() |
| 399 |
query = { |
| 400 |
'query' : { |
| 401 |
'match' : {'bank': 'test'} |
| 402 |
} |
| 403 |
} |
| 404 |
BmajIndex.delete_all_bank('test') |
| 405 |
res = BmajIndex.search(query) |
| 406 |
assert len(res)==0 |