|
7 | 7 | # License https://www.backblaze.com/using_b2_code.html |
8 | 8 | # |
9 | 9 | ###################################################################### |
| 10 | + |
| 11 | +import contextlib |
10 | 12 | import sys |
11 | 13 |
|
12 | 14 | from os import environ, path |
13 | | -from pathlib import Path |
14 | | -from tempfile import TemporaryDirectory, gettempdir |
| 15 | +from tempfile import TemporaryDirectory |
15 | 16 |
|
16 | 17 | import pytest |
17 | 18 |
|
18 | 19 | from b2sdk.v2 import B2_ACCOUNT_INFO_ENV_VAR, XDG_CONFIG_HOME_ENV_VAR |
19 | | -from filelock import FileLock |
| 20 | +from b2sdk.exception import BucketIdNotFound |
20 | 21 |
|
21 | 22 | from .helpers import Api, CommandLine, bucket_name_part |
22 | 23 |
|
| 24 | +GENERAL_BUCKET_NAME_PREFIX = 'clitst' |
| 25 | + |
23 | 26 |
|
24 | 27 | @pytest.hookimpl |
25 | 28 | def pytest_addoption(parser): |
@@ -50,17 +53,31 @@ def realm() -> str: |
50 | 53 |
|
51 | 54 | @pytest.fixture(scope='function') |
52 | 55 | def bucket_name(b2_api) -> str: |
53 | | - yield b2_api.create_bucket().name |
| 56 | + bucket = b2_api.create_bucket() |
| 57 | + yield bucket.name |
| 58 | + with contextlib.suppress(BucketIdNotFound): |
| 59 | + b2_api.clean_bucket(bucket) |
| 60 | + |
| 61 | + |
| 62 | +@pytest.fixture(scope='function') # , autouse=True) |
| 63 | +def debug_print_buckets(b2_api): |
| 64 | + print('-' * 30) |
| 65 | + print('Buckets before test ' + environ['PYTEST_CURRENT_TEST']) |
| 66 | + num_buckets = b2_api.count_and_print_buckets() |
| 67 | + print('-' * 30) |
| 68 | + try: |
| 69 | + yield |
| 70 | + finally: |
| 71 | + print('-' * 30) |
| 72 | + print('Buckets after test ' + environ['PYTEST_CURRENT_TEST']) |
| 73 | + delta = b2_api.count_and_print_buckets() - num_buckets |
| 74 | + print(f'DELTA: {delta}') |
| 75 | + print('-' * 30) |
54 | 76 |
|
55 | 77 |
|
56 | 78 | @pytest.fixture(scope='session') |
57 | | -def general_bucket_name_prefix() -> str: |
58 | | - yield 'clitst' |
59 | | - |
60 | | - |
61 | | -@pytest.fixture(scope='session') |
62 | | -def this_run_bucket_name_prefix(general_bucket_name_prefix) -> str: |
63 | | - yield general_bucket_name_prefix + bucket_name_part(8) |
| 79 | +def this_run_bucket_name_prefix() -> str: |
| 80 | + yield GENERAL_BUCKET_NAME_PREFIX + bucket_name_part(8) |
64 | 81 |
|
65 | 82 |
|
66 | 83 | @pytest.fixture(scope='module') |
@@ -91,45 +108,13 @@ def auto_change_account_info_dir(monkey_patch) -> dir: |
91 | 108 |
|
92 | 109 |
|
93 | 110 | @pytest.fixture(scope='module') |
94 | | -def b2_api( |
95 | | - application_key_id, application_key, realm, general_bucket_name_prefix, |
96 | | - this_run_bucket_name_prefix |
97 | | -) -> Api: |
| 111 | +def b2_api(application_key_id, application_key, realm, this_run_bucket_name_prefix) -> Api: |
98 | 112 | yield Api( |
99 | | - application_key_id, application_key, realm, general_bucket_name_prefix, |
| 113 | + application_key_id, application_key, realm, GENERAL_BUCKET_NAME_PREFIX, |
100 | 114 | this_run_bucket_name_prefix |
101 | 115 | ) |
102 | 116 |
|
103 | 117 |
|
104 | | -@pytest.fixture(scope='module', autouse=True) |
105 | | -def auto_clean_buckets(b2_api, request, testrun_uid): |
106 | | - """ Automatically clean buckets before and after the whole module testing """ |
107 | | - |
108 | | - lock_file = Path(gettempdir()) / f'{testrun_uid}.lock' |
109 | | - |
110 | | - # lock file cannot be used to store info - use another file to track # of active workers |
111 | | - worker_count_file = Path(gettempdir()) / f'{testrun_uid}.active-workers-count.txt' |
112 | | - |
113 | | - with FileLock(str(lock_file)): |
114 | | - if not worker_count_file.is_file(): |
115 | | - b2_api.clean_buckets() |
116 | | - worker_count_file.write_text('1') |
117 | | - |
118 | | - else: |
119 | | - worker_count = int(worker_count_file.read_text()) |
120 | | - worker_count_file.write_text(str(worker_count + 1)) |
121 | | - |
122 | | - yield |
123 | | - |
124 | | - if request.config.getoption('--cleanup'): |
125 | | - with FileLock(str(lock_file)): |
126 | | - worker_count = int(worker_count_file.read_text()) |
127 | | - worker_count_file.write_text(str(worker_count - 1)) |
128 | | - if worker_count == 1: |
129 | | - b2_api.clean_buckets() |
130 | | - worker_count_file.unlink() |
131 | | - |
132 | | - |
133 | 118 | @pytest.fixture(scope='module') |
134 | 119 | def b2_tool( |
135 | 120 | request, application_key_id, application_key, realm, this_run_bucket_name_prefix |
|
0 commit comments