Skip to content

Commit a18317b

Browse files
Merge pull request #545 from kacper-wolkiewicz-reef/B2-171-tests-commons
Integration tests tools public api
2 parents fd4274f + 762321a commit a18317b

File tree

25 files changed

+727
-440
lines changed

25 files changed

+727
-440
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ venv
1313
.venv
1414
.vscode
1515
.pdm-build/
16-
.pdm-python
16+
.pdm-python
17+
.env
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
######################################################################
2+
#
3+
# File: b2sdk/_internal/testing/__init__.py
4+
#
5+
# Copyright 2021 Backblaze Inc. All Rights Reserved.
6+
#
7+
# License https://www.backblaze.com/using_b2_code.html
8+
#
9+
######################################################################
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
######################################################################
2+
#
3+
# File: b2sdk/_internal/testing/fixtures/__init__.py
4+
#
5+
# Copyright 2021 Backblaze Inc. All Rights Reserved.
6+
#
7+
# License https://www.backblaze.com/using_b2_code.html
8+
#
9+
######################################################################
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
######################################################################
2+
#
3+
# File: b2sdk/_internal/testing/fixtures/api.py
4+
#
5+
# Copyright 2021 Backblaze Inc. All Rights Reserved.
6+
#
7+
# License https://www.backblaze.com/using_b2_code.html
8+
#
9+
######################################################################
10+
from __future__ import annotations
11+
12+
import http
13+
import http.client
14+
import os
15+
16+
import pytest
17+
18+
from b2sdk._internal.testing.helpers.api import authorize, get_b2_auth_data, get_realm
19+
20+
21+
@pytest.fixture(scope='session')
22+
def realm():
23+
yield get_realm()
24+
25+
26+
@pytest.fixture(autouse=True, scope='session')
27+
def set_http_debug():
28+
if os.environ.get('B2_DEBUG_HTTP'):
29+
http.client.HTTPConnection.debuglevel = 1
30+
31+
32+
@pytest.fixture(scope='session')
33+
def b2_auth_data():
34+
try:
35+
return get_b2_auth_data()
36+
except ValueError as ex:
37+
pytest.fail(ex.args[0])
38+
39+
40+
@pytest.fixture(scope='session')
41+
def _b2_api(b2_auth_data, realm):
42+
b2_api, _ = authorize(b2_auth_data, realm)
43+
return b2_api
44+
45+
46+
@pytest.fixture(scope='session')
47+
def b2_api(_b2_api, bucket_manager):
48+
return _b2_api
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
######################################################################
2+
#
3+
# File: b2sdk/_internal/testing/fixtures/buckets.py
4+
#
5+
# Copyright 2021 Backblaze Inc. All Rights Reserved.
6+
#
7+
# License https://www.backblaze.com/using_b2_code.html
8+
#
9+
######################################################################
10+
from __future__ import annotations
11+
12+
import secrets
13+
14+
import pytest
15+
16+
from b2sdk._internal.testing.helpers.bucket_manager import BucketManager
17+
from b2sdk._internal.testing.helpers.buckets import (
18+
GENERAL_BUCKET_NAME_PREFIX,
19+
get_bucket_name_prefix,
20+
)
21+
22+
23+
def pytest_addoption(parser):
24+
"""Add a flag for not cleaning up old buckets"""
25+
parser.addoption(
26+
'--dont-cleanup-old-buckets',
27+
action='store_true',
28+
default=False,
29+
)
30+
31+
32+
@pytest.fixture(scope='session')
33+
def dont_cleanup_old_buckets(request):
34+
return request.config.getoption('--dont-cleanup-old-buckets')
35+
36+
37+
@pytest.fixture(scope='session')
38+
def bucket_name_prefix():
39+
return get_bucket_name_prefix(8)
40+
41+
42+
@pytest.fixture(scope='session')
43+
def general_bucket_name_prefix():
44+
return GENERAL_BUCKET_NAME_PREFIX
45+
46+
47+
@pytest.fixture(scope='session')
48+
def bucket_manager(
49+
bucket_name_prefix, general_bucket_name_prefix, dont_cleanup_old_buckets, _b2_api
50+
):
51+
manager = BucketManager(
52+
dont_cleanup_old_buckets,
53+
_b2_api,
54+
current_run_prefix=bucket_name_prefix,
55+
general_prefix=general_bucket_name_prefix,
56+
)
57+
yield manager
58+
manager.clean_buckets()
59+
60+
61+
@pytest.fixture
62+
def bucket(bucket_name_prefix, bucket_manager):
63+
bucket = bucket_manager.create_bucket()
64+
yield bucket
65+
bucket_manager.clean_bucket(bucket)
66+
67+
68+
@pytest.fixture
69+
def b2_subfolder(bucket, request):
70+
subfolder_name = f'{request.node.name}_{secrets.token_urlsafe(4)}'
71+
return f'b2://{bucket.name}/{subfolder_name}'
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
######################################################################
2+
#
3+
# File: b2sdk/_internal/testing/helpers/__init__.py
4+
#
5+
# Copyright 2021 Backblaze Inc. All Rights Reserved.
6+
#
7+
# License https://www.backblaze.com/using_b2_code.html
8+
#
9+
######################################################################
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
######################################################################
2+
#
3+
# File: b2sdk/_internal/testing/helpers/api.py
4+
#
5+
# Copyright 2022 Backblaze Inc. All Rights Reserved.
6+
#
7+
# License https://www.backblaze.com/using_b2_code.html
8+
#
9+
######################################################################
10+
from __future__ import annotations
11+
12+
import os
13+
14+
from b2sdk._internal.account_info.in_memory import InMemoryAccountInfo
15+
from b2sdk._internal.api import B2Api
16+
from b2sdk._internal.api_config import DEFAULT_HTTP_API_CONFIG
17+
18+
19+
def get_realm() -> str:
20+
return os.environ.get('B2_TEST_ENVIRONMENT', 'production')
21+
22+
23+
def get_b2_auth_data():
24+
application_key_id = os.environ.get('B2_TEST_APPLICATION_KEY_ID')
25+
if application_key_id is None:
26+
raise ValueError('B2_TEST_APPLICATION_KEY_ID is not set.')
27+
28+
application_key = os.environ.get('B2_TEST_APPLICATION_KEY')
29+
if application_key is None:
30+
raise ValueError('B2_TEST_APPLICATION_KEY is not set.')
31+
return application_key_id, application_key
32+
33+
34+
def authorize(b2_auth_data, realm, api_config=DEFAULT_HTTP_API_CONFIG):
35+
info = InMemoryAccountInfo()
36+
b2_api = B2Api(info, api_config=api_config)
37+
b2_api.authorize_account(*b2_auth_data, realm=realm)
38+
return b2_api, info
Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
######################################################################
22
#
3-
# File: test/integration/base.py
3+
# File: b2sdk/_internal/testing/helpers/base.py
44
#
55
# Copyright 2022 Backblaze Inc. All Rights Reserved.
66
#
@@ -11,27 +11,23 @@
1111

1212
import pytest
1313

14-
from b2sdk.v2 import B2Api, current_time_millis
14+
from b2sdk._internal.testing.helpers.bucket_manager import BucketManager
15+
from b2sdk.v2 import B2Api
1516
from b2sdk.v2.exception import DuplicateBucketName
16-
from test.integration.bucket_cleaner import BucketCleaner
17-
from test.integration.helpers import (
18-
BUCKET_CREATED_AT_MILLIS,
19-
random_bucket_name,
20-
)
2117

2218

2319
@pytest.mark.usefixtures('cls_setup')
2420
class IntegrationTestBase:
2521
b2_api: B2Api
2622
this_run_bucket_name_prefix: str
27-
bucket_cleaner: BucketCleaner
23+
bucket_manager: BucketManager
2824

2925
@pytest.fixture(autouse=True, scope='class')
30-
def cls_setup(self, request, b2_api, b2_auth_data, bucket_name_prefix, bucket_cleaner):
26+
def cls_setup(self, request, b2_api, b2_auth_data, bucket_name_prefix, bucket_manager):
3127
cls = request.cls
3228
cls.b2_auth_data = b2_auth_data
3329
cls.this_run_bucket_name_prefix = bucket_name_prefix
34-
cls.bucket_cleaner = bucket_cleaner
30+
cls.bucket_manager = bucket_manager
3531
cls.b2_api = b2_api
3632
cls.info = b2_api.account_info
3733

@@ -40,10 +36,7 @@ def setup_method(self):
4036
self.buckets_created = []
4137
yield
4238
for bucket in self.buckets_created:
43-
self.bucket_cleaner.cleanup_bucket(bucket)
44-
45-
def generate_bucket_name(self):
46-
return random_bucket_name(self.this_run_bucket_name_prefix)
39+
self.bucket_manager.clean_bucket(bucket)
4740

4841
def write_zeros(self, file, number):
4942
line = b'0' * 1000 + b'\n'
@@ -54,13 +47,9 @@ def write_zeros(self, file, number):
5447
written += line_len
5548

5649
def create_bucket(self):
57-
bucket_name = self.generate_bucket_name()
50+
bucket_name = self.bucket_manager.new_bucket_name()
5851
try:
59-
bucket = self.b2_api.create_bucket(
60-
bucket_name,
61-
'allPublic',
62-
bucket_info={BUCKET_CREATED_AT_MILLIS: str(current_time_millis())},
63-
)
52+
bucket = self.bucket_manager.create_bucket(name=bucket_name)
6453
except DuplicateBucketName:
6554
self._duplicated_bucket_name_debug_info(bucket_name)
6655
raise

0 commit comments

Comments
 (0)