Skip to content

Commit 3e5f2d8

Browse files
committed
fix: legacy region get_xxx_host and add more cases
1 parent ba62a7d commit 3e5f2d8

File tree

3 files changed

+137
-20
lines changed

3 files changed

+137
-20
lines changed

qiniu/region.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def decorator(func):
1717
@functools.wraps(func)
1818
def wrapper(self, *args, **kwargs):
1919
if hasattr(self, key) and getattr(self, key):
20-
return self.rs_host
20+
return getattr(self, key)
2121
if is_customized_default('default_' + key):
2222
return get_default('default_' + key)
2323
return func(self, *args, **kwargs)
@@ -196,6 +196,19 @@ def get_bucket_hosts(self, ak, bucket, home_dir=None, force=False):
196196
return bucket_hosts
197197

198198
def get_bucket_hosts_to_cache(self, key, home_dir):
199+
"""
200+
.. deprecated::
201+
The cache has been replaced by CachedRegionsProvider
202+
203+
Parameters
204+
----------
205+
key: str
206+
home_dir: str
207+
208+
Returns
209+
-------
210+
dict
211+
"""
199212
ret = {}
200213
if len(self.host_cache) == 0:
201214
self.host_cache_from_file(home_dir)
@@ -209,11 +222,29 @@ def get_bucket_hosts_to_cache(self, key, home_dir):
209222
return ret
210223

211224
def set_bucket_hosts_to_cache(self, key, val, home_dir):
225+
"""
226+
.. deprecated::
227+
The cache has been replaced by CachedRegionsProvider
228+
229+
Parameters
230+
----------
231+
key: str
232+
val: dict
233+
home_dir: str
234+
"""
212235
self.host_cache[key] = val
213236
self.host_cache_to_file(home_dir)
214237
return
215238

216239
def host_cache_from_file(self, home_dir):
240+
"""
241+
.. deprecated::
242+
The cache has been replaced by CachedRegionsProvider
243+
244+
Parameters
245+
----------
246+
home_dir: str
247+
"""
217248
if home_dir is not None:
218249
self.home_dir = home_dir
219250
path = self.host_cache_file_path()
@@ -229,9 +260,26 @@ def host_cache_from_file(self, home_dir):
229260
return
230261

231262
def host_cache_file_path(self):
263+
"""
264+
.. deprecated::
265+
The cache has been replaced by CachedRegionsProvider
266+
267+
Returns
268+
-------
269+
str
270+
"""
232271
return os.path.join(self.home_dir, ".qiniu_pythonsdk_hostscache.json")
233272

234273
def host_cache_to_file(self, home_dir):
274+
"""
275+
.. deprecated::
276+
The cache has been replaced by CachedRegionsProvider
277+
278+
Parameters
279+
----------
280+
home_dir: str
281+
282+
"""
235283
path = self.host_cache_file_path()
236284
with open(path, 'w') as f:
237285
json.dump(self.host_cache, f)

tests/cases/test_services/test_storage/test_uploader.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from qiniu.http.endpoint import Endpoint
1616
from qiniu.http.region import ServiceName
1717
from qiniu.services.storage.uploader import _form_put
18+
from qiniu.services.storage.uploaders.abc import UploaderBase
1819

1920
KB = 1024
2021
MB = 1024 * KB
@@ -885,3 +886,24 @@ def test_upload_acc_fallback_src_by_acc_unavailable(
885886
)
886887

887888
assert ret['key'] == key, resp
889+
890+
def test_uploader_base_compatible(self, qn_auth, bucket_name):
891+
if is_py2:
892+
class MockUploader(UploaderBase):
893+
def upload(
894+
self,
895+
**kwargs
896+
):
897+
pass
898+
uploader = MockUploader(
899+
bucket_name=bucket_name,
900+
auth=qn_auth
901+
)
902+
else:
903+
uploader = UploaderBase(
904+
bucket_name=bucket_name,
905+
auth=qn_auth
906+
)
907+
908+
up_hosts = uploader._get_up_hosts()
909+
assert len(up_hosts) > 0

tests/cases/test_zone/test_lagacy_region.py

Lines changed: 66 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,78 @@
1+
import pytest
2+
13
from qiniu.http.region import Region, ServiceName
24
from qiniu.region import LegacyRegion
3-
from qiniu.compat import json
5+
from qiniu.compat import json, is_py2
6+
7+
8+
@pytest.fixture
9+
def mocked_hosts():
10+
mocked_hosts = {
11+
ServiceName.UP: ['https://up.python-example.qiniu.com', 'https://up-2.python-example.qiniu.com'],
12+
ServiceName.IO: ['https://io.python-example.qiniu.com'],
13+
ServiceName.RS: ['https://rs.python-example.qiniu.com'],
14+
ServiceName.RSF: ['https://rsf.python-example.qiniu.com'],
15+
ServiceName.API: ['https://api.python-example.qiniu.com']
16+
}
17+
yield mocked_hosts
18+
19+
20+
@pytest.fixture
21+
def mock_legacy_region(mocked_hosts):
22+
region = LegacyRegion(
23+
up_host=mocked_hosts[ServiceName.UP][0],
24+
up_host_backup=mocked_hosts[ServiceName.UP][1],
25+
io_host=mocked_hosts[ServiceName.IO][0],
26+
rs_host=mocked_hosts[ServiceName.RS][0],
27+
rsf_host=mocked_hosts[ServiceName.RSF][0],
28+
api_host=mocked_hosts[ServiceName.API][0]
29+
)
30+
yield region
431

532

633
class TestLegacyRegion:
7-
def test_compatible_with_http_region(self):
8-
mocked_hosts = {
9-
ServiceName.UP: ['https://up.python-example.qiniu.com', 'https://up-2.python-example.qiniu.com'],
10-
ServiceName.IO: ['https://io.python-example.qiniu.com'],
11-
ServiceName.RS: ['https://rs.python-example.qiniu.com'],
12-
ServiceName.RSF: ['https://rsf.python-example.qiniu.com'],
13-
ServiceName.API: ['https://api.python-example.qiniu.com']
14-
}
34+
def test_get_hosts_from_self(self, mocked_hosts, mock_legacy_region, qn_auth, bucket_name):
35+
cases = [
36+
# up will always query from the old version,
37+
# which version implements the `get_up_host_*` method
38+
(
39+
mock_legacy_region.get_io_host(qn_auth.get_access_key(), None),
40+
mocked_hosts[ServiceName.IO][0]
41+
),
42+
(
43+
mock_legacy_region.get_rs_host(qn_auth.get_access_key(), None),
44+
mocked_hosts[ServiceName.RS][0]
45+
),
46+
(
47+
mock_legacy_region.get_rsf_host(qn_auth.get_access_key(), None),
48+
mocked_hosts[ServiceName.RSF][0]
49+
),
50+
(
51+
mock_legacy_region.get_api_host(qn_auth.get_access_key(), None),
52+
mocked_hosts[ServiceName.API][0]
53+
)
54+
]
55+
for actual, expect in cases:
56+
assert actual == expect
57+
58+
def test_get_hosts_from_query(self, qn_auth, bucket_name):
59+
up_token = qn_auth.upload_token(bucket_name)
60+
region = LegacyRegion()
61+
up_host = region.get_up_host_by_token(up_token, None)
62+
up_host_backup = region.get_up_host_backup_by_token(up_token, None)
63+
if is_py2:
64+
up_host = up_host.encode()
65+
up_host_backup = up_host_backup.encode()
66+
assert type(up_host) is str and len(up_host) > 0
67+
assert type(up_host_backup) is str and len(up_host_backup) > 0
68+
assert up_host != up_host_backup
1569

16-
region = LegacyRegion(
17-
up_host=mocked_hosts[ServiceName.UP][0],
18-
up_host_backup=mocked_hosts[ServiceName.UP][1],
19-
io_host=mocked_hosts[ServiceName.IO][0],
20-
rs_host=mocked_hosts[ServiceName.RS][0],
21-
rsf_host=mocked_hosts[ServiceName.RSF][0],
22-
api_host=mocked_hosts[ServiceName.API][0]
23-
)
24-
assert isinstance(region, Region)
70+
def test_compatible_with_http_region(self, mocked_hosts, mock_legacy_region):
71+
assert isinstance(mock_legacy_region, Region)
2572
assert mocked_hosts == {
2673
k: [
2774
e.get_value()
28-
for e in region.services[k]
75+
for e in mock_legacy_region.services[k]
2976
]
3077
for k in mocked_hosts
3178
}

0 commit comments

Comments
 (0)