Skip to content

Commit c0496dd

Browse files
Fix test issues on Windows (#601)
* Fix test issues on Windows --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a11f5f5 commit c0496dd

File tree

2 files changed

+42
-30
lines changed

2 files changed

+42
-30
lines changed

tests/test_minio_adapter.py

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,23 @@
3232

3333
from servicex.minio_adapter import MinioAdapter
3434
from servicex.models import ResultFile
35+
from pathlib import Path
3536

3637
DOWNLOAD_PATCH_COUNTER = 0
3738

3839

39-
def mock_downloader(**args):
40-
global DOWNLOAD_PATCH_COUNTER
41-
DOWNLOAD_PATCH_COUNTER += 1
42-
if DOWNLOAD_PATCH_COUNTER == 1:
43-
raise Exception("lol")
44-
elif DOWNLOAD_PATCH_COUNTER == 2:
45-
open("/tmp/foo/test.txt", "wb").write(b"\x01" * 5)
46-
elif DOWNLOAD_PATCH_COUNTER == 3:
47-
open("/tmp/foo/test.txt", "wb").write(b"\x01" * 10)
40+
def make_mock_downloader(target: Path):
41+
def mock_downloader(**args):
42+
global DOWNLOAD_PATCH_COUNTER
43+
DOWNLOAD_PATCH_COUNTER += 1
44+
if DOWNLOAD_PATCH_COUNTER == 1:
45+
raise Exception("lol")
46+
elif DOWNLOAD_PATCH_COUNTER == 2:
47+
open(target, "wb").write(b"\x01" * 5)
48+
elif DOWNLOAD_PATCH_COUNTER == 3:
49+
open(target, "wb").write(b"\x01" * 10)
50+
51+
return mock_downloader
4852

4953

5054
@fixture
@@ -65,7 +69,6 @@ async def populate_bucket(request, minio_adapter):
6569
Bucket=minio_adapter.bucket, Key=request.param, Body=b"\x01" * 10
6670
)
6771
yield
68-
await s3.delete_object(Bucket=minio_adapter.bucket, Key=request.param)
6972

7073

7174
@fixture
@@ -95,8 +98,8 @@ async def test_list_bucket(minio_adapter, populate_bucket):
9598

9699
@pytest.mark.parametrize("populate_bucket", ["test.txt"], indirect=True)
97100
@pytest.mark.asyncio
98-
async def test_download_file(minio_adapter, populate_bucket):
99-
result = await minio_adapter.download_file("test.txt", local_dir="/tmp/foo")
101+
async def test_download_file(minio_adapter, populate_bucket, tmp_path):
102+
result = await minio_adapter.download_file("test.txt", local_dir=tmp_path)
100103
assert str(result).endswith("test.txt")
101104
assert result.exists()
102105
assert result.read_bytes() == (b"\x01" * 10)
@@ -105,10 +108,12 @@ async def test_download_file(minio_adapter, populate_bucket):
105108

106109
@pytest.mark.parametrize("populate_bucket", ["test.txt"], indirect=True)
107110
@pytest.mark.asyncio
108-
async def test_download_file_with_expected_size(minio_adapter, populate_bucket):
111+
async def test_download_file_with_expected_size(
112+
minio_adapter, populate_bucket, tmp_path
113+
):
109114
info = await minio_adapter.list_bucket()
110115
result = await minio_adapter.download_file(
111-
"test.txt", local_dir="/tmp/foo", expected_size=info[0].size
116+
"test.txt", local_dir=tmp_path, expected_size=info[0].size
112117
)
113118
assert str(result).endswith("test.txt")
114119
assert result.exists()
@@ -118,8 +123,8 @@ async def test_download_file_with_expected_size(minio_adapter, populate_bucket):
118123

119124
@pytest.mark.parametrize("populate_bucket", ["t::est.txt"], indirect=True)
120125
@pytest.mark.asyncio
121-
async def test_download_bad_filename(minio_adapter, populate_bucket):
122-
result = await minio_adapter.download_file("t::est.txt", local_dir="/tmp/foo")
126+
async def test_download_bad_filename(minio_adapter, populate_bucket, tmp_path):
127+
result = await minio_adapter.download_file("t::est.txt", local_dir=tmp_path)
123128
assert str(result).endswith("t__est.txt")
124129
assert result.exists()
125130
assert result.read_bytes() == (b"\x01" * 10)
@@ -128,9 +133,11 @@ async def test_download_bad_filename(minio_adapter, populate_bucket):
128133

129134
@pytest.mark.parametrize("populate_bucket", ["test.txt"], indirect=True)
130135
@pytest.mark.asyncio
131-
async def test_download_short_filename_no_change(minio_adapter, populate_bucket):
136+
async def test_download_short_filename_no_change(
137+
minio_adapter, populate_bucket, tmp_path
138+
):
132139
result = await minio_adapter.download_file(
133-
"test.txt", local_dir="/tmp/foo", shorten_filename=True
140+
"test.txt", local_dir=tmp_path, shorten_filename=True
134141
)
135142
assert str(result).endswith("test.txt")
136143
assert result.exists()
@@ -146,10 +153,10 @@ async def test_download_short_filename_no_change(minio_adapter, populate_bucket)
146153
indirect=True,
147154
)
148155
@pytest.mark.asyncio
149-
async def test_download_short_filename_change(minio_adapter, populate_bucket):
156+
async def test_download_short_filename_change(minio_adapter, populate_bucket, tmp_path):
150157
result = await minio_adapter.download_file(
151158
"test12345678901234567890123456789012345678901234567898012345678901234567890.txt",
152-
local_dir="/tmp/foo",
159+
local_dir=tmp_path,
153160
shorten_filename=True,
154161
)
155162

@@ -164,19 +171,18 @@ async def test_download_short_filename_change(minio_adapter, populate_bucket):
164171
result.unlink() # it should exist, from above ...
165172

166173

167-
@pytest.mark.parametrize("populate_bucket", ["test2.txt"], indirect=True)
174+
@pytest.mark.parametrize("populate_bucket", ["test.txt"], indirect=True)
168175
@pytest.mark.asyncio
169-
async def test_download_repeat(minio_adapter, populate_bucket):
176+
async def test_download_repeat(minio_adapter, populate_bucket, tmp_path):
170177
import asyncio
171178

172-
print(await minio_adapter.list_bucket())
173-
result = await minio_adapter.download_file("test2.txt", local_dir="/tmp/foo")
174-
assert str(result).endswith("test2.txt")
179+
result = await minio_adapter.download_file("test.txt", local_dir=tmp_path)
180+
assert str(result).endswith("test.txt")
175181
assert result.exists()
176182
t0 = result.stat().st_mtime_ns
177183
await asyncio.sleep(4) # hopefully long enough for Windows/FAT32 ... ?
178184

179-
result2 = await minio_adapter.download_file("test2.txt", local_dir="/tmp/foo")
185+
result2 = await minio_adapter.download_file("test.txt", local_dir=tmp_path)
180186
assert result2.exists()
181187
assert result2 == result
182188
assert t0 == result2.stat().st_mtime_ns
@@ -192,11 +198,12 @@ async def test_get_signed_url(minio_adapter, moto_services, populate_bucket):
192198

193199
@pytest.mark.parametrize("populate_bucket", ["test.txt"], indirect=True)
194200
@pytest.mark.asyncio
195-
async def test_download_file_retry(minio_adapter, populate_bucket, mocker):
201+
async def test_download_file_retry(minio_adapter, populate_bucket, mocker, tmp_path):
196202
download_patch = mocker.patch(
197-
"aioboto3.s3.inject.download_file", side_effect=mock_downloader
203+
"aioboto3.s3.inject.download_file",
204+
side_effect=make_mock_downloader(tmp_path / "test.txt"),
198205
)
199-
result = await minio_adapter.download_file("test.txt", local_dir="/tmp/foo")
206+
result = await minio_adapter.download_file("test.txt", local_dir=tmp_path)
200207
assert str(result).endswith("test.txt")
201208
assert result.exists()
202209
assert download_patch.call_count == 3

tests/test_servicex_adapter.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,15 @@ async def test_get_transforms_auth_error(mock_get, servicex):
9494

9595

9696
@pytest.mark.asyncio
97+
@patch("servicex.servicex_adapter.RetryClient.post")
9798
@patch("servicex.servicex_adapter.jwt.decode")
9899
async def test_get_transforms_wlcg_bearer_token(
99-
decode, servicex, transform_status_response
100+
decode, post, servicex, transform_status_response
100101
):
102+
post.return_value.__aenter__.return_value.json.return_value = {
103+
"access_token": "luckycharms"
104+
}
105+
post.return_value.__aenter__.return_value.status = 401
101106
token_file = tempfile.NamedTemporaryFile(mode="w+t", delete=False)
102107
token_file.write(
103108
""""

0 commit comments

Comments
 (0)