|
5 | 5 | "testing"
|
6 | 6 | "time"
|
7 | 7 |
|
| 8 | + "github.com/google/go-cmp/cmp" |
| 9 | + "google.golang.org/protobuf/testing/protocmp" |
| 10 | + |
8 | 11 | "github.com/armadaproject/armada-operator/internal/controller/builders"
|
9 | 12 |
|
10 | 13 | "k8s.io/utils/ptr"
|
@@ -344,6 +347,155 @@ func TestLookoutReconciler_CreateCronJobErrorDueToApplicationConfig(t *testing.T
|
344 | 347 | assert.Equal(t, "yaml: line 1: did not find expected ',' or '}'", err.Error())
|
345 | 348 | }
|
346 | 349 |
|
| 350 | +func TestLookoutReconciler_CreateCronJob(t *testing.T) { |
| 351 | + t.Parallel() |
| 352 | + |
| 353 | + dbPruningEnabled := true |
| 354 | + dbPruningSchedule := "1d" |
| 355 | + |
| 356 | + lookout := v1alpha1.Lookout{ |
| 357 | + TypeMeta: metav1.TypeMeta{ |
| 358 | + Kind: "Lookout", |
| 359 | + APIVersion: "install.armadaproject.io/v1alpha1", |
| 360 | + }, |
| 361 | + ObjectMeta: metav1.ObjectMeta{ |
| 362 | + Namespace: "default", |
| 363 | + Name: "lookout", |
| 364 | + DeletionTimestamp: &metav1.Time{Time: time.Now()}, |
| 365 | + Finalizers: []string{operatorFinalizer}, |
| 366 | + }, |
| 367 | + Spec: v1alpha1.LookoutSpec{ |
| 368 | + CommonSpecBase: installv1alpha1.CommonSpecBase{ |
| 369 | + Labels: nil, |
| 370 | + Image: v1alpha1.Image{ |
| 371 | + Repository: "testrepo", |
| 372 | + Tag: "1.0.0", |
| 373 | + }, |
| 374 | + ApplicationConfig: runtime.RawExtension{Raw: []byte(`{}`)}, |
| 375 | + Resources: &corev1.ResourceRequirements{}, |
| 376 | + }, |
| 377 | + Replicas: ptr.To[int32](2), |
| 378 | + ClusterIssuer: "test", |
| 379 | + Ingress: &v1alpha1.IngressConfig{ |
| 380 | + IngressClass: "nginx", |
| 381 | + }, |
| 382 | + DbPruningEnabled: &dbPruningEnabled, |
| 383 | + DbPruningSchedule: &dbPruningSchedule, |
| 384 | + }, |
| 385 | + } |
| 386 | + cronJob, err := createLookoutCronJob(&lookout, "lookout") |
| 387 | + assert.NoError(t, err) |
| 388 | + |
| 389 | + var expectedParallelism int32 = 1 |
| 390 | + var expectedCompletions int32 = 1 |
| 391 | + var expectedBackoffLimit int32 = 0 |
| 392 | + var expectedTerminationGracePeriodSeconds int64 = 0 |
| 393 | + |
| 394 | + expectedCronJob := &batchv1.CronJob{ |
| 395 | + ObjectMeta: metav1.ObjectMeta{ |
| 396 | + Name: "lookout-db-pruner", |
| 397 | + Namespace: "default", |
| 398 | + Labels: map[string]string{ |
| 399 | + "app": "lookout-db-pruner", |
| 400 | + "release": "lookout-db-pruner", |
| 401 | + }, |
| 402 | + Annotations: map[string]string{ |
| 403 | + "checksum/config": "44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a", |
| 404 | + }, |
| 405 | + }, |
| 406 | + Spec: batchv1.CronJobSpec{ |
| 407 | + JobTemplate: batchv1.JobTemplateSpec{ |
| 408 | + ObjectMeta: metav1.ObjectMeta{ |
| 409 | + Name: "lookout-db-pruner", |
| 410 | + Namespace: "default", |
| 411 | + Labels: map[string]string{ |
| 412 | + "app": "lookout-db-pruner", |
| 413 | + "release": "lookout-db-pruner", |
| 414 | + }, |
| 415 | + }, |
| 416 | + Spec: batchv1.JobSpec{ |
| 417 | + Parallelism: &expectedParallelism, |
| 418 | + Completions: &expectedCompletions, |
| 419 | + BackoffLimit: &expectedBackoffLimit, |
| 420 | + Template: corev1.PodTemplateSpec{ |
| 421 | + ObjectMeta: metav1.ObjectMeta{ |
| 422 | + Name: "lookout-db-pruner", |
| 423 | + Namespace: "default", |
| 424 | + Labels: map[string]string{ |
| 425 | + "app": "lookout-db-pruner", |
| 426 | + "release": "lookout-db-pruner", |
| 427 | + }, |
| 428 | + }, |
| 429 | + Spec: corev1.PodSpec{ |
| 430 | + Containers: []corev1.Container{ |
| 431 | + { |
| 432 | + Args: []string{ |
| 433 | + "--pruneDatabase", |
| 434 | + "--config", |
| 435 | + "/config/application_config.yaml", |
| 436 | + }, |
| 437 | + Image: "testrepo:1.0.0", |
| 438 | + ImagePullPolicy: "IfNotPresent", |
| 439 | + Name: "lookout-db-pruner", |
| 440 | + VolumeMounts: []corev1.VolumeMount{ |
| 441 | + { |
| 442 | + Name: "user-config", |
| 443 | + ReadOnly: true, |
| 444 | + MountPath: appConfigFilepath, |
| 445 | + SubPath: "lookout-config.yaml", |
| 446 | + }, |
| 447 | + }, |
| 448 | + }, |
| 449 | + }, |
| 450 | + InitContainers: []corev1.Container{ |
| 451 | + { |
| 452 | + Name: "lookout-db-pruner-db-wait", |
| 453 | + Image: "alpine:3.10", |
| 454 | + Command: []string{ |
| 455 | + "/bin/sh", |
| 456 | + "-c", |
| 457 | + `echo "Waiting for Postres..." |
| 458 | + while ! nc -z $PGHOST $PGPORT; do |
| 459 | + sleep 1 |
| 460 | + done |
| 461 | + echo "Postres started!"`, |
| 462 | + }, |
| 463 | + Env: []corev1.EnvVar{ |
| 464 | + { |
| 465 | + Name: "PGHOST", |
| 466 | + }, |
| 467 | + { |
| 468 | + Name: "PGPORT", |
| 469 | + }, |
| 470 | + }, |
| 471 | + }, |
| 472 | + }, |
| 473 | + RestartPolicy: "Never", |
| 474 | + ServiceAccountName: "lookout", |
| 475 | + TerminationGracePeriodSeconds: &expectedTerminationGracePeriodSeconds, |
| 476 | + Volumes: []corev1.Volume{ |
| 477 | + { |
| 478 | + Name: "user-config", |
| 479 | + VolumeSource: corev1.VolumeSource{ |
| 480 | + Secret: &corev1.SecretVolumeSource{ |
| 481 | + SecretName: "lookout", |
| 482 | + }, |
| 483 | + }, |
| 484 | + }, |
| 485 | + }, |
| 486 | + }, |
| 487 | + }, |
| 488 | + }, |
| 489 | + }, |
| 490 | + Schedule: "1d", |
| 491 | + }, |
| 492 | + } |
| 493 | + |
| 494 | + if !cmp.Equal(expectedCronJob, cronJob, protocmp.Transform()) { |
| 495 | + t.Fatalf("cronjob is not the same %s", cmp.Diff(expectedCronJob, cronJob, protocmp.Transform())) |
| 496 | + } |
| 497 | +} |
| 498 | + |
347 | 499 | func TestLookoutReconciler_ReconcileDeletingLookout(t *testing.T) {
|
348 | 500 | t.Parallel()
|
349 | 501 |
|
|
0 commit comments