Skip to content

Commit 8876fb1

Browse files
committed
Chore: Address minor nitpicks and more from code review by CodeRabbit
1 parent c713073 commit 8876fb1

File tree

8 files changed

+55
-5
lines changed

8 files changed

+55
-5
lines changed

cratedb_toolkit/cluster/cli.py

+5
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ def ping(ctx: click.Context, cluster_id: str, cluster_name: str):
106106
with handle_command_errors("ping cluster"):
107107
cluster = ManagedCluster(cluster_id=cluster_id, cluster_name=cluster_name).start()
108108
has_db_result = bool(cluster.query("SELECT 42 AS availability_check -- ctk"))
109+
110+
# Ensure cluster.info is present.
111+
if not cluster.info:
112+
raise CroudException("Unable to retrieve cluster information")
113+
109114
response = {
110115
"meta": cluster.info.meta,
111116
"cloud": cluster.info.ready,

cratedb_toolkit/cluster/croud.py

+8
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,28 @@ def with_cluster_id(self, cluster_id: str):
7575
self.cluster_id = cluster_id
7676
return self
7777

78+
def validate(self):
79+
if not self.cluster_id:
80+
raise ValueError("Cluster ID is not set")
81+
7882
@property
7983
def home(self):
84+
self.validate()
8085
return f"/api/v2/clusters/{self.cluster_id}/"
8186

8287
@property
8388
def suspend(self):
89+
self.validate()
8490
return f"/api/v2/clusters/{self.cluster_id}/suspend/"
8591

8692
@property
8793
def jwt(self):
94+
self.validate()
8895
return f"/api/v2/clusters/{self.cluster_id}/jwt/"
8996

9097
@property
9198
def import_jobs(self):
99+
self.validate()
92100
return f"/api/v2/clusters/{self.cluster_id}/import-jobs/"
93101

94102

cratedb_toolkit/testing/testcontainers/util.py

+5
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ def __init__(self):
180180

181181
@abstractmethod
182182
def setup(self):
183+
"""
184+
Override this method to initialize self.container with a DockerContainer instance.
185+
This method should create the container but NOT start it, as start() will be called
186+
automatically after setup() completes successfully.
187+
"""
183188
raise NotImplementedError("Must be implemented by child class")
184189

185190
def run_setup(self):

cratedb_toolkit/util/setting.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,12 @@ def obtain_settings(specs: t.List[Setting], prog_name: str = None) -> t.Dict[str
4949
- Environment variable. Example: `export FOOBAR=bazqux`.
5050
- Environment variable prefix. Example: `export APPNAME_FOOBAR=bazqux`.
5151
"""
52+
5253
# Load environment variables from `.env` file.
53-
init_dotenv()
54+
try:
55+
init_dotenv()
56+
except Exception as ex:
57+
logger.warning(f"Failed to load environment variables from .env file: {ex}")
5458

5559
# Decode settings from command-line arguments or environment variables.
5660
prog_name = prog_name or sys.argv[0]
@@ -97,8 +101,10 @@ def check_mutual_exclusiveness(
97101
raise ValueError(message_none)
98102
if values.count(None) < len(values) - 1:
99103
if message_multiple is None:
104+
# Collect and format the specified settings.
105+
specified = [f"{param}={val}" for param, val in zip(parameter_names, values) if val is not None]
100106
message_multiple = (
101-
f"The settings are mutually exclusive, but multiple of them have been specified. {guidance}"
107+
f"The settings are mutually exclusive, but multiple were specified: {', '.join(specified)}. {guidance}"
102108
)
103109
raise ValueError(message_multiple)
104110
return values

doc/cluster/_address.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
:::{rubric} Cluster address
22
:::
3+
34
The program provides various options for addressing the database cluster
45
in different situations, managed or not.
56

doc/cluster/backlog.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,4 @@ a patch for some item, it will be very much welcome.
9595
- CLI: Shrink address URLs to single parameter `--cluster-url`
9696
- Changelog: Notify about breaking change with input address parameter names
9797
- Docs: Update guidelines about input address parameter preferences
98-
- Managed: Use `keyring` for caching the JWT token, and compensate token expiry
98+
- Managed: Use `keyring` for caching the JWT token, and compensate for token expiry

doc/cluster/python.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# CrateDB Cluster Python API
44

5-
The `cratedb_toolkit.ManagedCluster` class provides higher level API/SDK
5+
The `cratedb_toolkit.ManagedCluster` class provides the higher level API/SDK
66
entrypoints to start/deploy/resume a database cluster, inquire information
77
about it, and stop/suspend it again.
88

tests/cluster/test_cli.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
1+
import pytest
2+
from click import ClickException
13
from click.testing import CliRunner
24

3-
from cratedb_toolkit.cluster.cli import cli
5+
from cratedb_toolkit.cluster.cli import cli, handle_command_errors
6+
from cratedb_toolkit.exception import CroudException
7+
8+
9+
def test_exception_handling_croud(caplog):
10+
with pytest.raises(CroudException):
11+
with handle_command_errors("test operation"):
12+
raise CroudException("test error")
13+
assert "Failed to test operation" in caplog.text
14+
assert "test error" in caplog.text
15+
16+
17+
def test_exception_handling_click():
18+
with pytest.raises(ClickException):
19+
with handle_command_errors("test operation"):
20+
raise ClickException("click error")
21+
22+
23+
def test_exception_handling_generic_to_system_exit(caplog):
24+
with pytest.raises(SystemExit) as excinfo:
25+
with handle_command_errors("test operation"):
26+
raise ValueError("value error")
27+
assert excinfo.value.code == 1
28+
assert "Unexpected error on operation: test operation" in caplog.text
429

530

631
def test_managed_cluster_info_default(cloud_environment):

0 commit comments

Comments
 (0)