-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforeman_test.go
193 lines (156 loc) · 4.29 KB
/
foreman_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
186
187
188
189
190
191
192
193
package foreman
import (
"sync"
"testing"
parser "github.com/IslamWalid/foreman/internal/procparser"
)
const (
testProcfile = "./test-procfiles/Procfile-test"
testBadProcfile = "./test-procfiles/Procfile-bad-test"
testCyclicProcfile = "./test-procfiles/Procfile-cyclic-test"
)
func TestNew(t *testing.T) {
t.Run("Parse existing procfile with correct syntax", func(t *testing.T) {
want := Foreman{
services: map[string]parser.Service{},
servicesMutex: sync.Mutex{},
}
sleeper := parser.Service{
ServiceName: "sleeper",
Process: nil,
Cmd: "sleep infinity",
RunOnce: true,
Deps: []string{"hello"},
Checks: parser.ServiceChecks{
Cmd: "ls",
TcpPorts: []string{"4759", "1865"},
UdpPorts: []string{"4500", "3957"},
},
}
want.services["sleeper"] = sleeper
hello := parser.Service{
ServiceName: "hello",
Process: nil,
Cmd: `echo "hello"`,
RunOnce: true,
Deps: []string{},
}
want.services["hello"] = hello
got, _ := New(testProcfile, false)
assertForeman(t, got, &want)
})
t.Run("Run existing file with bad yml syntax", func(t *testing.T) {
_, err := New(testBadProcfile, false)
if err == nil {
t.Error("Expcted error: yaml: unmarshal errors")
}
})
t.Run("Run non-existing file", func(t *testing.T) {
_, err := New("uknown_file", false)
want := "open uknown_file: no such file or directory"
assertError(t, err, want)
})
}
func TestBuildDependencyGraph(t *testing.T) {
foreman, _ := New(testProcfile, false)
got := foreman.buildDependencyGraph()
want := make(map[string][]string)
want["sleeper"] = []string{"hello"}
assertGraph(t, got, want)
}
func TestIsCyclic(t *testing.T) {
t.Run("run cyclic graph", func(t *testing.T) {
foreman, _ := New(testCyclicProcfile, false)
graph := foreman.buildDependencyGraph()
got := graph.IsCyclic()
if !got {
t.Error("got:true, want:false")
}
})
t.Run("run acyclic graph", func(t *testing.T) {
foreman, _ := New(testProcfile, false)
graph := foreman.buildDependencyGraph()
got := graph.IsCyclic()
if got {
t.Error("got:false, want:true")
}
})
}
func TestTopSort(t *testing.T) {
foreman, _ := New(testProcfile, false)
depGraph := foreman.buildDependencyGraph()
got := depGraph.TopSort()
assertTopSortResult(t, foreman, got)
}
func assertForeman(t *testing.T, got, want *Foreman) {
t.Helper()
for serviceName, service := range got.services {
assertService(t, service, want.services[serviceName])
}
}
func assertService(t *testing.T, got, want parser.Service) {
t.Helper()
if got.ServiceName != want.ServiceName {
t.Errorf("got:\n%q\nwant:\n%q", got.ServiceName, want.ServiceName)
}
if got.Process != want.Process {
t.Errorf("got:\n%v\nwant:\n%v", got.Process, want.Process)
}
if got.Cmd != want.Cmd {
t.Errorf("got:\n%q\nwant:\n%q", got.Cmd, want.Cmd)
}
if got.Cmd != want.Cmd {
t.Errorf("got:\n%q\nwant:\n%q", got.Cmd, want.Cmd)
}
if got.RunOnce != want.RunOnce {
t.Errorf("got:\n%t\nwant:\n%t", got.RunOnce, want.RunOnce)
}
assertList(t, got.Deps, want.Deps)
}
func assertChecks(t *testing.T, got, want *parser.ServiceChecks) {
t.Helper()
if got.Cmd != want.Cmd {
t.Errorf("got:\n%q\nwant:\n%q", got.Cmd, want.Cmd)
}
assertList(t, got.TcpPorts, want.TcpPorts)
assertList(t, got.UdpPorts, want.UdpPorts)
}
func assertList(t *testing.T, got, want []string) {
t.Helper()
if len(want) != len(got) {
t.Errorf("got:\n%v\nwant:\n%v", got, want)
}
n := len(want)
for i := 0; i < n; i++ {
if got[i] != want[i] {
t.Errorf("got:\n%v\nwant:\n%v", got, want)
}
}
}
func assertError(t *testing.T, err error, want string) {
t.Helper()
if err == nil {
t.Fatal("Expected Error: open uknown_file: no such file or directory")
}
if err.Error() != want {
t.Errorf("got:\n%q\nwant:\n%q", err.Error(), want)
}
}
func assertGraph(t *testing.T, got, want map[string][]string) {
t.Helper()
for key, value := range got {
assertList(t, value, want[key])
}
}
func assertTopSortResult(t *testing.T, foreman *Foreman, got []string) {
t.Helper()
nodesSet := make(map[string]any)
for _, dep := range got {
for _, depDep := range foreman.services[dep].Deps {
if _, ok := nodesSet[depDep]; !ok {
t.Fatalf("not expected to run %v before %v", dep, depDep)
}
}
nodesSet[dep] = 1
}
}