Skip to content

Commit

Permalink
Merge pull request #15 from ushanemani/master
Browse files Browse the repository at this point in the history
Addition of Socket_timeout into Azure Store creation
  • Loading branch information
crepererum authored Jul 31, 2019
2 parents 70f84d5 + 404a3a2 commit f0a75af
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 1 deletion.
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ sphinx_rtd_theme
reno
pytest
pytest-cov
pytest-mock
coverage
simplekv
1 change: 1 addition & 0 deletions storefact/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def get_store(type, create_if_missing=True, **params):
``"create_if_missing"`` has to be ``False`` if ``"use_sas"`` is set. When ``"use_sas"`` is set,
``"account_key"`` is interpreted as Shared Access Signature (SAS) token.FIRE
``"max_connections"``: Maximum number of network connections used by one store (default: ``2``).
``"socket_timeout"``: maximum timeout value in seconds (socket_timeout: ``100``).
* ``"s3"``: Returns a plain ``simplekv.net.botostore.BotoStore``.
Parameters must include ``"host"``, ``"bucket"``, ``"access_key"``, ``"secret_key"``.
Optional parameters are
Expand Down
2 changes: 2 additions & 0 deletions storefact/_store_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def _create_store_azure(type, params):
create_if_missing=params['create_if_missing'],
checksum=params.get('checksum', True),
max_connections=params.get('max_connections', 2),
socket_timeout=params.get('socket_timeout', (20, 100)),
)
else:
return HAzureBlockBlobStore(
Expand All @@ -55,6 +56,7 @@ def _create_store_azure(type, params):
create_if_missing=params['create_if_missing'],
checksum=params.get('checksum', True),
max_connections=params.get('max_connections', 2),
socket_timeout=params.get('socket_timeout', (20, 100)),
)


Expand Down
4 changes: 3 additions & 1 deletion storefact/_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def url2dict(url, raise_on_extra_params=False):
fs://path
s3://access_key:secret_key@endpoint/bucket[?create_if_missing=true]
azure://account_name:account_key@container[?create_if_missing=true][?max_connections=2]
azure://account_name:shared_access_signature@container?use_sas&create_if_missing=false[?max_connections=2]
azure://account_name:shared_access_signature@container?use_sas&create_if_missing=false[?max_connections=2&socket_timeout=(20,100)]
"""
u = urisplit(url)
parsed = dict(
Expand Down Expand Up @@ -85,6 +85,8 @@ def extract_params(scheme, host, port, path, query, userinfo):
params['use_sas'] = True
if u'max_connections' in query:
params['max_connections'] = int(query.pop(u'max_connections')[-1])
if u'socket_timeout' in query:
params['socket_timeout'] = query.pop(u'socket_timeout')
return params

raise ValueError('Unknown storage type "{}"'.format(scheme))
Expand Down
178 changes: 178 additions & 0 deletions tests/test_store_creation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
from storefact._store_creation import create_store
import pytest


def test_create_store_azure(mocker):
# Mock HAzureBlockBlobStore also here, becase otherwise it will try to inherit from
# the mock object `mock_azure` created below, which will fail.
mock_hazure = mocker.patch("storefact._hstores.HAzureBlockBlobStore")
mock_azure = mocker.patch("simplekv.net.azurestore.AzureBlockBlobStore")
create_store(
"azure",
{
"account_name": "ACCOUNT",
"account_key": "KEY",
"container": "cont_name",
"create_if_missing": True,
},
)
mock_azure.assert_called_once_with(
checksum=True,
conn_string="DefaultEndpointsProtocol=https;AccountName=ACCOUNT;AccountKey=KEY",
container="cont_name",
create_if_missing=True,
max_connections=2,
public=False,
socket_timeout=(20, 100),
)
mock_hazure.assert_not_called()


def test_create_store_hazure(mocker):
mock_hazure = mocker.patch("storefact._hstores.HAzureBlockBlobStore")
create_store(
"hazure",
{
"account_name": "ACCOUNT",
"account_key": "KEY",
"container": "cont_name",
"create_if_missing": True,
},
)
mock_hazure.assert_called_once_with(
checksum=True,
conn_string="DefaultEndpointsProtocol=https;AccountName=ACCOUNT;AccountKey=KEY",
container="cont_name",
create_if_missing=True,
max_connections=2,
public=False,
socket_timeout=(20, 100),
)


def test_create_store_azure_inconsistent_params():
with pytest.raises(
Exception, match="create_if_missing is incompatible with the use of SAS tokens"
):
create_store(
"hazure",
{
"account_name": "ACCOUNT",
"account_key": "KEY",
"container": "cont_name",
"create_if_missing": True,
"use_sas": True,
},
)


def test_create_store_hs3(mocker):
mock_hs3 = mocker.patch("storefact._boto._get_s3bucket")
mock_hbotostores = mocker.patch("storefact._hstores.HBotoStore")
create_store(
"hs3",
{
'host': u'endpoint:1234',
'access_key': u'access_key',
'secret_key': u'secret_key',
'bucket': u'bucketname',
},
)
mock_hs3.assert_called_once_with(
host=u'endpoint:1234',
access_key=u'access_key',
secret_key=u'secret_key',
bucket=u'bucketname',
)


def test_create_store_s3(mocker):
mock_s3 = mocker.patch("storefact._boto._get_s3bucket")
mock_hbotostores = mocker.patch("storefact._hstores.HBotoStore")
create_store(
"s3",
{
'host': u'endpoint:1234',
'access_key': u'access_key',
'secret_key': u'secret_key',
'bucket': u'bucketname',
},
)
mock_s3.assert_called_once_with(
host=u'endpoint:1234',
access_key=u'access_key',
secret_key=u'secret_key',
bucket=u'bucketname',
)


def test_create_store_hfs(mocker):
mock_hfs = mocker.patch("storefact._hstores.HFilesystemStore")
create_store(
"hfs",
{
'type': 'hfs',
'path': 'this/is/a/relative/path',
'create_if_missing': True
},
)
mock_hfs.assert_called_once_with('this/is/a/relative/path')


@pytest.mark.skip(reason="some issue here")
def test_create_store_fs(mocker):
mock_fs = mocker.patch("simplekv.fs.FilesystemStore")
create_store(
"fs",
{
'type': 'fs',
'path': 'this/is/a/relative/fspath',
'create_if_missing': True
}
)
mock_fs.assert_called_once_with('this/is/a/relative/fspath')


def test_create_store_mem(mocker):
mock_mem = mocker.patch("simplekv.memory.DictStore")
create_store(
"memory",
{'type': u'memory', 'wrap': u'readonly'},
)
mock_mem.assert_called_once_with()


def test_create_store_hmem(mocker):
mock_hmem = mocker.patch("storefact._hstores.HDictStore")
create_store(
"hmemory",
{'type': u'memory', 'wrap': u'readonly'},
)
mock_hmem.assert_called_once_with()


@pytest.mark.skip(reason="some issue here")
def test_create_store_redis(mocker):
mock_redis = mocker.patch("simplekv.memory.redisstore.RedisStore")
mock_Strictredis = mocker.patch("redis.StrictRedis")
create_store(
"redis",
{'type': u'redis', 'host': u'localhost', 'db': 2},
)
mock_Strictredis.assert_called_once_with()


def test_create_store_valueerror():
with pytest.raises(
Exception, match="Unknown store type: ABC"
):
create_store(
"ABC",
{
"account_name": "ACCOUNT",
"account_key": "KEY",
"container": "cont_name",
"create_if_missing": True,
"use_sas": True,
},
)
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ deps =
pytest-cov
coverage
simplekv
pytest-mock
usedevelop = true
commands = py.test --cov=storefact -rs tests

0 comments on commit f0a75af

Please sign in to comment.