|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + avngen "github.com/aiven/go-client-codegen" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/mock" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + corev1 "k8s.io/api/core/v1" |
| 12 | + "k8s.io/apimachinery/pkg/api/meta" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + "k8s.io/apimachinery/pkg/runtime" |
| 15 | + "k8s.io/apimachinery/pkg/types" |
| 16 | + clientgoscheme "k8s.io/client-go/kubernetes/scheme" |
| 17 | + "k8s.io/client-go/tools/record" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 19 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 20 | + |
| 21 | + "github.com/aiven/aiven-operator/api/v1alpha1" |
| 22 | +) |
| 23 | + |
| 24 | +func TestInstanceReconcilerHelper_updateInstanceStateAndSecretUntilRunning(t *testing.T) { |
| 25 | + connectionSecret := func(pg *v1alpha1.PostgreSQL) *corev1.Secret { |
| 26 | + return &corev1.Secret{ |
| 27 | + ObjectMeta: metav1.ObjectMeta{ |
| 28 | + Name: pg.Name, |
| 29 | + Namespace: pg.Namespace, |
| 30 | + }, |
| 31 | + StringData: map[string]string{ |
| 32 | + "PGPASSWORD": "pw", |
| 33 | + }, |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + t.Run("Marks resource not ready when connection Secret publish fails", func(t *testing.T) { |
| 38 | + pg := newReadyPostgreSQL(t) |
| 39 | + controller := true |
| 40 | + |
| 41 | + scheme := newBasicControllerTestScheme(t) |
| 42 | + k8sClient := fake.NewClientBuilder(). |
| 43 | + WithScheme(scheme). |
| 44 | + WithObjects(&corev1.Secret{ |
| 45 | + ObjectMeta: metav1.ObjectMeta{ |
| 46 | + Name: pg.Name, |
| 47 | + Namespace: pg.Namespace, |
| 48 | + OwnerReferences: []metav1.OwnerReference{ |
| 49 | + { |
| 50 | + APIVersion: "aiven.io/v1alpha1", |
| 51 | + Kind: "PostgreSQL", |
| 52 | + Name: "other", |
| 53 | + UID: types.UID("other-uid"), |
| 54 | + Controller: &controller, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }). |
| 59 | + Build() |
| 60 | + |
| 61 | + h := NewMockHandlers(t) |
| 62 | + h.EXPECT(). |
| 63 | + get(mock.Anything, mock.Anything, pg). |
| 64 | + Run(func(_ context.Context, _ avngen.Client, _ client.Object) { |
| 65 | + metav1.SetMetaDataAnnotation(&pg.ObjectMeta, instanceIsRunningAnnotation, "true") |
| 66 | + meta.SetStatusCondition(&pg.Status.Conditions, getRunningCondition(metav1.ConditionTrue, "CheckRunning", "Instance is running on Aiven side")) |
| 67 | + }). |
| 68 | + Return(connectionSecret(pg), nil). |
| 69 | + Once() |
| 70 | + |
| 71 | + helper := &instanceReconcilerHelper{ |
| 72 | + k8s: k8sClient, |
| 73 | + h: h, |
| 74 | + rec: record.NewFakeRecorder(10), |
| 75 | + } |
| 76 | + |
| 77 | + err := helper.updateInstanceStateAndSecretUntilRunning(t.Context(), pg) |
| 78 | + require.Error(t, err) |
| 79 | + |
| 80 | + require.False(t, hasIsRunningAnnotation(pg)) |
| 81 | + |
| 82 | + running := meta.FindStatusCondition(pg.Status.Conditions, conditionTypeRunning) |
| 83 | + require.NotNil(t, running) |
| 84 | + require.Equal(t, metav1.ConditionUnknown, running.Status) |
| 85 | + require.Equal(t, string(errConditionConnInfoSecret), running.Reason) |
| 86 | + |
| 87 | + errCond := meta.FindStatusCondition(pg.Status.Conditions, ConditionTypeError) |
| 88 | + require.NotNil(t, errCond) |
| 89 | + require.Equal(t, metav1.ConditionUnknown, errCond.Status) |
| 90 | + require.Equal(t, string(errConditionConnInfoSecret), errCond.Reason) |
| 91 | + }) |
| 92 | + |
| 93 | + t.Run("Clears connection Secret publish error after successful publish", func(t *testing.T) { |
| 94 | + pg := newReadyPostgreSQL(t) |
| 95 | + meta.SetStatusCondition(&pg.Status.Conditions, getErrorCondition(errConditionConnInfoSecret, assert.AnError)) |
| 96 | + |
| 97 | + scheme := newBasicControllerTestScheme(t) |
| 98 | + k8sClient := fake.NewClientBuilder(). |
| 99 | + WithScheme(scheme). |
| 100 | + Build() |
| 101 | + |
| 102 | + h := NewMockHandlers(t) |
| 103 | + h.EXPECT(). |
| 104 | + get(mock.Anything, mock.Anything, pg). |
| 105 | + Run(func(_ context.Context, _ avngen.Client, _ client.Object) { |
| 106 | + metav1.SetMetaDataAnnotation(&pg.ObjectMeta, instanceIsRunningAnnotation, "true") |
| 107 | + meta.SetStatusCondition(&pg.Status.Conditions, getRunningCondition(metav1.ConditionTrue, "CheckRunning", "Instance is running on Aiven side")) |
| 108 | + }). |
| 109 | + Return(connectionSecret(pg), nil). |
| 110 | + Once() |
| 111 | + |
| 112 | + helper := &instanceReconcilerHelper{ |
| 113 | + k8s: k8sClient, |
| 114 | + h: h, |
| 115 | + rec: record.NewFakeRecorder(10), |
| 116 | + } |
| 117 | + |
| 118 | + require.NoError(t, helper.updateInstanceStateAndSecretUntilRunning(t.Context(), pg)) |
| 119 | + require.True(t, hasIsRunningAnnotation(pg)) |
| 120 | + require.Nil(t, meta.FindStatusCondition(pg.Status.Conditions, ConditionTypeError)) |
| 121 | + |
| 122 | + got := &corev1.Secret{} |
| 123 | + require.NoError(t, k8sClient.Get(t.Context(), types.NamespacedName{Name: pg.Name, Namespace: pg.Namespace}, got)) |
| 124 | + require.Equal(t, []byte("pw"), got.Data["PGPASSWORD"]) |
| 125 | + }) |
| 126 | +} |
| 127 | + |
| 128 | +func TestInstanceReconcilerHelper_resourceNeedsConnectionSecretSync(t *testing.T) { |
| 129 | + ownedSecret := func(pg *v1alpha1.PostgreSQL) *corev1.Secret { |
| 130 | + controller := true |
| 131 | + return &corev1.Secret{ |
| 132 | + ObjectMeta: metav1.ObjectMeta{ |
| 133 | + Name: pg.Name, |
| 134 | + Namespace: pg.Namespace, |
| 135 | + OwnerReferences: []metav1.OwnerReference{ |
| 136 | + { |
| 137 | + APIVersion: "aiven.io/v1alpha1", |
| 138 | + Kind: "PostgreSQL", |
| 139 | + Name: pg.Name, |
| 140 | + UID: pg.UID, |
| 141 | + Controller: &controller, |
| 142 | + }, |
| 143 | + }, |
| 144 | + }, |
| 145 | + Data: map[string][]byte{ |
| 146 | + "PGPASSWORD": []byte("pw"), |
| 147 | + }, |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + tests := map[string]struct { |
| 152 | + mutate func(*v1alpha1.PostgreSQL) |
| 153 | + objects []runtime.Object |
| 154 | + wantNeedsSync bool |
| 155 | + }{ |
| 156 | + "does not need sync for powered-off resource": { |
| 157 | + mutate: func(pg *v1alpha1.PostgreSQL) { |
| 158 | + metav1.SetMetaDataAnnotation(&pg.ObjectMeta, instanceIsRunningAnnotation, "false") |
| 159 | + }, |
| 160 | + wantNeedsSync: false, |
| 161 | + }, |
| 162 | + "does not need sync when connection Secret creation is disabled": { |
| 163 | + mutate: func(pg *v1alpha1.PostgreSQL) { |
| 164 | + pg.Spec.ConnInfoSecretTargetDisabled = new(bool) |
| 165 | + *pg.Spec.ConnInfoSecretTargetDisabled = true |
| 166 | + }, |
| 167 | + wantNeedsSync: false, |
| 168 | + }, |
| 169 | + "needs sync when connection Secret is missing": { |
| 170 | + wantNeedsSync: true, |
| 171 | + }, |
| 172 | + "needs sync when connection Secret is not controlled by the resource": { |
| 173 | + objects: []runtime.Object{ |
| 174 | + &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "pg-no-ref", Namespace: "default"}}, |
| 175 | + }, |
| 176 | + wantNeedsSync: true, |
| 177 | + }, |
| 178 | + "needs sync when a stale connection Secret error is present": { |
| 179 | + mutate: func(pg *v1alpha1.PostgreSQL) { |
| 180 | + meta.SetStatusCondition(&pg.Status.Conditions, getErrorCondition(errConditionConnInfoSecret, assert.AnError)) |
| 181 | + }, |
| 182 | + objects: []runtime.Object{ |
| 183 | + ownedSecret(newReadyPostgreSQL(t)), |
| 184 | + }, |
| 185 | + wantNeedsSync: true, |
| 186 | + }, |
| 187 | + "does not need sync when connection Secret is controlled by the resource": { |
| 188 | + objects: []runtime.Object{ |
| 189 | + ownedSecret(newReadyPostgreSQL(t)), |
| 190 | + }, |
| 191 | + wantNeedsSync: false, |
| 192 | + }, |
| 193 | + } |
| 194 | + |
| 195 | + for name, tt := range tests { |
| 196 | + t.Run(name, func(t *testing.T) { |
| 197 | + pg := newReadyPostgreSQL(t) |
| 198 | + if tt.mutate != nil { |
| 199 | + tt.mutate(pg) |
| 200 | + } |
| 201 | + |
| 202 | + scheme := newBasicControllerTestScheme(t) |
| 203 | + k8sClient := fake.NewClientBuilder(). |
| 204 | + WithScheme(scheme). |
| 205 | + WithRuntimeObjects(tt.objects...). |
| 206 | + Build() |
| 207 | + |
| 208 | + helper := &instanceReconcilerHelper{k8s: k8sClient} |
| 209 | + require.Equal(t, tt.wantNeedsSync, helper.resourceNeedsConnectionSecretSync(t.Context(), pg)) |
| 210 | + }) |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +func newBasicControllerTestScheme(t *testing.T) *runtime.Scheme { |
| 215 | + t.Helper() |
| 216 | + |
| 217 | + scheme := runtime.NewScheme() |
| 218 | + require.NoError(t, clientgoscheme.AddToScheme(scheme)) |
| 219 | + require.NoError(t, v1alpha1.AddToScheme(scheme)) |
| 220 | + return scheme |
| 221 | +} |
| 222 | + |
| 223 | +func newReadyPostgreSQL(t *testing.T) *v1alpha1.PostgreSQL { |
| 224 | + t.Helper() |
| 225 | + |
| 226 | + pg := newObjectFromYAML[v1alpha1.PostgreSQL](t, yamlPostgres) |
| 227 | + pg.UID = types.UID("pg-uid") |
| 228 | + pg.Generation = 1 |
| 229 | + metav1.SetMetaDataAnnotation(&pg.ObjectMeta, processedGenerationAnnotation, "1") |
| 230 | + metav1.SetMetaDataAnnotation(&pg.ObjectMeta, instanceIsRunningAnnotation, "true") |
| 231 | + meta.SetStatusCondition(&pg.Status.Conditions, getRunningCondition(metav1.ConditionTrue, "CheckRunning", "Instance is running on Aiven side")) |
| 232 | + return pg |
| 233 | +} |
0 commit comments