Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 77 additions & 7 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,7 +38,7 @@ import (

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

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

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

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

const (
Expand All @@ -77,6 +93,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 @@ -119,15 +136,19 @@ var _ = Describe("PostgresCluster Controller", Label("postgres"), func() {
Spec: enterprisev4.PostgresClusterSpec{
Class: className,
ClusterDeletionPolicy: &[]string{deletePolicy}[0],
ManagedRoles: []enterprisev4.ManagedRole{
{Name: "app_user", Exists: true},
{Name: "app_user_rw", Exists: true},
},
},
}

fakeRecorder = record.NewFakeRecorder(100)
reconciler = &PostgresClusterReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
Recorder: record.NewFakeRecorder(100),
Metrics: &pgprometheus.NoopRecorder{},
FleetCollector: pgprometheus.NewFleetCollector(),
Recorder: fakeRecorder,
Metrics: &prometheus.NoopRecorder{},
FleetCollector: prometheus.NewFleetCollector(),
}
req = reconcile.Request{NamespacedName: types.NamespacedName{Name: clusterName, Namespace: namespace}}
})
Expand Down Expand Up @@ -205,12 +226,17 @@ 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{}
Expect(k8sClient.Get(ctx, pgClusterKey, cnpg)).To(Succeed())
cnpg.Status.Phase = cnpgv1.PhaseHealthy
cnpg.Status.ManagedRolesStatus = cnpgv1.ManagedRoles{
ByStatus: map[cnpgv1.RoleStatus][]string{
cnpgv1.RoleStatusReconciled: {"app_user", "app_user_rw"},
},
}
Expect(k8sClient.Status().Update(ctx, cnpg)).To(Succeed())
reconcileNTimes(1)

Expand All @@ -220,9 +246,47 @@ var _ = Describe("PostgresCluster Controller", Label("postgres"), func() {
Expect(cond).NotTo(BeNil())
Expect(cond.Status).To(Equal(metav1.ConditionTrue))
Expect(cond.Reason).To(Equal("CNPGClusterHealthy"))

secretCond := meta.FindStatusCondition(pc.Status.Conditions, "SecretsReady")
Expect(secretCond).NotTo(BeNil())
Expect(secretCond.Status).To(Equal(metav1.ConditionTrue))
Expect(secretCond.Reason).To(Equal("SuperUserSecretReady"))

configMapCond := meta.FindStatusCondition(pc.Status.Conditions, "ConfigMapsReady")
Expect(configMapCond).NotTo(BeNil())
Expect(configMapCond.Status).To(Equal(metav1.ConditionTrue))
Expect(configMapCond.Reason).To(Equal("ConfigMapReconciled"))

managedRolesCond := meta.FindStatusCondition(pc.Status.Conditions, "ManagedRolesReady")
Expect(managedRolesCond).NotTo(BeNil())
Expect(managedRolesCond.Status).To(Equal(metav1.ConditionTrue))
Expect(managedRolesCond.Reason).To(Equal("ManagedRolesReconciled"))

// Pooler is disabled in this suite fixture, but converge publishes PoolerReady=True with disabled message.
poolerCond := meta.FindStatusCondition(pc.Status.Conditions, "PoolerReady")
Expect(poolerCond).NotTo(BeNil())
Expect(poolerCond.Status).To(Equal(metav1.ConditionTrue))
Expect(poolerCond.Reason).To(Equal("AllInstancesReady"))
Expect(poolerCond.Message).To(Equal("Connection pooler disabled"))

Expect(pc.Status.ManagedRolesStatus).NotTo(BeNil())
Expect(pc.Status.ManagedRolesStatus.Reconciled).To(ContainElements("app_user", "app_user_rw"))

Expect(pc.Status.Phase).NotTo(BeNil())
Expect(*pc.Status.Phase).To(Equal("Ready"))
Expect(pc.Status.ProvisionerRef).NotTo(BeNil())
Expect(pc.Status.ProvisionerRef.Kind).To(Equal("Cluster"))
Expect(pc.Status.ProvisionerRef.Name).To(Equal(clusterName))

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)
Expect(containsEvents(
&received, fakeRecorder,
v1.EventTypeNormal, core.EventClusterReady,
)).To(BeTrue(), "events seen: %v", received)
})

// PC-07
Expand Down Expand Up @@ -291,7 +355,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 @@ -316,6 +380,12 @@ 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)
Expect(containsEvents(
&received, fakeRecorder,
v1.EventTypeWarning, core.EventClusterClassNotFound,
)).To(BeTrue(), "events seen: %v", received)
})
})

Expand Down
Loading
Loading