-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathfirst_reconcile_test.go
More file actions
228 lines (188 loc) · 5.52 KB
/
first_reconcile_test.go
File metadata and controls
228 lines (188 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package imagecollector
import (
"context"
"sync"
"testing"
eraserv1 "github.com/eraser-dev/eraser/api/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)
func TestFirstReconcileMutexBasic(t *testing.T) {
// Test that the mutex prevents concurrent execution
var executionCount int
var mu sync.Mutex
// First acquisition should succeed
if !mu.TryLock() {
t.Error("First TryLock should succeed")
}
// Simulate work
executionCount++
// Second acquisition should fail while first is held
if mu.TryLock() {
t.Error("Second TryLock should fail while first is held")
}
// Release first lock
mu.Unlock()
// Now should be able to acquire again
if !mu.TryLock() {
t.Error("TryLock should succeed after release")
}
mu.Unlock()
if executionCount != 1 {
t.Errorf("Expected executionCount 1, got %d", executionCount)
}
}
func TestFirstReconcileDoneFlag(t *testing.T) {
// Test that the done flag prevents subsequent executions
firstReconcileDone := false
// First call should set it to true
if firstReconcileDone {
t.Error("firstReconcileDone should be false initially")
}
// Simulate first reconcile completion
firstReconcileDone = true
// Subsequent calls should check and skip
if !firstReconcileDone {
t.Error("firstReconcileDone should be true after first reconcile")
}
}
func TestReconcileWithRunningJob(t *testing.T) {
// Create a scheme and fake client
scheme := runtime.NewScheme()
if err := eraserv1.AddToScheme(scheme); err != nil {
t.Fatalf("Failed to add scheme: %v", err)
}
// Create a running job
runningJob := &eraserv1.ImageJob{
ObjectMeta: metav1.ObjectMeta{
Name: "test-job-1",
Namespace: "eraser-system",
},
Status: eraserv1.ImageJobStatus{
Phase: eraserv1.PhaseRunning,
},
}
// Create a fake client with the running job
client := fake.NewClientBuilder().
WithScheme(scheme).
WithObjects(runningJob).
Build()
// Test that the reconcile logic would find the running job
jobList := &eraserv1.ImageJobList{}
if err := client.List(context.Background(), jobList); err != nil {
t.Fatalf("Failed to list jobs: %v", err)
}
if len(jobList.Items) != 1 {
t.Fatalf("Expected 1 job, got %d", len(jobList.Items))
}
if jobList.Items[0].Status.Phase != eraserv1.PhaseRunning {
t.Errorf("Expected running job, got %s", jobList.Items[0].Status.Phase)
}
}
func TestReconcileWithNoJobs(t *testing.T) {
// Create a scheme and fake client with no jobs
scheme := runtime.NewScheme()
if err := eraserv1.AddToScheme(scheme); err != nil {
t.Fatalf("Failed to add scheme: %v", err)
}
client := fake.NewClientBuilder().
WithScheme(scheme).
Build()
// Test that the reconcile logic would find no jobs
jobList := &eraserv1.ImageJobList{}
if err := client.List(context.Background(), jobList); err != nil {
t.Fatalf("Failed to list jobs: %v", err)
}
if len(jobList.Items) != 0 {
t.Errorf("Expected 0 jobs, got %d", len(jobList.Items))
}
}
func TestReconcileWithMultipleNonRunningJobs(t *testing.T) {
// Create a scheme and fake client
scheme := runtime.NewScheme()
if err := eraserv1.AddToScheme(scheme); err != nil {
t.Fatalf("Failed to add scheme: %v", err)
}
// Create multiple completed/failed jobs
completedJob := &eraserv1.ImageJob{
ObjectMeta: metav1.ObjectMeta{
Name: "test-job-completed",
Namespace: "eraser-system",
},
Status: eraserv1.ImageJobStatus{
Phase: eraserv1.PhaseCompleted,
},
}
failedJob := &eraserv1.ImageJob{
ObjectMeta: metav1.ObjectMeta{
Name: "test-job-failed",
Namespace: "eraser-system",
},
Status: eraserv1.ImageJobStatus{
Phase: eraserv1.PhaseFailed,
},
}
// Create a fake client with multiple non-running jobs
client := fake.NewClientBuilder().
WithScheme(scheme).
WithObjects(completedJob, failedJob).
Build()
// Test that the reconcile logic would find multiple non-running jobs
jobList := &eraserv1.ImageJobList{}
if err := client.List(context.Background(), jobList); err != nil {
t.Fatalf("Failed to list jobs: %v", err)
}
if len(jobList.Items) != 2 {
t.Fatalf("Expected 2 jobs, got %d", len(jobList.Items))
}
// All should be non-running
for _, job := range jobList.Items {
if job.Status.Phase == eraserv1.PhaseRunning {
t.Errorf("Expected non-running job, got running: %s", job.Name)
}
}
}
func TestNamespacedNameForFirstReconcile(t *testing.T) {
// Test the NamespacedName used for first-reconcile
req := types.NamespacedName{
Name: "first-reconcile",
Namespace: "",
}
if req.Name != "first-reconcile" {
t.Errorf("Expected name 'first-reconcile', got '%s'", req.Name)
}
}
func TestMutexPreventsConcurrentFirstReconcile(t *testing.T) {
// Test that the mutex prevents concurrent execution in a realistic scenario
var (
firstReconcileMutex sync.Mutex
firstReconcileDone bool
executionCount int
)
// Simulate concurrent goroutines trying to run first-reconcile
runFirstReconcile := func() {
// Try to acquire lock
if !firstReconcileMutex.TryLock() {
return // Already running
}
defer firstReconcileMutex.Unlock()
// Check if already done
if firstReconcileDone {
return
}
// Do work
executionCount++
firstReconcileDone = true
}
// Start multiple concurrent calls
for i := 0; i < 10; i++ {
go runFirstReconcile()
}
// Small wait to let goroutines complete
// In real usage, the controller-runtime handles synchronization
if executionCount > 1 {
t.Errorf("Expected at most 1 execution, got %d", executionCount)
}
}