@@ -24,7 +24,9 @@ import (
2424 v1 "k8s.io/api/core/v1"
2525 "k8s.io/apimachinery/pkg/api/resource"
2626 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+ "k8s.io/apimachinery/pkg/runtime"
2728 "k8s.io/client-go/kubernetes/fake"
29+ k8stesting "k8s.io/client-go/testing"
2830 "k8s.io/client-go/tools/record"
2931)
3032
@@ -94,6 +96,7 @@ func TestCreate(t *testing.T) {
9496 clusterNamespace := "test"
9597
9698 client := k8sutil.KubernetesClient {
99+ ConfigMapsGetter : clientSet .CoreV1 (),
97100 DeploymentsGetter : clientSet .AppsV1 (),
98101 CronJobsGetter : clientSet .BatchV1 (),
99102 EndpointsGetter : clientSet .CoreV1 (),
@@ -2202,3 +2205,120 @@ func TestGetSwitchoverSchedule(t *testing.T) {
22022205 })
22032206 }
22042207}
2208+
2209+ func TestUpdatePITRResources (t * testing.T ) {
2210+ clusterName := "test-cluster"
2211+ clusterNamespace := "default"
2212+
2213+ tests := []struct {
2214+ name string
2215+ state string
2216+ cmExists bool
2217+ patchFails bool
2218+ expectedErr bool
2219+ expectedLabel string
2220+ }{
2221+ {
2222+ "successful patch - update label to finished" ,
2223+ PitrStateLabelValueFinished ,
2224+ true ,
2225+ false ,
2226+ false ,
2227+ PitrStateLabelValueFinished ,
2228+ },
2229+ {
2230+ "successful patch - update label to in-progress" ,
2231+ PitrStateLabelValueInProgress ,
2232+ true ,
2233+ false ,
2234+ false ,
2235+ PitrStateLabelValueInProgress ,
2236+ },
2237+ {
2238+ "config map does not exist - no error" ,
2239+ PitrStateLabelValueFinished ,
2240+ false ,
2241+ false ,
2242+ false ,
2243+ "" ,
2244+ },
2245+ {
2246+ "patch fails with non-NotFound error" ,
2247+ PitrStateLabelValueFinished ,
2248+ true ,
2249+ true ,
2250+ true ,
2251+ "" ,
2252+ },
2253+ }
2254+
2255+ for _ , tt := range tests {
2256+ t .Run (tt .name , func (t * testing.T ) {
2257+ clientSet := fake .NewSimpleClientset ()
2258+ acidClientSet := fakeacidv1 .NewSimpleClientset ()
2259+
2260+ if tt .cmExists {
2261+ cmName := fmt .Sprintf (PitrConfigMapNameTemplate , clusterName )
2262+ cm := & v1.ConfigMap {
2263+ ObjectMeta : metav1.ObjectMeta {
2264+ Name : cmName ,
2265+ Namespace : clusterNamespace ,
2266+ Labels : map [string ]string {
2267+ PitrStateLabelKey : PitrStateLabelValuePending ,
2268+ },
2269+ },
2270+ }
2271+ _ , err := clientSet .CoreV1 ().ConfigMaps (clusterNamespace ).Create (context .TODO (), cm , metav1.CreateOptions {})
2272+ if err != nil {
2273+ t .Fatalf ("could not create configmap: %v" , err )
2274+ }
2275+ }
2276+
2277+ if tt .patchFails {
2278+ clientSet .PrependReactor ("patch" , "configmaps" , func (action k8stesting.Action ) (handled bool , ret runtime.Object , err error ) {
2279+ return true , nil , fmt .Errorf ("synthetic patch error" )
2280+ })
2281+ }
2282+
2283+ client := k8sutil.KubernetesClient {
2284+ ConfigMapsGetter : clientSet .CoreV1 (),
2285+ PostgresqlsGetter : acidClientSet .AcidV1 (),
2286+ }
2287+
2288+ pg := acidv1.Postgresql {
2289+ ObjectMeta : metav1.ObjectMeta {
2290+ Name : clusterName ,
2291+ Namespace : clusterNamespace ,
2292+ },
2293+ }
2294+
2295+ cluster := New (
2296+ Config {
2297+ OpConfig : config.Config {
2298+ PodManagementPolicy : "ordered_ready" ,
2299+ },
2300+ }, client , pg , logger , eventRecorder )
2301+
2302+ err := cluster .updatePITRResources (tt .state )
2303+
2304+ if err != nil {
2305+ if ! tt .expectedErr {
2306+ t .Fatalf ("unexpected error: %v" , err )
2307+ }
2308+ } else if tt .expectedErr {
2309+ t .Fatalf ("expected error, but got none" )
2310+ }
2311+
2312+ if tt .cmExists && ! tt .patchFails && tt .expectedLabel != "" {
2313+ cmName := fmt .Sprintf (PitrConfigMapNameTemplate , clusterName )
2314+ updatedCm , err := clientSet .CoreV1 ().ConfigMaps (clusterNamespace ).Get (context .TODO (), cmName , metav1.GetOptions {})
2315+ if err != nil {
2316+ t .Fatalf ("could not get configmap: %v" , err )
2317+ }
2318+ if updatedCm .Labels [PitrStateLabelKey ] != tt .expectedLabel {
2319+ t .Errorf ("expected label %q but got %q" , tt .expectedLabel , updatedCm .Labels [PitrStateLabelKey ])
2320+ }
2321+ }
2322+ })
2323+ }
2324+ }
0 commit comments