Skip to content

Commit

Permalink
Enable exclude-severity only on csv format
Browse files Browse the repository at this point in the history
  • Loading branch information
moshemorad committed Nov 7, 2024
1 parent 4f83391 commit c16f33a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
5 changes: 3 additions & 2 deletions robusta_krr/formatters/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from robusta_krr.core.abstract import formatters
from robusta_krr.core.models.allocations import NONE_LITERAL, format_diff, format_recommendation_value
from robusta_krr.core.models.config import settings
from robusta_krr.core.models.result import ResourceScan, ResourceType, Result

logger = logging.getLogger("krr")
Expand Down Expand Up @@ -52,10 +53,10 @@ def csv_exporter(result: Result) -> str:
# We need to order the resource columns so that they are in the format of Namespace,Name,Pods,Old Pods,Type,Container,CPU Diff,CPU Requests,CPU Limits,Memory Diff,Memory Requests,Memory Limits
csv_columns = ["Namespace", "Name", "Pods", "Old Pods", "Type", "Container"]

if result.config and result.config.show_cluster_name:
if settings.show_cluster_name:
csv_columns.insert(0, "Cluster")

if result.config and result.config.show_severity:
if settings.show_severity:
csv_columns.append("Severity")

for resource in ResourceType:
Expand Down
3 changes: 3 additions & 0 deletions robusta_krr/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import List, Optional
from uuid import UUID

import click
import typer
import urllib3
from pydantic import ValidationError # noqa: F401
Expand Down Expand Up @@ -268,6 +269,8 @@ def run_strategy(
**strategy_args,
) -> None:
f"""Run KRR using the `{_strategy_name}` strategy"""
if not show_severity and format != "csv":
raise click.BadOptionUsage("--exclude-severity", "--exclude-severity works only with format=csv")

try:
config = Config(
Expand Down
2 changes: 2 additions & 0 deletions tests/formatters/test_csv_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import pytest

from robusta_krr.core.models.config import Config
from robusta_krr.core.models.result import Result
from robusta_krr.formatters.csv import csv_exporter

Expand Down Expand Up @@ -151,6 +152,7 @@ def _load_result(override_config: dict[str, Any]) -> Result:
res_data = json.loads(RESULT)
res_data["config"].update(override_config)
result = Result(**res_data)
Config.set_config(result.config)
return result


Expand Down
2 changes: 1 addition & 1 deletion tests/test_krr.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_run(log_flag: str):
raise e from result.exception


@pytest.mark.parametrize("format", ["json", "yaml", "table", "pprint"])
@pytest.mark.parametrize("format", ["json", "yaml", "table", "pprint", "csv"])
@pytest.mark.parametrize("output", ["--logtostderr", "-q"])
def test_output_formats(format: str, output: str):
result = runner.invoke(app, [STRATEGY_NAME, output, "-f", format])
Expand Down
21 changes: 21 additions & 0 deletions tests/test_runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest
from click.testing import Result
from typer.testing import CliRunner

from robusta_krr.main import app, load_commands

runner = CliRunner(mix_stderr=False)
load_commands()


@pytest.mark.parametrize(
"args, expected_exit_code",
[
(["--exclude-severity", "-f", "csv"], 0),
(["--exclude-severity", "-f", "table"], 2),
(["--exclude-severity"], 2),
],
)
def test_exclude_severity_option(args: list[str], expected_exit_code: int) -> None:
result: Result = runner.invoke(app, ["simple", *args])
assert result.exit_code == expected_exit_code

0 comments on commit c16f33a

Please sign in to comment.