-
Notifications
You must be signed in to change notification settings - Fork 0
/
stages_test.go
128 lines (121 loc) · 4.55 KB
/
stages_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package migration
import (
"context"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"testing"
)
func _WaitingTask() *Task {
return &Task{
Request: Request{ID: "test-1", FromSource: "github.com", FromOwner: "estafette", FromName: "migration", ToSource: "github.com", ToOwner: "estafette_new", ToName: "migration_new"},
Status: StatusQueued,
LastStep: StepWaiting,
}
}
func _RestartedTask() *Task {
return &Task{
Request: Request{ID: "test-1", FromSource: "github.com", FromOwner: "estafette", FromName: "migration", ToSource: "github.com", ToOwner: "estafette_new", ToName: "migration_new"},
Status: StatusQueued,
LastStep: StepBuildsFailed,
}
}
func _RestartedTask2() *Task {
return &Task{
Request: Request{ID: "test-1", FromSource: "github.com", FromOwner: "estafette", FromName: "migration", ToSource: "github.com", ToOwner: "estafette_new", ToName: "migration_new"},
Status: StatusQueued,
LastStep: StepBuildsDone,
}
}
type mockUpdater struct {
mock.Mock
}
func (m *mockUpdater) update(ctx context.Context, task *Task) error {
args := m.Called(ctx, task)
return args.Error(0)
}
func TestStages(t *testing.T) {
mockedUpdater := &mockUpdater{}
mockedUpdater.On("update", mock.Anything, mock.Anything).Return(nil)
mockedExecutor := &mockExecutor{}
mockedExecutor.On("execute", mock.Anything, mock.Anything).Return(nil)
ss := NewStages(mockedUpdater.update, _WaitingTask())
cont := t.Run("Set_and_Len", func(t *testing.T) {
ss.
Set(ReleasesStage, mockedExecutor.execute).
Set(ReleaseLogsStage, mockedExecutor.execute).
Set(ReleaseLogObjectsStage, mockedExecutor.execute).
Set(BuildsStage, mockedExecutor.execute).
Set(BuildLogsStage, mockedExecutor.execute).
Set(BuildLogObjectsStage, mockedExecutor.execute).
Set(BuildVersionsStage, mockedExecutor.execute).
Set(CallbackStage, mockedExecutor.execute)
assert.Equal(t, 8, ss.Len())
})
if !cont {
return
}
cont = t.Run("HasNext", func(t *testing.T) { assert.True(t, ss.HasNext()) })
if !cont {
return
}
cont = t.Run("ExecuteNext", func(t *testing.T) {
for ss.HasNext() {
failed := ss.ExecuteNext(context.TODO())
assert.True(t, failed)
}
})
if !cont {
return
}
t.Run("Current", func(t *testing.T) { assert.Equal(t, CallbackStage, ss.Current().Name()) })
mockedExecutor.AssertNumberOfCalls(t, "execute", 8)
mockedUpdater.AssertNumberOfCalls(t, "update", 8)
}
func TestStages_Skipped(t *testing.T) {
mockedUpdater := &mockUpdater{}
mockedUpdater.On("update", mock.Anything, mock.Anything).Return(nil)
skippedExecutor := &mockExecutor{}
mockedExecutor := &mockExecutor{}
mockedExecutor.On("execute", mock.Anything, mock.Anything).Return(nil)
ss := NewStages(mockedUpdater.update, _RestartedTask()).
Set(ReleasesStage, skippedExecutor.execute).
Set(ReleaseLogsStage, skippedExecutor.execute).
Set(ReleaseLogObjectsStage, skippedExecutor.execute).
Set(BuildsStage, mockedExecutor.execute).
Set(BuildLogsStage, mockedExecutor.execute).
Set(BuildLogObjectsStage, mockedExecutor.execute).
Set(BuildVersionsStage, mockedExecutor.execute).
Set(CallbackStage, mockedExecutor.execute)
for ss.HasNext() {
failed := ss.ExecuteNext(context.TODO())
assert.True(t, failed)
}
t.Run("Current", func(t *testing.T) { assert.Equal(t, CallbackStage, ss.Current().Name()) })
mockedExecutor.AssertNumberOfCalls(t, "execute", 5)
mockedUpdater.AssertNumberOfCalls(t, "update", 5)
skippedExecutor.AssertNotCalled(t, "execute", mock.Anything, mock.Anything)
}
func TestStages_Skipped_Success(t *testing.T) {
mockedUpdater := &mockUpdater{}
mockedUpdater.On("update", mock.Anything, mock.Anything).Return(nil)
skippedExecutor := &mockExecutor{}
mockedExecutor := &mockExecutor{}
mockedExecutor.On("execute", mock.Anything, mock.Anything).Return(nil)
ss := NewStages(mockedUpdater.update, _RestartedTask2()).
Set(ReleasesStage, skippedExecutor.execute).
Set(ReleaseLogsStage, skippedExecutor.execute).
Set(ReleaseLogObjectsStage, skippedExecutor.execute).
Set(BuildsStage, mockedExecutor.execute).
Set(BuildLogsStage, mockedExecutor.execute).
Set(BuildLogObjectsStage, mockedExecutor.execute).
Set(BuildVersionsStage, mockedExecutor.execute).
Set(CallbackStage, mockedExecutor.execute)
for ss.HasNext() {
failed := ss.ExecuteNext(context.TODO())
assert.True(t, failed)
}
t.Run("Current", func(t *testing.T) { assert.Equal(t, CallbackStage, ss.Current().Name()) })
mockedExecutor.AssertNumberOfCalls(t, "execute", 4)
mockedUpdater.AssertNumberOfCalls(t, "update", 4)
skippedExecutor.AssertNotCalled(t, "execute", mock.Anything, mock.Anything)
}