forked from coinbase/step
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparallel_state_test.go
More file actions
132 lines (121 loc) · 3.62 KB
/
parallel_state_test.go
File metadata and controls
132 lines (121 loc) · 3.62 KB
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
package machine
import (
"encoding/json"
"github.com/coinbase/step/utils/to"
"github.com/stretchr/testify/assert"
"testing"
)
// Tests on Non Valid Json
func Test_ParallelState_NonValid_State_No_Next_Or_End(t *testing.T) {
state := parseParallelTaskState([]byte(`{
"Type": "Parallel",
"Branches": [
{
"StartAt": "Branch_A_Start",
"States": {
"Branch_A_Start": {
"Type": "TaskFn",
"Resource": "host1:app1:func1",
"Next": "Branch_A_Done"
},
"Branch_A_Done": {
"Type": "Succeed"
}
}
},
{
"StartAt": "Branch_B_Start",
"States": {
"Branch_B_Start": {
"Type": "TaskFn",
"Resource": "host1:app2:func2",
"End": true
}
}
}
]
}`), t)
err := state.Validate()
assert.Error(t, err)
assert.EqualErrorf(t, err, "parallel state must have either \"Next\" or \"End\" property", "Checking that Validate fails with no \"End\" or \"Next\" property")
}
func Test_ParallelState_NonValid_Branches_Empty(t *testing.T) {
state := parseParallelTaskState([]byte(`{
"Type": "Parallel",
"End": true,
"Branches": [
]
}`), t)
err := state.Validate()
assert.Error(t, err)
assert.EqualErrorf(t, err, "branches can't be nil or empty (len = 0)", "Checking that Validate fails with empty array")
}
func Test_ParallelState_NonValid_Branches_Unreachable_Next(t *testing.T) {
state := parseParallelTaskState([]byte(`{
"Type": "Parallel",
"End": true,
"Branches": [
{
"StartAt": "Branch_A_Start",
"States": {
"Branch_A_Start": {
"Type": "TaskFn",
"Resource": "host1:app1:func1",
"Next": "Branch_A_Done"
}
}
}
]
}`), t)
err := state.Validate()
assert.Error(t, err)
assert.EqualErrorf(t, err, "state \"Branch_A_Start\" next state \"Branch_A_Done\" is unreachable",
"Checking that Validate fails when branches task next is unreachable")
}
func Test_ParallelState_Valid(t *testing.T) {
state := parseParallelTaskState([]byte(`{
"Type": "Parallel",
"Next": "Parallel_Block_Done",
"Branches": [
{
"StartAt": "Branch_A_Start",
"States": {
"Branch_A_Start": {
"Type": "TaskFn",
"Resource": "host1:app1:func1",
"Next": "Branch_A_Next_One"
},
"Branch_A_Next_One": {
"Type": "TaskFn",
"Resource": "host1:app1:func2",
"Next": "Branch_A_Done"
},
"Branch_A_Done": {
"Type": "Succeed"
}
}
},
{
"StartAt": "Branch_B_Start",
"States": {
"Branch_B_Start": {
"Type": "TaskFn",
"Resource": "host1:app2:func2",
"End": true
}
}
}
]
}`), t)
err := state.Validate()
assert.Nil(t, err)
}
// Private functions
func parseParallelTaskState(b []byte, t *testing.T) *ParallelState {
var p ParallelState
err := json.Unmarshal(b, &p)
assert.NoError(t, err)
p.SetName(to.Strp("TestState"))
p.SetType(to.Strp("Parallel"))
return &p
}