Skip to content

Commit 8da1e64

Browse files
Merge pull request #547 from reef-technologies/new-seed-gen
Improve seed generation for containers names in tests
2 parents 1fff8c2 + 924e11f commit 8da1e64

File tree

6 files changed

+58
-14
lines changed

6 files changed

+58
-14
lines changed

b2sdk/_internal/testing/fixtures/buckets.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ def dont_cleanup_old_buckets(request):
3535

3636

3737
@pytest.fixture(scope='session')
38-
def bucket_name_prefix():
39-
return get_bucket_name_prefix(8)
38+
def general_bucket_name_prefix():
39+
return GENERAL_BUCKET_NAME_PREFIX
4040

4141

4242
@pytest.fixture(scope='session')
43-
def general_bucket_name_prefix():
44-
return GENERAL_BUCKET_NAME_PREFIX
43+
def bucket_name_prefix(general_bucket_name_prefix):
44+
return get_bucket_name_prefix(8, general_bucket_name_prefix)
4545

4646

4747
@pytest.fixture(scope='session')

b2sdk/_internal/testing/helpers/bucket_manager.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from __future__ import annotations
1111

1212
import logging
13-
import platform
1413
from collections.abc import Iterable
1514
from datetime import datetime, timedelta
1615
from itertools import chain
@@ -26,11 +25,11 @@
2625
BUCKET_CREATED_AT_MILLIS,
2726
BUCKET_NAME_LENGTH,
2827
GENERAL_BUCKET_NAME_PREFIX,
29-
random_token,
28+
NODE_DESCRIPTION,
29+
bucket_name_part,
3030
)
3131
from b2sdk._internal.utils import current_time_millis
3232

33-
NODE_DESCRIPTION = f'{platform.node()}: {platform.platform()}'
3433
ONE_HOUR_MILLIS = 60 * 60 * 1000
3534
BUCKET_CLEANUP_PERIOD_MILLIS = timedelta(hours=3).total_seconds() * 1000
3635

@@ -52,7 +51,7 @@ def __init__(
5251
self.bucket_name_log: list[str] = []
5352

5453
def new_bucket_name(self) -> str:
55-
bucket_name = self.current_run_prefix + random_token(
54+
bucket_name = self.current_run_prefix + bucket_name_part(
5655
BUCKET_NAME_LENGTH - len(self.current_run_prefix)
5756
)
5857
self.bucket_name_log.append(bucket_name)

b2sdk/_internal/testing/helpers/buckets.py

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,62 @@
99
######################################################################
1010
from __future__ import annotations
1111

12+
import logging
13+
import os
14+
import platform
15+
import random
1216
import secrets
17+
import time
18+
from hashlib import sha256
1319

1420
from b2sdk._internal.http_constants import BUCKET_NAME_CHARS_UNIQ, BUCKET_NAME_LENGTH_RANGE
1521

22+
logger = logging.getLogger(__name__)
23+
1624
GENERAL_BUCKET_NAME_PREFIX = 'sdktst'
1725
BUCKET_NAME_LENGTH = BUCKET_NAME_LENGTH_RANGE[1]
1826
BUCKET_CREATED_AT_MILLIS = 'created_at_millis'
27+
NODE_DESCRIPTION = f'{platform.node()}: {platform.platform()} {platform.python_version()}'
28+
29+
30+
def get_seed() -> str:
31+
"""
32+
Get seed for random number generator.
33+
34+
The `WORKFLOW_ID` variable has to be set in the CI to uniquely identify
35+
the current workflow (including the attempt)
36+
"""
37+
seed = ''.join(
38+
(
39+
os.getenv('WORKFLOW_ID', secrets.token_hex(8)),
40+
NODE_DESCRIPTION,
41+
str(time.time_ns()),
42+
os.getenv('PYTEST_XDIST_WORKER', 'gw0'),
43+
)
44+
)
45+
return sha256(seed.encode()).hexdigest()[:16]
1946

20-
RNG = secrets.SystemRandom()
47+
48+
RNG_SEED = get_seed()
49+
RNG = random.Random(RNG_SEED)
50+
RNG_COUNTER = 0
2151

2252

2353
def random_token(length: int, chars: str = BUCKET_NAME_CHARS_UNIQ) -> str:
2454
return ''.join(RNG.choice(chars) for _ in range(length))
2555

2656

27-
def get_bucket_name_prefix(rnd_len: int = 8) -> str:
28-
return GENERAL_BUCKET_NAME_PREFIX + random_token(rnd_len)
57+
def bucket_name_part(length: int) -> str:
58+
assert length >= 1
59+
global RNG_COUNTER
60+
RNG_COUNTER += 1
61+
name_part = random_token(length, BUCKET_NAME_CHARS_UNIQ)
62+
logger.info('RNG_SEED: %s', RNG_SEED)
63+
logger.info('RNG_COUNTER: %i, length: %i', RNG_COUNTER, length)
64+
logger.info('name_part: %s', name_part)
65+
logger.info('WORKFLOW_ID: %s', os.getenv('WORKFLOW_ID'))
66+
return name_part
67+
68+
69+
def get_bucket_name_prefix(rnd_len: int = 8, prefix: str = GENERAL_BUCKET_NAME_PREFIX) -> str:
70+
return prefix + bucket_name_part(rnd_len)

b2sdk/v3/testing/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
GENERAL_BUCKET_NAME_PREFIX,
1818
BUCKET_NAME_LENGTH,
1919
BUCKET_CREATED_AT_MILLIS,
20+
NODE_DESCRIPTION,
2021
RNG,
21-
random_token,
22+
RNG_COUNTER,
23+
RNG_SEED,
2224
get_bucket_name_prefix,
25+
random_token,
2326
)
2427
from b2sdk._internal.testing.helpers.bucket_manager import (
25-
NODE_DESCRIPTION,
2628
ONE_HOUR_MILLIS,
2729
BUCKET_CLEANUP_PERIOD_MILLIS,
2830
BucketManager,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve seed generation for containers' names in tests.

test/integration/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
######################################################################
1010

1111

12-
pytest_plugins = ["b2sdk.v3.testing"]
12+
pytest_plugins = ['b2sdk.v3.testing']

0 commit comments

Comments
 (0)