-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
test_utils.py
209 lines (161 loc) · 6.29 KB
/
test_utils.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
from tempfile import NamedTemporaryFile
import sys
import pytest
from ray.dashboard.modules.job.common import JobSubmitRequest
from ray.dashboard.modules.job.utils import (
file_tail_iterator,
strip_keys_with_value_none,
parse_and_validate_request,
redact_url_password,
)
@pytest.fixture
def tmp():
with NamedTemporaryFile() as f:
yield f.name
def test_strip_keys_with_value_none():
d = {"a": 1, "b": None, "c": 3}
assert strip_keys_with_value_none(d) == {"a": 1, "c": 3}
d = {"a": 1, "b": 2, "c": 3}
assert strip_keys_with_value_none(d) == d
d = {"a": 1, "b": None, "c": None}
assert strip_keys_with_value_none(d) == {"a": 1}
def test_redact_url_password():
url = "http://user:password@host:port"
assert redact_url_password(url) == "http://user:<redacted>@host:port"
url = "http://user:password@host:port?query=1"
assert redact_url_password(url) == "http://user:<redacted>@host:port?query=1"
url = "http://user:password@host:port?query=1&password=2"
assert (
redact_url_password(url)
== "http://user:<redacted>@host:port?query=1&password=2"
)
url = "https://user:password@127.0.0.1:8080"
assert redact_url_password(url) == "https://user:<redacted>@127.0.0.1:8080"
url = "https://user:password@host:port?query=1"
assert redact_url_password(url) == "https://user:<redacted>@host:port?query=1"
url = "https://user:password@host:port?query=1&password=2"
assert (
redact_url_password(url)
== "https://user:<redacted>@host:port?query=1&password=2"
)
# Mock for aiohttp.web.Request, which should not be constructed directly.
class MockRequest:
def __init__(self, **kwargs):
self._json = kwargs
async def json(self):
return self._json
@pytest.mark.asyncio
async def test_mock_request():
request = MockRequest(a=1, b=2)
assert await request.json() == {"a": 1, "b": 2}
request = MockRequest(a=1, b=None)
assert await request.json() == {"a": 1, "b": None}
# async test
@pytest.mark.asyncio
class TestParseAndValidateRequest:
async def test_basic(self):
request = MockRequest(entrypoint="echo hi")
expected = JobSubmitRequest(entrypoint="echo hi")
assert await parse_and_validate_request(request, JobSubmitRequest) == expected
async def test_forward_compatibility(self):
request = MockRequest(entrypoint="echo hi", new_client_field=None)
expected = JobSubmitRequest(entrypoint="echo hi")
assert await parse_and_validate_request(request, JobSubmitRequest) == expected
class TestIterLine:
def test_invalid_type(self):
with pytest.raises(TypeError, match="path must be a string"):
next(file_tail_iterator(1))
def test_file_not_created(self, tmp):
it = file_tail_iterator(tmp)
assert next(it) is None
f = open(tmp, "w")
f.write("hi\n")
f.flush()
assert next(it) is not None
def test_wait_for_newline(self, tmp):
it = file_tail_iterator(tmp)
assert next(it) is None
f = open(tmp, "w")
f.write("no_newline_yet")
assert next(it) is None
f.write("\n")
f.flush()
assert next(it) == ["no_newline_yet\n"]
def test_multiple_lines(self, tmp):
it = file_tail_iterator(tmp)
assert next(it) is None
f = open(tmp, "w")
num_lines = 10
for i in range(num_lines):
s = f"{i}\n"
f.write(s)
f.flush()
assert next(it) == [s]
assert next(it) is None
def test_batching(self, tmp):
it = file_tail_iterator(tmp)
assert next(it) is None
f = open(tmp, "w")
# Write lines in batches of 10, check that we get them back in batches.
for _ in range(100):
num_lines = 10
for i in range(num_lines):
f.write(f"{i}\n")
f.flush()
assert next(it) == [f"{i}\n" for i in range(10)]
assert next(it) is None
def test_max_line_batching(self, tmp):
it = file_tail_iterator(tmp)
assert next(it) is None
f = open(tmp, "w")
# Write lines in batches of 50, check that we get them back in batches of 10.
for _ in range(100):
num_lines = 50
for i in range(num_lines):
f.write(f"{i}\n")
f.flush()
assert next(it) == [f"{i}\n" for i in range(10)]
assert next(it) == [f"{i}\n" for i in range(10, 20)]
assert next(it) == [f"{i}\n" for i in range(20, 30)]
assert next(it) == [f"{i}\n" for i in range(30, 40)]
assert next(it) == [f"{i}\n" for i in range(40, 50)]
assert next(it) is None
def test_max_char_batching(self, tmp):
it = file_tail_iterator(tmp)
assert next(it) is None
f = open(tmp, "w")
# Write a single line that is over 60000 characters,
# check we get it in batches of 20000
f.write(f"{'1234567890' * 6000}\n")
f.flush()
assert next(it) == ["1234567890" * 2000]
assert next(it) == ["1234567890" * 2000]
assert next(it) == ["1234567890" * 2000]
assert next(it) == ["\n"]
assert next(it) is None
# Write a 10 lines where last line is over 20000 characters,
# check we get it in batches of 20000
for i in range(9):
f.write(f"{i}\n")
f.write(f"{'1234567890' * 2000}\n")
f.flush()
first_nine_lines = [f"{i}\n" for i in range(9)]
first_nine_lines_length = sum(len(line) for line in first_nine_lines)
assert next(it) == first_nine_lines + [
f"{'1234567890' * 2000}"[0:-first_nine_lines_length]
]
# Remainder of last line
assert next(it) == [f"{'1234567890' * 2000}"[-first_nine_lines_length:] + "\n"]
assert next(it) is None
def test_delete_file(self):
with NamedTemporaryFile() as tmp:
it = file_tail_iterator(tmp.name)
f = open(tmp.name, "w")
assert next(it) is None
f.write("hi\n")
f.flush()
assert next(it) == ["hi\n"]
# Calls should continue returning None after file deleted.
assert next(it) is None
if __name__ == "__main__":
sys.exit(pytest.main(["-v", __file__]))