-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction_test.go
185 lines (167 loc) · 4.78 KB
/
action_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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package action
import (
"bytes"
"context"
"errors"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
const _rootActionName = "root"
func getTestHookFunc(buf *bytes.Buffer, name string) func(act *Action) error {
return func(act *Action) error {
_, _ = fmt.Fprintf(buf, "%s action name: %s\n", name, act.Name)
return nil
}
}
func getTargetFunc(istarget bool) func(act *Action) bool {
return func(act *Action) bool {
return istarget
}
}
func getRootAction(buf *bytes.Buffer) *Action {
return &Action{
Name: "root",
PersistentPreRun: getTestHookFunc(buf, "PersistentPreRun"),
PreRun: getTestHookFunc(buf, "PreRun"),
Run: getTestHookFunc(buf, "Run"),
PostRun: getTestHookFunc(buf, "PostRun"),
PersistentPostRun: getTestHookFunc(buf, "PersistentPostRun"),
Executable: getTargetFunc(false),
}
}
func getSubActions(buf *bytes.Buffer, parent string, count int) []*Action {
if count < 1 {
return nil
}
var actions []*Action
for i := 0; i < count; i++ {
actions = append(actions, &Action{
Name: fmt.Sprintf("%s-sub-action-%d", parent, i+1),
PersistentPreRun: getTestHookFunc(buf, "PersistentPreRun"),
PreRun: getTestHookFunc(buf, "PreRun"),
Run: getTestHookFunc(buf, "Run"),
PostRun: getTestHookFunc(buf, "PostRun"),
PersistentPostRun: getTestHookFunc(buf, "PersistentPostRun"),
Executable: getTargetFunc(false),
})
}
return actions
}
func TestActionRunnable(t *testing.T) {
var buf bytes.Buffer
act := getRootAction(&buf)
assert.True(t, act.Runnable())
act.Run = nil
assert.False(t, act.Runnable())
}
func TestActionAddAction(t *testing.T) {
t.Run("successfully add 5 actions", func(t *testing.T) {
var buf bytes.Buffer
act := getRootAction(&buf)
acts := getSubActions(&buf, _rootActionName, 5)
_ = act.AddAction(acts...)
assert.Equal(t, 5, len(act.Actions()))
})
t.Run("doesn't accept self as sub action", func(t *testing.T) {
var buf bytes.Buffer
act := getRootAction(&buf)
err := act.AddAction(act)
assert.EqualError(t, err, "action can't be a child of itself")
})
}
func TestActionParent(t *testing.T) {
t.Run("should returns parent action", func(t *testing.T) {
var buf bytes.Buffer
act := getRootAction(&buf)
acts := getSubActions(&buf, _rootActionName, 1)
_ = act.AddAction(acts...)
assert.False(t, act.HasParent())
assert.True(t, act.HasSubActions())
assert.True(t, acts[0].HasParent())
assert.Equal(t, act, acts[0].Parent())
})
}
func TestActionRemove(t *testing.T) {
var buf bytes.Buffer
act := getRootAction(&buf)
acts := getSubActions(&buf, _rootActionName, 10)
_ = act.AddAction(acts...)
acts[9].Executable = getTargetFunc(true)
assert.Equal(t, acts[9], act.Find())
act.RemoveAction(acts[9])
assert.Nil(t, act.Find())
}
func TestActionRoot(t *testing.T) {
var buf bytes.Buffer
act := getRootAction(&buf)
acts := getSubActions(&buf, _rootActionName, 10)
_ = act.AddAction(acts...)
assert.Equal(t, act, act)
assert.Equal(t, act, acts[9].Root())
}
func TestActionExecuteContext(t *testing.T) {
var buf bytes.Buffer
act := getRootAction(&buf)
act.Run = func(act *Action) error {
for {
select {
case <-act.Context().Done():
return errors.New("done")
default:
time.Sleep(100 * time.Millisecond)
}
}
}
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(500 * time.Millisecond)
cancel()
}()
err := act.ExecuteContext(ctx)
assert.EqualError(t, err, "done")
}
func TestActionExecute(t *testing.T) {
t.Run("ignore action without Run", func(t *testing.T) {
var buf bytes.Buffer
act := getRootAction(&buf)
assert.Nil(t, act.Execute())
})
t.Run("returns error in PreRun", func(t *testing.T) {
var buf bytes.Buffer
act := getRootAction(&buf)
act.PreRun = func(act *Action) error {
return errors.New("PreRun error")
}
err := act.Execute()
assert.EqualError(t, err, "PreRun error")
})
t.Run("returns error in PostRun", func(t *testing.T) {
var buf bytes.Buffer
act := getRootAction(&buf)
act.PostRun = func(act *Action) error {
return errors.New("PostRun error")
}
err := act.Execute()
assert.EqualError(t, err, "PostRun error")
})
t.Run("returns error in PersistentPreRun", func(t *testing.T) {
var buf bytes.Buffer
act := getRootAction(&buf)
act.PersistentPreRun = func(act *Action) error {
return errors.New("PersistentPreRun error")
}
err := act.Execute()
assert.EqualError(t, err, "PersistentPreRun error")
})
t.Run("returns error in PersistentPostRun", func(t *testing.T) {
var buf bytes.Buffer
act := getRootAction(&buf)
act.PersistentPostRun = func(act *Action) error {
return errors.New("PersistentPostRun error")
}
err := act.Execute()
assert.EqualError(t, err, "PersistentPostRun error")
})
}