-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate_test.go
More file actions
146 lines (138 loc) · 5.05 KB
/
Copy pathstate_test.go
File metadata and controls
146 lines (138 loc) · 5.05 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package state
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/require"
)
func TestAmbigousMatch(t *testing.T) {
noop := func(ctx context.Context, t any) (string, any, error) { return "", nil, nil }
for _, tc := range []struct {
name string
states []State[any, any]
wantErr error
}{
{
name: "no states",
states: []State[any, any]{},
wantErr: nil,
},
{
name: "one state",
states: []State[any, any]{
{Name: "state1", F: noop, Match: []MatchCondition{{Type: "Ready", Status: "True"}}},
},
wantErr: nil,
},
{
name: "one empty state",
states: []State[any, any]{
{Name: "state1", F: noop, Match: []MatchCondition{}},
{Name: "state2", F: noop, Match: []MatchCondition{{Type: "Ready", Status: "True"}}},
},
wantErr: ErrAmbigousMatch,
},
{
name: "two states with different match conditions",
states: []State[any, any]{
{Name: "state1", F: noop, Match: []MatchCondition{{Type: "Ready", Status: "True"}}},
{Name: "state2", F: noop, Match: []MatchCondition{{Type: "Healthy", Status: "True"}}},
},
wantErr: ErrAmbigousMatch,
},
{
name: "two states with different match conditions, Type is same but Status is different",
states: []State[any, any]{
{Name: "state1", F: noop, Match: []MatchCondition{{Type: "Ready", Status: "True"}}},
{Name: "state2", F: noop, Match: []MatchCondition{{Type: "Ready", Status: "False"}}},
},
wantErr: nil,
},
{
name: "two states with partially different match conditions, Type is same but Status is different",
states: []State[any, any]{
{Name: "state1", F: noop, Match: []MatchCondition{{Type: "Healthy", Status: "True"}, {Type: "Ready", Status: "True"}}},
{Name: "state2", F: noop, Match: []MatchCondition{{Type: "Healthy", Status: "True"}, {Type: "Ready", Status: "False"}}},
},
wantErr: nil,
},
{
name: "two states with partially different match conditions, Type is same but Status is different",
states: []State[any, any]{
{Name: "state1", F: noop, Match: []MatchCondition{{Type: "Ready", Status: "True"}, {Type: "Healthy", Status: "True"}}},
{Name: "state2", F: noop, Match: []MatchCondition{{Type: "Ready", Status: "False"}, {Type: "Healthy", Status: "True"}}},
},
wantErr: nil,
},
{
name: "two states with partially different match conditions, Type is same but Status is different",
states: []State[any, any]{
{Name: "state1", F: noop, Match: []MatchCondition{{Type: "Ready", Status: "True"}, {Type: "Healthy", Status: "True"}}},
{Name: "state2", F: noop, Match: []MatchCondition{{Type: "Healthy", Status: "True"}, {Type: "Ready", Status: "False"}}},
},
wantErr: nil,
},
{
name: "two states with same match conditions",
states: []State[any, any]{
{Name: "state1", F: noop, Match: []MatchCondition{{Type: "Ready", Status: "True"}}},
{Name: "state2", F: noop, Match: []MatchCondition{{Type: "Ready", Status: "True"}}},
},
wantErr: ErrAmbigousMatch,
},
{
name: "two states with same match conditions in different order",
states: []State[any, any]{
{Name: "state1", F: noop, Match: []MatchCondition{{Type: "Ready", Status: "True"}, {Type: "Healthy", Status: "True"}}},
{Name: "state2", F: noop, Match: []MatchCondition{{Type: "Healthy", Status: "True"}, {Type: "Ready", Status: "True"}}},
},
wantErr: ErrAmbigousMatch,
},
{
name: "two states with different match conditions but one is subset of the other",
states: []State[any, any]{
{Name: "state1", F: noop, Match: []MatchCondition{{Type: "Ready", Status: "True"}}},
{Name: "state2", F: noop, Match: []MatchCondition{{Type: "Ready", Status: "True"}, {Type: "Healthy", Status: "True"}}},
},
wantErr: ErrAmbigousMatch,
},
} {
t.Run(tc.name, func(t *testing.T) {
builder := &Builder[any, any]{}
for _, state := range tc.states {
builder.AddState(state.Name, state.F, state.Match...)
}
_, err := builder.Build()
if tc.wantErr == nil && err != nil {
t.Fatalf("expected no error, got %v", err)
}
if tc.wantErr != nil && err == nil {
t.Fatalf("expected error %v, got nil", tc.wantErr)
}
if tc.wantErr != nil && err != nil && !errors.Is(err, tc.wantErr) {
t.Fatalf("expected error %v, got %v", tc.wantErr, err)
}
})
}
}
func TestGraphviz(t *testing.T) {
builder := &Builder[any, any]{}
builder.AddState("state1", nil, MatchCondition{Type: "Ready", Status: "True"})
builder.AddState("state2", nil, MatchCondition{Type: "Ready", Status: "False"})
builder.AddState("state3", nil, MatchCondition{Type: "Healthy", Status: "True"}, MatchCondition{Type: "Running", Status: "True"})
builder.AddTransition("state1", "state2", nil)
builder.AddTransition("state2", "state3", nil)
builder.AddTransition("state3", "state1", nil)
graphviz, err := builder.Graphviz()
require.NoError(t, err)
expected := `digraph G {
state1 [label="state1\n\nReady=True"];
state2 [label="state2\n\nReady=False"];
state3 [label="state3\n\nHealthy=True\nRunning=True"];
state1 -> state2;
state2 -> state3;
state3 -> state1;
}
`
require.Equal(t, expected, graphviz)
}