@@ -30,6 +30,7 @@ import (
3030 "gotest.tools/v3/assert"
3131 "k8s.io/apimachinery/pkg/util/wait"
3232 "k8s.io/client-go/rest"
33+ "sigs.k8s.io/controller-runtime/pkg/manager"
3334
3435 "github.com/crunchydata/postgres-operator/internal/logging"
3536 "github.com/crunchydata/postgres-operator/internal/testing/cmp"
@@ -56,14 +57,6 @@ func (m *MockClient) Do(req *http.Request) (*http.Response, error) {
5657 return funcFoo ()
5758}
5859
59- type MockCacheClient struct {
60- works bool
61- }
62-
63- func (cc * MockCacheClient ) WaitForCacheSync (ctx context.Context ) bool {
64- return cc .works
65- }
66-
6760func TestCheckForUpgrades (t * testing.T ) {
6861 fakeClient := setupFakeClientWithPGOScheme (t , false )
6962 ctx := logging .NewContext (context .Background (), logging .Discard ())
@@ -168,11 +161,9 @@ func TestCheckForUpgradesScheduler(t *testing.T) {
168161 _ , server := setupVersionServer (t , true )
169162 defer server .Close ()
170163 cfg := & rest.Config {Host : server .URL }
171- const testUpgradeCheckURL = "http://localhost:8080"
172164
173165 t .Run ("panic from checkForUpgrades doesn't bubble up" , func (t * testing.T ) {
174- ctx , cancel := context .WithCancel (context .Background ())
175- defer cancel ()
166+ ctx := context .Background ()
176167
177168 // capture logs
178169 var calls []string
@@ -187,44 +178,18 @@ func TestCheckForUpgradesScheduler(t *testing.T) {
187178 panic (fmt .Errorf ("oh no!" ))
188179 }
189180
190- go CheckForUpgradesScheduler (ctx , "4.7.3" , testUpgradeCheckURL , fakeClient , cfg , false ,
191- & MockCacheClient {works : true })
192- time .Sleep (1 * time .Second )
193- cancel ()
181+ s := CheckForUpgradesScheduler {
182+ Client : fakeClient ,
183+ Config : cfg ,
184+ }
185+ s .check (ctx )
194186
195- // Sleeping leads to some non-deterministic results, but we expect at least 1 execution
196- // plus one log for the failure to apply the configmap
197- assert .Assert (t , len (calls ) >= 2 )
187+ assert .Equal (t , len (calls ), 2 )
198188 assert .Assert (t , cmp .Contains (calls [1 ], `encountered panic in upgrade check` ))
199189 })
200190
201- t .Run ("cache sync fail leads to log and exit" , func (t * testing.T ) {
202- ctx , cancel := context .WithCancel (context .Background ())
203- defer cancel ()
204-
205- // capture logs
206- var calls []string
207- ctx = logging .NewContext (ctx , funcr .NewJSON (func (object string ) {
208- calls = append (calls , object )
209- }, funcr.Options {
210- Verbosity : 1 ,
211- }))
212-
213- // Set loop time to 1s and sleep for 2s before sending the done signal -- though the cache sync
214- // failure will exit the func before the sleep ends
215- upgradeCheckPeriod = 1 * time .Second
216- go CheckForUpgradesScheduler (ctx , "4.7.3" , testUpgradeCheckURL , fakeClient , cfg , false ,
217- & MockCacheClient {works : false })
218- time .Sleep (2 * time .Second )
219- cancel ()
220-
221- assert .Assert (t , len (calls ) == 1 )
222- assert .Assert (t , cmp .Contains (calls [0 ], `unable to sync cache for upgrade check` ))
223- })
224-
225191 t .Run ("successful log each loop, ticker works" , func (t * testing.T ) {
226- ctx , cancel := context .WithCancel (context .Background ())
227- defer cancel ()
192+ ctx := context .Background ()
228193
229194 // capture logs
230195 var calls []string
@@ -244,11 +209,14 @@ func TestCheckForUpgradesScheduler(t *testing.T) {
244209 }
245210
246211 // Set loop time to 1s and sleep for 2s before sending the done signal
247- upgradeCheckPeriod = 1 * time .Second
248- go CheckForUpgradesScheduler (ctx , "4.7.3" , testUpgradeCheckURL , fakeClient , cfg , false ,
249- & MockCacheClient {works : true })
250- time .Sleep (2 * time .Second )
251- cancel ()
212+ ctx , cancel := context .WithTimeout (ctx , 2 * time .Second )
213+ defer cancel ()
214+ s := CheckForUpgradesScheduler {
215+ Client : fakeClient ,
216+ Config : cfg ,
217+ Refresh : 1 * time .Second ,
218+ }
219+ assert .ErrorIs (t , context .DeadlineExceeded , s .Start (ctx ))
252220
253221 // Sleeping leads to some non-deterministic results, but we expect at least 2 executions
254222 // plus one log for the failure to apply the configmap
@@ -258,3 +226,11 @@ func TestCheckForUpgradesScheduler(t *testing.T) {
258226 assert .Assert (t , cmp .Contains (calls [3 ], `{\"pgo_versions\":[{\"tag\":\"v5.0.4\"},{\"tag\":\"v5.0.3\"},{\"tag\":\"v5.0.2\"},{\"tag\":\"v5.0.1\"},{\"tag\":\"v5.0.0\"}]}` ))
259227 })
260228}
229+
230+ func TestCheckForUpgradesSchedulerLeaderOnly (t * testing.T ) {
231+ // CheckForUpgradesScheduler should implement this interface.
232+ var s manager.LeaderElectionRunnable = new (CheckForUpgradesScheduler )
233+
234+ assert .Assert (t , s .NeedLeaderElection (),
235+ "expected to only run on the leader" )
236+ }
0 commit comments