Skip to content
39 changes: 35 additions & 4 deletions internal/controller/postgrescluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package controller
import (
"context"
"fmt"
"strings"

v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/api/resource"
Expand All @@ -36,6 +38,7 @@ import (

enterprisev4 "github.com/splunk/splunk-operator/api/v4"
"github.com/splunk/splunk-operator/pkg/postgresql/cluster/core"
"github.com/splunk/splunk-operator/pkg/postgresql/shared/adapter/prometheus"
)

/*
Expand All @@ -51,6 +54,18 @@ import (
* PC-09 ignores no-op updates
*/

func containsEvent(events []string, recorder *record.FakeRecorder, eventType string, event string) bool {
for {
select {
case e := <-recorder.Events:
events = append(events, e)
return strings.Contains(e, eventType) && strings.Contains(e, event)
default:
return false
}
}
}

var _ = Describe("PostgresCluster Controller", Label("postgres"), func() {

const (
Expand All @@ -76,6 +91,7 @@ var _ = Describe("PostgresCluster Controller", Label("postgres"), func() {
pgClusterClassKey types.NamespacedName
reconciler *PostgresClusterReconciler
req reconcile.Request
fakeRecorder *record.FakeRecorder
)

reconcileNTimes := func(times int) {
Expand Down Expand Up @@ -120,11 +136,12 @@ var _ = Describe("PostgresCluster Controller", Label("postgres"), func() {
ClusterDeletionPolicy: &[]string{deletePolicy}[0],
},
}

fakeRecorder = record.NewFakeRecorder(100)
reconciler = &PostgresClusterReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
Recorder: record.NewFakeRecorder(100),
Recorder: fakeRecorder,
Metrics: &prometheus.NoopRecorder{},
}
req = reconcile.Request{NamespacedName: types.NamespacedName{Name: clusterName, Namespace: namespace}}
})
Expand Down Expand Up @@ -202,7 +219,7 @@ var _ = Describe("PostgresCluster Controller", Label("postgres"), func() {
cond := meta.FindStatusCondition(pc.Status.Conditions, "ClusterReady")
Expect(cond).NotTo(BeNil())
Expect(cond.Status).To(Equal(metav1.ConditionFalse))
Expect(cond.Reason).To(Equal("ClusterBuildSucceeded"))
Expect(cond.Reason).To(Equal("CNPGClusterProvisioning"))

// Simulate external CNPG controller status progression.
cnpg := &cnpgv1.Cluster{}
Expand All @@ -220,6 +237,13 @@ var _ = Describe("PostgresCluster Controller", Label("postgres"), func() {
Expect(pc.Status.Resources).NotTo(BeNil())
Expect(pc.Status.Resources.SuperUserSecretRef).NotTo(BeNil())
Expect(pc.Status.Resources.ConfigMapRef).NotTo(BeNil())

received := make([]string, 0, 8)
Eventually(func() bool {
return containsEvent(
received, fakeRecorder,
v1.EventTypeNormal, core.EventClusterReady)
}, "5s", "100ms").Should(BeTrue(), "events seen: %v", received)
})

// PC-07
Expand Down Expand Up @@ -288,7 +312,7 @@ var _ = Describe("PostgresCluster Controller", Label("postgres"), func() {
When("reconciling with invalid or drifted dependencies", func() {
// PC-05
Context("when referenced class does not exist", func() {
It("fails with class-not-found condition", func() {
It("fails with class-not-found condition and emits a warning event", func() {
badName := "bad-" + clusterName
badKey := types.NamespacedName{Name: badName, Namespace: namespace}

Expand All @@ -313,6 +337,13 @@ var _ = Describe("PostgresCluster Controller", Label("postgres"), func() {
cond := meta.FindStatusCondition(current.Status.Conditions, "ClusterReady")
return cond != nil && cond.Reason == "ClusterClassNotFound"
}, "20s", "250ms").Should(BeTrue())

received := make([]string, 0, 8)
Eventually(func() bool {
return containsEvent(
received, fakeRecorder,
v1.EventTypeWarning, core.EventClusterClassNotFound)
}, "5s", "100ms").Should(BeTrue(), "events seen: %v", received)
})
})

Expand Down
2 changes: 2 additions & 0 deletions internal/controller/postgresdatabase_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
enterprisev4 "github.com/splunk/splunk-operator/api/v4"
"github.com/splunk/splunk-operator/pkg/postgresql/shared/adapter/prometheus"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -83,6 +84,7 @@ func reconcilePostgresDatabase(ctx context.Context, nn types.NamespacedName) (ct
Client: k8sClient,
Scheme: k8sClient.Scheme(),
Recorder: record.NewFakeRecorder(100),
Metrics: &prometheus.NoopRecorder{},
}
return reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: nn})
}
Expand Down
Loading
Loading