Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

conftest.py 18 KB

You have to be logged in to leave a comment. Sign In
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
  1. import requests_mock
  2. import os
  3. from click.testing import CliRunner
  4. import pytest
  5. from wandb.history import History
  6. from tests.api_mocks import *
  7. import wandb
  8. from wandb import wandb_run
  9. from wandb.apis import InternalApi
  10. import six
  11. import json
  12. import sys
  13. import threading
  14. import logging
  15. from multiprocessing import Process
  16. from vcr.request import Request
  17. from wandb import wandb_socket
  18. from wandb import env
  19. from wandb import util
  20. from wandb.wandb_run import Run
  21. from tests import utils
  22. from tests.mock_server import create_app
  23. def pytest_runtest_setup(item):
  24. # This is used to find tests that are leaking outside of tmp directories
  25. os.environ["WANDB_DESCRIPTION"] = item.parent.name + "#" + item.name
  26. def request_repr(self):
  27. try:
  28. body = json.loads(self.body)
  29. query = body.get("query") or "no_query"
  30. render = query.split("(")[0].split("\n")[0] + " - vars: " + str(body.get("variables", {}).get("files", {}))
  31. except (ValueError, TypeError):
  32. render = "BINARY"
  33. return "({}) {} - {}".format(self.method, self.uri, render)
  34. Request.__repr__ = request_repr
  35. # To enable VCR logging uncomment below
  36. #logging.basicConfig() # you need to initialize logging, otherwise you will not see anything from vcrpy
  37. #vcr_log = logging.getLogger("vcr")
  38. #vcr_log.setLevel(logging.INFO)
  39. @pytest.fixture(scope='module')
  40. def vcr_config():
  41. def replace_body(request):
  42. if "storage.googleapis.com" in request.uri:
  43. request.body = "BINARY DATA"
  44. elif "/file_stream" in request.uri:
  45. request.body = json.dumps({"files": list(json.loads(request.body).get("files", {}.keys()))})
  46. return request
  47. def replace_response_body(response, *args):
  48. """Remove gzip response from pypi"""
  49. if response["headers"].get("Access-Control-Expose-Headers") == ['X-PyPI-Last-Serial']:
  50. if response["headers"].get("Content-Encoding"):
  51. del response["headers"]["Content-Encoding"]
  52. response["body"]["string"] = '{"info":{"version": "%s"}' % wandb.__version__
  53. return response
  54. return {
  55. # Replace the Authorization request header with "DUMMY" in cassettes
  56. "filter_headers": [('authorization', 'DUMMY')],
  57. "match_on": ['method', 'uri', 'query', 'graphql'],
  58. "before_record": replace_body,
  59. "before_record_response": replace_response_body,
  60. }
  61. @pytest.fixture(scope='module')
  62. def vcr(vcr):
  63. def vcr_graphql_matcher(r1, r2):
  64. if "/graphql" in r1.uri and "/graphql" in r2.uri:
  65. body1 = json.loads(r1.body.decode("utf-8"))
  66. body2 = json.loads(r2.body.decode("utf-8"))
  67. return body1["query"].strip() == body2["query"].strip()
  68. elif "/file_stream" in r1.uri and "/file_stream" in r2.uri:
  69. body1 = json.loads(r1.body.decode("utf-8"))
  70. body2 = json.loads(r2.body.decode("utf-8"))
  71. return body1["files"] == body2["files"]
  72. vcr.register_matcher('graphql', vcr_graphql_matcher)
  73. return vcr
  74. @pytest.fixture
  75. def local_netrc(monkeypatch):
  76. with CliRunner().isolated_filesystem():
  77. # TODO: this seems overkill...
  78. origexpand = os.path.expanduser
  79. def expand(path):
  80. return os.path.realpath("netrc") if "netrc" in path else origexpand(path)
  81. monkeypatch.setattr(os.path, "expanduser", expand)
  82. yield
  83. @pytest.fixture
  84. def history():
  85. with CliRunner().isolated_filesystem():
  86. yield Run().history
  87. @pytest.fixture
  88. def wandb_init_run(request, tmpdir, request_mocker, mock_server, monkeypatch, mocker, capsys, local_netrc):
  89. """Fixture that calls wandb.init(), yields a run (or an exception) that
  90. gets created, then cleans up afterward. This is meant to test the logic
  91. in wandb.init, it should generally not spawn a run_manager. If you need
  92. to test run_manager logic use that fixture.
  93. """
  94. # save the environment so we can restore it later. pytest
  95. # may actually do this itself. didn't check.
  96. orig_environ = dict(os.environ)
  97. orig_namespace = None
  98. run = None
  99. # Reset the tensorboard and pytest state
  100. wandb.tensorboard.reset_state()
  101. wandb._global_watch_idx = 0
  102. try:
  103. with CliRunner().isolated_filesystem():
  104. if request.node.get_closest_marker('jupyter'):
  105. def fake_ipython():
  106. class Jupyter(object):
  107. __module__ = "jupyter"
  108. def __init__(self):
  109. class Hook(object):
  110. def register(self, what, where):
  111. pass
  112. self.events = Hook()
  113. def register_magics(self, magic):
  114. pass
  115. return Jupyter()
  116. wandb.get_ipython = fake_ipython
  117. # no i/o wrapping - it breaks pytest
  118. os.environ['WANDB_MODE'] = 'clirun'
  119. if request.node.get_closest_marker('headless'):
  120. mocker.patch('subprocess.Popen')
  121. else:
  122. def mock_headless(run, cloud=True):
  123. print("_init_headless called with cloud=%s" % cloud)
  124. mocker.patch('wandb._init_headless', mock_headless)
  125. if not request.node.get_closest_marker('unconfigured'):
  126. os.environ['WANDB_API_KEY'] = 'test'
  127. os.environ['WANDB_ENTITY'] = 'test'
  128. os.environ['WANDB_PROJECT'] = 'unit-test-project'
  129. else:
  130. # when unconfigured we enable run mode to test missing creds
  131. os.environ['WANDB_MODE'] = 'run'
  132. monkeypatch.setattr('wandb.apis.InternalApi.api_key', None)
  133. monkeypatch.setattr(
  134. 'getpass.getpass', lambda x: "0123456789012345678901234567890123456789")
  135. assert InternalApi().api_key == None
  136. os.environ['WANDB_RUN_DIR'] = str(tmpdir)
  137. if request.node.get_closest_marker('silent'):
  138. os.environ['WANDB_SILENT'] = "true"
  139. assert wandb.run is None
  140. orig_namespace = vars(wandb)
  141. # Mock out run_manager, we add it to run to access state in tests
  142. orig_rm = wandb.run_manager.RunManager
  143. mock = mocker.patch('wandb.run_manager.RunManager')
  144. def fake_init(run, port=None, output=None, cloud=True):
  145. print("Initialized fake run manager")
  146. rm = fake_run_manager(mocker, run, cloud=cloud, rm_class=orig_rm)
  147. rm._block_file_observer()
  148. run.run_manager = rm
  149. return rm
  150. mock.side_effect = fake_init
  151. if request.node.get_closest_marker('args'):
  152. kwargs = request.node.get_closest_marker('args').kwargs
  153. # Unfortunate to enable the test to work
  154. if kwargs.get("dir"):
  155. del os.environ['WANDB_RUN_DIR']
  156. if kwargs.get("tensorboard"):
  157. # The test uses tensorboardX so we need to be sure it's imported
  158. # we use get_module because tensorboardX isn't available in py2
  159. wandb.util.get_module("tensorboardX")
  160. if kwargs.get("error"):
  161. err = kwargs["error"]
  162. del kwargs['error']
  163. if err == "io":
  164. @classmethod
  165. def error(cls):
  166. raise IOError
  167. monkeypatch.setattr(
  168. 'wandb.wandb_run.Run.from_environment_or_defaults', error)
  169. elif err == "socket":
  170. class Error(object):
  171. @property
  172. def port(self):
  173. return 123
  174. def listen(self, secs):
  175. return False, None
  176. monkeypatch.setattr("wandb.wandb_socket.Server", Error)
  177. if kwargs.get('k8s') is not None:
  178. token_path = "/var/run/secrets/kubernetes.io/serviceaccount/token"
  179. crt_path = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
  180. orig_exist = os.path.exists
  181. def exists(path):
  182. return True if path in token_path else orig_exist(path)
  183. def magic(path, *args, **kwargs):
  184. if path == token_path:
  185. return six.StringIO('token')
  186. mocker.patch('wandb.util.open', magic, create=True)
  187. mocker.patch('wandb.util.os.path.exists', exists)
  188. os.environ["KUBERNETES_SERVICE_HOST"] = "k8s"
  189. os.environ["KUBERNETES_PORT_443_TCP_PORT"] = "123"
  190. os.environ["HOSTNAME"] = "test"
  191. if kwargs["k8s"]:
  192. request_mocker.register_uri("GET", "https://k8s:123/api/v1/namespaces/default/pods/test",
  193. content=b'{"status":{"containerStatuses":[{"imageID":"docker-pullable://test@sha256:1234"}]}}')
  194. else:
  195. request_mocker.register_uri("GET", "https://k8s:123/api/v1/namespaces/default/pods/test",
  196. content=b'{}', status_code=500)
  197. del kwargs["k8s"]
  198. if kwargs.get('sagemaker'):
  199. del kwargs['sagemaker']
  200. config_path = "/opt/ml/input/config/hyperparameters.json"
  201. resource_path = "/opt/ml/input/config/resourceconfig.json"
  202. secrets_path = "secrets.env"
  203. os.environ['TRAINING_JOB_NAME'] = 'sage'
  204. os.environ['CURRENT_HOST'] = 'maker'
  205. orig_exist = os.path.exists
  206. def exists(path):
  207. return True if path in (config_path, secrets_path, resource_path) else orig_exist(path)
  208. mocker.patch('wandb.os.path.exists', exists)
  209. def magic(path, *args, **kwargs):
  210. if path == config_path:
  211. return six.StringIO('{"fuckin": "A"}')
  212. elif path == resource_path:
  213. return six.StringIO('{"hosts":["a", "b"]}')
  214. elif path == secrets_path:
  215. return six.StringIO('WANDB_TEST_SECRET=TRUE')
  216. else:
  217. return six.StringIO()
  218. mocker.patch('wandb.open', magic, create=True)
  219. mocker.patch('wandb.util.open', magic, create=True)
  220. elif kwargs.get("tf_config"):
  221. os.environ['TF_CONFIG'] = json.dumps(kwargs['tf_config'])
  222. del kwargs['tf_config']
  223. elif kwargs.get("env"):
  224. for k, v in six.iteritems(kwargs["env"]):
  225. os.environ[k] = v
  226. del kwargs["env"]
  227. else:
  228. kwargs = {}
  229. if request.node.get_closest_marker('resume'):
  230. # env was leaking when running the whole suite...
  231. if os.getenv(env.RUN_ID):
  232. del os.environ[env.RUN_ID]
  233. os.mkdir(wandb.wandb_dir())
  234. with open(os.path.join(wandb.wandb_dir(), wandb_run.RESUME_FNAME), "w") as f:
  235. f.write(json.dumps({"run_id": "test"}))
  236. try:
  237. print("Initializing with", kwargs)
  238. run = wandb.init(**kwargs)
  239. if request.node.get_closest_marker('resume') or request.node.get_closest_marker('mocked_run_manager'):
  240. # Reset history
  241. run._history = None
  242. rm = wandb.run_manager.RunManager(run)
  243. rm.init_run(os.environ)
  244. if request.node.get_closest_marker('mock_socket'):
  245. run.socket = mocker.MagicMock()
  246. assert run is wandb.run
  247. assert run.config is wandb.config
  248. except wandb.LaunchError as e:
  249. print("!!! wandb LaunchError raised")
  250. run = e
  251. yield run
  252. if hasattr(run, "run_manager"):
  253. print("Shutting down run manager")
  254. run.run_manager.test_shutdown()
  255. finally:
  256. # restore the original environment
  257. os.environ.clear()
  258. os.environ.update(orig_environ)
  259. wandb.uninit()
  260. wandb.get_ipython = lambda: None
  261. assert vars(wandb) == orig_namespace
  262. def fake_run_manager(mocker, run=None, cloud=True, rm_class=wandb.run_manager.RunManager):
  263. # NOTE: This will create a run directory so make sure it's called in an isolated file system
  264. # We have an optional rm_class object because we mock it above so we need it before it's mocked
  265. api = InternalApi(load_settings=False)
  266. api.set_setting('project', 'testing')
  267. if wandb.run is None:
  268. wandb.run = run or Run()
  269. wandb.config = wandb.run.config
  270. wandb.run._api = api
  271. wandb.run._mkdir()
  272. wandb.run.socket = wandb_socket.Server()
  273. api.set_current_run_id(wandb.run.id)
  274. mocker.patch('wandb.apis.internal.FileStreamApi')
  275. api._file_stream_api = mocker.MagicMock()
  276. run_manager = rm_class(wandb.run, cloud=cloud, port=wandb.run.socket.port)
  277. class FakeProc(object):
  278. def poll(self):
  279. return None
  280. def exit(self, code=0):
  281. return None
  282. run_manager.proc = FakeProc()
  283. run_manager._meta = mocker.MagicMock()
  284. run_manager._stdout_tee = mocker.MagicMock()
  285. run_manager._stderr_tee = mocker.MagicMock()
  286. run_manager._output_log = mocker.MagicMock()
  287. run_manager._stdout_stream = mocker.MagicMock()
  288. run_manager._stderr_stream = mocker.MagicMock()
  289. run_manager.mirror_stdout_stderr = mocker.MagicMock()
  290. run_manager.unmirror_stdout_stderr = mocker.MagicMock()
  291. socket_thread = threading.Thread(
  292. target=wandb.run.socket.listen)
  293. socket_thread.start()
  294. run_manager._socket.ready()
  295. thread = threading.Thread(
  296. target=run_manager._sync_etc)
  297. thread.daemon = True
  298. thread.start()
  299. def test_shutdown():
  300. if wandb.run and wandb.run.socket:
  301. wandb.run.socket.done()
  302. # TODO: is this needed?
  303. socket_thread.join()
  304. thread.join()
  305. run_manager.test_shutdown = test_shutdown
  306. run_manager._unblock_file_observer()
  307. run_manager._file_pusher._push_function = mocker.MagicMock()
  308. return run_manager
  309. @pytest.fixture
  310. def run_manager(mocker, mock_server):
  311. """This fixture emulates the run_manager headless mode in a single process
  312. Just call run_manager.test_shutdown() to join the threads
  313. """
  314. # Reset the tensorboard state
  315. wandb.tensorboard.reset_state()
  316. with CliRunner().isolated_filesystem():
  317. run_manager = fake_run_manager(mocker)
  318. yield run_manager
  319. wandb.uninit()
  320. @pytest.fixture
  321. def loggedin():
  322. orig_environ = dict(os.environ)
  323. try:
  324. with CliRunner().isolated_filesystem():
  325. os.environ["WANDB_API_KEY"] = "X"*40
  326. yield os.environ
  327. finally:
  328. os.environ.clear()
  329. os.environ.update(orig_environ)
  330. wandb.uninit()
  331. @pytest.fixture
  332. def dryrun():
  333. orig_environ = dict(os.environ)
  334. try:
  335. with CliRunner().isolated_filesystem():
  336. os.environ["WANDB_MODE"] = "dryrun"
  337. yield os.environ
  338. finally:
  339. os.environ.clear()
  340. os.environ.update(orig_environ)
  341. wandb.uninit()
  342. # "Error: 'Session' object has no attribute 'request'""
  343. # @pytest.fixture(autouse=True)
  344. # def no_requests(monkeypatch):
  345. # monkeypatch.delattr("requests.sessions.Session.request")
  346. @pytest.fixture
  347. def request_mocker(request):
  348. """
  349. :param request: pytest request object for cleaning up.
  350. :return: Returns instance of requests mocker used to mock HTTP calls.
  351. """
  352. m = requests_mock.Mocker()
  353. m.start()
  354. request.addfinalizer(m.stop)
  355. return m
  356. @pytest.fixture(autouse=True)
  357. def preserve_environ():
  358. environ = dict(os.environ)
  359. try:
  360. yield
  361. finally:
  362. os.environ.clear()
  363. os.environ.update(environ)
  364. @pytest.fixture(autouse=True)
  365. def check_environ():
  366. """Warn about WANDB_ environment variables the user has set
  367. Sometimes it's useful to set things like WANDB_DEBUG intentionally, or
  368. set other things for hacky debugging, but we want to make sure the user
  369. knows about it.
  370. """
  371. # we ignore WANDB_DESCRIPTION because we set it intentionally in
  372. # pytest_runtest_setup()
  373. wandb_keys = [key for key in os.environ.keys() if key.startswith(
  374. 'WANDB_') and key not in ['WANDB_TEST', 'WANDB_DESCRIPTION']]
  375. if wandb_keys:
  376. wandb.termwarn('You have WANDB_ environment variable(s) set. These may interfere with tests:')
  377. for key in wandb_keys:
  378. wandb.termwarn(' {} = {}'.format(key, repr(os.environ[key])))
  379. @pytest.fixture
  380. def mock_server(mocker, request_mocker):
  381. app = create_app()
  382. mock = utils.RequestsMock(app.test_client(), {})
  383. mocker.patch("gql.transport.requests.requests", mock)
  384. mocker.patch("wandb.apis.file_stream.requests", mock)
  385. mocker.patch("wandb.apis.internal.requests", mock)
  386. return mock
  387. @pytest.fixture
  388. def live_mock_server(request):
  389. if request.node.get_closest_marker('port'):
  390. port = request.node.get_closest_marker('port').args[0]
  391. else:
  392. port = 8765
  393. app = create_app()
  394. server = Process(target=app.run, kwargs={"port": port, "debug": True, "use_reloader": False})
  395. server.start()
  396. yield server
  397. server.terminate()
  398. server.join()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...