|
5 | 5 | "fmt"
|
6 | 6 | "strings"
|
7 | 7 | "testing"
|
| 8 | + "time" |
8 | 9 |
|
9 | 10 | "github.com/google/go-cmp/cmp"
|
10 | 11 | "github.com/stretchr/testify/assert"
|
@@ -200,3 +201,89 @@ func Test_updateResultHistory(t *testing.T) {
|
200 | 201 | })
|
201 | 202 | }
|
202 | 203 | }
|
| 204 | + |
| 205 | +type fakeStore struct{} |
| 206 | + |
| 207 | +func (f fakeStore) GetStoredResult(_ *corev1.Pod) map[string]string { |
| 208 | + return map[string]string{"foo": "bar"} |
| 209 | +} |
| 210 | +func (f fakeStore) DeleteData(_ corev1.Pod) { /* no-op */ } |
| 211 | + |
| 212 | +func TestResisterResultSavingToInformer_FilterFunc(t *testing.T) { |
| 213 | + t.Parallel() |
| 214 | + |
| 215 | + ctx := context.Background() |
| 216 | + now := metav1.NewTime(time.Now()) |
| 217 | + |
| 218 | + podAlive := &corev1.Pod{ |
| 219 | + ObjectMeta: metav1.ObjectMeta{ |
| 220 | + Name: "pod-alive", |
| 221 | + Namespace: "default", |
| 222 | + }, |
| 223 | + } |
| 224 | + podDeleting := &corev1.Pod{ |
| 225 | + ObjectMeta: metav1.ObjectMeta{ |
| 226 | + Name: "pod-deleting", |
| 227 | + Namespace: "default", |
| 228 | + DeletionTimestamp: &now, |
| 229 | + }, |
| 230 | + } |
| 231 | + |
| 232 | + client := fake.NewSimpleClientset(podAlive, podDeleting) |
| 233 | + |
| 234 | + r, ok := New().(*reflector) |
| 235 | + if !ok { |
| 236 | + t.Fatalf("reflector failed") |
| 237 | + } |
| 238 | + r.AddResultStore(fakeStore{}, "fake") |
| 239 | + |
| 240 | + stopCh := make(chan struct{}) |
| 241 | + defer close(stopCh) |
| 242 | + |
| 243 | + if err := r.ResisterResultSavingToInformer(client, stopCh); err != nil { |
| 244 | + t.Fatalf("register failed: %v", err) |
| 245 | + } |
| 246 | + |
| 247 | + // Update both Pods to trigger the Update event |
| 248 | + patchPods := func(podName string) { |
| 249 | + _ = retry(100*time.Millisecond, 50, func() error { |
| 250 | + pod, err := client.CoreV1().Pods("default").Get(ctx, podName, metav1.GetOptions{}) |
| 251 | + if err != nil { |
| 252 | + return err |
| 253 | + } |
| 254 | + // Modify a label to ensure the update is triggered |
| 255 | + if pod.Labels == nil { |
| 256 | + pod.Labels = map[string]string{} |
| 257 | + } |
| 258 | + pod.Labels["touched"] = "true" |
| 259 | + _, err = client.CoreV1().Pods("default").Update(ctx, pod, metav1.UpdateOptions{}) |
| 260 | + return err |
| 261 | + }) |
| 262 | + } |
| 263 | + patchPods("pod-alive") |
| 264 | + patchPods("pod-deleting") |
| 265 | + |
| 266 | + time.Sleep(500 * time.Millisecond) |
| 267 | + |
| 268 | + // Assert that pod-alive has the annotation written |
| 269 | + pod1, _ := client.CoreV1().Pods("default").Get(ctx, "pod-alive", metav1.GetOptions{}) |
| 270 | + if v := pod1.Annotations["foo"]; v != "bar" { |
| 271 | + t.Fatalf("pod-alive should have annotation foo=bar, got %#v", pod1.Annotations) |
| 272 | + } |
| 273 | + |
| 274 | + // Assert that pod-deleting does NOT have the annotation |
| 275 | + pod2, _ := client.CoreV1().Pods("default").Get(ctx, "pod-deleting", metav1.GetOptions{}) |
| 276 | + if pod2.Annotations != nil && pod2.Annotations["foo"] == "bar" { |
| 277 | + t.Fatalf("pod-deleting should NOT have annotation foo=bar, but it has: %#v", pod2.Annotations) |
| 278 | + } |
| 279 | +} |
| 280 | + |
| 281 | +func retry(interval time.Duration, maxTry int, f func() error) (err error) { |
| 282 | + for i := 0; i < maxTry; i++ { |
| 283 | + if err = f(); err == nil { |
| 284 | + return nil |
| 285 | + } |
| 286 | + time.Sleep(interval) |
| 287 | + } |
| 288 | + return |
| 289 | +} |
0 commit comments