Skip to content

Commit

Permalink
add new test
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ashen committed Dec 15, 2024
1 parent e1375f8 commit 8e8d36e
Showing 1 changed file with 264 additions and 0 deletions.
264 changes: 264 additions & 0 deletions tests/spcs/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,141 @@ def test_service_events_disabled(mock_is_disabled, runner):
), f"Expected formatted output not found: {result.output}"


@patch(
"snowflake.cli.api.feature_flags.FeatureFlag.ENABLE_SPCS_SERVICE_EVENTS.is_disabled"
)
@patch("snowflake.cli._plugins.spcs.services.manager.ServiceManager.execute_query")
def test_events_all_filters(mock_execute_query, mock_is_disabled, runner):
mock_is_disabled.return_value = False
mock_execute_query.side_effect = [
[
{
"key": "EVENT_TABLE",
"value": "event_table_db.data_schema.snowservices_logs",
}
],
[
(
"2024-12-14 22:27:25.420",
None,
"2024-12-14 22:27:25.420",
None,
None,
json.dumps(
{
"snow.compute_pool.id": 230,
"snow.compute_pool.name": "MY_POOL",
"snow.database.id": 5,
"snow.database.name": "TESTDB",
"snow.schema.id": 5,
"snow.schema.name": "PUBLIC",
"snow.service.container.name": "log-printer",
"snow.service.id": 1568,
"snow.service.instance": "0",
"snow.service.name": "LOG_EVENT",
"snow.service.type": "SERVICE",
}
),
json.dumps({"name": "snow.spcs.platform"}),
None,
"LOG",
json.dumps({"severity_text": "INFO"}),
json.dumps({"event.name": "CONTAINER.STATUS_CHANGE"}),
json.dumps({"message": "Running", "status": "READY"}),
None,
)
],
]

result = runner.invoke(
[
"spcs",
"service",
"events",
"LOG_EVENT",
"--container-name",
"log-printer",
"--instance-id",
"0",
"--since",
"2 hours",
"--until",
"1 hour",
"--last",
"10",
"--warehouse",
"XSMALL",
"--role",
"sysadmin",
]
)

assert result.exit_code == 0, f"Command failed with output: {result.output}"

call_0 = mock_execute_query.mock_calls[0].args[0]
assert (
call_0 == "show parameters like 'event_table' in account"
), f"Unexpected query in Call 0: {call_0}"

actual_query = mock_execute_query.mock_calls[1].args[0]
expected_query = (
" select * from (\n"
" select *\n"
" from event_table_db.data_schema.snowservices_logs\n"
" where (\n"
' resource_attributes:"snow.service.name" = '
"'LOG_EVENT' and (resource_attributes:\"snow.service.instance\" = '0' OR "
"resource_attributes:\"snow.service.container.instance\" = '0') and "
"resource_attributes:\"snow.service.container.name\" = 'log-printer'\n"
" and timestamp >= sysdate() - interval '2 hours'\n"
" and timestamp <= sysdate() - interval '1 hour'\n"
" )\n"
" AND RECORD_TYPE = 'LOG'\n"
" AND SCOPE['name'] = 'snow.spcs.platform'\n"
" order by timestamp desc\n"
" limit 10\n"
" ) order by timestamp asc\n"
" \n"
" "
)

assert (
actual_query == expected_query
), f"Generated query does not match expected query.\n\nActual:\n{actual_query}\n\nExpected:\n{expected_query}"


@patch(
"snowflake.cli.api.feature_flags.FeatureFlag.ENABLE_SPCS_SERVICE_EVENTS.is_disabled"
)
def test_events_first_last_incompatibility(mock_is_disabled, runner):
mock_is_disabled.return_value = False
result = runner.invoke(
[
"spcs",
"service",
"events",
"LOG_EVENT",
"--container-name",
"log-printer",
"--instance-id",
"0",
"--first",
"10",
"--last",
"5",
"--warehouse",
"XSMALL",
"--role",
"sysadmin",
]
)

assert result.exit_code != 0, result.output

expected_error = "Parameters '--first' and '--last' are incompatible"
assert expected_error in result.output


@patch(
"snowflake.cli.api.feature_flags.FeatureFlag.ENABLE_SPCS_SERVICE_METRICS.is_disabled"
)
Expand Down Expand Up @@ -769,6 +904,135 @@ def test_service_metrics_disabled(mock_is_disabled, runner):
), f"Expected formatted output not found: {result.output}"


@patch(
"snowflake.cli.api.feature_flags.FeatureFlag.ENABLE_SPCS_SERVICE_EVENTS.is_disabled"
)
@patch("snowflake.cli._plugins.spcs.services.manager.ServiceManager.execute_query")
def test_metrics_all_filters(mock_execute_query, mock_is_disabled, runner):
mock_is_disabled.return_value = False
mock_execute_query.side_effect = [
[
{
"key": "EVENT_TABLE",
"value": "event_table_db.data_schema.snowservices_logs",
}
],
[
(
datetime(2024, 12, 10, 18, 53, 21, 809000),
datetime(2024, 12, 10, 18, 52, 51, 809000),
None,
None,
None,
json.dumps(
{
"snow.account.name": "XACCOUNTTEST1",
"snow.compute_pool.id": 20641,
"snow.compute_pool.name": "MY_POOL",
"snow.service.container.name": "log-printer",
"snow.service.name": "LOG_EVENT",
}
),
json.dumps({"name": "snow.spcs.platform"}),
None,
"METRIC",
json.dumps({"metric": {"name": "container.cpu.usage", "unit": "cpu"}}),
None,
"0.0005007168666666691",
None,
)
],
]

result = runner.invoke(
[
"spcs",
"service",
"metrics",
"LOG_EVENT",
"--container-name",
"log-printer",
"--instance-id",
"0",
"--since",
"2 hours",
"--until",
"1 hour",
"--last",
"10",
"--warehouse",
"XSMALL",
"--role",
"sysadmin",
]
)

assert result.exit_code == 0, f"Command failed with output: {result.output}"

call_0 = mock_execute_query.mock_calls[0].args[0]
assert (
call_0 == "show parameters like 'event_table' in account"
), f"Unexpected query in Call 0: {call_0}"

actual_query = mock_execute_query.mock_calls[1].args[0]
expected_query = (
" select * from (\n"
" select *\n"
" from event_table_db.data_schema.snowservices_logs\n"
" where (\n"
' resource_attributes:"snow.service.name" = '
"'LOG_EVENT' and (resource_attributes:\"snow.service.instance\" = '0' OR "
"resource_attributes:\"snow.service.container.instance\" = '0') and "
"resource_attributes:\"snow.service.container.name\" = 'log-printer'\n"
" and timestamp >= sysdate() - interval '2 hours'\n"
" and timestamp <= sysdate() - interval '1 hour'\n"
" )\n"
" AND RECORD_TYPE = 'METRIC'\n"
" AND SCOPE['name'] = 'snow.spcs.platform'\n"
" order by timestamp desc\n"
" limit 10\n"
" ) order by timestamp asc\n"
" \n"
" "
)

assert (
actual_query == expected_query
), f"Generated query does not match expected query.\n\nActual:\n{actual_query}\n\nExpected:\n{expected_query}"


@patch(
"snowflake.cli.api.feature_flags.FeatureFlag.ENABLE_SPCS_SERVICE_METRICS.is_disabled"
)
def test_metrics_first_last_incompatibility(mock_is_disabled, runner):
mock_is_disabled.return_value = False
result = runner.invoke(
[
"spcs",
"service",
"metrics",
"LOG_EVENT",
"--container-name",
"log-printer",
"--instance-id",
"0",
"--first",
"10",
"--last",
"5",
"--warehouse",
"XSMALL",
"--role",
"sysadmin",
]
)

assert result.exit_code != 0, result.output

expected_error = "Parameters '--first' and '--last' are incompatible"
assert expected_error in result.output


def test_read_yaml(other_directory):
tmp_dir = Path(other_directory)
spec_path = tmp_dir / "spec.yml"
Expand Down

0 comments on commit 8e8d36e

Please sign in to comment.