Skip to content

Commit

Permalink
fix comment and use new error message
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ashen committed Dec 10, 2024
1 parent 20dac9f commit ad7402d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/snowflake/cli/_plugins/spcs/services/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def logs(
Retrieves local logs from a service container.
"""
if follow:
if not FeatureFlag.ENABLE_SPCS_LOG_STREAMING.is_enabled():
if FeatureFlag.ENABLE_SPCS_LOG_STREAMING.is_disabled():
raise FeatureNotEnabledError(
"ENABLE_SPCS_LOG_STREAMING",
"Streaming logs from spcs containers is disabled.",
Expand Down
4 changes: 2 additions & 2 deletions src/snowflake/cli/api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ def __init__(self, show_obj_query: str):
)


class FeatureNotEnabledError(RuntimeError):
class FeatureNotEnabledError(ClickException):
def __init__(self, feature_name: str, custom_message: Optional[str] = None):
base_message = f"Set the following field in your configuration to enable it: '{feature_name}'."
base_message = f"To enable it, add '{feature_name} = true' to '[cli.features]' section of your configuration file."
if custom_message:
message = f"{custom_message} {base_message}"
else:
Expand Down
64 changes: 36 additions & 28 deletions tests/spcs/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from snowflake.cli._plugins.spcs.services.commands import _service_name_callback
from snowflake.cli._plugins.spcs.services.manager import ServiceManager
from snowflake.cli.api.constants import ObjectType
from snowflake.cli.api.exceptions import FeatureNotEnabledError
from snowflake.cli.api.identifiers import FQN
from snowflake.cli.api.project.util import to_string_literal
from snowflake.connector.cursor import SnowflakeCursor
Expand Down Expand Up @@ -607,10 +606,10 @@ def test_stream_logs_with_include_timestamps_true(mock_sleep, mock_logs):

@patch("snowflake.cli._plugins.spcs.services.manager.ServiceManager.execute_query")
@patch(
"snowflake.cli.api.feature_flags.FeatureFlag.ENABLE_SPCS_LOG_STREAMING.is_enabled"
"snowflake.cli.api.feature_flags.FeatureFlag.ENABLE_SPCS_LOG_STREAMING.is_disabled"
)
def test_logs_incompatible_flags(mock_is_enabled, mock_execute_query, runner):
mock_is_enabled.return_value = True
def test_logs_incompatible_flags(mock_is_disabled, mock_execute_query, runner):
mock_is_disabled.return_value = False
result = runner.invoke(
[
"spcs",
Expand All @@ -634,12 +633,12 @@ def test_logs_incompatible_flags(mock_is_enabled, mock_execute_query, runner):

@patch("snowflake.cli._plugins.spcs.services.manager.ServiceManager.execute_query")
@patch(
"snowflake.cli.api.feature_flags.FeatureFlag.ENABLE_SPCS_LOG_STREAMING.is_enabled"
"snowflake.cli.api.feature_flags.FeatureFlag.ENABLE_SPCS_LOG_STREAMING.is_disabled"
)
def test_logs_incompatible_flags_follow_previous_logs(
mock_is_enabled, mock_execute_query, runner
mock_is_disabled, mock_execute_query, runner
):
mock_is_enabled.return_value = True
mock_is_disabled.return_value = False
result = runner.invoke(
[
"spcs",
Expand All @@ -665,30 +664,39 @@ def test_logs_incompatible_flags_follow_previous_logs(


@patch(
"snowflake.cli.api.feature_flags.FeatureFlag.ENABLE_SPCS_LOG_STREAMING.is_enabled"
"snowflake.cli.api.feature_flags.FeatureFlag.ENABLE_SPCS_LOG_STREAMING.is_disabled"
)
def test_logs_streaming_disabled(mock_is_enabled, runner):
mock_is_enabled.return_value = False
with pytest.raises(FeatureNotEnabledError) as exc_info:
runner.invoke(
[
"spcs",
"service",
"logs",
"test_service",
"--container-name",
"test_container",
"--instance-id",
"0",
"--follow",
"--num-lines",
"100",
]
)
def test_logs_streaming_disabled(mock_is_disabled, runner):
mock_is_disabled.return_value = True
result = runner.invoke(
[
"spcs",
"service",
"logs",
"test_service",
"--container-name",
"test_container",
"--instance-id",
"0",
"--follow",
"--num-lines",
"100",
]
)
assert (
"Streaming logs from spcs containers is disabled. Set the following field in your configuration to enable it: 'ENABLE_SPCS_LOG_STREAMING'."
in str(exc_info.value)
result.exit_code != 0
), "Expected a non-zero exit code due to feature flag disabled"

expected_output = (
"+- Error ----------------------------------------------------------------------+\n"
"| Streaming logs from spcs containers is disabled. To enable it, add |\n"
"| 'ENABLE_SPCS_LOG_STREAMING = true' to '[cli.features]' section of your |\n"
"| configuration file. |\n"
"+------------------------------------------------------------------------------+\n"
)
assert (
result.output == expected_output
), f"Expected formatted output not found: {result.output}"


def test_read_yaml(other_directory):
Expand Down

0 comments on commit ad7402d

Please sign in to comment.