|
21 | 21 | package k8sutil |
22 | 22 |
|
23 | 23 | import ( |
| 24 | + "context" |
24 | 25 | "testing" |
25 | 26 |
|
26 | 27 | "github.com/stretchr/testify/assert" |
27 | 28 | "github.com/stretchr/testify/require" |
28 | 29 | core "k8s.io/api/core/v1" |
| 30 | + meta "k8s.io/apimachinery/pkg/apis/meta/v1" |
29 | 31 |
|
30 | 32 | "github.com/arangodb/kube-arangodb/pkg/handlers/utils" |
| 33 | + "github.com/arangodb/kube-arangodb/pkg/util/constants" |
| 34 | + "github.com/arangodb/kube-arangodb/pkg/util/kclient" |
31 | 35 | ) |
32 | 36 |
|
33 | 37 | // TestIsPodReady tests IsPodReady. |
@@ -330,3 +334,85 @@ func Test_extractContainerNamesFromConditionMessage(t *testing.T) { |
330 | 334 | require.NotContains(t, c, "sidecar") |
331 | 335 | }) |
332 | 336 | } |
| 337 | + |
| 338 | +func Test_EnsureFinalizer(t *testing.T) { |
| 339 | + c := kclient.NewFakeClient() |
| 340 | + |
| 341 | + f := "test.arangodb.com/test" |
| 342 | + |
| 343 | + pod := &core.Pod{ |
| 344 | + ObjectMeta: meta.ObjectMeta{ |
| 345 | + Name: "test", |
| 346 | + Namespace: "test", |
| 347 | + |
| 348 | + Finalizers: nil, |
| 349 | + }, |
| 350 | + } |
| 351 | + refresh := func(t *testing.T) { |
| 352 | + p, err := c.Kubernetes().CoreV1().Pods(pod.GetNamespace()).Get(context.Background(), pod.GetName(), meta.GetOptions{}) |
| 353 | + require.NoError(t, err) |
| 354 | + pod = p |
| 355 | + } |
| 356 | + |
| 357 | + _, err := c.Kubernetes().CoreV1().Pods(pod.GetNamespace()).Create(context.Background(), pod, meta.CreateOptions{}) |
| 358 | + require.NoError(t, err) |
| 359 | + |
| 360 | + t.Run("Ensure finalizers", func(t *testing.T) { |
| 361 | + require.NoError(t, EnsureFinalizerPresent(context.Background(), c.Kubernetes().CoreV1().Pods(pod.GetNamespace()), pod, constants.FinalizerPodGracefulShutdown)) |
| 362 | + |
| 363 | + refresh(t) |
| 364 | + |
| 365 | + require.Len(t, pod.Finalizers, 1) |
| 366 | + require.Contains(t, pod.Finalizers, constants.FinalizerPodGracefulShutdown) |
| 367 | + }) |
| 368 | + |
| 369 | + t.Run("Add finalizers", func(t *testing.T) { |
| 370 | + require.NoError(t, EnsureFinalizerPresent(context.Background(), c.Kubernetes().CoreV1().Pods(pod.GetNamespace()), pod, f)) |
| 371 | + |
| 372 | + refresh(t) |
| 373 | + |
| 374 | + require.Len(t, pod.Finalizers, 2) |
| 375 | + require.Contains(t, pod.Finalizers, constants.FinalizerPodGracefulShutdown) |
| 376 | + require.Contains(t, pod.Finalizers, f) |
| 377 | + }) |
| 378 | + |
| 379 | + t.Run("Re Add finalizers", func(t *testing.T) { |
| 380 | + require.NoError(t, EnsureFinalizerPresent(context.Background(), c.Kubernetes().CoreV1().Pods(pod.GetNamespace()), pod, f)) |
| 381 | + |
| 382 | + refresh(t) |
| 383 | + |
| 384 | + require.Len(t, pod.Finalizers, 2) |
| 385 | + require.Contains(t, pod.Finalizers, constants.FinalizerPodGracefulShutdown) |
| 386 | + require.Contains(t, pod.Finalizers, f) |
| 387 | + }) |
| 388 | + |
| 389 | + t.Run("Remove finalizers", func(t *testing.T) { |
| 390 | + require.NoError(t, EnsureFinalizerAbsent(context.Background(), c.Kubernetes().CoreV1().Pods(pod.GetNamespace()), pod, f)) |
| 391 | + |
| 392 | + refresh(t) |
| 393 | + |
| 394 | + require.Len(t, pod.Finalizers, 1) |
| 395 | + require.Contains(t, pod.Finalizers, constants.FinalizerPodGracefulShutdown) |
| 396 | + require.NotContains(t, pod.Finalizers, f) |
| 397 | + }) |
| 398 | + |
| 399 | + t.Run("Re - remove finalizers", func(t *testing.T) { |
| 400 | + require.NoError(t, EnsureFinalizerAbsent(context.Background(), c.Kubernetes().CoreV1().Pods(pod.GetNamespace()), pod, f)) |
| 401 | + |
| 402 | + refresh(t) |
| 403 | + |
| 404 | + require.Len(t, pod.Finalizers, 1) |
| 405 | + require.Contains(t, pod.Finalizers, constants.FinalizerPodGracefulShutdown) |
| 406 | + require.NotContains(t, pod.Finalizers, f) |
| 407 | + }) |
| 408 | + |
| 409 | + t.Run("Remove final finalizers", func(t *testing.T) { |
| 410 | + require.NoError(t, EnsureFinalizerAbsent(context.Background(), c.Kubernetes().CoreV1().Pods(pod.GetNamespace()), pod, constants.FinalizerPodGracefulShutdown)) |
| 411 | + |
| 412 | + refresh(t) |
| 413 | + |
| 414 | + require.Len(t, pod.Finalizers, 0) |
| 415 | + require.NotContains(t, pod.Finalizers, constants.FinalizerPodGracefulShutdown) |
| 416 | + require.NotContains(t, pod.Finalizers, f) |
| 417 | + }) |
| 418 | +} |
0 commit comments