-
Notifications
You must be signed in to change notification settings - Fork 0
/
sfn_test.go
111 lines (91 loc) · 3.47 KB
/
sfn_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
package sfndepents_test
import (
"context"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/sfn"
"github.com/aws/aws-sdk-go-v2/service/sfn/types"
sfndepents "github.com/kanmu/sfn-depends"
"github.com/stretchr/testify/assert"
)
type MockSfnAPI struct {
MockListExecutions func(ctx context.Context, params *sfn.ListExecutionsInput, optFns ...func(*sfn.Options)) (*sfn.ListExecutionsOutput, error)
}
func (api *MockSfnAPI) ListExecutions(ctx context.Context, params *sfn.ListExecutionsInput, optFns ...func(*sfn.Options)) (*sfn.ListExecutionsOutput, error) {
return api.MockListExecutions(ctx, params, optFns...)
}
func TestSuccess(t *testing.T) {
assert := assert.New(t)
client := &sfndepents.Client{
Sfn: &MockSfnAPI{
MockListExecutions: func(ctx context.Context, params *sfn.ListExecutionsInput, optFns ...func(*sfn.Options)) (*sfn.ListExecutionsOutput, error) {
now := time.Now()
output := &sfn.ListExecutionsOutput{
Executions: []types.ExecutionListItem{
{ExecutionArn: aws.String("exec1"), Status: types.ExecutionStatusSucceeded, StartDate: aws.Time(now)},
{ExecutionArn: aws.String("exec2"), Status: types.ExecutionStatusSucceeded, StartDate: aws.Time(now.Add(-time.Second))},
},
}
return output, nil
},
},
}
err := client.Validate([]string{"state1"}, time.Hour)
assert.NoError(err)
}
func TestSuccessLastExecution(t *testing.T) {
assert := assert.New(t)
client := &sfndepents.Client{
Sfn: &MockSfnAPI{
MockListExecutions: func(ctx context.Context, params *sfn.ListExecutionsInput, optFns ...func(*sfn.Options)) (*sfn.ListExecutionsOutput, error) {
now := time.Now()
output := &sfn.ListExecutionsOutput{
Executions: []types.ExecutionListItem{
{ExecutionArn: aws.String("exec1"), Status: types.ExecutionStatusSucceeded, StartDate: aws.Time(now)},
{ExecutionArn: aws.String("exec2"), Status: types.ExecutionStatusFailed, StartDate: aws.Time(now.Add(-time.Second))},
},
}
return output, nil
},
},
}
err := client.Validate([]string{"state1"}, time.Hour)
assert.NoError(err)
}
func TestNotSuccess(t *testing.T) {
assert := assert.New(t)
client := &sfndepents.Client{
Sfn: &MockSfnAPI{
MockListExecutions: func(ctx context.Context, params *sfn.ListExecutionsInput, optFns ...func(*sfn.Options)) (*sfn.ListExecutionsOutput, error) {
now := time.Now()
output := &sfn.ListExecutionsOutput{
Executions: []types.ExecutionListItem{
{ExecutionArn: aws.String("exec1"), Status: types.ExecutionStatusFailed, StartDate: aws.Time(now)},
{ExecutionArn: aws.String("exec2"), Status: types.ExecutionStatusSucceeded, StartDate: aws.Time(now.Add(-time.Second))},
},
}
return output, nil
},
},
}
err := client.Validate([]string{"state1"}, time.Hour)
assert.EqualError(err, "last execution state is FAILED: exec1")
}
func TestExecutionNotFound(t *testing.T) {
assert := assert.New(t)
client := &sfndepents.Client{
Sfn: &MockSfnAPI{
MockListExecutions: func(ctx context.Context, params *sfn.ListExecutionsInput, optFns ...func(*sfn.Options)) (*sfn.ListExecutionsOutput, error) {
output := &sfn.ListExecutionsOutput{
Executions: []types.ExecutionListItem{},
}
return output, nil
},
},
Region: "ap-northeast-1",
AccountId: "123456789012",
}
err := client.Validate([]string{"state1"}, time.Hour)
assert.EqualError(err, "execution not found: arn:aws:states:ap-northeast-1:123456789012:stateMachine:state1")
}