-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
K8SPSMDB-1089: ignore finalizers on errors when
.status.state="Error"
- Loading branch information
Showing
3 changed files
with
132 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package perconaservermongodb | ||
|
||
import ( | ||
"context" | ||
"slices" | ||
"testing" | ||
|
||
"k8s.io/apimachinery/pkg/types" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
|
||
api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1" | ||
"github.com/percona/percona-server-mongodb-operator/version" | ||
) | ||
|
||
func TestCheckFinalizers(t *testing.T) { | ||
ctx := context.Background() | ||
crName := "check-finalizers" | ||
ns := crName + "-ns" | ||
|
||
defaultCR := readDefaultCR(t, crName, ns) | ||
obj := append( | ||
fakePodsForRS(defaultCR, defaultCR.Spec.Replsets[0]), | ||
fakeStatefulset(defaultCR, defaultCR.Spec.Replsets[0].Name, defaultCR.Spec.Replsets[0].Size, ""), | ||
) | ||
|
||
tests := []struct { | ||
name string | ||
cr *api.PerconaServerMongoDB | ||
|
||
expectedShouldReconcile bool | ||
expectedFinalizers []string | ||
}{ | ||
{ | ||
name: "no-finalizers", | ||
cr: updateObj(t, defaultCR.DeepCopy(), func(cr *api.PerconaServerMongoDB) { | ||
cr.Finalizers = nil | ||
}), | ||
expectedShouldReconcile: false, | ||
}, | ||
{ | ||
name: "delete-pvc pass", | ||
cr: updateObj(t, defaultCR.DeepCopy(), func(cr *api.PerconaServerMongoDB) { | ||
cr.Finalizers = []string{api.FinalizerDeletePVC} | ||
}), | ||
expectedShouldReconcile: false, | ||
expectedFinalizers: nil, | ||
}, | ||
{ | ||
name: "delete pods fails", | ||
cr: updateObj(t, defaultCR.DeepCopy(), func(cr *api.PerconaServerMongoDB) { | ||
cr.Finalizers = []string{api.FinalizerDeletePSMDBPodsInOrder} | ||
}), | ||
expectedFinalizers: []string{api.FinalizerDeletePSMDBPodsInOrder}, | ||
}, | ||
{ | ||
name: "cr with error state, delete pods fails with delete-pvc", | ||
cr: updateObj(t, defaultCR.DeepCopy(), func(cr *api.PerconaServerMongoDB) { | ||
cr.Finalizers = []string{api.FinalizerDeletePSMDBPodsInOrder} | ||
cr.Status.State = api.AppStateError | ||
}), | ||
expectedFinalizers: []string{}, | ||
}, | ||
{ | ||
name: "delete pods fails with delete-pvc", | ||
cr: updateObj(t, defaultCR.DeepCopy(), func(cr *api.PerconaServerMongoDB) { | ||
cr.Finalizers = []string{api.FinalizerDeletePVC, api.FinalizerDeletePSMDBPodsInOrder} | ||
}), | ||
expectedFinalizers: []string{api.FinalizerDeletePSMDBPodsInOrder, api.FinalizerDeletePVC}, | ||
}, | ||
{ | ||
name: "cr with error state, delete pods fails with delete-pvc", | ||
cr: updateObj(t, defaultCR.DeepCopy(), func(cr *api.PerconaServerMongoDB) { | ||
cr.Finalizers = []string{api.FinalizerDeletePVC, api.FinalizerDeletePSMDBPodsInOrder} | ||
cr.Status.State = api.AppStateError | ||
}), | ||
expectedFinalizers: []string{}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
r := buildFakeClient(append(obj, tt.cr)...) | ||
|
||
cr := &api.PerconaServerMongoDB{} | ||
if err := r.client.Get(ctx, types.NamespacedName{Name: crName, Namespace: ns}, cr); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if err := cr.CheckNSetDefaults(version.PlatformKubernetes, logf.FromContext(ctx)); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
shouldReconcile, err := r.checkFinalizers(ctx, cr) | ||
if err != nil { | ||
t.Fatal("unexpected error:", err.Error()) | ||
} | ||
if shouldReconcile != tt.expectedShouldReconcile { | ||
t.Fatal("unexpected shouldReconcile:", shouldReconcile) | ||
} | ||
|
||
if err := r.client.Get(ctx, types.NamespacedName{Name: crName, Namespace: ns}, cr); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !slices.Equal(cr.Finalizers, tt.expectedFinalizers) { | ||
t.Fatal("unexpected finalizers:", cr.Finalizers, "; expected:", tt.expectedFinalizers) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters