|
| 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 annotation |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | + corev1 "k8s.io/api/core/v1" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 26 | +) |
| 27 | + |
| 28 | +func TestRetryCountIncrementsOnUpdate(t *testing.T) { |
| 29 | + ctx := context.Background() |
| 30 | + nodeName := "test-node" |
| 31 | + groupName := "test-group" |
| 32 | + |
| 33 | + node := &corev1.Node{ |
| 34 | + ObjectMeta: metav1.ObjectMeta{ |
| 35 | + Name: nodeName, |
| 36 | + Annotations: map[string]string{}, |
| 37 | + }, |
| 38 | + } |
| 39 | + |
| 40 | + client := fake.NewClientBuilder().WithObjects(node).Build() |
| 41 | + annotationManager := NodeAnnotationManager{client: client} |
| 42 | + |
| 43 | + // First update - retry count should be 0 |
| 44 | + err := annotationManager.UpdateRemediationState(ctx, nodeName, groupName, "cr-1", "RESTART_BM") |
| 45 | + require.NoError(t, err) |
| 46 | + |
| 47 | + state, _, err := annotationManager.GetRemediationState(ctx, nodeName) |
| 48 | + require.NoError(t, err) |
| 49 | + assert.Equal(t, 0, state.EquivalenceGroups[groupName].RetryCount, "First attempt should have retry count 0") |
| 50 | + |
| 51 | + // Second update - retry count should be 1 |
| 52 | + err = annotationManager.UpdateRemediationState(ctx, nodeName, groupName, "cr-2", "RESTART_BM") |
| 53 | + require.NoError(t, err) |
| 54 | + |
| 55 | + state, _, err = annotationManager.GetRemediationState(ctx, nodeName) |
| 56 | + require.NoError(t, err) |
| 57 | + assert.Equal(t, 1, state.EquivalenceGroups[groupName].RetryCount, "Second attempt should have retry count 1") |
| 58 | + |
| 59 | + // Third update - retry count should be 2 |
| 60 | + err = annotationManager.UpdateRemediationState(ctx, nodeName, groupName, "cr-3", "RESTART_BM") |
| 61 | + require.NoError(t, err) |
| 62 | + |
| 63 | + state, _, err = annotationManager.GetRemediationState(ctx, nodeName) |
| 64 | + require.NoError(t, err) |
| 65 | + assert.Equal(t, 2, state.EquivalenceGroups[groupName].RetryCount, "Third attempt should have retry count 2") |
| 66 | +} |
| 67 | + |
| 68 | +func TestRetryCountIndependentPerGroup(t *testing.T) { |
| 69 | + ctx := context.Background() |
| 70 | + nodeName := "test-node" |
| 71 | + |
| 72 | + node := &corev1.Node{ |
| 73 | + ObjectMeta: metav1.ObjectMeta{ |
| 74 | + Name: nodeName, |
| 75 | + Annotations: map[string]string{}, |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + client := fake.NewClientBuilder().WithObjects(node).Build() |
| 80 | + annotationManager := NodeAnnotationManager{client: client} |
| 81 | + |
| 82 | + // Create group-1 twice |
| 83 | + err := annotationManager.UpdateRemediationState(ctx, nodeName, "group-1", "cr-1", "RESTART_BM") |
| 84 | + require.NoError(t, err) |
| 85 | + err = annotationManager.UpdateRemediationState(ctx, nodeName, "group-1", "cr-2", "RESTART_BM") |
| 86 | + require.NoError(t, err) |
| 87 | + |
| 88 | + // Create group-2 once |
| 89 | + err = annotationManager.UpdateRemediationState(ctx, nodeName, "group-2", "cr-3", "COMPONENT_RESET") |
| 90 | + require.NoError(t, err) |
| 91 | + |
| 92 | + state, _, err := annotationManager.GetRemediationState(ctx, nodeName) |
| 93 | + require.NoError(t, err) |
| 94 | + |
| 95 | + assert.Equal(t, 1, state.EquivalenceGroups["group-1"].RetryCount, "group-1 should have retry count 1") |
| 96 | + assert.Equal(t, 0, state.EquivalenceGroups["group-2"].RetryCount, "group-2 should have retry count 0") |
| 97 | +} |
| 98 | + |
| 99 | +func TestRetryCountPersistsAcrossPodRestarts(t *testing.T) { |
| 100 | + ctx := context.Background() |
| 101 | + nodeName := "test-node" |
| 102 | + groupName := "test-group" |
| 103 | + |
| 104 | + node := &corev1.Node{ |
| 105 | + ObjectMeta: metav1.ObjectMeta{ |
| 106 | + Name: nodeName, |
| 107 | + Annotations: map[string]string{}, |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + client := fake.NewClientBuilder().WithObjects(node).Build() |
| 112 | + |
| 113 | + // Simulate first pod session |
| 114 | + manager1 := NodeAnnotationManager{client: client} |
| 115 | + err := manager1.UpdateRemediationState(ctx, nodeName, groupName, "cr-1", "RESTART_BM") |
| 116 | + require.NoError(t, err) |
| 117 | + |
| 118 | + // Simulate pod restart - create new annotation manager instance |
| 119 | + manager2 := NodeAnnotationManager{client: client} |
| 120 | + err = manager2.UpdateRemediationState(ctx, nodeName, groupName, "cr-2", "RESTART_BM") |
| 121 | + require.NoError(t, err) |
| 122 | + |
| 123 | + // Verify retry count persisted |
| 124 | + state, _, err := manager2.GetRemediationState(ctx, nodeName) |
| 125 | + require.NoError(t, err) |
| 126 | + assert.Equal(t, 1, state.EquivalenceGroups[groupName].RetryCount, |
| 127 | + "Retry count should persist across pod restarts") |
| 128 | +} |
0 commit comments