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

test_meta.py 4.1 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
  1. import pytest
  2. from .utils import git_repo
  3. import os
  4. import glob
  5. import sys
  6. import six
  7. from click.testing import CliRunner
  8. import wandb
  9. import types
  10. import subprocess
  11. from wandb import env
  12. from wandb import util
  13. from wandb.meta import Meta
  14. from wandb.apis import InternalApi
  15. def test_meta(git_repo, mocker):
  16. mocker.patch.object(sys, 'argv', ["foo", "bar"])
  17. meta = Meta(InternalApi())
  18. meta.write()
  19. print(meta.data)
  20. assert meta.data["cpu_count"] > 0
  21. assert meta.data["git"]["commit"]
  22. assert meta.data["heartbeatAt"]
  23. assert meta.data["startedAt"]
  24. assert meta.data["host"]
  25. assert meta.data["root"] == os.getcwd()
  26. assert meta.data["python"]
  27. assert meta.data["program"]
  28. assert meta.data["executable"]
  29. assert meta.data["args"] == ["bar"]
  30. assert meta.data["state"] == "running"
  31. assert meta.data["username"]
  32. assert meta.data["os"]
  33. def test_anonymous_redaction(mocker):
  34. mocker.patch.object(sys, 'argv', ["foo", "bar"])
  35. api = InternalApi()
  36. api.set_setting('anonymous', 'true')
  37. meta = Meta(api, "wandb")
  38. meta.write()
  39. print(meta.data)
  40. assert "host" not in meta.data
  41. assert "username" not in meta.data
  42. assert "executable" not in meta.data
  43. assert "email" not in meta.data
  44. assert "root" not in meta.data
  45. def test_disable_code(git_repo):
  46. os.environ[env.DISABLE_CODE] = "true"
  47. meta = Meta(InternalApi())
  48. assert meta.data.get("git") is None
  49. del os.environ[env.DISABLE_CODE]
  50. def test_colab(mocker, monkeypatch):
  51. with CliRunner().isolated_filesystem():
  52. mocker.patch('wandb._get_python_type', lambda: "jupyter")
  53. with open("test.ipynb", "w") as f:
  54. f.write("{}")
  55. module = types.ModuleType("fake_jupyter")
  56. module.notebook_metadata = lambda: {"path": "fileId=123abc", "name": "test.ipynb", "root": os.getcwd()}
  57. monkeypatch.setattr(wandb, 'jupyter', module)
  58. meta = Meta(InternalApi())
  59. assert meta.data["colab"] == "https://colab.research.google.com/drive/123abc"
  60. assert meta.data["program"] == "test.ipynb"
  61. assert meta.data["codeSaved"]
  62. assert os.path.exists("code/test.ipynb")
  63. def test_git_untracked_notebook_env(monkeypatch, git_repo, mocker):
  64. mocker.patch('wandb._get_python_type', lambda: "jupyter")
  65. with open("test.ipynb", "w") as f:
  66. f.write("{}")
  67. os.environ[env.NOTEBOOK_NAME] = "test.ipynb"
  68. meta = Meta(InternalApi())
  69. assert meta.data["program"] == "test.ipynb"
  70. assert meta.data["codeSaved"]
  71. assert os.path.exists("code/test.ipynb")
  72. os.environ[env.NOTEBOOK_NAME]
  73. def test_git_untracked_notebook_env_subdir(monkeypatch, git_repo, mocker):
  74. mocker.patch('wandb._get_python_type', lambda: "jupyter")
  75. util.mkdir_exists_ok("sub")
  76. with open("sub/test.ipynb", "w") as f:
  77. f.write("{}")
  78. os.environ[env.NOTEBOOK_NAME] = "sub/test.ipynb"
  79. meta = Meta(InternalApi())
  80. assert meta.data["program"] == "sub/test.ipynb"
  81. assert meta.data["codeSaved"]
  82. assert os.path.exists("code/sub/test.ipynb")
  83. os.environ[env.NOTEBOOK_NAME]
  84. def test_git_tracked_notebook_env(monkeypatch, git_repo, mocker):
  85. mocker.patch('wandb._get_python_type', lambda: "jupyter")
  86. with open("test.ipynb", "w") as f:
  87. f.write("{}")
  88. subprocess.check_call(['git', 'add', 'test.ipynb'])
  89. os.environ[env.NOTEBOOK_NAME] = "test.ipynb"
  90. meta = Meta(InternalApi())
  91. assert meta.data["program"] == "test.ipynb"
  92. assert not meta.data.get("codeSaved")
  93. assert not os.path.exists("code/test.ipynb")
  94. os.environ[env.NOTEBOOK_NAME]
  95. def test_meta_cuda(mocker):
  96. mocker.patch('wandb.meta.os.path.exists', lambda path: True)
  97. def magic(path, mode="w"):
  98. if "cuda/version.txt" in path:
  99. return six.StringIO("CUDA Version 9.0.176")
  100. else:
  101. return open(path, mode=mode)
  102. mocker.patch('wandb.meta.open', magic)
  103. meta = Meta(InternalApi())
  104. assert meta.data["cuda"] == "9.0.176"
  105. def test_meta_thread(git_repo):
  106. meta = Meta(InternalApi(), "wandb")
  107. meta.start()
  108. meta.shutdown()
  109. print("GO", glob.glob("**"))
  110. assert os.path.exists("wandb/wandb-metadata.json")
Tip!

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

Comments

Loading...