Skip to content

Commit

Permalink
Merge pull request #726 from reef-technologies/test-buket-name-entropy
Browse files Browse the repository at this point in the history
Bucket name entropy in tests increased and added possibility to install SDK from local folder instead of pypi in tests
  • Loading branch information
ppolewicz committed Jun 7, 2021
2 parents 186265e + 8842d04 commit 9d9780b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
* Add possibility to change realm during integration tests
* Add possibility to install SDK from local folder instead of pypi when running tests

### Fixed
* Fix integration tests on non-production environments
Expand All @@ -25,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Add zoneinfo to the Windows bundle
* Fixed unit tests failing on new attributes of FileVersionInfo
* Removing old buckets in integration tests
* Bucket name entropy in tests increased

## [2.4.0] - 2021-04-22

Expand Down
10 changes: 9 additions & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import nox

CI = os.environ.get('CI') is not None
INSTALL_SDK_FROM = os.environ.get('INSTALL_SDK_FROM')
NOX_PYTHONS = os.environ.get('NOX_PYTHONS')

PYTHON_VERSIONS = ['3.5', '3.6', '3.7', '3.8', '3.9'
Expand Down Expand Up @@ -46,14 +47,21 @@
def install_myself(session, extras=None):
"""Install from the source."""
# In CI, install B2 SDK from the master branch
if CI:

if CI and not INSTALL_SDK_FROM:
session.install('git+git://github.com/Backblaze/b2-sdk-python#egg=b2sdk')

arg = '.'
if extras:
arg += '[%s]' % ','.join(extras)

session.install('-e', arg)
if INSTALL_SDK_FROM:
cwd = os.getcwd()
os.chdir(INSTALL_SDK_FROM)
session.run('pip', 'uninstall', 'b2sdk', '-y')
session.run('python', 'setup.py', 'develop')
os.chdir(cwd)


@nox.session(name='format', python=PYTHON_DEFAULT_VERSION)
Expand Down
42 changes: 28 additions & 14 deletions test/integration/test_b2_command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
######################################################################

import argparse
import atexit
import base64
import hashlib
import json
Expand All @@ -21,6 +20,7 @@
import random
import re
import shutil
import string
import subprocess
import sys
import tempfile
Expand Down Expand Up @@ -54,6 +54,13 @@
ONE_DAY_MILLIS = ONE_HOUR_MILLIS * 24
BUCKET_CREATED_AT_MILLIS = 'created_at_millis'

BUCKET_NAME_CHARS = string.ascii_letters + string.digits + '-'
BUCKET_NAME_LENGTH = 50


def bucket_name_part(length):
return ''.join(random.choice(BUCKET_NAME_CHARS) for _ in range(length))


def parse_args(tests):
parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -238,7 +245,9 @@ def __init__(
self.api.authorize_account(self.realm, self.account_id, self.application_key)

def create_bucket(self):
bucket_name = self.this_run_bucket_name_prefix + '-' + random_hex(24)
bucket_name = self.this_run_bucket_name_prefix + bucket_name_part(
BUCKET_NAME_LENGTH - len(self.this_run_bucket_name_prefix)
)
print('Creating bucket:', bucket_name)
self.api.create_bucket(
bucket_name, 'allPublic', bucket_info={'created_at_millis': str(current_time_millis())}
Expand Down Expand Up @@ -322,6 +331,11 @@ def __init__(self, command, account_id, application_key, realm, bucket_name_pref
self.realm = realm
self.bucket_name_prefix = bucket_name_prefix

def generate_bucket_name(self):
return self.bucket_name_prefix + bucket_name_part(
BUCKET_NAME_LENGTH - len(self.bucket_name_prefix)
)

def run_command(self, args, additional_env: Optional[dict] = None):
"""
Runs the command with the given arguments, returns a tuple in form of
Expand Down Expand Up @@ -567,7 +581,7 @@ def basic_test(b2_tool, bucket_name):

def key_restrictions_test(b2_tool, bucket_name):

second_bucket_name = b2_tool.bucket_name_prefix + '-' + random_hex(24)
second_bucket_name = b2_tool.generate_bucket_name()
b2_tool.should_succeed(['create-bucket', second_bucket_name, 'allPublic'],)

key_one_name = 'clt-testKey-01' + random_hex(6)
Expand Down Expand Up @@ -626,7 +640,7 @@ def key_restrictions_test(b2_tool, bucket_name):
def account_test(b2_tool, bucket_name):
# actually a high level operations test - we run bucket tests here since this test doesn't use it
b2_tool.should_succeed(['delete-bucket', bucket_name])
new_bucket_name = b2_tool.bucket_name_prefix + '-' + random_hex(24)
new_bucket_name = b2_tool.generate_bucket_name()
# apparently server behaves erratically when we delete a bucket and recreate it right away
b2_tool.should_succeed(['create-bucket', new_bucket_name, 'allPrivate'])
b2_tool.should_succeed(['update-bucket', new_bucket_name, 'allPublic'])
Expand Down Expand Up @@ -656,7 +670,7 @@ def account_test(b2_tool, bucket_name):

# first, let's make sure "create-bucket" doesn't work without auth data - i.e. that the sqlite file hs been
# successfully removed
bucket_name = b2_tool.bucket_name_prefix + '-' + random_hex(24)
bucket_name = b2_tool.generate_bucket_name()
b2_tool.should_fail(
['create-bucket', bucket_name, 'allPrivate'],
r'ERROR: Missing account data: \'NoneType\' object is not subscriptable (\(key 0\) )? '
Expand All @@ -670,7 +684,7 @@ def account_test(b2_tool, bucket_name):
os.environ['B2_APPLICATION_KEY_ID'] = os.environ['B2_TEST_APPLICATION_KEY_ID']
os.environ['B2_ENVIRONMENT'] = b2_tool.realm

bucket_name = b2_tool.bucket_name_prefix + '-' + random_hex(24)
bucket_name = b2_tool.generate_bucket_name()
b2_tool.should_succeed(['create-bucket', bucket_name, 'allPrivate'])
b2_tool.should_succeed(['delete-bucket', bucket_name])
assert os.path.exists(new_creds), 'sqlite file not created'
Expand Down Expand Up @@ -1181,7 +1195,7 @@ def prepare_and_run_sync_copy_tests(
else:
b2_file_prefix = ''

other_bucket_name = b2_tool.bucket_name_prefix + '-' + random_hex(24)
other_bucket_name = b2_tool.generate_bucket_name()
success, _ = b2_tool.run_command(['create-bucket', other_bucket_name, 'allPublic'])

other_b2_sync_point = 'b2:%s' % other_bucket_name
Expand Down Expand Up @@ -1360,7 +1374,7 @@ def default_sse_b2_test(b2_tool, bucket_name):
should_equal(bucket_default_sse, bucket_info['defaultServerSideEncryption'])

# Set default encryption via create-bucket
second_bucket_name = b2_tool.bucket_name_prefix + '-' + random_hex(24)
second_bucket_name = b2_tool.generate_bucket_name()
b2_tool.should_succeed(
['create-bucket', '--defaultServerSideEncryption=SSE-B2', second_bucket_name, 'allPublic']
)
Expand Down Expand Up @@ -1643,7 +1657,7 @@ def sse_c_test(b2_tool, bucket_name):


def file_lock_test(b2_tool, bucket_name):
lock_disabled_bucket_name = b2_tool.bucket_name_prefix + '-' + random_hex(24)
lock_disabled_bucket_name = b2_tool.generate_bucket_name()
b2_tool.should_succeed([
'create-bucket',
lock_disabled_bucket_name,
Expand Down Expand Up @@ -1693,7 +1707,7 @@ def file_lock_test(b2_tool, bucket_name):
'compliance', '--defaultRetentionPeriod', '7 days'
], r'ERROR: The bucket is not file lock enabled \(bucket_missing_file_lock\)'
)
lock_enabled_bucket_name = b2_tool.bucket_name_prefix + '-' + random_hex(24)
lock_enabled_bucket_name = b2_tool.generate_bucket_name()
b2_tool.should_succeed(
[
'create-bucket',
Expand Down Expand Up @@ -2168,8 +2182,8 @@ def test_integration(sut, cleanup):
realm = os.environ.get('B2_TEST_ENVIRONMENT', 'production')

sys.argv = ['test_b2_command_line.py', '--command', sut]
general_bucket_name_prefix = 'test-b2-cli-'
this_run_bucket_name_prefix = general_bucket_name_prefix + random_hex(8)
general_bucket_name_prefix = 'clitst'
this_run_bucket_name_prefix = general_bucket_name_prefix + bucket_name_part(8)

try:
main(realm, general_bucket_name_prefix, this_run_bucket_name_prefix)
Expand All @@ -2182,7 +2196,7 @@ def test_integration(sut, cleanup):


if __name__ == '__main__':
general_bucket_name_prefix = 'test-b2-cli-'
general_bucket_name_prefix = 'clitst'
realm = os.environ.get('B2_TEST_ENVIRONMENT', 'production')
this_run_bucket_name_prefix = general_bucket_name_prefix + random_hex(8)
this_run_bucket_name_prefix = general_bucket_name_prefix + bucket_name_part(8)
main(realm, general_bucket_name_prefix, this_run_bucket_name_prefix)

0 comments on commit 9d9780b

Please sign in to comment.