forked from deanishe/awgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workflow_update_test.go
107 lines (94 loc) · 2.46 KB
/
workflow_update_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) 2019 Dean Jackson <[email protected]>
// MIT Licence applies http://opensource.org/licenses/MIT
package aw
import (
"errors"
"testing"
"time"
)
// ensure mockUpdater implements Updater
var _ Updater = (*mockUpdater)(nil)
type mockUpdater struct {
updateIntervalCalled bool
checkDueCalled bool
checkForUpdateCalled bool
updateAvailableCalled bool
installCalled bool
checkShouldFail bool
installShouldFail bool
}
// UpdateInterval implements Updater.
func (d *mockUpdater) UpdateInterval(_ time.Duration) {
d.updateIntervalCalled = true
}
// UpdateAvailable implements Updater.
func (d *mockUpdater) UpdateAvailable() bool {
d.updateAvailableCalled = true
return true
}
// CheckDue implements Updater.
func (d *mockUpdater) CheckDue() bool {
d.checkDueCalled = true
return true
}
// CheckForUpdate implements Updater.
func (d *mockUpdater) CheckForUpdate() error {
d.checkForUpdateCalled = true
if d.checkShouldFail {
return errors.New("check failed")
}
return nil
}
// Install implements Updater.
func (d *mockUpdater) Install() error {
d.installCalled = true
if d.installShouldFail {
return errors.New("install failed")
}
return nil
}
// Test that Workflow API responses match configured Updater's.
func TestWorkflowUpdater(t *testing.T) {
t.Parallel()
wf := New()
// false/fail when Updater is unset
if wf.UpdateCheckDue() {
t.Error("Bad UpdateCheckDue. Expected=false, Got=true")
}
if wf.UpdateAvailable() {
t.Error("Bad UpdateAvailable. Expected=false, Got=true")
}
if err := wf.CheckForUpdate(); err == nil {
t.Error("CheckForUpdate() succeeded, expected failure")
}
if err := wf.InstallUpdate(); err == nil {
t.Error("InstallUpdate() succeeded, expected failure")
}
// true/success with mockUpdater
u := &mockUpdater{}
_ = wf.Configure(Update(u))
if !wf.UpdateCheckDue() {
t.Error("Bad UpdateCheckDue. Expected=true, Got=false")
}
if !u.checkDueCalled {
t.Error("Bad Update. CheckDue not called")
}
if !wf.UpdateAvailable() {
t.Error("Bad UpdateAvailable. Expected=true, Got=false")
}
if !u.updateAvailableCalled {
t.Error("Bad Update. UpdateAvailable not called")
}
if err := wf.CheckForUpdate(); err != nil {
t.Error("CheckForUpdate() failed")
}
if !u.checkForUpdateCalled {
t.Error("Bad Update. CheckForUpdate not called")
}
if err := wf.InstallUpdate(); err != nil {
t.Error("InstallUpdate() failed")
}
if !u.installCalled {
t.Error("Bad Update. Install not called")
}
}