Skip to content

Commit

Permalink
deduplicate bucket cleanup code in integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mjurbanski-reef committed Nov 15, 2023
1 parent a00b8d1 commit 2b01cf5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 36 deletions.
20 changes: 5 additions & 15 deletions test/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
from tempfile import TemporaryDirectory

import pytest
from b2sdk.exception import BadRequest, BucketIdNotFound
from b2sdk.v2 import B2_ACCOUNT_INFO_ENV_VAR, XDG_CONFIG_HOME_ENV_VAR
from b2sdk.exception import BucketIdNotFound
from b2sdk.v2 import B2_ACCOUNT_INFO_ENV_VAR, XDG_CONFIG_HOME_ENV_VAR, Bucket

from .helpers import Api, CommandLine, bucket_name_part

Expand Down Expand Up @@ -64,19 +64,9 @@ def realm() -> str:
yield environ.get('B2_TEST_ENVIRONMENT', 'production')


@pytest.fixture(scope='function')
def bucket(b2_api) -> str:
try:
bucket = b2_api.create_bucket()
except BadRequest as e:
if e.code != 'too_many_buckets':
raise
num_buckets = b2_api.count_and_print_buckets()
print('current number of buckets:', num_buckets)
raise
yield bucket
with contextlib.suppress(BucketIdNotFound):
b2_api.clean_bucket(bucket)
@pytest.fixture
def bucket(bucket_factory) -> Bucket:
return bucket_factory()


@pytest.fixture
Expand Down
38 changes: 17 additions & 21 deletions test/integration/test_b2_command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def test_bucket(b2_tool, bucket_name):
]


def test_key_restrictions(b2_api, b2_tool, bucket_name, sample_file, bucket_factory):
def test_key_restrictions(b2_tool, bucket_name, sample_file, bucket_factory):
second_bucket_name = bucket_factory().name
# A single file for rm to fail on.
b2_tool.should_succeed(['upload-file', '--noProgress', bucket_name, sample_file, 'test'])
Expand Down Expand Up @@ -344,7 +344,6 @@ def test_key_restrictions(b2_api, b2_tool, bucket_name, sample_file, bucket_fact
b2_tool.application_key
]
)
b2_api.clean_bucket(second_bucket_name)
b2_tool.should_succeed(['delete-key', key_one_id])
b2_tool.should_succeed(['delete-key', key_two_id])

Expand Down Expand Up @@ -834,13 +833,15 @@ def sync_down_helper(b2_tool, bucket_name, folder_in_bucket, sample_file, encryp
)


def test_sync_copy(b2_api, b2_tool, bucket_name, sample_file):
prepare_and_run_sync_copy_tests(b2_api, b2_tool, bucket_name, 'sync', sample_file=sample_file)
def test_sync_copy(bucket_factory, b2_tool, bucket_name, sample_file):
prepare_and_run_sync_copy_tests(
bucket_factory, b2_tool, bucket_name, 'sync', sample_file=sample_file
)


def test_sync_copy_no_prefix_default_encryption(b2_api, b2_tool, bucket_name, sample_file):
def test_sync_copy_no_prefix_default_encryption(bucket_factory, b2_tool, bucket_name, sample_file):
prepare_and_run_sync_copy_tests(
b2_api,
bucket_factory,
b2_tool,
bucket_name,
'',
Expand All @@ -850,9 +851,9 @@ def test_sync_copy_no_prefix_default_encryption(b2_api, b2_tool, bucket_name, sa
)


def test_sync_copy_no_prefix_no_encryption(b2_api, b2_tool, bucket_name, sample_file):
def test_sync_copy_no_prefix_no_encryption(bucket_factory, b2_tool, bucket_name, sample_file):
prepare_and_run_sync_copy_tests(
b2_api,
bucket_factory,
b2_tool,
bucket_name,
'',
Expand All @@ -862,9 +863,9 @@ def test_sync_copy_no_prefix_no_encryption(b2_api, b2_tool, bucket_name, sample_
)


def test_sync_copy_no_prefix_sse_b2(b2_api, b2_tool, bucket_name, sample_file):
def test_sync_copy_no_prefix_sse_b2(bucket_factory, b2_tool, bucket_name, sample_file):
prepare_and_run_sync_copy_tests(
b2_api,
bucket_factory,
b2_tool,
bucket_name,
'',
Expand All @@ -874,9 +875,9 @@ def test_sync_copy_no_prefix_sse_b2(b2_api, b2_tool, bucket_name, sample_file):
)


def test_sync_copy_no_prefix_sse_c(b2_api, b2_tool, bucket_name, sample_file):
def test_sync_copy_no_prefix_sse_c(bucket_factory, b2_tool, bucket_name, sample_file):
prepare_and_run_sync_copy_tests(
b2_api,
bucket_factory,
b2_tool,
bucket_name,
'',
Expand Down Expand Up @@ -920,7 +921,7 @@ def test_sync_copy_sse_c_single_bucket(b2_tool, bucket_name, sample_file):


def prepare_and_run_sync_copy_tests(
b2_api,
bucket_factory,
b2_tool,
bucket_name,
folder_in_bucket,
Expand All @@ -936,10 +937,7 @@ def prepare_and_run_sync_copy_tests(
else:
b2_file_prefix = ''

other_bucket_name = b2_tool.generate_bucket_name()
success, _ = b2_tool.run_command(
['create-bucket', other_bucket_name, 'allPublic', *get_bucketinfo()]
)
other_bucket_name = bucket_factory().name

other_b2_sync_point = 'b2:%s' % other_bucket_name
if folder_in_bucket:
Expand Down Expand Up @@ -975,8 +973,6 @@ def prepare_and_run_sync_copy_tests(
file_version_summary_with_encryption(file_versions),
)

b2_api.clean_bucket(other_bucket_name)


def run_sync_copy_with_basic_checks(
b2_tool,
Expand Down Expand Up @@ -2102,7 +2098,7 @@ def test_profile_switch(b2_tool):
os.environ[B2_ACCOUNT_INFO_ENV_VAR] = B2_ACCOUNT_INFO


def test_replication_basic(b2_api, b2_tool, bucket_name, schedule_bucket_cleanup):
def test_replication_basic(b2_tool, bucket_name, schedule_bucket_cleanup):
key_one_name = 'clt-testKey-01' + random_hex(6)
created_key_stdout = b2_tool.should_succeed(
[
Expand Down Expand Up @@ -2544,7 +2540,7 @@ def test_replication_monitoring(b2_tool, bucket_name, sample_file, schedule_buck
]


def test_enable_file_lock_first_retention_second(b2_tool, b2_api, bucket_name):
def test_enable_file_lock_first_retention_second(b2_tool, bucket_name):
# enable file lock only
b2_tool.should_succeed(['update-bucket', bucket_name, '--fileLockEnabled'])

Expand Down

0 comments on commit 2b01cf5

Please sign in to comment.