package controller import ( "errors" "testing" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/runtime/schema" terdutv1alpha1 "git.ryuvia.com/niklas/terdut-operator/api/v1alpha1" ) // These exercise classifyClusterGetError directly, against hand-constructed // errors, rather than through envtest -- the point is the classification // logic, not a real Get, and the "CRD not installed" case specifically // can't be produced in the shared envtest suite (its CRDs, stub included, // are always installed for the whole suite's lifetime). func TestClassifyClusterGetError_CRDNotInstalled(t *testing.T) { noMatch := &meta.NoKindMatchError{GroupKind: schema.GroupKind{Group: "acid.zalan.do", Kind: "postgresql"}} got := classifyClusterGetError(noMatch, "terdut-postgres", "oncall") if got.reason != terdutv1alpha1.ReasonPostgresOperatorCRDNotInstalled { t.Errorf("reason = %q, want %q", got.reason, terdutv1alpha1.ReasonPostgresOperatorCRDNotInstalled) } } func TestClassifyClusterGetError_ClusterNotFound(t *testing.T) { notFound := apierrors.NewNotFound(schema.GroupResource{Group: "acid.zalan.do", Resource: "postgresqls"}, "terdut-postgres") got := classifyClusterGetError(notFound, "terdut-postgres", "oncall") if got.reason != terdutv1alpha1.ReasonPostgresClusterNotFound { t.Errorf("reason = %q, want %q", got.reason, terdutv1alpha1.ReasonPostgresClusterNotFound) } } func TestClassifyClusterGetError_OtherErrorStillReturnsACondition(t *testing.T) { // Not one of the two classified cases -- still returns a databaseError // (Reconcile treats every databaseError as wait-and-retry today; see // the function's own comment on why), never nil, so a caller can't // accidentally treat an unclassified error as success. got := classifyClusterGetError(errors.New("etcd is on fire"), "terdut-postgres", "oncall") if got == nil { t.Fatal("classifyClusterGetError returned nil for an unclassified error") } }