|
| 1 | +package operatorstateanalyzer |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/openshift/origin/pkg/monitor/monitorapi" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestOperatorStateAnalyzer(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + intervals monitorapi.Intervals |
| 16 | + expectedMetrics map[string]*OperatorStateMetrics |
| 17 | + expectedRows []map[string]string |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "single operator, progressing and degraded", |
| 21 | + intervals: monitorapi.Intervals{ |
| 22 | + makeTestInterval("operator-a", "Progressing", 10), |
| 23 | + makeTestInterval("operator-a", "Progressing", 5), |
| 24 | + makeTestInterval("operator-a", "Degraded", 15), |
| 25 | + }, |
| 26 | + expectedMetrics: map[string]*OperatorStateMetrics{ |
| 27 | + "operator-a": { |
| 28 | + OperatorName: "operator-a", |
| 29 | + ProgressingCount: 2, |
| 30 | + TotalProgressingSeconds: 15, |
| 31 | + ProgressingDurations: []float64{10, 5}, |
| 32 | + DegradedCount: 1, |
| 33 | + TotalDegradedSeconds: 15, |
| 34 | + DegradedDurations: []float64{15}, |
| 35 | + }, |
| 36 | + }, |
| 37 | + expectedRows: []map[string]string{ |
| 38 | + {"Operator": "operator-a", "State": "Progressing", "Metric": "Count", "Value": "2.000000"}, |
| 39 | + {"Operator": "operator-a", "State": "Progressing", "Metric": "TotalSeconds", "Value": "15.000000"}, |
| 40 | + {"Operator": "operator-a", "State": "Progressing", "Metric": "IndividualDurationSeconds", "Value": "10.000000"}, |
| 41 | + {"Operator": "operator-a", "State": "Progressing", "Metric": "IndividualDurationSeconds", "Value": "5.000000"}, |
| 42 | + {"Operator": "operator-a", "State": "Degraded", "Metric": "Count", "Value": "1.000000"}, |
| 43 | + {"Operator": "operator-a", "State": "Degraded", "Metric": "TotalSeconds", "Value": "15.000000"}, |
| 44 | + {"Operator": "operator-a", "State": "Degraded", "Metric": "IndividualDurationSeconds", "Value": "15.000000"}, |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "multiple operators", |
| 49 | + intervals: monitorapi.Intervals{ |
| 50 | + makeTestInterval("operator-a", "Progressing", 10), |
| 51 | + makeTestInterval("operator-b", "Degraded", 20), |
| 52 | + }, |
| 53 | + expectedMetrics: map[string]*OperatorStateMetrics{ |
| 54 | + "operator-a": { |
| 55 | + OperatorName: "operator-a", |
| 56 | + ProgressingCount: 1, |
| 57 | + TotalProgressingSeconds: 10, |
| 58 | + ProgressingDurations: []float64{10}, |
| 59 | + }, |
| 60 | + "operator-b": { |
| 61 | + OperatorName: "operator-b", |
| 62 | + DegradedCount: 1, |
| 63 | + TotalDegradedSeconds: 20, |
| 64 | + DegradedDurations: []float64{20}, |
| 65 | + }, |
| 66 | + }, |
| 67 | + expectedRows: []map[string]string{ |
| 68 | + {"Operator": "operator-a", "State": "Progressing", "Metric": "Count", "Value": "1.000000"}, |
| 69 | + {"Operator": "operator-a", "State": "Progressing", "Metric": "TotalSeconds", "Value": "10.000000"}, |
| 70 | + {"Operator": "operator-a", "State": "Progressing", "Metric": "IndividualDurationSeconds", "Value": "10.000000"}, |
| 71 | + {"Operator": "operator-b", "State": "Degraded", "Metric": "Count", "Value": "1.000000"}, |
| 72 | + {"Operator": "operator-b", "State": "Degraded", "Metric": "TotalSeconds", "Value": "20.000000"}, |
| 73 | + {"Operator": "operator-b", "State": "Degraded", "Metric": "IndividualDurationSeconds", "Value": "20.000000"}, |
| 74 | + }, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "no relevant intervals", |
| 78 | + intervals: monitorapi.Intervals{}, |
| 79 | + expectedMetrics: map[string]*OperatorStateMetrics{}, |
| 80 | + expectedRows: []map[string]string{}, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "operator with only degraded state", |
| 84 | + intervals: monitorapi.Intervals{ |
| 85 | + makeTestInterval("operator-c", "Degraded", 30), |
| 86 | + }, |
| 87 | + expectedMetrics: map[string]*OperatorStateMetrics{ |
| 88 | + "operator-c": { |
| 89 | + OperatorName: "operator-c", |
| 90 | + DegradedCount: 1, |
| 91 | + TotalDegradedSeconds: 30, |
| 92 | + DegradedDurations: []float64{30}, |
| 93 | + }, |
| 94 | + }, |
| 95 | + expectedRows: []map[string]string{ |
| 96 | + {"Operator": "operator-c", "State": "Degraded", "Metric": "Count", "Value": "1.000000"}, |
| 97 | + {"Operator": "operator-c", "State": "Degraded", "Metric": "TotalSeconds", "Value": "30.000000"}, |
| 98 | + {"Operator": "operator-c", "State": "Degraded", "Metric": "IndividualDurationSeconds", "Value": "30.000000"}, |
| 99 | + }, |
| 100 | + }, |
| 101 | + } |
| 102 | + |
| 103 | + for _, tc := range tests { |
| 104 | + t.Run(tc.name, func(t *testing.T) { |
| 105 | + // Test calculateOperatorStateMetrics |
| 106 | + metrics := calculateOperatorStateMetrics(tc.intervals) |
| 107 | + require.Equal(t, len(tc.expectedMetrics), len(metrics), "number of operators should match") |
| 108 | + for op, expected := range tc.expectedMetrics { |
| 109 | + actual, ok := metrics[op] |
| 110 | + require.True(t, ok, "operator %s not found in metrics", op) |
| 111 | + assert.Equal(t, expected.OperatorName, actual.OperatorName, "OperatorName should match") |
| 112 | + assert.Equal(t, expected.ProgressingCount, actual.ProgressingCount, "ProgressingCount should match") |
| 113 | + assert.InDelta(t, expected.TotalProgressingSeconds, actual.TotalProgressingSeconds, 0.001, "TotalProgressingSeconds should match") |
| 114 | + assert.ElementsMatch(t, expected.ProgressingDurations, actual.ProgressingDurations, "ProgressingDurations should match") |
| 115 | + assert.Equal(t, expected.DegradedCount, actual.DegradedCount, "DegradedCount should match") |
| 116 | + assert.InDelta(t, expected.TotalDegradedSeconds, actual.TotalDegradedSeconds, 0.001, "TotalDegradedSeconds should match") |
| 117 | + assert.ElementsMatch(t, expected.DegradedDurations, actual.DegradedDurations, "DegradedDurations should match") |
| 118 | + } |
| 119 | + |
| 120 | + // Test generateRowsFromMetrics |
| 121 | + rows := generateRowsFromMetrics(metrics) |
| 122 | + assert.ElementsMatch(t, tc.expectedRows, rows, "generated rows should match expected rows") |
| 123 | + }) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// Helper function to create intervals for testing |
| 128 | +func makeTestInterval(operatorName, condition string, durationSeconds float64) monitorapi.Interval { |
| 129 | + from := time.Unix(1, 0) |
| 130 | + to := from.Add(time.Duration(durationSeconds * float64(time.Second))) |
| 131 | + return monitorapi.Interval{ |
| 132 | + Source: monitorapi.SourceOperatorState, |
| 133 | + Condition: monitorapi.Condition{ |
| 134 | + Locator: monitorapi.Locator{ |
| 135 | + Type: monitorapi.LocatorTypeClusterOperator, |
| 136 | + Keys: map[monitorapi.LocatorKey]string{ |
| 137 | + monitorapi.LocatorClusterOperatorKey: operatorName, |
| 138 | + }, |
| 139 | + }, |
| 140 | + Message: monitorapi.Message{ |
| 141 | + Annotations: map[monitorapi.AnnotationKey]string{ |
| 142 | + monitorapi.AnnotationCondition: condition, |
| 143 | + }, |
| 144 | + }, |
| 145 | + }, |
| 146 | + From: from, |
| 147 | + To: to, |
| 148 | + } |
| 149 | +} |
0 commit comments