|
| 1 | +// Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package reconciler |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + datamodels "github.com/nvidia/nvsentinel/data-models/pkg/model" |
| 21 | + protos "github.com/nvidia/nvsentinel/data-models/pkg/protos" |
| 22 | + config "github.com/nvidia/nvsentinel/health-events-analyzer/pkg/config" |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | +) |
| 26 | + |
| 27 | +// TestGetPipelineStages_AlwaysIncludesAgentFilter verifies that the agent filter |
| 28 | +// is ALWAYS the first stage in the pipeline to prevent the health-events-analyzer |
| 29 | +// from matching its own generated events, which would cause infinite loops and |
| 30 | +// incorrect rule evaluations. |
| 31 | +// |
| 32 | +// This is a regression test for the bug introduced in commit 7be1a34 where the |
| 33 | +// agent filter was accidentally removed during refactoring. |
| 34 | +func TestGetPipelineStages_AlwaysIncludesAgentFilter(t *testing.T) { |
| 35 | + reconciler := &Reconciler{ |
| 36 | + config: HealthEventsAnalyzerReconcilerConfig{}, |
| 37 | + } |
| 38 | + |
| 39 | + testCases := []struct { |
| 40 | + name string |
| 41 | + rule config.HealthEventsAnalyzerRule |
| 42 | + event datamodels.HealthEventWithStatus |
| 43 | + description string |
| 44 | + }{ |
| 45 | + { |
| 46 | + name: "rule with multiple stages", |
| 47 | + rule: config.HealthEventsAnalyzerRule{ |
| 48 | + Name: "multi-stage-rule", |
| 49 | + Stage: []string{ |
| 50 | + `{"$match": {"healthevent.nodename": "this.healthevent.nodename"}}`, |
| 51 | + `{"$count": "total"}`, |
| 52 | + `{"$match": {"total": {"$gte": 5}}}`, |
| 53 | + }, |
| 54 | + }, |
| 55 | + event: datamodels.HealthEventWithStatus{ |
| 56 | + HealthEvent: &protos.HealthEvent{ |
| 57 | + NodeName: "test-node", |
| 58 | + Agent: "gpu-health-monitor", |
| 59 | + }, |
| 60 | + }, |
| 61 | + description: "Multi-stage rule should have agent filter as first stage", |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "rule with single stage", |
| 65 | + rule: config.HealthEventsAnalyzerRule{ |
| 66 | + Name: "single-stage-rule", |
| 67 | + Stage: []string{ |
| 68 | + `{"$match": {"healthevent.isfatal": true}}`, |
| 69 | + }, |
| 70 | + }, |
| 71 | + event: datamodels.HealthEventWithStatus{ |
| 72 | + HealthEvent: &protos.HealthEvent{ |
| 73 | + NodeName: "test-node", |
| 74 | + Agent: "syslog-monitor", |
| 75 | + }, |
| 76 | + }, |
| 77 | + description: "Single-stage rule should have agent filter as first stage", |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "rule with empty stages", |
| 81 | + rule: config.HealthEventsAnalyzerRule{ |
| 82 | + Name: "empty-rule", |
| 83 | + Stage: []string{}, |
| 84 | + }, |
| 85 | + event: datamodels.HealthEventWithStatus{ |
| 86 | + HealthEvent: &protos.HealthEvent{ |
| 87 | + NodeName: "test-node", |
| 88 | + Agent: "gpu-health-monitor", |
| 89 | + }, |
| 90 | + }, |
| 91 | + description: "Even with empty stages, agent filter should be present", |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "rule with complex MongoDB operators", |
| 95 | + rule: config.HealthEventsAnalyzerRule{ |
| 96 | + Name: "complex-operator-rule", |
| 97 | + Stage: []string{ |
| 98 | + `{"$match": {"$expr": {"$gte": ["$healthevent.generatedtimestamp.seconds", {"$subtract": [{"$divide": [{"$toLong": "$$NOW"}, 1000]}, 180]}]}}}`, |
| 99 | + `{"$match": {"healthevent.nodename": "this.healthevent.nodename"}}`, |
| 100 | + }, |
| 101 | + }, |
| 102 | + event: datamodels.HealthEventWithStatus{ |
| 103 | + HealthEvent: &protos.HealthEvent{ |
| 104 | + NodeName: "operator-node", |
| 105 | + Agent: "platform-connector", |
| 106 | + }, |
| 107 | + }, |
| 108 | + description: "Complex operator rule should have agent filter as first stage", |
| 109 | + }, |
| 110 | + } |
| 111 | + |
| 112 | + for _, tc := range testCases { |
| 113 | + t.Run(tc.name, func(t *testing.T) { |
| 114 | + pipeline, err := reconciler.getPipelineStages(tc.rule, tc.event) |
| 115 | + require.NoError(t, err, "getPipelineStages should not return an error") |
| 116 | + |
| 117 | + // Verify pipeline has at least the agent filter stage |
| 118 | + expectedMinLength := 1 + len(tc.rule.Stage) |
| 119 | + assert.GreaterOrEqual(t, len(pipeline), 1, tc.description) |
| 120 | + |
| 121 | + // CRITICAL CHECK: First stage must be the agent filter |
| 122 | + firstStage, ok := pipeline[0].(map[string]interface{}) |
| 123 | + require.True(t, ok, "First stage should be a map") |
| 124 | + |
| 125 | + matchStage, ok := firstStage["$match"].(map[string]interface{}) |
| 126 | + require.True(t, ok, "First stage should be a $match stage") |
| 127 | + |
| 128 | + // Verify the agent filter exists and excludes "health-events-analyzer" |
| 129 | + agentFilter, ok := matchStage["healthevent.agent"].(map[string]interface{}) |
| 130 | + require.True(t, ok, "Agent filter must be present in first $match stage") |
| 131 | + |
| 132 | + excludeValue, ok := agentFilter["$ne"].(string) |
| 133 | + require.True(t, ok, "Agent filter must use $ne operator") |
| 134 | + assert.Equal(t, "health-events-analyzer", excludeValue, |
| 135 | + "Agent filter must exclude 'health-events-analyzer' to prevent infinite loops") |
| 136 | + |
| 137 | + // Verify the configured stages come after the agent filter |
| 138 | + if len(tc.rule.Stage) > 0 { |
| 139 | + assert.Equal(t, expectedMinLength, len(pipeline), |
| 140 | + "Pipeline should have agent filter + configured stages") |
| 141 | + } |
| 142 | + }) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +// TestGetPipelineStages_AgentFilterPreventsInfiniteLoop tests that events |
| 147 | +// generated by health-events-analyzer itself would be excluded by the filter. |
| 148 | +func TestGetPipelineStages_AgentFilterPreventsInfiniteLoop(t *testing.T) { |
| 149 | + reconciler := &Reconciler{ |
| 150 | + config: HealthEventsAnalyzerReconcilerConfig{}, |
| 151 | + } |
| 152 | + |
| 153 | + rule := config.HealthEventsAnalyzerRule{ |
| 154 | + Name: "test-rule", |
| 155 | + Stage: []string{ |
| 156 | + `{"$match": {"healthevent.nodename": "this.healthevent.nodename"}}`, |
| 157 | + }, |
| 158 | + } |
| 159 | + |
| 160 | + // Create an event that was generated BY health-events-analyzer |
| 161 | + // This simulates the case where the analyzer publishes an event that |
| 162 | + // would match its own rules if the agent filter is not present |
| 163 | + eventFromAnalyzer := datamodels.HealthEventWithStatus{ |
| 164 | + HealthEvent: &protos.HealthEvent{ |
| 165 | + NodeName: "test-node", |
| 166 | + Agent: "health-events-analyzer", // This is the critical field |
| 167 | + CheckName: "RepeatedXidError", |
| 168 | + IsFatal: true, |
| 169 | + }, |
| 170 | + } |
| 171 | + |
| 172 | + pipeline, err := reconciler.getPipelineStages(rule, eventFromAnalyzer) |
| 173 | + require.NoError(t, err) |
| 174 | + |
| 175 | + // Extract the agent filter from the first stage |
| 176 | + firstStage := pipeline[0].(map[string]interface{}) |
| 177 | + matchStage := firstStage["$match"].(map[string]interface{}) |
| 178 | + agentFilter := matchStage["healthevent.agent"].(map[string]interface{}) |
| 179 | + excludeValue := agentFilter["$ne"].(string) |
| 180 | + |
| 181 | + // CRITICAL: Verify that the event's agent matches what we're excluding |
| 182 | + assert.Equal(t, eventFromAnalyzer.HealthEvent.Agent, excludeValue, |
| 183 | + "The agent filter must exclude events from health-events-analyzer itself") |
| 184 | + |
| 185 | + // In a real MongoDB aggregation pipeline, this $match stage would filter out |
| 186 | + // any documents where healthevent.agent == "health-events-analyzer" |
| 187 | + // This prevents the analyzer from processing its own generated events |
| 188 | +} |
| 189 | + |
| 190 | +// TestGetPipelineStages_AgentFilterPosition verifies that the agent filter |
| 191 | +// is always the FIRST stage, before any user-configured stages. |
| 192 | +func TestGetPipelineStages_AgentFilterPosition(t *testing.T) { |
| 193 | + reconciler := &Reconciler{ |
| 194 | + config: HealthEventsAnalyzerReconcilerConfig{}, |
| 195 | + } |
| 196 | + |
| 197 | + rule := config.HealthEventsAnalyzerRule{ |
| 198 | + Name: "positioned-rule", |
| 199 | + Stage: []string{ |
| 200 | + `{"$match": {"healthevent.isfatal": true}}`, |
| 201 | + `{"$match": {"healthevent.nodename": "specific-node"}}`, |
| 202 | + `{"$count": "total"}`, |
| 203 | + }, |
| 204 | + } |
| 205 | + |
| 206 | + event := datamodels.HealthEventWithStatus{ |
| 207 | + HealthEvent: &protos.HealthEvent{ |
| 208 | + NodeName: "test-node", |
| 209 | + Agent: "gpu-health-monitor", |
| 210 | + }, |
| 211 | + } |
| 212 | + |
| 213 | + pipeline, err := reconciler.getPipelineStages(rule, event) |
| 214 | + require.NoError(t, err) |
| 215 | + |
| 216 | + // Pipeline should have: 1 agent filter + 3 configured stages = 4 total |
| 217 | + assert.Equal(t, 4, len(pipeline), "Pipeline should have agent filter + 3 configured stages") |
| 218 | + |
| 219 | + // Verify first stage is agent filter |
| 220 | + firstStage := pipeline[0].(map[string]interface{}) |
| 221 | + matchStage := firstStage["$match"].(map[string]interface{}) |
| 222 | + _, hasAgentFilter := matchStage["healthevent.agent"] |
| 223 | + assert.True(t, hasAgentFilter, "First stage must be the agent filter") |
| 224 | + |
| 225 | + // Verify second stage is the first configured stage |
| 226 | + secondStage := pipeline[1].(map[string]interface{}) |
| 227 | + secondMatch := secondStage["$match"].(map[string]interface{}) |
| 228 | + isFatal, hasIsFatal := secondMatch["healthevent.isfatal"] |
| 229 | + assert.True(t, hasIsFatal, "Second stage should be first configured stage") |
| 230 | + assert.Equal(t, true, isFatal, "Second stage should match isfatal: true") |
| 231 | + |
| 232 | + // Verify third stage is the second configured stage |
| 233 | + thirdStage := pipeline[2].(map[string]interface{}) |
| 234 | + thirdMatch := thirdStage["$match"].(map[string]interface{}) |
| 235 | + nodeName, hasNodeName := thirdMatch["healthevent.nodename"] |
| 236 | + assert.True(t, hasNodeName, "Third stage should be second configured stage") |
| 237 | + assert.Equal(t, "specific-node", nodeName, "Third stage should match specific-node") |
| 238 | + |
| 239 | + // Verify fourth stage is the count stage |
| 240 | + fourthStage := pipeline[3].(map[string]interface{}) |
| 241 | + countField, hasCount := fourthStage["$count"] |
| 242 | + assert.True(t, hasCount, "Fourth stage should be the $count stage") |
| 243 | + assert.Equal(t, "total", countField, "Count field should be 'total'") |
| 244 | +} |
0 commit comments