|
1 | 1 | package evaluator |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "strings" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | "github.com/stackrox/sensor-metrics-analyzer/internal/parser" |
@@ -403,3 +404,140 @@ func TestIsRuleApplicable(t *testing.T) { |
403 | 404 | }) |
404 | 405 | } |
405 | 406 | } |
| 407 | + |
| 408 | +func TestEvaluateHistogramInfOverflow(t *testing.T) { |
| 409 | + tests := map[string]struct { |
| 410 | + metrics parser.MetricsData |
| 411 | + wantStatus map[string]rules.Status // metric name -> expected status |
| 412 | + wantCount int // expected number of results |
| 413 | + }{ |
| 414 | + "should return red status when >50% in +Inf": { |
| 415 | + metrics: parser.MetricsData{ |
| 416 | + "test_histogram_bucket": &parser.Metric{ |
| 417 | + Name: "test_histogram_bucket", |
| 418 | + Type: "histogram", |
| 419 | + Values: []parser.MetricValue{ |
| 420 | + {Value: 10, Labels: map[string]string{"le": "0.1"}}, |
| 421 | + {Value: 20, Labels: map[string]string{"le": "0.5"}}, |
| 422 | + {Value: 30, Labels: map[string]string{"le": "1.0"}}, |
| 423 | + {Value: 100, Labels: map[string]string{"le": "+Inf"}}, // 70 in +Inf (70%) |
| 424 | + }, |
| 425 | + }, |
| 426 | + }, |
| 427 | + wantStatus: map[string]rules.Status{ |
| 428 | + "test_histogram (+Inf overflow check)": rules.StatusRed, |
| 429 | + }, |
| 430 | + wantCount: 1, |
| 431 | + }, |
| 432 | + "should return yellow status when >25% but <=50% in +Inf": { |
| 433 | + metrics: parser.MetricsData{ |
| 434 | + "test_histogram_bucket": &parser.Metric{ |
| 435 | + Name: "test_histogram_bucket", |
| 436 | + Type: "histogram", |
| 437 | + Values: []parser.MetricValue{ |
| 438 | + {Value: 10, Labels: map[string]string{"le": "0.1"}}, |
| 439 | + {Value: 20, Labels: map[string]string{"le": "0.5"}}, |
| 440 | + {Value: 30, Labels: map[string]string{"le": "1.0"}}, |
| 441 | + {Value: 50, Labels: map[string]string{"le": "+Inf"}}, // 20 in +Inf (40%) |
| 442 | + }, |
| 443 | + }, |
| 444 | + }, |
| 445 | + wantStatus: map[string]rules.Status{ |
| 446 | + "test_histogram (+Inf overflow check)": rules.StatusYellow, |
| 447 | + }, |
| 448 | + wantCount: 1, |
| 449 | + }, |
| 450 | + "should return green status when <=25% in +Inf": { |
| 451 | + metrics: parser.MetricsData{ |
| 452 | + "test_histogram_bucket": &parser.Metric{ |
| 453 | + Name: "test_histogram_bucket", |
| 454 | + Type: "histogram", |
| 455 | + Values: []parser.MetricValue{ |
| 456 | + {Value: 10, Labels: map[string]string{"le": "0.1"}}, |
| 457 | + {Value: 20, Labels: map[string]string{"le": "0.5"}}, |
| 458 | + {Value: 30, Labels: map[string]string{"le": "1.0"}}, |
| 459 | + {Value: 35, Labels: map[string]string{"le": "+Inf"}}, // 5 in +Inf (14.3%) |
| 460 | + }, |
| 461 | + }, |
| 462 | + }, |
| 463 | + wantStatus: map[string]rules.Status{ |
| 464 | + "test_histogram (+Inf overflow check)": rules.StatusGreen, |
| 465 | + }, |
| 466 | + wantCount: 1, |
| 467 | + }, |
| 468 | + "should skip histogram without +Inf bucket": { |
| 469 | + metrics: parser.MetricsData{ |
| 470 | + "test_histogram_bucket": &parser.Metric{ |
| 471 | + Name: "test_histogram_bucket", |
| 472 | + Type: "histogram", |
| 473 | + Values: []parser.MetricValue{ |
| 474 | + {Value: 10, Labels: map[string]string{"le": "0.1"}}, |
| 475 | + {Value: 20, Labels: map[string]string{"le": "0.5"}}, |
| 476 | + }, |
| 477 | + }, |
| 478 | + }, |
| 479 | + wantStatus: map[string]rules.Status{}, |
| 480 | + wantCount: 0, |
| 481 | + }, |
| 482 | + "should handle multiple histograms": { |
| 483 | + metrics: parser.MetricsData{ |
| 484 | + "hist1_bucket": &parser.Metric{ |
| 485 | + Name: "hist1_bucket", |
| 486 | + Type: "histogram", |
| 487 | + Values: []parser.MetricValue{ |
| 488 | + {Value: 10, Labels: map[string]string{"le": "1.0"}}, |
| 489 | + {Value: 100, Labels: map[string]string{"le": "+Inf"}}, // 90 in +Inf (90%) |
| 490 | + }, |
| 491 | + }, |
| 492 | + "hist2_bucket": &parser.Metric{ |
| 493 | + Name: "hist2_bucket", |
| 494 | + Type: "histogram", |
| 495 | + Values: []parser.MetricValue{ |
| 496 | + {Value: 10, Labels: map[string]string{"le": "1.0"}}, |
| 497 | + {Value: 15, Labels: map[string]string{"le": "+Inf"}}, // 5 in +Inf (33%) |
| 498 | + }, |
| 499 | + }, |
| 500 | + }, |
| 501 | + wantStatus: map[string]rules.Status{ |
| 502 | + "hist1 (+Inf overflow check)": rules.StatusRed, |
| 503 | + "hist2 (+Inf overflow check)": rules.StatusYellow, |
| 504 | + }, |
| 505 | + wantCount: 2, |
| 506 | + }, |
| 507 | + } |
| 508 | + |
| 509 | + for name, tt := range tests { |
| 510 | + t.Run(name, func(t *testing.T) { |
| 511 | + results := EvaluateHistogramInfOverflow(tt.metrics, rules.LoadLevelMedium) |
| 512 | + |
| 513 | + if len(results) != tt.wantCount { |
| 514 | + t.Errorf("EvaluateHistogramInfOverflow() returned %d results, want %d", len(results), tt.wantCount) |
| 515 | + } |
| 516 | + |
| 517 | + for _, result := range results { |
| 518 | + wantStatus, exists := tt.wantStatus[result.RuleName] |
| 519 | + if !exists { |
| 520 | + t.Errorf("Unexpected result for metric %s", result.RuleName) |
| 521 | + continue |
| 522 | + } |
| 523 | + |
| 524 | + if result.Status != wantStatus { |
| 525 | + t.Errorf("EvaluateHistogramInfOverflow() for %s = %v, want %v", result.RuleName, result.Status, wantStatus) |
| 526 | + } |
| 527 | + |
| 528 | + // Verify message contains expected information |
| 529 | + if result.Status != rules.StatusGreen { |
| 530 | + if result.Message == "" { |
| 531 | + t.Error("Message should not be empty for non-green status") |
| 532 | + } |
| 533 | + if !strings.Contains(result.Message, "Highest non-infinity bucket") { |
| 534 | + t.Error("Message should contain 'Highest non-infinity bucket'") |
| 535 | + } |
| 536 | + if !strings.Contains(result.Message, "didn't expect") { |
| 537 | + t.Error("Message should contain explanation about designer expectations") |
| 538 | + } |
| 539 | + } |
| 540 | + } |
| 541 | + }) |
| 542 | + } |
| 543 | +} |
0 commit comments