-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathplugin_test.go
More file actions
172 lines (132 loc) · 3.98 KB
/
Copy pathplugin_test.go
File metadata and controls
172 lines (132 loc) · 3.98 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
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
package service
import (
"context"
stderr "errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"testing"
"time"
"github.com/roadrunner-server/errors"
"github.com/roadrunner-server/pool/v2/state/process"
"github.com/stretchr/testify/require"
)
// testConfigurer feeds Init a service section without going through viper.
type testConfigurer struct {
has bool
services map[string]*Service
err error
}
func (c *testConfigurer) Has(string) bool { return c.has }
func (c *testConfigurer) UnmarshalKey(_ string, out any) error {
if c.err != nil {
return c.err
}
dst, ok := out.(*map[string]*Service)
if !ok {
return fmt.Errorf("unexpected destination type %T", out)
}
*dst = c.services
return nil
}
type testLogger struct {
l *slog.Logger
}
func (l *testLogger) NamedLogger(string) *slog.Logger { return l.l }
func TestPluginInitDisabled(t *testing.T) {
log, _ := newCaptureLogger()
p := &Plugin{}
err := p.Init(&testConfigurer{has: false}, &testLogger{l: log})
require.True(t, errors.Is(errors.Disabled, err))
}
func TestPluginInitUnmarshalError(t *testing.T) {
log, _ := newCaptureLogger()
p := &Plugin{}
err := p.Init(&testConfigurer{has: true, err: stderr.New("broken section")}, &testLogger{l: log})
require.ErrorContains(t, err, "broken section")
}
func TestPluginInitAppliesDefaults(t *testing.T) {
log, _ := newCaptureLogger()
p := &Plugin{}
cfg := &testConfigurer{has: true, services: map[string]*Service{
"some_service": {Command: "sleep 30"},
}}
require.NoError(t, p.Init(cfg, &testLogger{l: log}))
require.Equal(t, 1, p.cfg.Services["some_service"].ProcessNum)
}
func TestPluginServeAndStop(t *testing.T) {
p, _ := newTestPlugin(t, map[string]*Service{
"some_service": {Command: "sleep 30", ProcessNum: 2},
})
errCh := p.Serve()
workers := p.Workers()
require.Len(t, workers, 2)
require.Empty(t, errCh)
for _, st := range workers {
require.NotZero(t, st.Pid)
}
require.NoError(t, p.Stop(t.Context()))
require.Empty(t, p.Workers())
for _, st := range workers {
require.Eventually(t, func() bool { return !processAlive(st.Pid) },
10*time.Second, 20*time.Millisecond)
}
}
func TestPluginServeReportsStartError(t *testing.T) {
p, _ := newTestPlugin(t, map[string]*Service{
"some_service": {Command: filepath.Join(t.TempDir(), "no-such-binary"), ProcessNum: 1},
})
errCh := p.Serve()
select {
case err := <-errCh:
require.Error(t, err)
default:
t.Fatal("Serve returned before reporting the start error")
}
require.Empty(t, p.Workers())
}
func TestPluginResetReplacesProcesses(t *testing.T) {
p, _ := newTestPlugin(t, map[string]*Service{
"some_service": {Command: "sleep 30", ProcessNum: 2},
})
p.Serve()
before := workerPids(p.Workers())
require.Len(t, before, 2)
require.NoError(t, p.Reset())
after := workerPids(p.Workers())
require.Len(t, after, 2)
for _, pid := range after {
require.NotContains(t, before, pid)
}
}
func TestPluginResetLogsFailedRestart(t *testing.T) {
script := writeScript(t, "reset.sh", "#!/bin/sh\necho ready\nexec sleep 30\n")
p, store := newTestPlugin(t, map[string]*Service{
"some_service": {Command: script, ProcessNum: 1},
})
p.Serve()
require.Eventually(t, func() bool { return store.count("ready") == 1 },
10*time.Second, 20*time.Millisecond)
require.NoError(t, os.Remove(script))
require.NoError(t, p.Reset())
require.Equal(t, 1, store.count("unable to start the service"))
require.Empty(t, p.Workers())
}
// newTestPlugin returns a plugin wired to an in-memory logger, stopped at the
// end of the test.
func newTestPlugin(t *testing.T, services map[string]*Service) (*Plugin, *captureStore) {
t.Helper()
log, store := newCaptureLogger()
p := &Plugin{logger: log, cfg: Config{Services: services}}
t.Cleanup(func() { _ = p.Stop(context.Background()) })
return p, store
}
// workerPids collects the pids the plugin reports.
func workerPids(states []*process.State) []int64 {
pids := make([]int64, 0, len(states))
for _, st := range states {
pids = append(pids, st.Pid)
}
return pids
}