Skip to content

Commit c2bf625

Browse files
committed
Add use_default_tempdir cloud config options
1 parent 26a6a6d commit c2bf625

3 files changed

Lines changed: 42 additions & 4 deletions

File tree

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Not released
55
Features
66
--------
77
* Make geomet an optional dependency at runtime (PYTHON-1237)
8+
* Add use_default_tempdir cloud config options (PYTHON-1245)
89

910
Bug Fixes
1011
---------

cassandra/datastax/cloud/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ def get_cloud_config(cloud_config, create_pyopenssl_context=False):
9797

9898
def read_cloud_config_from_zip(cloud_config, create_pyopenssl_context):
9999
secure_bundle = cloud_config['secure_connect_bundle']
100+
use_default_tempdir = cloud_config.get('use_default_tempdir', None)
100101
with ZipFile(secure_bundle) as zipfile:
101-
base_dir = os.path.dirname(secure_bundle)
102+
base_dir = tempfile.gettempdir() if use_default_tempdir else os.path.dirname(secure_bundle)
102103
tmp_dir = tempfile.mkdtemp(dir=base_dir)
103104
try:
104105
zipfile.extractall(path=tmp_dir)

tests/unit/advanced/cloud/test_cloud.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,29 @@
66
# You may obtain a copy of the License at
77
#
88
# http://www.datastax.com/terms/datastax-dse-driver-license-terms
9+
import tempfile
10+
import os
11+
import shutil
12+
import six
13+
914
try:
1015
import unittest2 as unittest
1116
except ImportError:
1217
import unittest # noqa
1318

14-
import os
15-
19+
from cassandra import DriverException
1620
from cassandra.datastax import cloud
1721

1822
from mock import patch
1923

24+
from tests import notwindows
2025

2126
class CloudTests(unittest.TestCase):
2227

2328
current_path = os.path.dirname(os.path.abspath(__file__))
29+
creds_path = os.path.join(current_path, './creds.zip')
2430
config_zip = {
25-
'secure_connect_bundle': os.path.join(current_path, './creds.zip')
31+
'secure_connect_bundle': creds_path
2632
}
2733
metadata_json = """
2834
{"region":"local",
@@ -75,3 +81,33 @@ def test_parse_metadata_info(self):
7581
]
7682
for host_id in host_ids:
7783
self.assertIn(host_id, config.host_ids)
84+
85+
@notwindows
86+
def test_use_default_tempdir(self):
87+
tmpdir = tempfile.mkdtemp()
88+
89+
def clean_tmp_dir():
90+
os.chmod(tmpdir, 0o777)
91+
shutil.rmtree(tmpdir)
92+
self.addCleanup(clean_tmp_dir)
93+
94+
tmp_creds_path = os.path.join(tmpdir, 'creds.zip')
95+
shutil.copyfile(self.creds_path, tmp_creds_path)
96+
os.chmod(tmpdir, 0o544)
97+
config = {
98+
'secure_connect_bundle': tmp_creds_path
99+
}
100+
101+
# The directory is not writtable.. we expect a permission error
102+
exc = PermissionError if six.PY3 else OSError
103+
with self.assertRaises(exc):
104+
cloud.get_cloud_config(config)
105+
106+
# With use_default_tempdir, we expect an connection refused
107+
# since the cluster doesn't exist
108+
with self.assertRaises(DriverException):
109+
config = {
110+
'secure_connect_bundle': tmp_creds_path,
111+
'use_default_tempdir': True
112+
}
113+
cloud.get_cloud_config(config)

0 commit comments

Comments
 (0)