forked from Backblaze/B2_Command_Line_Tool
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #301 from kris-konina-reef/master
Implement persistent bucket fixtures for integration tests
- Loading branch information
Showing
5 changed files
with
538 additions
and
345 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Improve internal testing infrastructure by updating integration tests to use persistent buckets. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
###################################################################### | ||
# | ||
# File: test/integration/persistent_bucket.py | ||
# | ||
# Copyright 2024 Backblaze Inc. All Rights Reserved. | ||
# | ||
# License https://www.backblaze.com/using_b2_code.html | ||
# | ||
###################################################################### | ||
import hashlib | ||
import os | ||
from dataclasses import dataclass | ||
from functools import cached_property | ||
from test.integration.helpers import BUCKET_NAME_LENGTH, Api | ||
|
||
import backoff | ||
from b2sdk.v2 import Bucket | ||
from b2sdk.v2.exception import DuplicateBucketName, NonExistentBucket | ||
|
||
PERSISTENT_BUCKET_NAME_PREFIX = "constst" | ||
|
||
|
||
@dataclass | ||
class PersistentBucketAggregate: | ||
bucket_name: str | ||
subfolder: str | ||
|
||
@cached_property | ||
def virtual_bucket_name(self): | ||
return f"{self.bucket_name}/{self.subfolder}" | ||
|
||
|
||
def get_persistent_bucket_name(b2_api: Api) -> str: | ||
bucket_base = os.environ.get("GITHUB_REPOSITORY_ID", b2_api.api.get_account_id()) | ||
bucket_hash = hashlib.sha256(bucket_base.encode()).hexdigest() | ||
return f"{PERSISTENT_BUCKET_NAME_PREFIX}-{bucket_hash}" [:BUCKET_NAME_LENGTH] | ||
|
||
|
||
@backoff.on_exception( | ||
backoff.expo, | ||
DuplicateBucketName, | ||
max_tries=3, | ||
jitter=backoff.full_jitter, | ||
) | ||
def get_or_create_persistent_bucket(b2_api: Api) -> Bucket: | ||
bucket_name = get_persistent_bucket_name(b2_api) | ||
try: | ||
bucket = b2_api.api.get_bucket_by_name(bucket_name) | ||
except NonExistentBucket: | ||
bucket = b2_api.api.create_bucket( | ||
bucket_name, | ||
bucket_type="allPublic", | ||
lifecycle_rules=[ | ||
{ | ||
"daysFromHidingToDeleting": 1, | ||
"daysFromUploadingToHiding": 1, | ||
"fileNamePrefix": "", | ||
} | ||
], | ||
) | ||
# add the new bucket name to the list of bucket names | ||
b2_api.bucket_name_log.append(bucket_name) | ||
return bucket |
Oops, something went wrong.