Skip to content

Commit

Permalink
Add first csv formatter test
Browse files Browse the repository at this point in the history
  • Loading branch information
moshemorad committed Nov 7, 2024
1 parent 94d0063 commit 55ce148
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,4 @@ dmypy.json
.DS_Store
robusta_lib
.idea
.vscode
5 changes: 3 additions & 2 deletions robusta_krr/core/models/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class Recommendation(pd.BaseModel):


class ResourceRecommendation(pd.BaseModel):
requests: dict[ResourceType, RecommendationValue]
limits: dict[ResourceType, RecommendationValue]
requests: dict[ResourceType, RecommendationValue | Recommendation]
limits: dict[ResourceType, RecommendationValue | Recommendation]
info: dict[ResourceType, Optional[str]]


Expand All @@ -40,6 +40,7 @@ def calculate(cls, object: K8sObjectData, recommendation: ResourceAllocations) -

current_severity = Severity.calculate(current, recommended, resource_type)

#TODO: consider... changing field after model created doesn't validate it.
getattr(recommendation_processed, selector)[resource_type] = Recommendation(
value=recommended, severity=current_severity
)
Expand Down
185 changes: 185 additions & 0 deletions tests/formatters/test_csv_formatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import json
from typing import Any
from robusta_krr.core.models.result import Result
from robusta_krr.formatters.csv import csv_exporter
import io
import csv

RESULT = """
{
"scans": [
{
"object": {
"cluster": "mock-cluster",
"name": "mock-object-1",
"container": "mock-container-1",
"pods": [
{
"name": "mock-pod-1",
"deleted": false
},
{
"name": "mock-pod-2",
"deleted": false
},
{
"name": "mock-pod-3",
"deleted": true
}
],
"hpa": null,
"namespace": "default",
"kind": "Deployment",
"allocations": {
"requests": {
"cpu": 1.0,
"memory": 1.0
},
"limits": {
"cpu": 2.0,
"memory": 2.0
},
"info": {}
},
"warnings": []
},
"recommended": {
"requests": {
"cpu": {
"value": "?",
"severity": "UNKNOWN"
},
"memory": {
"value": "?",
"severity": "UNKNOWN"
}
},
"limits": {
"cpu": {
"value": "?",
"severity": "UNKNOWN"
},
"memory": {
"value": "?",
"severity": "UNKNOWN"
}
},
"info": {
"cpu": "Not enough data",
"memory": "Not enough data"
}
},
"severity": "UNKNOWN"
}
],
"score": 100,
"resources": [
"cpu",
"memory"
],
"description": "tests data",
"strategy": {
"name": "simple",
"settings": {
"history_duration": 336.0,
"timeframe_duration": 1.25,
"cpu_percentile": 95.0,
"memory_buffer_percentage": 15.0,
"points_required": 100,
"allow_hpa": false,
"use_oomkill_data": false,
"oom_memory_buffer_percentage": 25.0
}
},
"errors": [],
"clusterSummary": {},
"config": {
"quiet": false,
"verbose": false,
"clusters": [],
"kubeconfig": null,
"impersonate_user": null,
"impersonate_group": null,
"namespaces": "*",
"resources": [],
"selector": null,
"cpu_min_value": 10,
"memory_min_value": 100,
"prometheus_url": null,
"prometheus_auth_header": null,
"prometheus_other_headers": {},
"prometheus_ssl_enabled": false,
"prometheus_cluster_label": null,
"prometheus_label": null,
"eks_managed_prom": false,
"eks_managed_prom_profile_name": null,
"eks_access_key": null,
"eks_secret_key": null,
"eks_service_name": "aps",
"eks_managed_prom_region": null,
"coralogix_token": null,
"openshift": false,
"max_workers": 10,
"format": "csv",
"show_cluster_name": false,
"strategy": "simple",
"log_to_stderr": false,
"width": null,
"file_output": null,
"slack_output": null,
"other_args": {
"history_duration": "336",
"timeframe_duration": "1.25",
"cpu_percentile": "95",
"memory_buffer_percentage": "15",
"points_required": "100",
"allow_hpa": false,
"use_oomkill_data": false,
"oom_memory_buffer_percentage": "25"
},
"inside_cluster": false,
"file_output_dynamic": false,
"bool": false
}
}
"""


def test_csv_headers() -> None:
res_data = json.loads(RESULT)
result = Result(**res_data)
x = csv_exporter(result)
reader = csv.DictReader(io.StringIO(x))

expected_headers: list[str] = [
"Namespace",
"Name",
"Pods",
"Old Pods",
"Type",
"Container",
"CPU Diff",
"CPU Requests",
"CPU Limits",
"Memory Diff",
"Memory Requests",
"Memory Limits",
]
assert reader.fieldnames == expected_headers

expected_first_row: dict[str, str] = {
"Namespace": "default",
"Name": "mock-object-1",
"Pods": "2",
"Old Pods": "1",
"Type": "Deployment",
"Container": "mock-container-1",
"CPU Diff": "",
"CPU Requests": "1.0 -> ?",
"CPU Limits": "2.0 -> ?",
"Memory Diff": "",
"Memory Requests": "1.0 -> ?",
"Memory Limits": "2.0 -> ?",
}
first_row: dict[str, Any] = next(reader)
assert first_row == expected_first_row

0 comments on commit 55ce148

Please sign in to comment.