|
4 | 4 |
|
5 | 5 | import asyncio |
6 | 6 | import os |
7 | | -import shutil |
8 | | -import socket |
9 | | -import subprocess |
10 | 7 | import time |
11 | 8 |
|
12 | 9 | import fsspec |
|
18 | 15 | _vectors_to_chunks, |
19 | 16 | ) |
20 | 17 |
|
21 | | -XROOTD_PORT = 1094 |
22 | 18 | TESTDATA1 = "apple\nbanana\norange\ngrape" |
23 | 19 | TESTDATA2 = "red\ngreen\nyellow\nblue" |
24 | 20 | sleep_time = 0.2 |
25 | 21 | expiry_time = 0.1 |
26 | 22 |
|
27 | 23 |
|
28 | | -def require_port_availability(port: int) -> bool: |
29 | | - """Raise an exception if the given port is already in use.""" |
30 | | - with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: |
31 | | - if s.connect_ex(("localhost", port)) == 0: |
32 | | - raise RuntimeError(f"This test requires port {port} to be available") |
33 | | - |
34 | | - |
35 | | -@pytest.fixture(scope="module") |
36 | | -def localserver(tmpdir_factory): |
37 | | - require_port_availability(XROOTD_PORT) |
38 | | - |
39 | | - srvdir = tmpdir_factory.mktemp("srv") |
40 | | - tempPath = os.path.join(srvdir, "Folder") |
41 | | - os.mkdir(tempPath) |
42 | | - cfgfile = os.path.join(srvdir, "xrd.cfg") |
43 | | - with open(cfgfile, "w") as fout: |
44 | | - fout.write("all.export /Folder\n") |
45 | | - fout.write(f"oss.localroot {srvdir}\n") |
46 | | - xrdexe = shutil.which("xrootd") |
47 | | - proc = subprocess.Popen([xrdexe, "-p", str(XROOTD_PORT), "-c", cfgfile]) |
48 | | - time.sleep(2) # give it some startup |
49 | | - yield "root://localhost//Folder", tempPath |
50 | | - proc.terminate() |
51 | | - proc.wait(timeout=10) |
52 | | - |
53 | | - |
54 | | -@pytest.fixture() |
55 | | -def clear_server(localserver): |
56 | | - remoteurl, localpath = localserver |
57 | | - fs, _, _ = fsspec.get_fs_token_paths(remoteurl) |
58 | | - # The open file handles on client side imply an open file handle on the server, |
59 | | - # so removing the directory doesn't actually work until the client closes its handles! |
60 | | - fs.invalidate_cache() |
61 | | - shutil.rmtree(localpath) |
62 | | - os.mkdir(localpath) |
63 | | - yield |
64 | | - |
65 | | - |
66 | | -def test_ping(localserver, clear_server): |
67 | | - remoteurl, localpath = localserver |
68 | | - from XRootD import client |
69 | | - |
70 | | - fs = client.FileSystem(remoteurl) |
71 | | - status, _n = fs.ping() |
72 | | - if not status.ok: |
73 | | - raise OSError(f"Server did not run properly: {status.message}") |
74 | | - |
75 | | - |
76 | 24 | def test_invalid_server(): |
77 | 25 | with pytest.raises(ValueError): |
78 | 26 | fsspec.core.url_to_fs("root://") |
|
0 commit comments