|
5 | 5 | "testing" |
6 | 6 |
|
7 | 7 | cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1" |
| 8 | + "github.com/cozystack/cozystack/pkg/config" |
8 | 9 | helmv2 "github.com/fluxcd/helm-controller/api/v2" |
9 | 10 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
10 | 11 | "k8s.io/apimachinery/pkg/runtime" |
@@ -352,3 +353,172 @@ func TestValuesFromEqual(t *testing.T) { |
352 | 353 | }) |
353 | 354 | } |
354 | 355 | } |
| 356 | + |
| 357 | +// vmInstanceRelease builds a VMInstance HelmRelease carrying the application |
| 358 | +// labels this reconciler selects on, with the given wait configuration. |
| 359 | +func vmInstanceRelease(install, upgrade *bool) *helmv2.HelmRelease { |
| 360 | + hr := &helmv2.HelmRelease{ |
| 361 | + ObjectMeta: metav1.ObjectMeta{ |
| 362 | + Name: "vm-instance-web", |
| 363 | + Namespace: "tenant-foo", |
| 364 | + Labels: map[string]string{ |
| 365 | + "apps.cozystack.io/application.kind": "VMInstance", |
| 366 | + "apps.cozystack.io/application.group": "apps.cozystack.io", |
| 367 | + }, |
| 368 | + }, |
| 369 | + Spec: helmv2.HelmReleaseSpec{ |
| 370 | + ChartRef: &helmv2.CrossNamespaceSourceReference{ |
| 371 | + Kind: "ExternalArtifact", |
| 372 | + Name: "vm-instance-app", |
| 373 | + Namespace: "cozy-system", |
| 374 | + }, |
| 375 | + }, |
| 376 | + } |
| 377 | + if install != nil { |
| 378 | + hr.Spec.Install = &helmv2.Install{DisableWait: *install} |
| 379 | + } |
| 380 | + if upgrade != nil { |
| 381 | + hr.Spec.Upgrade = &helmv2.Upgrade{DisableWait: *upgrade} |
| 382 | + } |
| 383 | + return hr |
| 384 | +} |
| 385 | + |
| 386 | +// vmInstanceAppDef builds the VMInstance ApplicationDefinition, optionally |
| 387 | +// carrying the disable-wait annotation. |
| 388 | +func vmInstanceAppDef(annotations map[string]string) *cozyv1alpha1.ApplicationDefinition { |
| 389 | + return &cozyv1alpha1.ApplicationDefinition{ |
| 390 | + ObjectMeta: metav1.ObjectMeta{Name: "vm-instance", Annotations: annotations}, |
| 391 | + Spec: cozyv1alpha1.ApplicationDefinitionSpec{ |
| 392 | + Application: cozyv1alpha1.ApplicationDefinitionApplication{Kind: "VMInstance"}, |
| 393 | + Release: cozyv1alpha1.ApplicationDefinitionRelease{ |
| 394 | + ChartRef: &helmv2.CrossNamespaceSourceReference{ |
| 395 | + Kind: "ExternalArtifact", |
| 396 | + Name: "vm-instance-app", |
| 397 | + Namespace: "cozy-system", |
| 398 | + }, |
| 399 | + }, |
| 400 | + }, |
| 401 | + } |
| 402 | +} |
| 403 | + |
| 404 | +func reconcileVMInstance(t *testing.T, appDef *cozyv1alpha1.ApplicationDefinition, hr *helmv2.HelmRelease) *helmv2.HelmRelease { |
| 405 | + t.Helper() |
| 406 | + |
| 407 | + scheme := newAppDefHelmScheme(t) |
| 408 | + fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(appDef, hr).Build() |
| 409 | + r := &ApplicationDefinitionHelmReconciler{Client: fakeClient, Scheme: scheme} |
| 410 | + |
| 411 | + if _, err := r.Reconcile(context.TODO(), reconcile.Request{ |
| 412 | + NamespacedName: types.NamespacedName{Name: appDef.Name}, |
| 413 | + }); err != nil { |
| 414 | + t.Fatalf("unexpected error: %v", err) |
| 415 | + } |
| 416 | + |
| 417 | + got := &helmv2.HelmRelease{} |
| 418 | + if err := fakeClient.Get(context.TODO(), types.NamespacedName{Name: hr.Name, Namespace: hr.Namespace}, got); err != nil { |
| 419 | + t.Fatalf("get HR: %v", err) |
| 420 | + } |
| 421 | + return got |
| 422 | +} |
| 423 | + |
| 424 | +// TestAppDefHelm_DisableWaitApplied pins the drift this reconciler exists to |
| 425 | +// close, for the one field a running release cannot pick up on its own: a |
| 426 | +// HelmRelease created before the definition carried |
| 427 | +// release.cozystack.io/helm-install-disable-wait keeps waiting on readiness |
| 428 | +// until its Application is written through the apps API again. On a cluster |
| 429 | +// where the wait is what broke (a KubeVirt VirtualMachine the kstatus poller |
| 430 | +// reads as permanently InProgress), that is every existing VM, each one |
| 431 | +// looping upgrade and timeout, and re-writing them by hand is the operator's |
| 432 | +// only way out. Bring the field to what the definition asks for instead. |
| 433 | +func TestAppDefHelm_DisableWaitApplied(t *testing.T) { |
| 434 | + no := false |
| 435 | + got := reconcileVMInstance(t, |
| 436 | + vmInstanceAppDef(map[string]string{config.HelmInstallDisableWaitAnnotation: "true"}), |
| 437 | + vmInstanceRelease(&no, &no)) |
| 438 | + |
| 439 | + if got.Spec.Install == nil || !got.Spec.Install.DisableWait { |
| 440 | + t.Fatalf("expected Install.DisableWait=true, got %+v", got.Spec.Install) |
| 441 | + } |
| 442 | + if got.Spec.Upgrade == nil || !got.Spec.Upgrade.DisableWait { |
| 443 | + t.Fatalf("expected Upgrade.DisableWait=true, got %+v", got.Spec.Upgrade) |
| 444 | + } |
| 445 | +} |
| 446 | + |
| 447 | +// TestAppDefHelm_DisableWaitAppliedToAbsentBlocks pins the same on a release |
| 448 | +// that carries no install/upgrade block at all: the field still has to land. |
| 449 | +func TestAppDefHelm_DisableWaitAppliedToAbsentBlocks(t *testing.T) { |
| 450 | + got := reconcileVMInstance(t, |
| 451 | + vmInstanceAppDef(map[string]string{config.HelmInstallDisableWaitAnnotation: "true"}), |
| 452 | + vmInstanceRelease(nil, nil)) |
| 453 | + |
| 454 | + if got.Spec.Install == nil || !got.Spec.Install.DisableWait { |
| 455 | + t.Fatalf("expected Install.DisableWait=true, got %+v", got.Spec.Install) |
| 456 | + } |
| 457 | + if got.Spec.Upgrade == nil || !got.Spec.Upgrade.DisableWait { |
| 458 | + t.Fatalf("expected Upgrade.DisableWait=true, got %+v", got.Spec.Upgrade) |
| 459 | + } |
| 460 | +} |
| 461 | + |
| 462 | +// TestAppDefHelm_DisableWaitRemoved pins the other direction, because the |
| 463 | +// definition is the source of truth in both: cozystack-api rebuilds the whole |
| 464 | +// spec on every write and emits DisableWait=false once the annotation is gone, |
| 465 | +// so a release left at true would drift back the moment anyone touched it. |
| 466 | +func TestAppDefHelm_DisableWaitRemoved(t *testing.T) { |
| 467 | + yes := true |
| 468 | + got := reconcileVMInstance(t, vmInstanceAppDef(nil), vmInstanceRelease(&yes, &yes)) |
| 469 | + |
| 470 | + if got.Spec.Install == nil || got.Spec.Install.DisableWait { |
| 471 | + t.Fatalf("expected Install.DisableWait=false, got %+v", got.Spec.Install) |
| 472 | + } |
| 473 | + if got.Spec.Upgrade == nil || got.Spec.Upgrade.DisableWait { |
| 474 | + t.Fatalf("expected Upgrade.DisableWait=false, got %+v", got.Spec.Upgrade) |
| 475 | + } |
| 476 | +} |
| 477 | + |
| 478 | +// TestAppDefHelm_DisableWaitLeavesAbsentBlocksAlone pins that the false case |
| 479 | +// does not materialise empty install/upgrade blocks on a release that has |
| 480 | +// none: writing them would change nothing about the release's behaviour and |
| 481 | +// would make every such reconcile an update. |
| 482 | +func TestAppDefHelm_DisableWaitLeavesAbsentBlocksAlone(t *testing.T) { |
| 483 | + got := reconcileVMInstance(t, vmInstanceAppDef(nil), vmInstanceRelease(nil, nil)) |
| 484 | + |
| 485 | + if got.Spec.Install != nil { |
| 486 | + t.Fatalf("expected Install left absent, got %+v", got.Spec.Install) |
| 487 | + } |
| 488 | + if got.Spec.Upgrade != nil { |
| 489 | + t.Fatalf("expected Upgrade left absent, got %+v", got.Spec.Upgrade) |
| 490 | + } |
| 491 | +} |
| 492 | + |
| 493 | +// TestAppDefHelm_DisableWaitInSyncSkipsUpdate pins idempotence: a release that |
| 494 | +// already matches must not be written, or every reconcile of every definition |
| 495 | +// bumps the resourceVersion of every release it owns and wakes helm-controller. |
| 496 | +func TestAppDefHelm_DisableWaitInSyncSkipsUpdate(t *testing.T) { |
| 497 | + yes := true |
| 498 | + hr := vmInstanceRelease(&yes, &yes) |
| 499 | + hr.Spec.ValuesFrom = expectedValuesFrom() |
| 500 | + |
| 501 | + scheme := newAppDefHelmScheme(t) |
| 502 | + appDef := vmInstanceAppDef(map[string]string{config.HelmInstallDisableWaitAnnotation: "true"}) |
| 503 | + fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(appDef, hr).Build() |
| 504 | + |
| 505 | + before := &helmv2.HelmRelease{} |
| 506 | + if err := fakeClient.Get(context.TODO(), types.NamespacedName{Name: hr.Name, Namespace: hr.Namespace}, before); err != nil { |
| 507 | + t.Fatalf("get HR: %v", err) |
| 508 | + } |
| 509 | + |
| 510 | + r := &ApplicationDefinitionHelmReconciler{Client: fakeClient, Scheme: scheme} |
| 511 | + if _, err := r.Reconcile(context.TODO(), reconcile.Request{ |
| 512 | + NamespacedName: types.NamespacedName{Name: appDef.Name}, |
| 513 | + }); err != nil { |
| 514 | + t.Fatalf("unexpected error: %v", err) |
| 515 | + } |
| 516 | + |
| 517 | + after := &helmv2.HelmRelease{} |
| 518 | + if err := fakeClient.Get(context.TODO(), types.NamespacedName{Name: hr.Name, Namespace: hr.Namespace}, after); err != nil { |
| 519 | + t.Fatalf("get HR after reconcile: %v", err) |
| 520 | + } |
| 521 | + if after.ResourceVersion != before.ResourceVersion { |
| 522 | + t.Fatalf("release rewritten with nothing to change: %q -> %q", before.ResourceVersion, after.ResourceVersion) |
| 523 | + } |
| 524 | +} |
0 commit comments