@@ -40,6 +40,7 @@ import (
40
40
"github.com/crunchydata/postgres-operator/internal/pgbackrest"
41
41
"github.com/crunchydata/postgres-operator/internal/pki"
42
42
"github.com/crunchydata/postgres-operator/internal/testing/cmp"
43
+ "github.com/crunchydata/postgres-operator/internal/testing/events"
43
44
"github.com/crunchydata/postgres-operator/internal/testing/require"
44
45
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
45
46
)
@@ -2601,6 +2602,17 @@ func TestCopyConfigurationResources(t *testing.T) {
2601
2602
}
2602
2603
2603
2604
func TestGenerateBackupJobIntent (t * testing.T ) {
2605
+ _ , cc := setupKubernetes (t )
2606
+ require .ParallelCapacity (t , 0 )
2607
+ ns := setupNamespace (t , cc )
2608
+
2609
+ recorder := events .NewRecorder (t , runtime .Scheme )
2610
+ r := & Reconciler {
2611
+ Client : cc ,
2612
+ Recorder : recorder ,
2613
+ Owner : ControllerName ,
2614
+ }
2615
+
2604
2616
ctx := context .Background ()
2605
2617
cluster := v1beta1.PostgresCluster {}
2606
2618
cluster .Name = "hippo-test"
@@ -2609,7 +2621,7 @@ func TestGenerateBackupJobIntent(t *testing.T) {
2609
2621
// If repo.Volume is nil, the code interprets this as a cloud repo backup,
2610
2622
// therefore, an "empty" input results in a job spec for a cloud repo backup
2611
2623
t .Run ("empty" , func (t * testing.T ) {
2612
- spec := generateBackupJobSpecIntent (ctx ,
2624
+ spec := r . generateBackupJobSpecIntent (ctx ,
2613
2625
& cluster , v1beta1.PGBackRestRepo {},
2614
2626
"" ,
2615
2627
nil , nil ,
@@ -2670,7 +2682,7 @@ volumes:
2670
2682
})
2671
2683
2672
2684
t .Run ("volumeRepo" , func (t * testing.T ) {
2673
- spec := generateBackupJobSpecIntent (ctx ,
2685
+ spec := r . generateBackupJobSpecIntent (ctx ,
2674
2686
& cluster , v1beta1.PGBackRestRepo {
2675
2687
Volume : & v1beta1.RepoPVC {
2676
2688
VolumeClaimSpec : v1beta1.VolumeClaimSpec {},
@@ -2747,7 +2759,7 @@ volumes:
2747
2759
ImagePullPolicy : corev1 .PullAlways ,
2748
2760
},
2749
2761
}
2750
- job := generateBackupJobSpecIntent (ctx ,
2762
+ job := r . generateBackupJobSpecIntent (ctx ,
2751
2763
cluster , v1beta1.PGBackRestRepo {},
2752
2764
"" ,
2753
2765
nil , nil ,
@@ -2762,7 +2774,7 @@ volumes:
2762
2774
cluster .Spec .Backups = v1beta1.Backups {
2763
2775
PGBackRest : v1beta1.PGBackRestArchive {},
2764
2776
}
2765
- job := generateBackupJobSpecIntent (ctx ,
2777
+ job := r . generateBackupJobSpecIntent (ctx ,
2766
2778
cluster , v1beta1.PGBackRestRepo {},
2767
2779
"" ,
2768
2780
nil , nil ,
@@ -2779,7 +2791,7 @@ volumes:
2779
2791
},
2780
2792
},
2781
2793
}
2782
- job := generateBackupJobSpecIntent (ctx ,
2794
+ job := r . generateBackupJobSpecIntent (ctx ,
2783
2795
cluster , v1beta1.PGBackRestRepo {},
2784
2796
"" ,
2785
2797
nil , nil ,
@@ -2818,7 +2830,7 @@ volumes:
2818
2830
},
2819
2831
},
2820
2832
}
2821
- job := generateBackupJobSpecIntent (ctx ,
2833
+ job := r . generateBackupJobSpecIntent (ctx ,
2822
2834
cluster , v1beta1.PGBackRestRepo {},
2823
2835
"" ,
2824
2836
nil , nil ,
@@ -2831,7 +2843,7 @@ volumes:
2831
2843
cluster .Spec .Backups .PGBackRest .Jobs = & v1beta1.BackupJobs {
2832
2844
PriorityClassName : initialize .String ("some-priority-class" ),
2833
2845
}
2834
- job := generateBackupJobSpecIntent (ctx ,
2846
+ job := r . generateBackupJobSpecIntent (ctx ,
2835
2847
cluster , v1beta1.PGBackRestRepo {},
2836
2848
"" ,
2837
2849
nil , nil ,
@@ -2849,7 +2861,7 @@ volumes:
2849
2861
cluster .Spec .Backups .PGBackRest .Jobs = & v1beta1.BackupJobs {
2850
2862
Tolerations : tolerations ,
2851
2863
}
2852
- job := generateBackupJobSpecIntent (ctx ,
2864
+ job := r . generateBackupJobSpecIntent (ctx ,
2853
2865
cluster , v1beta1.PGBackRestRepo {},
2854
2866
"" ,
2855
2867
nil , nil ,
@@ -2863,14 +2875,14 @@ volumes:
2863
2875
t .Run ("Undefined" , func (t * testing.T ) {
2864
2876
cluster .Spec .Backups .PGBackRest .Jobs = nil
2865
2877
2866
- spec := generateBackupJobSpecIntent (ctx ,
2878
+ spec := r . generateBackupJobSpecIntent (ctx ,
2867
2879
cluster , v1beta1.PGBackRestRepo {}, "" , nil , nil ,
2868
2880
)
2869
2881
assert .Assert (t , spec .TTLSecondsAfterFinished == nil )
2870
2882
2871
2883
cluster .Spec .Backups .PGBackRest .Jobs = & v1beta1.BackupJobs {}
2872
2884
2873
- spec = generateBackupJobSpecIntent (ctx ,
2885
+ spec = r . generateBackupJobSpecIntent (ctx ,
2874
2886
cluster , v1beta1.PGBackRestRepo {}, "" , nil , nil ,
2875
2887
)
2876
2888
assert .Assert (t , spec .TTLSecondsAfterFinished == nil )
@@ -2881,7 +2893,7 @@ volumes:
2881
2893
TTLSecondsAfterFinished : initialize .Int32 (0 ),
2882
2894
}
2883
2895
2884
- spec := generateBackupJobSpecIntent (ctx ,
2896
+ spec := r . generateBackupJobSpecIntent (ctx ,
2885
2897
cluster , v1beta1.PGBackRestRepo {}, "" , nil , nil ,
2886
2898
)
2887
2899
if assert .Check (t , spec .TTLSecondsAfterFinished != nil ) {
@@ -2894,14 +2906,169 @@ volumes:
2894
2906
TTLSecondsAfterFinished : initialize .Int32 (100 ),
2895
2907
}
2896
2908
2897
- spec := generateBackupJobSpecIntent (ctx ,
2909
+ spec := r . generateBackupJobSpecIntent (ctx ,
2898
2910
cluster , v1beta1.PGBackRestRepo {}, "" , nil , nil ,
2899
2911
)
2900
2912
if assert .Check (t , spec .TTLSecondsAfterFinished != nil ) {
2901
2913
assert .Equal (t , * spec .TTLSecondsAfterFinished , int32 (100 ))
2902
2914
}
2903
2915
})
2904
2916
})
2917
+
2918
+ t .Run ("CloudLogVolumeAnnotationNoPvc" , func (t * testing.T ) {
2919
+ cluster .Namespace = ns .Name
2920
+ cluster .Annotations = map [string ]string {}
2921
+ cluster .Annotations [naming .PGBackRestCloudLogVolume ] = "some-pvc"
2922
+ spec := r .generateBackupJobSpecIntent (ctx ,
2923
+ & cluster , v1beta1.PGBackRestRepo {},
2924
+ "" ,
2925
+ nil , nil ,
2926
+ )
2927
+ assert .Assert (t , cmp .MarshalMatches (spec .Template .Spec , `
2928
+ containers:
2929
+ - command:
2930
+ - /bin/pgbackrest
2931
+ - backup
2932
+ - --stanza=db
2933
+ - --repo=
2934
+ name: pgbackrest
2935
+ resources: {}
2936
+ securityContext:
2937
+ allowPrivilegeEscalation: false
2938
+ capabilities:
2939
+ drop:
2940
+ - ALL
2941
+ privileged: false
2942
+ readOnlyRootFilesystem: true
2943
+ runAsNonRoot: true
2944
+ seccompProfile:
2945
+ type: RuntimeDefault
2946
+ volumeMounts:
2947
+ - mountPath: /etc/pgbackrest/conf.d
2948
+ name: pgbackrest-config
2949
+ readOnly: true
2950
+ - mountPath: /tmp
2951
+ name: tmp
2952
+ enableServiceLinks: false
2953
+ restartPolicy: Never
2954
+ securityContext:
2955
+ fsGroup: 26
2956
+ fsGroupChangePolicy: OnRootMismatch
2957
+ volumes:
2958
+ - name: pgbackrest-config
2959
+ projected:
2960
+ sources:
2961
+ - configMap:
2962
+ items:
2963
+ - key: pgbackrest_cloud.conf
2964
+ path: pgbackrest_cloud.conf
2965
+ name: hippo-test-pgbackrest-config
2966
+ - secret:
2967
+ items:
2968
+ - key: pgbackrest.ca-roots
2969
+ path: ~postgres-operator/tls-ca.crt
2970
+ - key: pgbackrest-client.crt
2971
+ path: ~postgres-operator/client-tls.crt
2972
+ - key: pgbackrest-client.key
2973
+ mode: 384
2974
+ path: ~postgres-operator/client-tls.key
2975
+ name: hippo-test-pgbackrest
2976
+ - emptyDir:
2977
+ sizeLimit: 16Mi
2978
+ name: tmp
2979
+ ` ))
2980
+
2981
+ assert .Equal (t , len (recorder .Events ), 1 )
2982
+ assert .Equal (t , recorder .Events [0 ].Regarding .Name , cluster .Name )
2983
+ assert .Equal (t , recorder .Events [0 ].Reason , "PGBackRestCloudLogVolumeNotFound" )
2984
+ assert .Equal (t , recorder .Events [0 ].Note , "persistentvolumeclaims \" some-pvc\" not found" )
2985
+ })
2986
+
2987
+ t .Run ("CloudLogVolumeAnnotationPvcInPlace" , func (t * testing.T ) {
2988
+ cluster .Namespace = ns .Name
2989
+ cluster .Annotations = map [string ]string {}
2990
+ cluster .Annotations [naming .PGBackRestCloudLogVolume ] = "another-pvc"
2991
+
2992
+ pvc := & corev1.PersistentVolumeClaim {
2993
+ ObjectMeta : metav1.ObjectMeta {
2994
+ Name : "another-pvc" ,
2995
+ Namespace : ns .Name ,
2996
+ },
2997
+ Spec : corev1 .PersistentVolumeClaimSpec (testVolumeClaimSpec ()),
2998
+ }
2999
+ err := r .Client .Create (ctx , pvc )
3000
+ assert .NilError (t , err )
3001
+
3002
+ spec := r .generateBackupJobSpecIntent (ctx ,
3003
+ & cluster , v1beta1.PGBackRestRepo {},
3004
+ "" ,
3005
+ nil , nil ,
3006
+ )
3007
+ assert .Assert (t , cmp .MarshalMatches (spec .Template .Spec , `
3008
+ containers:
3009
+ - command:
3010
+ - /bin/pgbackrest
3011
+ - backup
3012
+ - --stanza=db
3013
+ - --repo=
3014
+ name: pgbackrest
3015
+ resources: {}
3016
+ securityContext:
3017
+ allowPrivilegeEscalation: false
3018
+ capabilities:
3019
+ drop:
3020
+ - ALL
3021
+ privileged: false
3022
+ readOnlyRootFilesystem: true
3023
+ runAsNonRoot: true
3024
+ seccompProfile:
3025
+ type: RuntimeDefault
3026
+ volumeMounts:
3027
+ - mountPath: /etc/pgbackrest/conf.d
3028
+ name: pgbackrest-config
3029
+ readOnly: true
3030
+ - mountPath: /tmp
3031
+ name: tmp
3032
+ - mountPath: /volumes/another-pvc
3033
+ name: another-pvc
3034
+ enableServiceLinks: false
3035
+ restartPolicy: Never
3036
+ securityContext:
3037
+ fsGroup: 26
3038
+ fsGroupChangePolicy: OnRootMismatch
3039
+ volumes:
3040
+ - name: pgbackrest-config
3041
+ projected:
3042
+ sources:
3043
+ - configMap:
3044
+ items:
3045
+ - key: pgbackrest_cloud.conf
3046
+ path: pgbackrest_cloud.conf
3047
+ name: hippo-test-pgbackrest-config
3048
+ - secret:
3049
+ items:
3050
+ - key: pgbackrest.ca-roots
3051
+ path: ~postgres-operator/tls-ca.crt
3052
+ - key: pgbackrest-client.crt
3053
+ path: ~postgres-operator/client-tls.crt
3054
+ - key: pgbackrest-client.key
3055
+ mode: 384
3056
+ path: ~postgres-operator/client-tls.key
3057
+ name: hippo-test-pgbackrest
3058
+ - emptyDir:
3059
+ sizeLimit: 16Mi
3060
+ name: tmp
3061
+ - name: another-pvc
3062
+ persistentVolumeClaim:
3063
+ claimName: another-pvc
3064
+ ` ))
3065
+
3066
+ // No new events
3067
+ assert .Equal (t , len (recorder .Events ), 1 )
3068
+ assert .Equal (t , recorder .Events [0 ].Regarding .Name , cluster .Name )
3069
+ assert .Equal (t , recorder .Events [0 ].Reason , "PGBackRestCloudLogVolumeNotFound" )
3070
+ assert .Equal (t , recorder .Events [0 ].Note , "persistentvolumeclaims \" some-pvc\" not found" )
3071
+ })
2905
3072
}
2906
3073
2907
3074
func TestGenerateRepoHostIntent (t * testing.T ) {
0 commit comments