Skip to content

Commit beb44c0

Browse files
authored
fix tracking tmp files in multipart tests (#1584)
1 parent 5b8f14b commit beb44c0

2 files changed

Lines changed: 19 additions & 12 deletions

File tree

tests/python/lib/k2_server.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def _process_json_log(self, log_record):
6464
log_record.pop("time", "")
6565
log_record.pop("thread_id", "")
6666
log_record.pop("component_name", "")
67+
log_record.pop("component_version", "")
6768
log_record.pop("level", "")
6869
log_record.pop("target", "")
6970
log_record.pop("instance_id", "")

tests/python/tests/http_server/test_multipart.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import os
2+
import re
23
from urllib.parse import urlencode
34

45
from python.lib.testcase import WebServerAutoTestCase
56

7+
def get_multipart_temp_files():
8+
"""Get files that look like multipart temp files (6 random alphanumeric chars in /tmp/, optionally with tmp. prefix)."""
9+
pattern = re.compile(r'^(tmp\.)?[a-zA-Z0-9]{6}$')
10+
return set(f for f in os.listdir("/tmp/") if pattern.match(f) and os.path.isfile(f"/tmp/{f}"))
11+
612

713
class TestMultipartContentType(WebServerAutoTestCase):
814

@@ -190,7 +196,7 @@ def test_multipart_non_terminating_boundary(self):
190196

191197
def test_multipart_filename_attribute(self):
192198

193-
tmp_files = os.listdir("/tmp/")
199+
tmp_files = get_multipart_temp_files()
194200
boundary = "------------------------d74496d66958873e"
195201

196202
file_bytes = b"Hello from test.txt\nSecond line\n"
@@ -221,12 +227,12 @@ def test_multipart_filename_attribute(self):
221227
self.assertTrue(response.content.find(b"filename : test.txt") != -1)
222228
self.assertTrue(response.content.find(b"Hello from test.txt") != -1)
223229

224-
tmp_files_after_script = os.listdir("/tmp/")
230+
tmp_files_after_script = get_multipart_temp_files()
225231
# check that script delete tmp files at the end
226232
self.assertEqual(sorted(tmp_files), sorted(tmp_files_after_script))
227233

228234
def test_multipart_filename_array_attribute(self):
229-
tmp_files = os.listdir("/tmp/")
235+
tmp_files = get_multipart_temp_files()
230236

231237
boundary = "------------------------d74496d66958873e"
232238

@@ -270,13 +276,13 @@ def test_multipart_filename_array_attribute(self):
270276
self.assertTrue(response.content.find(b"Hello from a.txt") != -1)
271277
self.assertTrue(response.content.find(b"Hello from b.txt") != -1)
272278

273-
tmp_files_after_script = os.listdir("/tmp/")
279+
tmp_files_after_script = get_multipart_temp_files()
274280
# check that script delete tmp files at the end
275281
self.assertEqual(sorted(tmp_files), sorted(tmp_files_after_script))
276282

277283
def test_multipart_superglobal_modify(self):
278284

279-
tmp_files = os.listdir("/tmp/")
285+
tmp_files = get_multipart_temp_files()
280286
boundary = "------------------------d74496d66958873e"
281287

282288
file_bytes = b"Hello from test.txt\nSecond line\n"
@@ -305,13 +311,13 @@ def test_multipart_superglobal_modify(self):
305311

306312
self.assertEqual(200, response.status_code)
307313

308-
tmp_files_after_script = os.listdir("/tmp/")
314+
tmp_files_after_script = get_multipart_temp_files()
309315
# check that script delete tmp files at the end
310316
self.assertEqual(sorted(tmp_files), sorted(tmp_files_after_script))
311317

312318
def test_multipart_mixed_files_and_fields(self):
313319
"""Test mixing files and regular form fields in the same request."""
314-
tmp_files = os.listdir("/tmp/")
320+
tmp_files = get_multipart_temp_files()
315321
boundary = '------------------------d74496d66958873e'
316322

317323
file_bytes = b'File content here\n'
@@ -350,13 +356,13 @@ def test_multipart_mixed_files_and_fields(self):
350356
self.assertTrue(response.content.find(b'filename : test.txt') != -1)
351357
self.assertTrue(response.content.find(b'another : another value') != -1)
352358

353-
tmp_files_after_script = os.listdir("/tmp/")
359+
tmp_files_after_script = get_multipart_temp_files()
354360
# check that script delete tmp files at the end
355361
self.assertEqual(sorted(tmp_files), sorted(tmp_files_after_script))
356362

357363
def test_multipart_file_without_content_type(self):
358364
"""Test file upload without explicit Content-Type header."""
359-
tmp_files = os.listdir("/tmp/")
365+
tmp_files = get_multipart_temp_files()
360366
boundary = '------------------------d74496d66958873e'
361367

362368
file_bytes = b'File without content type\n'
@@ -387,13 +393,13 @@ def test_multipart_file_without_content_type(self):
387393
# Should default to text/plain
388394
self.assertTrue(response.content.find(b'type : text/plain') != -1)
389395

390-
tmp_files_after_script = os.listdir("/tmp/")
396+
tmp_files_after_script = get_multipart_temp_files()
391397
# check that script delete tmp files at the end
392398
self.assertEqual(sorted(tmp_files), sorted(tmp_files_after_script))
393399

394400
def test_multipart_binary_file(self):
395401
"""Test uploading binary file content."""
396-
tmp_files = os.listdir("/tmp/")
402+
tmp_files = get_multipart_temp_files()
397403
boundary = '------------------------d74496d66958873e'
398404

399405
# Binary content with null bytes
@@ -424,7 +430,7 @@ def test_multipart_binary_file(self):
424430
self.assertTrue(response.content.find(b'size : 8') != -1)
425431
self.assertTrue(response.content.find(b'filename : data.bin') != -1)
426432

427-
tmp_files_after_script = os.listdir("/tmp/")
433+
tmp_files_after_script = get_multipart_temp_files()
428434
# check that script delete tmp files at the end
429435
self.assertEqual(sorted(tmp_files), sorted(tmp_files_after_script))
430436

0 commit comments

Comments
 (0)