Skip to content

Commit 95c50a2

Browse files
committed
Add/adjust tests for cloud repo backup job changes.
1 parent 9ee53b8 commit 95c50a2

File tree

7 files changed

+452
-80
lines changed

7 files changed

+452
-80
lines changed

internal/controller/postgrescluster/instance.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,7 @@ func (r *Reconciler) reconcileInstance(
11721172
}
11731173
if err == nil {
11741174
instanceCertificates, err = r.reconcileInstanceCertificates(
1175-
ctx, cluster, spec, instance, rootCA)
1175+
ctx, cluster, spec, instance, rootCA, backupsSpecFound)
11761176
}
11771177
if err == nil {
11781178
postgresDataVolume, err = r.reconcilePostgresDataVolume(ctx, cluster, spec, instance, clusterVolumes, nil)
@@ -1465,7 +1465,7 @@ func (r *Reconciler) reconcileInstanceConfigMap(
14651465
func (r *Reconciler) reconcileInstanceCertificates(
14661466
ctx context.Context, cluster *v1beta1.PostgresCluster,
14671467
spec *v1beta1.PostgresInstanceSetSpec, instance *appsv1.StatefulSet,
1468-
root *pki.RootCertificateAuthority,
1468+
root *pki.RootCertificateAuthority, backupsSpecFound bool,
14691469
) (*corev1.Secret, error) {
14701470
existing := &corev1.Secret{ObjectMeta: naming.InstanceCertificates(instance)}
14711471
err := errors.WithStack(client.IgnoreNotFound(
@@ -1508,7 +1508,7 @@ func (r *Reconciler) reconcileInstanceCertificates(
15081508
root.Certificate, leafCert.Certificate,
15091509
leafCert.PrivateKey, instanceCerts)
15101510
}
1511-
if err == nil {
1511+
if err == nil && backupsSpecFound {
15121512
err = pgbackrest.InstanceCertificates(ctx, cluster,
15131513
root.Certificate, leafCert.Certificate, leafCert.PrivateKey,
15141514
instanceCerts)

internal/controller/postgrescluster/instance_test.go

Lines changed: 26 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -544,49 +544,7 @@ func TestAddPGBackRestToInstancePodSpec(t *testing.T) {
544544
},
545545
}
546546

547-
t.Run("NoVolumeRepo", func(t *testing.T) {
548-
cluster := cluster.DeepCopy()
549-
cluster.Spec.Backups.PGBackRest.Repos = nil
550-
551-
out := pod.DeepCopy()
552-
addPGBackRestToInstancePodSpec(ctx, cluster, &certificates, out)
553-
554-
// Only Containers and Volumes fields have changed.
555-
assert.DeepEqual(t, pod, *out, cmpopts.IgnoreFields(pod, "Containers", "Volumes"))
556-
557-
// Only database container has mounts.
558-
// Other containers are ignored.
559-
assert.Assert(t, cmp.MarshalMatches(out.Containers, `
560-
- name: database
561-
resources: {}
562-
volumeMounts:
563-
- mountPath: /etc/pgbackrest/conf.d
564-
name: pgbackrest-config
565-
readOnly: true
566-
- name: other
567-
resources: {}
568-
`))
569-
570-
// Instance configuration files but no certificates.
571-
// Other volumes are ignored.
572-
assert.Assert(t, cmp.MarshalMatches(out.Volumes, `
573-
- name: other
574-
- name: postgres-data
575-
- name: postgres-wal
576-
- name: pgbackrest-config
577-
projected:
578-
sources:
579-
- configMap:
580-
items:
581-
- key: pgbackrest_instance.conf
582-
path: pgbackrest_instance.conf
583-
- key: config-hash
584-
path: config-hash
585-
name: hippo-pgbackrest-config
586-
`))
587-
})
588-
589-
t.Run("OneVolumeRepo", func(t *testing.T) {
547+
t.Run("CloudOrVolumeSameBehavior", func(t *testing.T) {
590548
alwaysExpect := func(t testing.TB, result *corev1.PodSpec) {
591549
// Only Containers and Volumes fields have changed.
592550
assert.DeepEqual(t, pod, *result, cmpopts.IgnoreFields(pod, "Containers", "Volumes"))
@@ -635,21 +593,31 @@ func TestAddPGBackRestToInstancePodSpec(t *testing.T) {
635593
`))
636594
}
637595

638-
cluster := cluster.DeepCopy()
639-
cluster.Spec.Backups.PGBackRest.Repos = []v1beta1.PGBackRestRepo{
596+
clusterWithVolume := cluster.DeepCopy()
597+
clusterWithVolume.Spec.Backups.PGBackRest.Repos = []v1beta1.PGBackRestRepo{
640598
{
641599
Name: "repo1",
642600
Volume: new(v1beta1.RepoPVC),
643601
},
644602
}
645603

646-
out := pod.DeepCopy()
647-
addPGBackRestToInstancePodSpec(ctx, cluster, &certificates, out)
648-
alwaysExpect(t, out)
604+
clusterWithCloudRepo := cluster.DeepCopy()
605+
clusterWithCloudRepo.Spec.Backups.PGBackRest.Repos = []v1beta1.PGBackRestRepo{
606+
{
607+
Name: "repo1",
608+
GCS: new(v1beta1.RepoGCS),
609+
},
610+
}
611+
612+
outWithVolume := pod.DeepCopy()
613+
addPGBackRestToInstancePodSpec(ctx, clusterWithVolume, &certificates, outWithVolume)
614+
alwaysExpect(t, outWithVolume)
649615

650-
// The TLS server is added and configuration mounted.
651-
// It has PostgreSQL volumes mounted while other volumes are ignored.
652-
assert.Assert(t, cmp.MarshalMatches(out.Containers, `
616+
outWithCloudRepo := pod.DeepCopy()
617+
addPGBackRestToInstancePodSpec(ctx, clusterWithCloudRepo, &certificates, outWithCloudRepo)
618+
alwaysExpect(t, outWithCloudRepo)
619+
620+
outContainers := `
653621
- name: database
654622
resources: {}
655623
volumeMounts:
@@ -737,7 +705,12 @@ func TestAddPGBackRestToInstancePodSpec(t *testing.T) {
737705
- mountPath: /etc/pgbackrest/conf.d
738706
name: pgbackrest-config
739707
readOnly: true
740-
`))
708+
`
709+
710+
// The TLS server is added and configuration mounted.
711+
// It has PostgreSQL volumes mounted while other volumes are ignored.
712+
assert.Assert(t, cmp.MarshalMatches(outWithVolume.Containers, outContainers))
713+
assert.Assert(t, cmp.MarshalMatches(outWithCloudRepo.Containers, outContainers))
741714

742715
t.Run("CustomResources", func(t *testing.T) {
743716
cluster := cluster.DeepCopy()
@@ -754,7 +727,7 @@ func TestAddPGBackRestToInstancePodSpec(t *testing.T) {
754727
},
755728
}
756729

757-
before := out.DeepCopy()
730+
before := outWithVolume.DeepCopy()
758731
out := pod.DeepCopy()
759732
addPGBackRestToInstancePodSpec(ctx, cluster, &certificates, out)
760733
alwaysExpect(t, out)

internal/controller/postgrescluster/pgbackrest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2034,7 +2034,7 @@ func (r *Reconciler) copyConfigurationResources(ctx context.Context, cluster,
20342034
return nil
20352035
}
20362036

2037-
// reconcilePGBackRestConfig is responsible for reconciling the pgBackRest ConfigMaps and Secrets.
2037+
// reconcilePGBackRestConfig is responsible for reconciling the pgBackRest ConfigMaps.
20382038
func (r *Reconciler) reconcilePGBackRestConfig(ctx context.Context,
20392039
postgresCluster *v1beta1.PostgresCluster,
20402040
repoHostName, configHash, serviceName, serviceNamespace string,

internal/controller/postgrescluster/pgbackrest_test.go

Lines changed: 90 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2602,9 +2602,81 @@ func TestCopyConfigurationResources(t *testing.T) {
26022602

26032603
func TestGenerateBackupJobIntent(t *testing.T) {
26042604
ctx := context.Background()
2605+
cluster := v1beta1.PostgresCluster{}
2606+
cluster.Name = "hippo-test"
2607+
cluster.Default()
2608+
2609+
// The code assumes that if repo.Volume is nil, then it is a cloud repo backup,
2610+
// therefore, an "empty" input results in a job spec for a cloud repo backup
26052611
t.Run("empty", func(t *testing.T) {
26062612
spec, err := generateBackupJobSpecIntent(ctx,
2607-
&v1beta1.PostgresCluster{}, v1beta1.PGBackRestRepo{},
2613+
&cluster, v1beta1.PGBackRestRepo{},
2614+
"",
2615+
nil, nil,
2616+
)
2617+
assert.NilError(t, err)
2618+
assert.Assert(t, cmp.MarshalMatches(spec.Template.Spec, `
2619+
containers:
2620+
- command:
2621+
- /bin/pgbackrest
2622+
- backup
2623+
- --stanza=db
2624+
- --repo=
2625+
name: pgbackrest
2626+
resources: {}
2627+
securityContext:
2628+
allowPrivilegeEscalation: false
2629+
capabilities:
2630+
drop:
2631+
- ALL
2632+
privileged: false
2633+
readOnlyRootFilesystem: true
2634+
runAsNonRoot: true
2635+
seccompProfile:
2636+
type: RuntimeDefault
2637+
volumeMounts:
2638+
- mountPath: /etc/pgbackrest/conf.d
2639+
name: pgbackrest-config
2640+
readOnly: true
2641+
- mountPath: /tmp
2642+
name: tmp
2643+
enableServiceLinks: false
2644+
restartPolicy: Never
2645+
securityContext:
2646+
fsGroup: 26
2647+
fsGroupChangePolicy: OnRootMismatch
2648+
volumes:
2649+
- name: pgbackrest-config
2650+
projected:
2651+
sources:
2652+
- configMap:
2653+
items:
2654+
- key: pgbackrest_cloud.conf
2655+
path: pgbackrest_cloud.conf
2656+
name: hippo-test-pgbackrest-config
2657+
- secret:
2658+
items:
2659+
- key: pgbackrest.ca-roots
2660+
path: ~postgres-operator/tls-ca.crt
2661+
- key: pgbackrest-client.crt
2662+
path: ~postgres-operator/client-tls.crt
2663+
- key: pgbackrest-client.key
2664+
mode: 384
2665+
path: ~postgres-operator/client-tls.key
2666+
name: hippo-test-pgbackrest
2667+
- emptyDir:
2668+
sizeLimit: 16Mi
2669+
name: tmp
2670+
`))
2671+
})
2672+
2673+
t.Run("volumeRepo", func(t *testing.T) {
2674+
spec, err := generateBackupJobSpecIntent(ctx,
2675+
&cluster, v1beta1.PGBackRestRepo{
2676+
Volume: &v1beta1.RepoPVC{
2677+
VolumeClaimSpec: v1beta1.VolumeClaimSpec{},
2678+
},
2679+
},
26082680
"",
26092681
nil, nil,
26102682
)
@@ -2621,10 +2693,10 @@ containers:
26212693
- name: COMPARE_HASH
26222694
value: "true"
26232695
- name: CONTAINER
2624-
value: database
2696+
value: pgbackrest
26252697
- name: NAMESPACE
26262698
- name: SELECTOR
2627-
value: postgres-operator.crunchydata.com/cluster=,postgres-operator.crunchydata.com/instance,postgres-operator.crunchydata.com/role=master
2699+
value: postgres-operator.crunchydata.com/cluster=hippo-test,postgres-operator.crunchydata.com/pgbackrest=,postgres-operator.crunchydata.com/pgbackrest-dedicated=
26282700
name: pgbackrest
26292701
resources: {}
26302702
securityContext:
@@ -2651,11 +2723,23 @@ volumes:
26512723
sources:
26522724
- configMap:
26532725
items:
2654-
- key: pgbackrest_instance.conf
2655-
path: pgbackrest_instance.conf
2726+
- key: pgbackrest_repo.conf
2727+
path: pgbackrest_repo.conf
26562728
- key: config-hash
26572729
path: config-hash
2658-
name: -pgbackrest-config
2730+
- key: pgbackrest-server.conf
2731+
path: ~postgres-operator_server.conf
2732+
name: hippo-test-pgbackrest-config
2733+
- secret:
2734+
items:
2735+
- key: pgbackrest.ca-roots
2736+
path: ~postgres-operator/tls-ca.crt
2737+
- key: pgbackrest-client.crt
2738+
path: ~postgres-operator/client-tls.crt
2739+
- key: pgbackrest-client.key
2740+
mode: 384
2741+
path: ~postgres-operator/client-tls.key
2742+
name: hippo-test-pgbackrest
26592743
`))
26602744
})
26612745

0 commit comments

Comments
 (0)