Skip to content

Commit d1db90c

Browse files
committed
ci: fix ci failure(#58)
fix: #58 Currently some of the tests relied on the test bucket status. Because the test files were created manually several months ago. We need to decouple this to make our tests more robust. Create test files before the tests session begin.
1 parent f208ddc commit d1db90c

File tree

4 files changed

+114
-78
lines changed

4 files changed

+114
-78
lines changed

tests/conftest.py

+54-9
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
# pylint: disable=missing-function-docstring
55
# pylint: disable=redefined-outer-name
66
import os
7+
import pathlib
78
import subprocess
89
import time
910
import uuid
1011

12+
import oss2
1113
import pytest
1214
import requests
1315

@@ -16,37 +18,46 @@
1618
PORT = 5555
1719
AccessKeyId = os.environ.get("OSS_ACCESS_KEY_ID", "")
1820
AccessKeySecret = os.environ.get("OSS_SECRET_ACCESS_KEY", "")
21+
LICENSE_PATH = os.path.join(
22+
pathlib.Path(__file__).parent.parent.resolve(), "LICENSE"
23+
)
24+
NUMBERS = b"1234567890\n"
1925

2026

2127
test_id = uuid.uuid4()
2228

2329

24-
@pytest.fixture()
30+
@pytest.fixture(scope="session")
2531
def emulator_endpoint():
26-
return "http://127.0.0.1:%s/" % PORT
32+
return f"http://127.0.0.1:{PORT}/"
2733

2834

29-
@pytest.fixture()
35+
@pytest.fixture(scope="session")
3036
def endpoint():
3137
return os.environ.get("OSS_ENDPOINT")
3238

3339

34-
@pytest.fixture()
40+
@pytest.fixture(scope="session")
3541
def test_bucket_name():
3642
return os.environ.get("OSS_TEST_BUCKET_NAME")
3743

3844

39-
@pytest.fixture()
40-
def test_path(test_bucket_name):
41-
return f"/{test_bucket_name}/ossfs_test/{test_id}"
45+
@pytest.fixture(scope="session")
46+
def test_directory():
47+
return f"ossfs_test/{test_id}"
48+
49+
50+
@pytest.fixture(scope="session")
51+
def test_path(test_bucket_name, test_directory):
52+
return f"/{test_bucket_name}/{test_directory}"
4253

4354

4455
@pytest.fixture()
4556
def oss_emulator_server_start(emulator_endpoint):
4657
"""
4758
Start a local emulator server
4859
"""
49-
with subprocess.Popen("ruby bin/emulator -r store -p {}".format(PORT)):
60+
with subprocess.Popen(f"ruby bin/emulator -r store -p {PORT}"):
5061

5162
timeout = 5
5263
while timeout > 0:
@@ -61,7 +72,7 @@ def oss_emulator_server_start(emulator_endpoint):
6172
yield
6273

6374

64-
@pytest.fixture()
75+
@pytest.fixture(scope="session")
6576
def init_config(endpoint):
6677
result = {}
6778
result["key"] = AccessKeyId
@@ -73,3 +84,37 @@ def init_config(endpoint):
7384
@pytest.fixture()
7485
def ossfs(init_config):
7586
return OSSFileSystem(**init_config)
87+
88+
89+
def put_object(endpoint, bucket_name, filename, contents):
90+
auth = oss2.Auth(AccessKeyId, AccessKeySecret)
91+
bucket = oss2.Bucket(auth, endpoint, bucket_name)
92+
bucket.put_object(filename, contents)
93+
94+
95+
def put_file(endpoint, bucket_name, key, filename):
96+
auth = oss2.Auth(AccessKeyId, AccessKeySecret)
97+
bucket = oss2.Bucket(auth, endpoint, bucket_name)
98+
bucket.put_object_from_file(key, filename)
99+
100+
101+
@pytest.fixture(scope="session")
102+
def file_in_anonymous(endpoint, test_directory):
103+
bucket = "dvc-anonymous"
104+
file = f"{test_directory}/file"
105+
put_object(endpoint, bucket, file, "foobar")
106+
return f"/{bucket}/{file}"
107+
108+
109+
@pytest.fixture(scope="session")
110+
def number_file(test_bucket_name, endpoint, test_directory):
111+
filename = f"{test_directory}/number"
112+
put_object(endpoint, test_bucket_name, filename, NUMBERS)
113+
return f"/{test_bucket_name}/{filename}"
114+
115+
116+
@pytest.fixture(scope="session")
117+
def license_file(test_bucket_name, endpoint, test_directory):
118+
filename = f"{test_directory}/LICENSE"
119+
put_file(endpoint, test_bucket_name, filename, LICENSE_PATH)
120+
return f"/{test_bucket_name}/{filename}"

tests/test_file.py

+39-45
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,7 @@
88
import os
99

1010
import pytest
11-
12-
files = {
13-
"LICENSE": (
14-
b" Apache License\n"
15-
b" Version 2.0, January 2004\n"
16-
b" http://www.apache.org/licenses/\n"
17-
),
18-
"number": (b"1234567890\n"),
19-
}
20-
21-
glob_files = {"file.dat": b"", "filexdat": b""}
11+
from conftest import LICENSE_PATH, NUMBERS
2212

2313

2414
def test_simple(ossfs, test_path):
@@ -62,35 +52,36 @@ def test_seek(ossfs, test_path):
6252
assert f.seek(i) == i
6353

6454

65-
def test_read_small(ossfs, test_bucket_name):
66-
fn = test_bucket_name + "/number"
67-
with ossfs.open(fn, "rb", block_size=3) as f:
55+
def test_read_small(ossfs, number_file):
56+
with ossfs.open(number_file, "rb", block_size=3) as f:
6857
out = []
6958
while True:
7059
data = f.read(2)
7160
if data == b"":
7261
break
7362
out.append(data)
74-
assert ossfs.cat(fn) == b"".join(out)
63+
assert ossfs.cat(number_file) == b"".join(out)
7564

7665

77-
def test_read_ossfs_block(ossfs, test_bucket_name):
78-
data = files["LICENSE"]
66+
def test_read_ossfs_block(ossfs, license_file, number_file):
67+
with open(LICENSE_PATH, "rb") as f_r:
68+
data = f_r.read()
7969
lines = io.BytesIO(data).readlines()
80-
path = test_bucket_name + "/LICENSE"
81-
assert ossfs.read_block(path, 0, 10, b"\n") == lines[0]
82-
assert ossfs.read_block(path, 40, 10, b"\n") == lines[1]
83-
assert ossfs.read_block(path, 0, 80, b"\n") == lines[0] + lines[1]
84-
assert ossfs.read_block(path, 0, 120, b"\n") == data
70+
assert ossfs.read_block(license_file, 0, 10, b"\n") == lines[0]
71+
assert ossfs.read_block(license_file, 40, 10, b"\n") == lines[1]
72+
assert ossfs.read_block(license_file, 0, 80, b"\n") == lines[0] + lines[1]
73+
assert ossfs.read_block(license_file, 0, 120, b"\n") == b"".join(
74+
[lines[0], lines[1], lines[2]]
75+
)
8576

86-
data = files["number"]
87-
lines = io.BytesIO(data).readlines()
88-
path = test_bucket_name + "/number"
89-
assert len(ossfs.read_block(path, 0, 5)) == 5
90-
assert len(ossfs.read_block(path, 4, 150)) == len(data) - 4
91-
assert ossfs.read_block(path, 20, 25) == b""
77+
lines = io.BytesIO(NUMBERS).readlines()
78+
assert len(ossfs.read_block(number_file, 0, 5)) == 5
79+
assert len(ossfs.read_block(number_file, 4, 150)) == len(NUMBERS) - 4
80+
assert ossfs.read_block(number_file, 20, 25) == b""
9281

93-
assert ossfs.read_block(path, 5, None) == ossfs.read_block(path, 5, 25)
82+
assert ossfs.read_block(number_file, 5, None) == ossfs.read_block(
83+
number_file, 5, 25
84+
)
9485

9586

9687
@pytest.mark.parametrize("size", [2 ** 10, 2 ** 20, 10 * 2 ** 20])
@@ -134,15 +125,18 @@ def test_write_blocks(ossfs, test_path):
134125
assert ossfs.info(file)["Size"] == 15 * 2 ** 20
135126

136127

137-
def test_readline(ossfs, test_bucket_name):
138-
all_items = files.items()
139-
for k, data in all_items:
140-
with ossfs.open("/".join([test_bucket_name, k]), "rb") as f:
141-
result = f.readline()
142-
expected = data.split(b"\n")[0] + (
143-
b"\n" if data.count(b"\n") else b""
144-
)
145-
assert result == expected
128+
def test_readline(ossfs, number_file, license_file):
129+
with ossfs.open("/".join([number_file]), "rb") as f_r:
130+
result = f_r.readline()
131+
expected = NUMBERS
132+
assert result == expected
133+
134+
with ossfs.open("/".join([license_file]), "rb") as f_r, open(
135+
LICENSE_PATH, "rb"
136+
) as f_l:
137+
result = f_r.readline()
138+
expected = f_l.readline()
139+
assert result == expected
146140

147141

148142
def test_readline_empty(ossfs, test_path):
@@ -174,10 +168,10 @@ def test_readline_blocksize(ossfs, test_path):
174168
assert result == expected
175169

176170

177-
def test_next(ossfs, test_bucket_name):
178-
expected = files["LICENSE"].split(b"\n")[0] + b"\n"
179-
with ossfs.open(test_bucket_name + "/LICENSE") as f:
180-
result = next(f)
171+
def test_next(ossfs, license_file):
172+
with open(LICENSE_PATH, "rb") as f_l, ossfs.open(license_file) as f_r:
173+
expected = f_l.readline()
174+
result = next(f_r)
181175
assert result == expected
182176

183177

@@ -219,7 +213,7 @@ def test_file_status(ossfs, test_path):
219213
@pytest.mark.parametrize("data_size", [0, 20, 10 * 2 ** 20])
220214
@pytest.mark.parametrize("append_size", [0, 20, 10 * 2 ** 20])
221215
def test_append(ossfs, test_path, data_size, append_size):
222-
file = test_path + "/test_append/file_{}_{}".format(data_size, append_size)
216+
file = test_path + f"/test_append/file_{data_size}_{append_size}"
223217
data = os.urandom(data_size)
224218
extra = os.urandom(append_size)
225219
with ossfs.open(file, "wb") as f:
@@ -230,8 +224,8 @@ def test_append(ossfs, test_path, data_size, append_size):
230224
assert ossfs.cat(file) == data + extra
231225

232226

233-
def test_bigger_than_block_read(ossfs, test_bucket_name):
234-
with ossfs.open(test_bucket_name + "/number", "rb", block_size=3) as f:
227+
def test_bigger_than_block_read(ossfs, number_file):
228+
with ossfs.open(number_file, "rb", block_size=3) as f:
235229
out = []
236230
while True:
237231
data = f.read(4)

tests/test_login.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ def test_env_endpoint(endpoint, test_bucket_name, monkeypatch):
7373
ossfs.ls(test_bucket_name)
7474

7575

76-
def test_anonymous_login():
77-
ossfs = OSSFileSystem(endpoint="http://oss-cn-hangzhou.aliyuncs.com")
78-
ossfs.cat("/dvc-test-anonymous/LICENSE", connect_timeout=600)
76+
def test_anonymous_login(file_in_anonymous, endpoint):
77+
ossfs = OSSFileSystem(endpoint=endpoint)
78+
ossfs.cat(f"{file_in_anonymous}")

tests/test_object.py

+18-21
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,13 @@ def test_bulk_delete(ossfs, test_path):
132132
)
133133

134134

135-
def test_ossfs_file_access(ossfs, test_bucket_name):
136-
fn = test_bucket_name + "/number"
135+
def test_ossfs_file_access(ossfs, number_file):
137136
data = b"1234567890\n"
138-
assert ossfs.cat(fn) == data
139-
assert ossfs.head(fn, 3) == data[:3]
140-
assert ossfs.tail(fn, 3) == data[-3:]
141-
assert ossfs.tail(fn, 10000) == data
142-
assert ossfs.info(fn)["Size"] == len(data)
137+
assert ossfs.cat(number_file) == data
138+
assert ossfs.head(number_file, 3) == data[:3]
139+
assert ossfs.tail(number_file, 3) == data[-3:]
140+
assert ossfs.tail(number_file, 10000) == data
141+
assert ossfs.info(number_file)["Size"] == len(data)
143142

144143

145144
def test_du(ossfs, test_path):
@@ -173,16 +172,16 @@ def test_ossfs_ls(ossfs, test_path):
173172
assert all(isinstance(item, dict) for item in L)
174173

175174

176-
def test_ossfs_big_ls(ossfs, test_bucket_name):
177-
path = test_bucket_name + "/test_ossfs_big_ls"
175+
def test_ossfs_big_ls(ossfs, test_path):
176+
path = test_path + "/test_ossfs_big_ls"
178177
if not ossfs.exists(path):
179178
for x in range(1200):
180-
ossfs.touch(path + "/%i.part" % x)
179+
ossfs.touch(path + f"/{x}.part")
181180
files = ossfs.find(path, connect_timeout=600)
182181
for x in range(1200):
183-
file = path + "/%i.part" % x
182+
file = path + f"/{x}.part"
184183
if file not in files:
185-
ossfs.touch(path + "/%i.part" % x)
184+
ossfs.touch(path + f"/{x}.part")
186185

187186
assert len(ossfs.find(path, connect_timeout=600)) == 1200
188187

@@ -210,15 +209,13 @@ def test_ossfs_glob(ossfs, test_path):
210209
assert fn2 not in ossfs.glob(path + "/nested/file.*")
211210

212211

213-
def test_copy(ossfs, test_bucket_name, test_path):
214-
number_file = test_bucket_name + "/number"
212+
def test_copy(ossfs, number_file, test_path):
215213
new_file = test_path + "/test_copy/file"
216214
ossfs.copy(number_file, new_file)
217215
assert ossfs.cat(number_file) == ossfs.cat(new_file)
218216

219217

220-
def test_move(ossfs, test_bucket_name, test_path):
221-
number_file = test_bucket_name + "/number"
218+
def test_move(ossfs, number_file, test_path):
222219
from_file = test_path + "/test_move/from"
223220
to_file = test_path + "/test_move/to"
224221
ossfs.copy(number_file, from_file)
@@ -378,7 +375,7 @@ def test_get_file_info_with_selector(ossfs, test_path):
378375
elif info["name"].rstrip("/").endswith(dir_a):
379376
assert info["type"] == "directory"
380377
else:
381-
raise ValueError("unexpected path {}".format(info["name"]))
378+
raise ValueError(f"unexpected path {info['name']}")
382379

383380

384381
def test_same_name_but_no_exact(ossfs, test_path):
@@ -419,8 +416,8 @@ def test_leading_forward_slash(ossfs, test_path):
419416
assert ossfs.exists("/" + path + "/some/file")
420417

421418

422-
def test_find_with_prefix(ossfs, test_bucket_name):
423-
path = test_bucket_name + "/test_find_with_prefix"
419+
def test_find_with_prefix(ossfs, test_path):
420+
path = test_path + "/test_find_with_prefix"
424421
if not ossfs.exists(path):
425422
for cursor in range(100):
426423
ossfs.touch(path + f"/prefixes/test_{cursor}")
@@ -448,10 +445,10 @@ def test_find_with_prefix(ossfs, test_bucket_name):
448445
READ_BLOCK_SIZE = 2 ** 14 # 16KB blocks
449446

450447

451-
def test_get_put_file(ossfs, tmpdir, test_bucket_name):
448+
def test_get_put_file(ossfs, tmpdir, test_path):
452449
src_file = str(tmpdir / "source")
453450
src2_file = str(tmpdir / "source_2")
454-
dest_file = test_bucket_name + "/get_put_file/dest"
451+
dest_file = test_path + "/get_put_file/dest"
455452

456453
data = b"test" * 2 ** 20
457454

0 commit comments

Comments
 (0)