@@ -2057,3 +2057,91 @@ func TestCompareVolumeMounts(t *testing.T) {
20572057 })
20582058 }
20592059}
2060+
2061+ func TestGetSwitchoverSchedule (t * testing.T ) {
2062+ now := time .Now ()
2063+
2064+ futureTimeStart := now .Add (1 * time .Hour )
2065+ futureWindowTimeStart := futureTimeStart .Format ("15:04" )
2066+ futureWindowTimeEnd := now .Add (2 * time .Hour ).Format ("15:04" )
2067+ pastTimeStart := now .Add (- 2 * time .Hour )
2068+ pastWindowTimeStart := pastTimeStart .Format ("15:04" )
2069+ pastWindowTimeEnd := now .Add (- 1 * time .Hour ).Format ("15:04" )
2070+
2071+ tests := []struct {
2072+ name string
2073+ windows []acidv1.MaintenanceWindow
2074+ expected string
2075+ }{
2076+ {
2077+ name : "everyday maintenance windows is later today" ,
2078+ windows : []acidv1.MaintenanceWindow {
2079+ {
2080+ Everyday : true ,
2081+ StartTime : mustParseTime (futureWindowTimeStart ),
2082+ EndTime : mustParseTime (futureWindowTimeEnd ),
2083+ },
2084+ },
2085+ expected : futureTimeStart .Format ("2006-01-02T15:04+00" ),
2086+ },
2087+ {
2088+ name : "everyday maintenance window is tomorrow" ,
2089+ windows : []acidv1.MaintenanceWindow {
2090+ {
2091+ Everyday : true ,
2092+ StartTime : mustParseTime (pastWindowTimeStart ),
2093+ EndTime : mustParseTime (pastWindowTimeEnd ),
2094+ },
2095+ },
2096+ expected : pastTimeStart .AddDate (0 , 0 , 1 ).Format ("2006-01-02T15:04+00" ),
2097+ },
2098+ {
2099+ name : "weekday maintenance windows is later today" ,
2100+ windows : []acidv1.MaintenanceWindow {
2101+ {
2102+ Weekday : now .Weekday (),
2103+ StartTime : mustParseTime (futureWindowTimeStart ),
2104+ EndTime : mustParseTime (futureWindowTimeEnd ),
2105+ },
2106+ },
2107+ expected : futureTimeStart .Format ("2006-01-02T15:04+00" ),
2108+ },
2109+ {
2110+ name : "weekday maintenance windows is passed for today" ,
2111+ windows : []acidv1.MaintenanceWindow {
2112+ {
2113+ Weekday : now .Weekday (),
2114+ StartTime : mustParseTime (pastWindowTimeStart ),
2115+ EndTime : mustParseTime (pastWindowTimeEnd ),
2116+ },
2117+ },
2118+ expected : pastTimeStart .AddDate (0 , 0 , 7 ).Format ("2006-01-02T15:04+00" ),
2119+ },
2120+ {
2121+ name : "choose the earliest window" ,
2122+ windows : []acidv1.MaintenanceWindow {
2123+ {
2124+ Weekday : now .AddDate (0 , 0 , 2 ).Weekday (),
2125+ StartTime : mustParseTime (futureWindowTimeStart ),
2126+ EndTime : mustParseTime (futureWindowTimeEnd ),
2127+ },
2128+ {
2129+ Everyday : true ,
2130+ StartTime : mustParseTime (pastWindowTimeStart ),
2131+ EndTime : mustParseTime (pastWindowTimeEnd ),
2132+ },
2133+ },
2134+ expected : pastTimeStart .AddDate (0 , 0 , 1 ).Format ("2006-01-02T15:04+00" ),
2135+ },
2136+ }
2137+
2138+ for _ , tt := range tests {
2139+ t .Run (tt .name , func (t * testing.T ) {
2140+ cluster .Spec .MaintenanceWindows = tt .windows
2141+ schedule := cluster .GetSwitchoverSchedule ()
2142+ if schedule != tt .expected {
2143+ t .Errorf ("Expected GetSwitchoverSchedule to return %s, returned: %s" , tt .expected , schedule )
2144+ }
2145+ })
2146+ }
2147+ }
0 commit comments