-
Notifications
You must be signed in to change notification settings - Fork 1
/
pm_test.go
160 lines (133 loc) · 3.39 KB
/
pm_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
package pm_test
import (
"context"
"fmt"
"log"
"os"
"strings"
"sync"
"testing"
"github.com/go-resty/resty/v2"
"github.com/stretchr/testify/suite"
"github.com/telnet2/pm"
)
type ProcManTester struct {
suite.Suite
}
func (tm *ProcManTester) TestSimpleRunner() {
pman := pm.NewProcMan()
_ = pman.Add(&pm.Runnable{
Id: "ls",
Command: "ls -al",
})
wg := sync.WaitGroup{}
// Wait for log printing
wg.Add(1)
lsStdout := pman.SubscribeLog("ls", true)
go func() {
for l := range lsStdout {
fmt.Println(l)
}
wg.Done()
}()
// Subscribe to the events of `ls`
sch := pman.SubscribeEvent("ls", pm.EventMap{"start": {}, "done": {}})
// Must subscribe before the process starting
ctx := context.TODO()
_ = pman.Start(ctx)
log.Println("wait start")
e := <-sch
tm.Equal(e.Id, "ls")
tm.Equal("start", e.Event)
e = <-sch
tm.Equal(e.Id, "ls")
tm.Equal("done", e.Event)
wg.Wait()
tm.NoError(pman.Restart(ctx, "ls"))
e = <-sch
tm.Equal(e.Id, "ls")
tm.Equal("start", e.Event)
lsStdout = pman.SubscribeLog("ls", true)
go func() {
for l := range lsStdout {
fmt.Println(l)
}
}()
e = <-sch
tm.Equal(e.Id, "ls")
tm.Equal("done", e.Event)
pman.Shutdown(nil)
}
func (tm *ProcManTester) TestSimpleConfig() {
ctx := context.TODO()
cfg, err := pm.LoadYaml(ctx, strings.NewReader(`---
services:
first:
command: "echo first > first.txt && echo done"
ready_condition:
stdout_message: done
second:
command: "[[ -f first.txt ]] && echo second > second.txt || echo error > error.txt; echo done"
ready_condition:
stdout_message: done
depends_on:
- first
`))
tm.NoError(err)
tm.NotNil(cfg)
procMan := pm.NewProcMan()
tm.NoError(procMan.AddConfig(cfg))
// Start will wait until the second prints 'done' due to ready_condition
tm.NoError(procMan.Start(ctx))
tm.FileExists("first.txt")
tm.FileExists("second.txt")
os.Remove("first.txt")
os.Remove("second.txt")
// should implement waitdone
// procMan.WaitDone()
}
func (tm *ProcManTester) TestEchoGood() {
ctx := context.TODO()
pman := pm.NewProcMan()
cfg, err := pm.LoadYamlFile(ctx, "./testdata/config_echo.yaml")
tm.NoError(err)
tm.NoError(pman.AddConfig(cfg))
tm.NoError(pman.Start(ctx))
cli := resty.New()
res, err := cli.R().Get("http://localhost:41234/echo")
tm.NoError(err)
tm.Equal("/echo", res.Header().Get("x-request-path"))
// echo_ready is already gone!
tm.NoError(pman.Stop("echo"))
pman.WaitDone()
// check if the echo server is really stopped
res, err = cli.R().Get("http://localhost:41234/echo")
log.Println(string(res.Body()), err)
log.Println(res.IsSuccess())
tm.False(res.IsSuccess())
tm.Error(err)
}
func (tm *ProcManTester) TestEchoFail() {
ctx := context.TODO()
pman := pm.NewProcMan()
cfg, err := pm.LoadYamlFile(ctx, "./testdata/config_echo_fail.yaml")
tm.NoError(err)
tm.NoError(pman.AddConfig(cfg))
// error after 5 secs because ready checker is listening a wrong port.
tm.Error(pman.Start(ctx))
pman.WaitDone()
}
func (tm *ProcManTester) TestMySQL() {
ctx := context.TODO()
pman := pm.NewProcMan()
cfg, err := pm.LoadYamlFile(ctx, "./testdata/config_echo_fail.yaml")
tm.NoError(err)
tm.NoError(pman.AddConfig(cfg))
// error after 5 secs because ready checker is listening a wrong port.
tm.Error(pman.Start(ctx))
pman.WaitDone()
}
func TestProcMan(t *testing.T) {
tm := new(ProcManTester)
suite.Run(t, tm)
}