github.com/ChicK00o/awgo@v0.29.4/workflow_update_test.go (about) 1 // Copyright (c) 2019 Dean Jackson <deanishe@deanishe.net> 2 // MIT Licence applies http://opensource.org/licenses/MIT 3 4 package aw 5 6 import ( 7 "errors" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 // ensure mockUpdater implements Updater 15 var _ Updater = (*mockUpdater)(nil) 16 17 type mockUpdater struct { 18 updateIntervalCalled bool 19 checkDueCalled bool 20 checkForUpdateCalled bool 21 updateAvailableCalled bool 22 installCalled bool 23 24 checkShouldFail bool 25 installShouldFail bool 26 } 27 28 // UpdateInterval implements Updater. 29 func (d *mockUpdater) UpdateInterval(_ time.Duration) { 30 d.updateIntervalCalled = true 31 } 32 33 // UpdateAvailable implements Updater. 34 func (d *mockUpdater) UpdateAvailable() bool { 35 d.updateAvailableCalled = true 36 return true 37 } 38 39 // CheckDue implements Updater. 40 func (d *mockUpdater) CheckDue() bool { 41 d.checkDueCalled = true 42 return true 43 } 44 45 // CheckForUpdate implements Updater. 46 func (d *mockUpdater) CheckForUpdate() error { 47 d.checkForUpdateCalled = true 48 if d.checkShouldFail { 49 return errors.New("check failed") 50 } 51 return nil 52 } 53 54 // Install implements Updater. 55 func (d *mockUpdater) Install() error { 56 d.installCalled = true 57 if d.installShouldFail { 58 return errors.New("install failed") 59 } 60 return nil 61 } 62 63 // Test that Workflow API responses match configured Updater's. 64 func TestWorkflowUpdater(t *testing.T) { 65 t.Parallel() 66 67 wf := New() 68 // false/fail when Updater is unset 69 assert.False(t, wf.UpdateCheckDue(), "unexpected UpdateCheckDue") 70 assert.False(t, wf.UpdateAvailable(), "unexpected UpdateAvailable") 71 assert.NotNil(t, wf.CheckForUpdate(), "CheckForUpdate succeeded") 72 assert.NotNil(t, wf.InstallUpdate(), "InstallUpdate succeeded") 73 74 // true/success with mockUpdater 75 u := &mockUpdater{} 76 _ = wf.Configure(Update(u)) 77 assert.True(t, wf.UpdateCheckDue(), "unexpected UpdateCheckDue") 78 assert.True(t, u.checkDueCalled, "checkDue not called") 79 assert.True(t, wf.UpdateAvailable(), "unexpected UpdateAvailable") 80 assert.True(t, u.updateAvailableCalled, "updateAvailable not called") 81 assert.Nil(t, wf.CheckForUpdate(), "CheckForUpdate failed") 82 assert.True(t, u.checkForUpdateCalled, "checkForUpdate not called") 83 assert.Nil(t, wf.InstallUpdate(), "InstallUpdate failed") 84 assert.True(t, u.installCalled, "installCalled not called") 85 }