|
| 1 | +/* |
| 2 | +Copyright 2026 The Cozystack Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package application |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "reflect" |
| 22 | + "testing" |
| 23 | + |
| 24 | + cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1" |
| 25 | + helmv2 "github.com/fluxcd/helm-controller/api/v2" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/runtime" |
| 28 | + "k8s.io/apimachinery/pkg/types" |
| 29 | + "k8s.io/apiserver/pkg/endpoints/request" |
| 30 | + "k8s.io/apiserver/pkg/registry/rest" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/client/interceptor" |
| 34 | + |
| 35 | + appsv1alpha1 "github.com/cozystack/cozystack/pkg/apis/apps/v1alpha1" |
| 36 | + "github.com/cozystack/cozystack/pkg/config" |
| 37 | +) |
| 38 | + |
| 39 | +const optionsTestNamespace = "tenant-options" |
| 40 | + |
| 41 | +func newOptionsTestREST(t *testing.T, funcs interceptor.Funcs, objects ...client.Object) *REST { |
| 42 | + t.Helper() |
| 43 | + scheme := runtime.NewScheme() |
| 44 | + if err := cozyv1alpha1.AddToScheme(scheme); err != nil { |
| 45 | + t.Fatalf("register Cozystack API scheme: %v", err) |
| 46 | + } |
| 47 | + if err := helmv2.AddToScheme(scheme); err != nil { |
| 48 | + t.Fatalf("register HelmRelease scheme: %v", err) |
| 49 | + } |
| 50 | + c := fake.NewClientBuilder(). |
| 51 | + WithScheme(scheme). |
| 52 | + WithObjects(objects...). |
| 53 | + WithInterceptorFuncs(funcs). |
| 54 | + Build() |
| 55 | + return NewREST(c, nil, &config.Resource{ |
| 56 | + Application: config.ApplicationConfig{ |
| 57 | + Kind: "PostgreSQL", |
| 58 | + Plural: "postgresqls", |
| 59 | + Singular: "postgresql", |
| 60 | + }, |
| 61 | + Release: config.ReleaseConfig{Prefix: "postgresql-"}, |
| 62 | + }) |
| 63 | +} |
| 64 | + |
| 65 | +func optionsTestApplication() *appsv1alpha1.Application { |
| 66 | + return &appsv1alpha1.Application{ |
| 67 | + TypeMeta: metav1.TypeMeta{APIVersion: "apps.cozystack.io/v1alpha1", Kind: "PostgreSQL"}, |
| 68 | + ObjectMeta: metav1.ObjectMeta{ |
| 69 | + Name: "example", |
| 70 | + Namespace: optionsTestNamespace, |
| 71 | + }, |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func optionsTestHelmRelease() *helmv2.HelmRelease { |
| 76 | + return &helmv2.HelmRelease{ObjectMeta: metav1.ObjectMeta{ |
| 77 | + Name: "postgresql-example", |
| 78 | + Namespace: optionsTestNamespace, |
| 79 | + Labels: map[string]string{ |
| 80 | + ApplicationKindLabel: "PostgreSQL", |
| 81 | + ApplicationGroupLabel: appsv1alpha1.GroupName, |
| 82 | + ApplicationNameLabel: "example", |
| 83 | + }, |
| 84 | + }} |
| 85 | +} |
| 86 | + |
| 87 | +func captureCreateOptions(opts ...client.CreateOption) *metav1.CreateOptions { |
| 88 | + converted := (&client.CreateOptions{}).ApplyOptions(opts) |
| 89 | + return converted.AsCreateOptions().DeepCopy() |
| 90 | +} |
| 91 | + |
| 92 | +func captureUpdateOptions(opts ...client.UpdateOption) *metav1.UpdateOptions { |
| 93 | + converted := (&client.UpdateOptions{}).ApplyOptions(opts) |
| 94 | + return converted.AsUpdateOptions().DeepCopy() |
| 95 | +} |
| 96 | + |
| 97 | +func captureDeleteOptions(opts ...client.DeleteOption) *metav1.DeleteOptions { |
| 98 | + converted := &client.DeleteOptions{} |
| 99 | + for _, option := range opts { |
| 100 | + option.ApplyToDelete(converted) |
| 101 | + } |
| 102 | + return converted.AsDeleteOptions().DeepCopy() |
| 103 | +} |
| 104 | + |
| 105 | +func TestCreatePropagatesWriteOptions(t *testing.T) { |
| 106 | + want := &metav1.CreateOptions{ |
| 107 | + DryRun: []string{metav1.DryRunAll}, |
| 108 | + FieldManager: "create-manager", |
| 109 | + FieldValidation: "Strict", |
| 110 | + } |
| 111 | + var got *metav1.CreateOptions |
| 112 | + r := newOptionsTestREST(t, interceptor.Funcs{ |
| 113 | + Create: func(_ context.Context, _ client.WithWatch, _ client.Object, opts ...client.CreateOption) error { |
| 114 | + got = captureCreateOptions(opts...) |
| 115 | + return nil |
| 116 | + }, |
| 117 | + }) |
| 118 | + ctx := request.WithNamespace(context.Background(), optionsTestNamespace) |
| 119 | + if _, err := r.Create(ctx, optionsTestApplication(), nil, want); err != nil { |
| 120 | + t.Fatalf("Create returned error: %v", err) |
| 121 | + } |
| 122 | + if !reflect.DeepEqual(got, want) { |
| 123 | + t.Fatalf("Create options lost in controller-runtime conversion:\n got: %#v\nwant: %#v", got, want) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func TestUpdatePropagatesWriteOptions(t *testing.T) { |
| 128 | + want := &metav1.UpdateOptions{ |
| 129 | + DryRun: []string{metav1.DryRunAll}, |
| 130 | + FieldManager: "update-manager", |
| 131 | + FieldValidation: "Warn", |
| 132 | + } |
| 133 | + var got *metav1.UpdateOptions |
| 134 | + r := newOptionsTestREST(t, interceptor.Funcs{ |
| 135 | + Update: func(_ context.Context, _ client.WithWatch, _ client.Object, opts ...client.UpdateOption) error { |
| 136 | + got = captureUpdateOptions(opts...) |
| 137 | + return nil |
| 138 | + }, |
| 139 | + }, optionsTestHelmRelease()) |
| 140 | + ctx := request.WithNamespace(context.Background(), optionsTestNamespace) |
| 141 | + if _, _, err := r.Update(ctx, "example", rest.DefaultUpdatedObjectInfo(optionsTestApplication()), nil, nil, false, want); err != nil { |
| 142 | + t.Fatalf("Update returned error: %v", err) |
| 143 | + } |
| 144 | + if !reflect.DeepEqual(got, want) { |
| 145 | + t.Fatalf("Update options lost in controller-runtime conversion:\n got: %#v\nwant: %#v", got, want) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func TestUpdateForceCreatePropagatesWriteOptions(t *testing.T) { |
| 150 | + update := &metav1.UpdateOptions{ |
| 151 | + DryRun: []string{metav1.DryRunAll}, |
| 152 | + FieldManager: "apply-manager", |
| 153 | + FieldValidation: "Strict", |
| 154 | + } |
| 155 | + want := &metav1.CreateOptions{ |
| 156 | + DryRun: update.DryRun, |
| 157 | + FieldManager: update.FieldManager, |
| 158 | + FieldValidation: update.FieldValidation, |
| 159 | + } |
| 160 | + var got *metav1.CreateOptions |
| 161 | + r := newOptionsTestREST(t, interceptor.Funcs{ |
| 162 | + Create: func(_ context.Context, _ client.WithWatch, _ client.Object, opts ...client.CreateOption) error { |
| 163 | + got = captureCreateOptions(opts...) |
| 164 | + return nil |
| 165 | + }, |
| 166 | + }) |
| 167 | + ctx := request.WithNamespace(context.Background(), optionsTestNamespace) |
| 168 | + if _, _, err := r.Update(ctx, "example", rest.DefaultUpdatedObjectInfo(optionsTestApplication()), nil, nil, true, update); err != nil { |
| 169 | + t.Fatalf("force-create Update returned error: %v", err) |
| 170 | + } |
| 171 | + if !reflect.DeepEqual(got, want) { |
| 172 | + t.Fatalf("force-create options lost in controller-runtime conversion:\n got: %#v\nwant: %#v", got, want) |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +func TestDeletePropagatesWriteOptions(t *testing.T) { |
| 177 | + grace := int64(3) |
| 178 | + propagation := metav1.DeletePropagationForeground |
| 179 | + uid := types.UID("expected-uid") |
| 180 | + rv := "17" |
| 181 | + want := &metav1.DeleteOptions{ |
| 182 | + GracePeriodSeconds: &grace, |
| 183 | + Preconditions: &metav1.Preconditions{UID: &uid, ResourceVersion: &rv}, |
| 184 | + PropagationPolicy: &propagation, |
| 185 | + DryRun: []string{metav1.DryRunAll}, |
| 186 | + } |
| 187 | + var got *metav1.DeleteOptions |
| 188 | + r := newOptionsTestREST(t, interceptor.Funcs{ |
| 189 | + Delete: func(_ context.Context, _ client.WithWatch, _ client.Object, opts ...client.DeleteOption) error { |
| 190 | + got = captureDeleteOptions(opts...) |
| 191 | + return nil |
| 192 | + }, |
| 193 | + }, optionsTestHelmRelease()) |
| 194 | + ctx := request.WithNamespace(context.Background(), optionsTestNamespace) |
| 195 | + if _, _, err := r.Delete(ctx, "example", nil, want); err != nil { |
| 196 | + t.Fatalf("Delete returned error: %v", err) |
| 197 | + } |
| 198 | + if !reflect.DeepEqual(got, want) { |
| 199 | + t.Fatalf("Delete options lost in controller-runtime conversion:\n got: %#v\nwant: %#v", got, want) |
| 200 | + } |
| 201 | +} |
0 commit comments