forked from SwissDataScienceCenter/renku-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
1221 lines (964 loc) · 34.4 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
#
# Copyright 2017-2020- Swiss Data Science Center (SDSC)
# A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and
# Eidgenössische Technische Hochschule Zürich (ETHZ).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Pytest configuration."""
import contextlib
import json
import os
import pathlib
import re
import shutil
import tarfile
import tempfile
import time
import urllib
import uuid
import warnings
from copy import deepcopy
from pathlib import Path
import fakeredis
import git
import pytest
import requests
import responses
import yaml
from _pytest.monkeypatch import MonkeyPatch
from click.testing import CliRunner
from git import Repo
from tests.utils import make_dataset_add_payload
from walrus import Database
IT_PROTECTED_REMOTE_REPO_URL = os.getenv(
'IT_PROTECTED_REMOTE_REPO',
'https://dev.renku.ch/gitlab/contact/protected-renku.git'
)
IT_REMOTE_REPO_URL = os.getenv(
'IT_REMOTE_REPOSITORY',
'https://dev.renku.ch/gitlab/contact/integration-test'
)
IT_GIT_ACCESS_TOKEN = os.getenv('IT_OAUTH_GIT_TOKEN')
@pytest.fixture(scope='module')
def renku_path(tmpdir_factory):
"""Temporary instance path."""
path = str(tmpdir_factory.mktemp('renku'))
yield path
shutil.rmtree(path)
@pytest.fixture()
def instance_path(renku_path, monkeypatch):
"""Temporary instance path."""
with monkeypatch.context() as m:
m.chdir(renku_path)
yield renku_path
@pytest.fixture()
def runner():
"""Create a runner on isolated filesystem."""
return CliRunner()
@pytest.fixture
def global_config_dir(monkeypatch, tmpdir_factory):
"""Create a temporary renku config directory."""
from renku.core.management.config import ConfigManagerMixin
with monkeypatch.context() as m:
home_dir = tmpdir_factory.mktemp('fake_home').strpath
m.setattr(ConfigManagerMixin, 'global_config_dir', home_dir)
yield m
@pytest.fixture()
def run_shell():
"""Create a shell cmd runner."""
import subprocess
def run_(cmd, return_ps=None, sleep_for=None):
"""Spawn subprocess and execute shell command.
:param return_ps: Return process object.
:param sleep_for: After executing command sleep for n seconds.
:returns: Process object or tuple (stdout, stderr).
"""
ps = subprocess.Popen(
cmd,
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
if return_ps:
return ps
output = ps.communicate()
if sleep_for:
time.sleep(sleep_for)
return output
return run_
@pytest.fixture()
def run(runner, capsys):
"""Return a callable runner."""
from renku.cli import cli
from renku.core.utils.contexts import Isolation
def generate(args=('update', ), cwd=None, **streams):
"""Generate an output."""
with capsys.disabled(), Isolation(cwd=cwd, **streams):
try:
cli.main(
args=args,
prog_name=runner.get_default_prog_name(cli),
)
except SystemExit as e:
return 0 if e.code is None else e.code
except Exception:
raise
return generate
@pytest.fixture()
def isolated_runner():
"""Create a runner on isolated filesystem."""
runner_ = CliRunner()
with runner_.isolated_filesystem():
yield runner_
@pytest.fixture()
def data_file(tmpdir):
"""Create a sample data file."""
p = tmpdir.mkdir('data').join('file')
p.write('1234')
return p
@pytest.fixture(scope='module')
def repository():
"""Yield a Renku repository."""
from renku.cli import cli
runner = CliRunner()
with runner.isolated_filesystem() as project_path:
result = runner.invoke(
cli, ['init', '.', '--template-id', 'python-minimal'],
'\n',
catch_exceptions=False
)
assert 0 == result.exit_code
yield project_path
@pytest.fixture
def project(repository):
"""Create a test project."""
from git import Repo
from renku.cli import cli
runner = CliRunner()
repo = Repo(repository, search_parent_directories=True)
commit = repo.head.commit
os.chdir(repository)
yield repository
os.chdir(repository)
repo.head.reset(commit, index=True, working_tree=True)
# remove any extra non-tracked files (.pyc, etc)
repo.git.clean('-xdff')
assert 0 == runner.invoke(
cli, ['githooks', 'install', '--force']
).exit_code
@pytest.fixture
def project_metadata(project):
"""Create project with metadata."""
metadata = {
'project_id': uuid.uuid4().hex,
'name': Path(project).name,
'fullname': 'full project name',
'email': 'my@email.com',
'owner': 'me',
'token': 'awesome token',
'git_url': 'git@gitlab.com'
}
yield project, metadata
@pytest.fixture
def client(project):
"""Return a Renku repository."""
from renku.core.management import LocalClient
original_get_value = LocalClient.get_value
def mocked_get_value(
self, section, key, local_only=False, global_only=False
):
"""We don't want lfs warnings in tests."""
if key == 'show_lfs_message':
return 'False'
return original_get_value(self, section, key, local_only, global_only)
LocalClient.get_value = mocked_get_value
yield LocalClient(path=project)
LocalClient.get_value = original_get_value
@pytest.fixture(scope='function')
def client_with_remote(client, tmpdir_factory):
"""Return a client with a (local) remote set."""
# create remote
path = str(tmpdir_factory.mktemp('remote'))
Repo().init(path, bare=True)
origin = client.repo.create_remote('origin', path)
client.repo.git.push('--set-upstream', 'origin', 'master')
yield {'client': client, 'origin': origin}
client.repo.git.branch('--unset-upstream')
client.repo.delete_remote(origin)
shutil.rmtree(path)
@pytest.fixture
def no_lfs_warning(client):
"""Sets show_lfs_message to False.
For those times in life when mocking just isn't enough.
"""
with client.commit():
client.set_value('renku', 'show_lfs_message', 'False')
yield client
@pytest.fixture(scope='function')
def client_with_lfs_warning(project):
"""Return a Renku repository with lfs warnings active."""
from renku.core.management import LocalClient
client = LocalClient(path=project)
client.set_value('renku', 'lfs_threshold', '0b')
client.repo.git.add('.renku/renku.ini')
client.repo.index.commit('update renku.ini')
yield client
@pytest.fixture
def dataset(client):
"""Create a dataset."""
from renku.core.models.provenance.agents import Person
with client.with_dataset('dataset', create=True) as dataset:
dataset.creator = [
Person(
**{
'affiliation': 'xxx',
'email': 'me@example.com',
'id': 'me_id',
'name': 'me',
}
)
]
return dataset
@pytest.fixture(params=['.', 'some/sub/directory'])
def subdirectory(request):
"""Runs tests in root directory and a subdirectory."""
from renku.core.utils.contexts import chdir
if request.param != '.':
path = Path(request.param) / '.gitkeep'
path.parent.mkdir(parents=True, exist_ok=True)
path.touch()
Repo().git.add(str(path))
Repo().index.commit('Create subdirectory')
with chdir(request.param):
yield
@pytest.fixture
def dataset_responses():
"""Authentication responses."""
with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
def request_callback(request):
return (200, {'Content-Type': 'application/text'}, '1234')
rsps.add_callback(
responses.GET,
'http://example.com/file',
callback=request_callback
)
rsps.add_callback(
responses.GET,
'https://example.com/file',
callback=request_callback
)
rsps.add_callback(
responses.GET,
'http://example.com/file.ext?foo=bar',
callback=request_callback
)
yield rsps
@pytest.fixture(scope='function')
def directory_tree(tmpdir_factory):
"""Create a test directory tree."""
# initialize
p = tmpdir_factory.mktemp('directory_tree')
p.join('file').write('1234')
p.join('dir2').mkdir()
p.join('dir2/file2').write('5678')
return p
@pytest.fixture(scope='function')
def data_repository(directory_tree):
"""Create a test repo."""
from git import Repo, Actor
# initialize
repo = Repo.init(directory_tree.strpath)
# add a file
repo.index.add([directory_tree.join('file').strpath])
repo.index.commit('test commit', author=Actor('me', 'me@example.com'))
# commit changes to the same file with a different user
directory_tree.join('file').write('5678')
repo.index.add([directory_tree.join('file').strpath])
repo.index.commit('test commit', author=Actor('me2', 'me2@example.com'))
# commit a second file
repo.index.add([directory_tree.join('dir2/file2').strpath])
repo.index.commit('test commit', author=Actor('me', 'me@example.com'))
# return the repo
return repo
@pytest.fixture(
params=[{
'name': 'old-datasets-v0.3.0.git',
'exit_code': 1
}, {
'name': 'old-datasets-v0.5.0.git',
'exit_code': 1
}, {
'name': 'old-datasets-v0.5.1.git',
'exit_code': 0
}, {
'name': 'test-renku-v0.3.0.git',
'exit_code': 1
}],
scope='module',
)
def old_bare_repository(request, tmpdir_factory):
"""Prepares a testing repo created by old version of renku."""
compressed_repo_path = Path(
__file__
).parent / 'tests' / 'fixtures' / '{0}.tar.gz'.format(
request.param['name']
)
working_dir_path = tmpdir_factory.mktemp(request.param['name'])
with tarfile.open(str(compressed_repo_path), 'r') as fixture:
fixture.extractall(working_dir_path.strpath)
yield {
'path': working_dir_path / request.param['name'],
'exit_code': request.param['exit_code']
}
shutil.rmtree(working_dir_path.strpath)
@pytest.fixture(
scope='function',
params=[{
'name': 'old-workflows-v0.10.3.git',
'log_path': 'catoutput.txt',
'expected_strings': [
'catoutput.txt', '_cat.yaml', '_echo.yaml', '9ecc28b2 stdin.txt',
'bdc801c6 stdout.txt'
]
}, {
'name': 'old-workflows-complicated-v0.10.3.git',
'log_path': 'concat2.txt',
'expected_strings': [
'concat2.txt', '5828275ae5344eba8bad475e7d3cf2d5.cwl',
'_migrated.yaml', '88add2ea output_rand', 'e6fa6bf3 input2.txt'
]
}]
)
def old_workflow_project(request, tmp_path_factory):
"""Prepares a testing repo created by old version of renku."""
import tarfile
from git import Repo
from pathlib import Path
name = request.param['name']
compressed_repo_path = Path(
__file__
).parent / 'tests' / 'fixtures' / '{name}.tar.gz'.format(name=name)
working_dir_path = tmp_path_factory.mktemp(name)
with tarfile.open(str(compressed_repo_path), 'r') as fixture:
fixture.extractall(str(working_dir_path))
path = working_dir_path / name
repo_path = tmp_path_factory.mktemp('repo')
repository = Repo(str(path),
search_parent_directories=True).clone(str(repo_path))
repository_path = repository.working_dir
commit = repository.head.commit
os.chdir(repository_path)
yield {
'repo': repository,
'path': repository_path,
'log_path': request.param['log_path'],
'expected_strings': request.param['expected_strings']
}
os.chdir(repository_path)
repository.head.reset(commit, index=True, working_tree=True)
# remove any extra non-tracked files (.pyc, etc)
repository.git.clean('-xdff')
shutil.rmtree(str(repo_path))
@pytest.fixture(scope='module')
def old_repository(tmpdir_factory, old_bare_repository):
"""Create git repo of old repository fixture."""
import shutil
from git import Repo
repo_path = tmpdir_factory.mktemp('repo')
yield {
'repo':
Repo(
old_bare_repository['path'].strpath,
search_parent_directories=True
).clone(repo_path.strpath),
'exit_code': old_bare_repository['exit_code']
}
shutil.rmtree(repo_path.strpath)
@pytest.fixture
def old_project(old_repository):
"""Create a test project."""
repo = old_repository['repo']
repository_path = repo.working_dir
commit = repo.head.commit
os.chdir(repository_path)
yield {
'repo': repo,
'path': repository_path,
'exit_code': old_repository['exit_code']
}
os.chdir(repository_path)
repo.head.reset(commit, index=True, working_tree=True)
# remove any extra non-tracked files (.pyc, etc)
repo.git.clean('-xdff')
@pytest.fixture
def old_repository_with_submodules(request, tmpdir_factory):
"""Prepares a testing repo that has datasets using git submodules."""
name = 'old-dataset-with-submodule-v0.6.0'
base_path = Path(__file__).parent / 'tests' / 'fixtures' / f'{name}.tar.gz'
working_dir = tmpdir_factory.mktemp(name)
with tarfile.open(str(base_path), 'r') as repo:
repo.extractall(working_dir.strpath)
repo_path = working_dir / name
repo = Repo(repo_path)
os.chdir(repo_path.strpath)
yield repo
shutil.rmtree(repo_path.strpath)
@pytest.fixture(autouse=True)
def add_client(doctest_namespace):
"""Add Renku client to doctest namespace."""
from renku.core.management import LocalClient
doctest_namespace['client'] = LocalClient(path=tempfile.mkdtemp())
@pytest.fixture
def local_client():
"""Add a Renku local client."""
from renku.core.management import LocalClient
with tempfile.TemporaryDirectory() as tempdir:
yield LocalClient(path=tempdir)
@pytest.fixture
def zenodo_sandbox(client):
"""Configure environment to use Zenodo sandbox environment."""
os.environ['ZENODO_USE_SANDBOX'] = 'true'
access_token = os.getenv('ZENODO_ACCESS_TOKEN', '')
client.set_value('zenodo', 'access_token', access_token)
client.repo.git.add('.renku/renku.ini')
client.repo.index.commit('update renku.ini')
@pytest.fixture
def dataverse_demo(client, dataverse_demo_cleanup):
"""Configure environment to use Dataverse demo environment."""
access_token = os.getenv('DATAVERSE_ACCESS_TOKEN', '')
client.set_value('dataverse', 'access_token', access_token)
client.set_value('dataverse', 'server_url', 'https://demo.dataverse.org')
client.repo.git.add('.renku/renku.ini')
client.repo.index.commit('renku.ini')
@pytest.fixture(scope='module')
def dataverse_demo_cleanup(request):
"""Delete all Dataverse datasets at the end of the test session."""
from renku.core.utils.requests import retry
server_url = 'https://demo.dataverse.org'
access_token = os.getenv('DATAVERSE_ACCESS_TOKEN', '')
headers = {'X-Dataverse-key': access_token}
def remove_datasets():
url = f'{server_url}/api/v1/dataverses/sdsc-test-dataverse/contents'
try:
with retry() as session:
response = session.get(url=url, headers=headers)
except (ConnectionError, requests.exceptions.RequestException):
warnings.warn('Cannot clean up Dataverse datasets')
return
if response.status_code != 200:
warnings.warn('Cannot clean up Dataverse datasets')
return
datasets = response.json().get('data', [])
for dataset in datasets:
id = dataset.get('id')
if id is not None:
url = f'https://demo.dataverse.org/api/v1/datasets/{id}'
try:
with retry() as session:
session.delete(url=url, headers=headers)
except (ConnectionError, requests.exceptions.RequestException):
pass
request.addfinalizer(remove_datasets)
@pytest.fixture
def doi_responses():
"""Responses for doi.org requests."""
from renku.core.commands.providers.doi import DOI_BASE_URL
from renku.core.commands.providers.dataverse import (
DATAVERSE_API_PATH, DATAVERSE_VERSION_API
)
with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
def doi_callback(request):
response_url = (
'https://dataverse.harvard.edu/citation'
'?persistentId=doi:10.11588/data/yyxx1122'
)
if 'zenodo' in request.url:
response_url = 'https://zenodo.org/record/3363060'
return (
200, {
'Content-Type': 'application/json'
},
json.dumps({
'type': 'dataset',
'id': request.url,
'author': [{
'family': 'Doe',
'given': 'John'
}],
'contributor': [{
'contributorType': 'ContactPerson',
'family': 'Doe',
'given': 'John'
}],
'issued': {
'date-parts': [[2019]]
},
'abstract': 'Test Dataset',
'DOI': '10.11588/data/yyxx1122',
'publisher': 'heiDATA',
'title': 'dataset',
'URL': response_url
})
)
rsps.add_callback(
method='GET',
url=re.compile('{base_url}/.*'.format(base_url=DOI_BASE_URL)),
callback=doi_callback
)
def version_callback(request):
return (
200, {
'Content-Type': 'application/json'
},
json.dumps({
'status': 'OK',
'data': {
'version': '4.1.3',
'build': 'abcdefg'
}
})
)
base_url = 'https://dataverse.harvard.edu'
url_parts = list(urllib.parse.urlparse(base_url))
url_parts[2] = pathlib.posixpath.join(
DATAVERSE_API_PATH, DATAVERSE_VERSION_API
)
pattern = '{url}.*'.format(url=urllib.parse.urlunparse(url_parts))
rsps.add_callback(
method='GET', url=re.compile(pattern), callback=version_callback
)
yield rsps
@pytest.fixture
def cli(client, run):
"""Return a callable Renku CLI.
It returns the exit code and content of the resulting CWL tool.
"""
import yaml
from renku.core.models.provenance.activities import Activity
def renku_cli(*args):
before_wf_files = set(client.workflow_path.glob('*.yaml'))
exit_code = run(args)
after_wf_files = set(client.workflow_path.glob('*.yaml'))
new_files = after_wf_files - before_wf_files
assert len(new_files) <= 1
if new_files:
wf_filepath = new_files.pop()
with wf_filepath.open('r') as f:
content = Activity.from_jsonld(
yaml.safe_load(f),
client=client,
commit=client.repo.head.commit
)
else:
content = None
return exit_code, content
return renku_cli
@pytest.fixture(
params=[{
'path':
Path(__file__).parent / 'tests' / 'fixtures' / 'doi-dataset.yml',
}, {
'path':
Path(__file__).parent / 'tests' / 'fixtures' /
'broken-dataset-v0.5.2.yml',
}]
)
def dataset_metadata(request):
"""Return dataset metadata fixture."""
from renku.core.models.jsonld import NoDatesSafeLoader
file_path = request.param['path']
data = yaml.load(file_path.read_text(), Loader=NoDatesSafeLoader)
yield data
@pytest.fixture
def dataset_metadata_before_calamus():
"""Return dataset metadata fixture."""
from renku.core.models.jsonld import NoDatesSafeLoader
path = (
Path(__file__).parent / 'tests' / 'fixtures' /
'dataset-v0.10.4-before-calamus.yml'
)
yield yaml.load(path.read_text(), Loader=NoDatesSafeLoader)
@pytest.fixture()
def sleep_after():
"""Fixture that causes a delay after executing a test.
Prevents spamming external providers when used, in case of rate limits.
"""
import time
yield
time.sleep(0.5)
@pytest.fixture
def remote_project(data_repository, directory_tree):
"""A second Renku project with a dataset."""
from renku.cli import cli
runner = CliRunner()
with runner.isolated_filesystem() as project_path:
runner.invoke(
cli, ['-S', 'init', '.', '--template-id', 'python-minimal'], '\n'
)
result = runner.invoke(
cli, ['-S', 'dataset', 'create', 'remote-dataset']
)
assert 0 == result.exit_code
result = runner.invoke(
cli,
[
'-S', 'dataset', 'add', '-s', 'file', '-s', 'dir2',
'remote-dataset', directory_tree.strpath
],
catch_exceptions=False,
)
assert 0 == result.exit_code
yield runner, project_path
@pytest.fixture(scope='function')
def datapack_zip(directory_tree):
"""Returns dummy data folder as a zip archive."""
from renku.core.utils.contexts import chdir
workspace_dir = tempfile.TemporaryDirectory()
with chdir(workspace_dir.name):
shutil.make_archive('datapack', 'zip', str(directory_tree))
yield Path(workspace_dir.name) / 'datapack.zip'
@pytest.fixture(scope='function')
def datapack_tar(directory_tree):
"""Returns dummy data folder as a tar archive."""
from renku.core.utils.contexts import chdir
workspace_dir = tempfile.TemporaryDirectory()
with chdir(workspace_dir.name):
shutil.make_archive('datapack', 'tar', str(directory_tree))
yield Path(workspace_dir.name) / 'datapack.tar'
@pytest.fixture(scope='module')
def mock_redis():
"""Monkey patch service cache with mocked redis."""
from renku.service.cache.base import BaseCache
from renku.service.cache.models.user import User
from renku.service.cache.models.job import Job
from renku.service.cache.models.file import File
from renku.service.cache.models.project import Project
from renku.service.jobs.queues import WorkerQueues
monkey_patch = MonkeyPatch()
with monkey_patch.context() as m:
fake_redis = fakeredis.FakeRedis()
fake_model_db = Database(connection_pool=fake_redis.connection_pool)
m.setattr(WorkerQueues, 'connection', fake_redis)
m.setattr(BaseCache, 'cache', fake_redis)
m.setattr(BaseCache, 'model_db', fake_model_db)
m.setattr(Job, '__database__', fake_model_db)
m.setattr(User, '__database__', fake_model_db)
m.setattr(File, '__database__', fake_model_db)
m.setattr(Project, '__database__', fake_model_db)
yield
monkey_patch.undo()
@pytest.fixture(scope='module')
def svc_client(mock_redis):
"""Renku service client."""
from renku.service.entrypoint import create_app
flask_app = create_app()
testing_client = flask_app.test_client()
testing_client.testing = True
ctx = flask_app.app_context()
ctx.push()
yield testing_client
ctx.pop()
@pytest.fixture(scope='function')
def svc_client_cache(mock_redis):
"""Service jobs fixture."""
from renku.service.entrypoint import create_app
flask_app = create_app()
testing_client = flask_app.test_client()
testing_client.testing = True
ctx = flask_app.app_context()
ctx.push()
headers = {
'Content-Type': 'application/json',
'Renku-User-Id': 'user',
'Renku-User-FullName': 'full name',
'Renku-User-Email': 'renku@sdsc.ethz.ch',
}
yield testing_client, headers, flask_app.config.get('cache')
ctx.pop()
def integration_repo_path(headers, url_components):
"""Constructs integration repo path."""
from renku.service.config import CACHE_PROJECTS_PATH
project_path = (
CACHE_PROJECTS_PATH / headers['Renku-User-Id'] / url_components.owner /
url_components.name
)
return project_path
@contextlib.contextmanager
def integration_repo(headers, url_components):
"""With integration repo helper."""
from renku.core.utils.contexts import chdir
with chdir(integration_repo_path(headers, url_components)):
repo = Repo('.')
yield repo
@pytest.fixture(scope='module')
def authentication_headers():
"""Get authentication headers."""
headers = {
'Content-Type': 'application/json',
'Renku-User-Id': 'b4b4de0eda0f471ab82702bd5c367fa7',
'Renku-User-FullName': 'Just Sam',
'Renku-User-Email': 'contact@justsam.io',
'Authorization': 'Bearer {0}'.format(os.getenv('IT_OAUTH_GIT_TOKEN')),
}
return headers
@pytest.fixture(scope='module')
def integration_lifecycle(svc_client, mock_redis, authentication_headers):
"""Setup and teardown steps for integration tests."""
from renku.core.models.git import GitURL
url_components = GitURL.parse(IT_REMOTE_REPO_URL)
payload = {'git_url': IT_REMOTE_REPO_URL}
response = svc_client.post(
'/cache.project_clone',
data=json.dumps(payload),
headers=authentication_headers,
)
assert response
assert 'result' in response.json
assert 'error' not in response.json
project_id = response.json['result']['project_id']
assert isinstance(uuid.UUID(project_id), uuid.UUID)
yield svc_client, authentication_headers, project_id, url_components
# Teardown step: Delete all branches except master (if needed).
if integration_repo_path(authentication_headers, url_components).exists():
with integration_repo(authentication_headers, url_components) as repo:
try:
repo.remote().push(
refspec=(':{0}'.format(repo.active_branch.name))
)
except git.exc.GitCommandError:
pass
@pytest.fixture
def svc_client_setup(integration_lifecycle):
"""Service client setup."""
svc_client, headers, project_id, url_components = integration_lifecycle
with integration_repo(headers, url_components) as repo:
repo.git.checkout('master')
new_branch = uuid.uuid4().hex
current = repo.create_head(new_branch)
current.checkout()
yield svc_client, deepcopy(headers), project_id, url_components
@pytest.fixture
def svc_client_with_repo(svc_client_setup):
"""Service client with a remote repository."""
svc_client, headers, project_id, url_components = svc_client_setup
svc_client.post(
'/cache.migrate',
data=json.dumps(dict(project_id=project_id)),
headers=headers
)
yield svc_client, deepcopy(headers), project_id, url_components
@pytest.fixture(scope='module')
def svc_client_with_templates(svc_client, mock_redis, authentication_headers):
"""Setup and teardown steps for templates tests."""
from tests.core.commands.test_init import TEMPLATE_URL, TEMPLATE_REF
template = {'url': TEMPLATE_URL, 'ref': TEMPLATE_REF}
yield svc_client, authentication_headers, template
@pytest.fixture
def svc_protected_repo(svc_client):
"""Service client with remote protected repository."""
headers = {
'Content-Type': 'application/json',
'Renku-User-Id': '{0}'.format(uuid.uuid4().hex),
'Renku-User-FullName': 'Just Sam',
'Renku-User-Email': 'contact@justsam.io',
'Authorization': 'Bearer {0}'.format(IT_GIT_ACCESS_TOKEN),
}
payload = {
'git_url': IT_PROTECTED_REMOTE_REPO_URL,
}