|
9 | 9 | "github.com/stretchr/testify/require" |
10 | 10 | corev1 "k8s.io/api/core/v1" |
11 | 11 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/client" |
12 | 13 | clientFake "sigs.k8s.io/controller-runtime/pkg/client/fake" |
13 | 14 | ) |
14 | 15 |
|
@@ -166,3 +167,111 @@ func getTestPod() []corev1.Pod { |
166 | 167 | }, |
167 | 168 | } |
168 | 169 | } |
| 170 | + |
| 171 | +func TestIsPodScaled(t *testing.T) { |
| 172 | + ctx := context.Background() |
| 173 | + tests := []struct { |
| 174 | + action ScaleAction |
| 175 | + expectedResult bool |
| 176 | + name string |
| 177 | + setupFunc func(client.Client, *corev1.Pod) |
| 178 | + }{ |
| 179 | + { |
| 180 | + name: "Create action - pod exists", |
| 181 | + action: Create, |
| 182 | + expectedResult: true, |
| 183 | + setupFunc: func(client client.Client, pod *corev1.Pod) { |
| 184 | + err := client.Create(ctx, pod) |
| 185 | + require.NoError(t, err) |
| 186 | + }, |
| 187 | + }, |
| 188 | + { |
| 189 | + name: "Create action - pod does not exist", |
| 190 | + action: Create, |
| 191 | + expectedResult: false, |
| 192 | + setupFunc: func(_ client.Client, _ *corev1.Pod) {}, |
| 193 | + }, |
| 194 | + { |
| 195 | + name: "Delete action - pod exists", |
| 196 | + action: Delete, |
| 197 | + expectedResult: false, |
| 198 | + setupFunc: func(client client.Client, pod *corev1.Pod) { |
| 199 | + err := client.Create(ctx, pod) |
| 200 | + require.NoError(t, err) |
| 201 | + }, |
| 202 | + }, |
| 203 | + { |
| 204 | + name: "Delete action - pod does not exist", |
| 205 | + action: Delete, |
| 206 | + expectedResult: true, |
| 207 | + setupFunc: func(_ client.Client, _ *corev1.Pod) {}, |
| 208 | + }, |
| 209 | + } |
| 210 | + |
| 211 | + for _, tt := range tests { |
| 212 | + t.Run(tt.name, func(t *testing.T) { |
| 213 | + fakeClient := clientFake.NewClientBuilder().WithRuntimeObjects().Build() |
| 214 | + exp := &rayClusterScaleExpectationImpl{ |
| 215 | + Client: fakeClient, |
| 216 | + itemsCache: nil, // Not used in isPodScaled |
| 217 | + } |
| 218 | + |
| 219 | + testPod := &corev1.Pod{ |
| 220 | + ObjectMeta: metav1.ObjectMeta{ |
| 221 | + Name: "test-pod", |
| 222 | + Namespace: "default", |
| 223 | + }, |
| 224 | + } |
| 225 | + |
| 226 | + rp := &rayPod{ |
| 227 | + name: testPod.Name, |
| 228 | + namespace: testPod.Namespace, |
| 229 | + action: tt.action, |
| 230 | + recordTimestamp: time.Now(), |
| 231 | + } |
| 232 | + |
| 233 | + tt.setupFunc(fakeClient, testPod) |
| 234 | + |
| 235 | + result := exp.isPodScaled(ctx, rp, testPod.Namespace) |
| 236 | + assert.Equal(t, tt.expectedResult, result) |
| 237 | + }) |
| 238 | + } |
| 239 | +} |
| 240 | + |
| 241 | +func TestIsPodScaledTimeout(t *testing.T) { |
| 242 | + ctx := context.Background() |
| 243 | + |
| 244 | + // Save original timeout and restore after test |
| 245 | + originalTimeout := ExpectationsTimeout |
| 246 | + ExpectationsTimeout = 20 * time.Millisecond |
| 247 | + defer func() { ExpectationsTimeout = originalTimeout }() |
| 248 | + |
| 249 | + fakeClient := clientFake.NewClientBuilder().WithRuntimeObjects().Build() |
| 250 | + exp := &rayClusterScaleExpectationImpl{ |
| 251 | + Client: fakeClient, |
| 252 | + itemsCache: nil, |
| 253 | + } |
| 254 | + |
| 255 | + testPod := &corev1.Pod{ |
| 256 | + ObjectMeta: metav1.ObjectMeta{ |
| 257 | + Name: "test-pod", |
| 258 | + Namespace: "default", |
| 259 | + }, |
| 260 | + } |
| 261 | + |
| 262 | + rp := &rayPod{ |
| 263 | + name: testPod.Name, |
| 264 | + namespace: testPod.Namespace, |
| 265 | + action: Create, |
| 266 | + recordTimestamp: time.Now(), |
| 267 | + } |
| 268 | + |
| 269 | + // Initially should return false (pod doesn't exist) |
| 270 | + result := exp.isPodScaled(ctx, rp, testPod.Namespace) |
| 271 | + assert.False(t, result) |
| 272 | + |
| 273 | + // After timeout, should return true even though pod doesn't exist |
| 274 | + time.Sleep(ExpectationsTimeout + 10*time.Millisecond) |
| 275 | + result = exp.isPodScaled(ctx, rp, testPod.Namespace) |
| 276 | + assert.True(t, result) |
| 277 | +} |
0 commit comments