-
Notifications
You must be signed in to change notification settings - Fork 17
/
run.py
executable file
·157 lines (121 loc) · 4.03 KB
/
run.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
#!/usr/bin/env python
from collections import defaultdict
import os
from authutils import ROLES as all_roles
from flask import current_app
from mock import patch, PropertyMock
from psqlgraph import PolyNode as Node
import requests
from sheepdog.api import run_for_development
requests.packages.urllib3.disable_warnings()
all_role_values = list(all_roles.values())
roles = defaultdict(lambda: all_role_values)
class FakeBotoKey(object):
def __init__(self, name):
self.name = name
def close(self):
pass
def open_read(self, *args, **kwargs):
pass
@property
def size(self):
return len("fake data for {}".format(self.name))
def __iter__(self):
for string in ["fake ", "data ", "for ", self.name]:
yield string
def fake_get_nodes(dids):
nodes = []
for did in dids:
try:
file_name = files.get(did, {})["data"]["file_name"]
except ValueError:
file_name = did
nodes.append(
Node(
node_id=did,
label="file",
acl=["open"],
properties={
"file_name": file_name,
"file_size": len("fake data for {}".format(did)),
"md5sum": "fake_md5sum",
"state": "live",
},
)
)
return nodes
def fake_urls_from_index_client(did):
return ["s3://fake-host/fake_bucket/{}".format(did)]
def fake_key_for(parsed):
return FakeBotoKey(parsed.netloc.split("/")[-1])
def fake_key_for_node(node):
return FakeBotoKey(node.node_id)
class FakeUser(object):
username = "test"
roles = roles
def set_user(*args, **kwargs):
from flask import g
g.user = FakeUser()
def run_with_fake_auth():
def get_project_ids(role="_member_", project_ids=None):
from gdcdatamodel import models as md
if project_ids is None:
project_ids = []
if not project_ids:
with current_app.db.session_scope():
project_ids += [
"{}-{}".format(p.programs[0].name, p.code)
for p in current_app.db.nodes(md.Project).all()
]
return project_ids
with patch(
"sheepdog.auth.FederatedUser.roles",
new_callable=PropertyMock,
return_value=roles,
), patch(
"sheepdog.auth.FederatedUser.logged_in",
new_callable=PropertyMock,
return_value=lambda: True,
), patch(
"sheepdog.auth.FederatedUser.get_project_ids",
new_callable=PropertyMock,
return_value=get_project_ids,
), patch(
"sheepdog.auth.verify_hmac", new=set_user
):
run_for_development(debug=debug, threaded=True)
def run_with_fake_authz():
"""
Mocks arborist calls.
"""
authorized = True # modify this to mock authorized/unauthorized
with patch(
"gen3authz.client.arborist.client.ArboristClient.create_resource",
new_callable=PropertyMock,
), patch(
"gen3authz.client.arborist.client.ArboristClient.auth_request",
new_callable=PropertyMock,
return_value=lambda jwt, service, methods, resources: authorized,
):
run_for_development(debug=debug, threaded=True)
def run_with_fake_download():
with patch("sheepdog.download.get_nodes", fake_get_nodes):
with patch.multiple(
"sheepdog.download",
key_for=fake_key_for,
key_for_node=fake_key_for_node,
urls_from_index_client=fake_urls_from_index_client,
):
if os.environ.get("GDC_FAKE_AUTH"):
run_with_fake_auth()
else:
run_for_development(debug=debug, threaded=True)
if __name__ == "__main__":
debug = bool(os.environ.get("SHEEPDOG_DEBUG", True))
if os.environ.get("GDC_FAKE_DOWNLOAD") == "True":
run_with_fake_download()
else:
if os.environ.get("GDC_FAKE_AUTH") == "True":
run_with_fake_auth()
else:
run_with_fake_authz()