Skip to content

Commit c61bce5

Browse files
authored
fix shared tier tests (#81)
1 parent 6c2552f commit c61bce5

File tree

2 files changed

+36
-48
lines changed

2 files changed

+36
-48
lines changed

singlestoredb/management/workspace.py

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,13 +1391,10 @@ def connect(self, **kwargs: Any) -> connection.Connection:
13911391
msg='An endpoint has not been set in this '
13921392
'starter workspace configuration',
13931393
)
1394-
# Parse endpoint as host:port
1395-
if ':' in self.endpoint:
1396-
host, port = self.endpoint.split(':', 1)
1397-
kwargs['host'] = host
1398-
kwargs['port'] = int(port)
1399-
else:
1400-
kwargs['host'] = self.endpoint
1394+
1395+
kwargs['host'] = self.endpoint
1396+
kwargs['database'] = self.database_name
1397+
14011398
return connection.connect(**kwargs)
14021399

14031400
def terminate(self) -> None:
@@ -1455,15 +1452,15 @@ def starter_workspaces(self) -> NamedList['StarterWorkspace']:
14551452

14561453
def create_user(
14571454
self,
1458-
user_name: str,
1455+
username: str,
14591456
password: Optional[str] = None,
14601457
) -> Dict[str, str]:
14611458
"""
14621459
Create a new user for this starter workspace.
14631460
14641461
Parameters
14651462
----------
1466-
user_name : str
1463+
username : str
14671464
The starter workspace user name to connect the new user to the database
14681465
password : str, optional
14691466
Password for the new user. If not provided, a password will be
@@ -1485,7 +1482,7 @@ def create_user(
14851482
)
14861483

14871484
payload = {
1488-
'userName': user_name,
1485+
'userName': username,
14891486
}
14901487
if password is not None:
14911488
payload['password'] = password
@@ -1854,7 +1851,8 @@ def create_starter_workspace(
18541851
self,
18551852
name: str,
18561853
database_name: str,
1857-
workspace_group: dict[str, str],
1854+
provider: str,
1855+
region_name: str,
18581856
) -> 'StarterWorkspace':
18591857
"""
18601858
Create a new starter (shared tier) workspace.
@@ -1865,28 +1863,21 @@ def create_starter_workspace(
18651863
Name of the starter workspace
18661864
database_name : str
18671865
Name of the database for the starter workspace
1868-
workspace_group : dict[str, str]
1869-
Workspace group input (dict with keys: 'cell_id' and 'name').
1866+
provider : str
1867+
Cloud provider for the starter workspace (e.g., 'aws', 'gcp', 'azure')
1868+
region_name : str
1869+
Cloud provider region for the starter workspace (e.g., 'us-east-1')
18701870
18711871
Returns
18721872
-------
18731873
:class:`StarterWorkspace`
18741874
"""
1875-
if not workspace_group or not isinstance(workspace_group, dict):
1876-
raise ValueError(
1877-
'workspace_group must be a dict with keys: '
1878-
"'cell_id' and 'name'",
1879-
)
1880-
if set(workspace_group.keys()) != {'cell_id', 'name'}:
1881-
raise ValueError("workspace_group must contain only 'cell_id' and 'name'")
18821875

18831876
payload = {
18841877
'name': name,
18851878
'databaseName': database_name,
1886-
'workspaceGroup': {
1887-
'name': workspace_group['name'],
1888-
'cellID': workspace_group['cell_id'],
1889-
},
1879+
'provider': provider,
1880+
'regionName': region_name,
18901881
}
18911882

18921883
res = self._post('sharedtier/virtualWorkspaces', json=payload)

singlestoredb/tests/test_management.py

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ def clean_name(s):
2525
return re.sub(r'[^\w]', r'-', s).replace('_', '-').lower()
2626

2727

28+
def shared_database_name(s):
29+
"""Return a shared database name. Cannot contain special characters except -"""
30+
return re.sub(r'[^\w]', '', s).replace('-', '_').lower()
31+
32+
2833
@pytest.mark.management
2934
class TestCluster(unittest.TestCase):
3035

@@ -370,39 +375,31 @@ class TestStarterWorkspace(unittest.TestCase):
370375

371376
manager = None
372377
starter_workspace = None
373-
starter_workspace_user = {
374-
'username': 'starter_user',
375-
'password': None,
376-
}
377-
378-
@property
379-
def starter_username(self):
380-
"""Return the username for the starter workspace user."""
381-
return self.starter_workspace_user['username']
382-
383-
@property
384-
def password(self):
385-
"""Return the password for the starter workspace user."""
386-
return self.starter_workspace_user['password']
387378

388379
@classmethod
389380
def setUpClass(cls):
390381
cls.manager = s2.manage_workspaces()
391382

392-
shared_tier_regions = [
383+
shared_tier_regions: NamedList[Region] = [
393384
x for x in cls.manager.shared_tier_regions if 'US' in x.name
394385
]
395-
cls.password = secrets.token_urlsafe(20) + '-x&$'
386+
cls.starter_username = 'starter_user'
387+
cls.password = secrets.token_urlsafe(20)
396388

397-
name = clean_name(secrets.token_urlsafe(20)[:20])
389+
name = shared_database_name(secrets.token_urlsafe(20)[:20])
390+
391+
cls.database_name = f'starter_db_{name}'
392+
393+
shared_tier_region: Region = random.choice(shared_tier_regions)
394+
395+
if not shared_tier_region:
396+
raise ValueError('No shared tier regions found')
398397

399398
cls.starter_workspace = cls.manager.create_starter_workspace(
400399
f'starter-ws-test-{name}',
401-
database_name=f'starter_db_{name}',
402-
workspace_group={
403-
'name': f'starter-wg-test-{name}',
404-
'cell_id': random.choice(shared_tier_regions).id,
405-
},
400+
database_name=cls.database_name,
401+
provider=shared_tier_region.provider,
402+
region_name=shared_tier_region.region_name,
406403
)
407404

408405
cls.starter_workspace.create_user(
@@ -470,14 +467,14 @@ def test_connect(self):
470467
) as conn:
471468
with conn.cursor() as cur:
472469
cur.execute('show databases')
473-
assert 'starter_db' in [x[0] for x in list(cur)]
470+
assert self.database_name in [x[0] for x in list(cur)]
474471

475472
# Test missing endpoint
476473
workspace = self.manager.get_starter_workspace(self.starter_workspace.id)
477474
workspace.endpoint = None
478475

479476
with self.assertRaises(s2.ManagementError) as cm:
480-
workspace.connect(user='admin', password=self.password)
477+
workspace.connect(user=self.starter_username, password=self.password)
481478

482479
assert 'endpoint' in cm.exception.msg, cm.exception.msg
483480

0 commit comments

Comments
 (0)