-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathprocess_test.go
More file actions
239 lines (227 loc) · 5.54 KB
/
process_test.go
File metadata and controls
239 lines (227 loc) · 5.54 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright (c) F5, Inc.
//
// This source code is licensed under the Apache License, Version 2.0 license found in the
// LICENSE file in the root directory of this source tree.
package nginxprocess_test
import (
"context"
"os"
"testing"
"github.com/nginx/agent/v3/pkg/nginxprocess"
"github.com/shirou/gopsutil/v4/process"
"github.com/stretchr/testify/require"
)
// gopsutilJunk is some junk that gopsutil CmdlineWithContext adds to the end of the command.
const gopsutilJunk = " ptr_munge= main_stack="
func TestList(t *testing.T) {
t.Skipf("this test is only useful if you are running nginx locally")
t.Parallel()
p, err := nginxprocess.List(context.Background())
require.NoError(t, err)
require.NotEmpty(t, p)
}
func TestFind_PIDIsNotNginx(t *testing.T) {
t.Parallel()
p, err := nginxprocess.Find(context.Background(), int32(os.Getpid()))
require.Error(t, err)
require.Nil(t, p)
require.True(t, nginxprocess.IsNotNginxErr(err))
}
func TestProcess_IsNginxWorker(t *testing.T) {
t.Parallel()
testcases := map[string]struct {
cmd string
want bool
}{
"Test 1: nginx worker": {
cmd: "nginx: worker process",
want: true,
},
"Test 2: nginx master": {
cmd: "nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off;",
want: false,
},
"Test 3: nginx master with worker in command": {
cmd: "nginx: master process -c /foo/bar/workers/nginx.conf",
want: false,
},
"Test 4: nginx cache manager": {
cmd: "nginx: cache manager process",
want: false,
},
}
for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
t.Parallel()
p := nginxprocess.Process{Cmd: tc.cmd + gopsutilJunk}
require.Equal(t, tc.want, p.IsWorker())
})
}
}
func TestProcess_IsNginxMaster(t *testing.T) {
t.Parallel()
testcases := map[string]struct {
cmd string
want bool
}{
"Test 1: nginx worker": {
cmd: "nginx: worker process",
want: false,
},
"Test 2: nginx master": {
cmd: "nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off;",
want: true,
},
"Test 3: nginx master with worker in command": {
cmd: "nginx: master process -c /foo/bar/workers/nginx.conf",
want: true,
},
"Test 4: nginx cache manager": {
cmd: "nginx: cache manager process",
want: false,
},
"Test 5: nginx debug master": {
cmd: "{nginx-debug} nginx: master process /usr/sbin/nginx-debug -g daemon off;",
want: true,
},
"Test 6: nginx debug worker": {
cmd: "{nginx-debug} nginx: worker process;",
want: false,
},
}
for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
t.Parallel()
p := nginxprocess.Process{Cmd: tc.cmd + gopsutilJunk}
require.Equal(t, tc.want, p.IsMaster())
})
}
}
func TestProcess_IsShuttingDown(t *testing.T) {
t.Parallel()
testcases := map[string]struct {
cmd string
want bool
}{
"Test 1: nginx worker": {
cmd: "nginx: worker process",
want: false,
},
"Test 2: nginx draining worker": {
cmd: "nginx: worker process is shutting down",
want: true,
},
"Test 3: nginx master": {
cmd: "nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off;",
want: false,
},
}
for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
t.Parallel()
p := nginxprocess.Process{Cmd: tc.cmd + gopsutilJunk}
require.Equal(t, tc.want, p.IsShuttingDown())
})
}
}
func TestProcess_IsHealthy(t *testing.T) {
t.Parallel()
testcases := map[string]struct {
status string
want bool
}{
"Test 1: Running": {
status: process.Running,
want: true,
},
"Test 2: Blocked": {
status: process.Blocked,
want: true,
},
"Test 3: Idle": {
status: process.Idle,
want: true,
},
"Test 4: Lock": {
status: process.Lock,
want: true,
},
"Test 5: Sleep": {
status: process.Sleep,
want: true,
},
"Test 6: Stop": {
status: process.Stop,
want: true,
},
"Test 7: Wait": {
status: process.Wait,
want: true,
},
"Test 8: Daemon": {
status: process.Daemon,
want: true,
},
"Test 9: Detached": {
status: process.Detached,
want: true,
},
"Test 10: System": {
status: process.System,
want: true,
},
"Test 11: Orphan": {
status: process.Orphan,
want: true,
},
"Test 12: Zombie": {
status: process.Zombie,
want: false,
},
"Test 13: empty string": {
status: "",
want: false,
},
"Test 14: multiple healthy flags": {
status: process.Running + " " + process.Lock,
want: true,
},
"Test 15: multiple flags ending in Zombie": {
status: process.Running + " " + process.Zombie,
want: false,
},
"Test 16: multiple flags beginning in Zombie": {
status: process.Zombie + " " + process.Running,
want: false,
},
"Test 17: multiple flags with Zombie in the middle": {
status: process.Running + " " + process.Zombie + " " + process.Blocked,
want: false,
},
}
for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
t.Parallel()
p := nginxprocess.Process{Status: tc.status}
require.Equal(t, tc.want, p.IsHealthy())
})
}
}
// BenchmarkList is useful if you are running NGINX locally and want to understand the performance impact of new changes
// to [nginxprocess.List].
func BenchmarkList(b *testing.B) {
b.Skipf("skipping to prevent CI flake")
ctx := context.Background()
b.Run("base", func(b *testing.B) {
for range b.N {
_, err := nginxprocess.List(ctx)
require.NoError(b, err)
}
})
b.Run("WithStatus", func(b *testing.B) {
for range b.N {
_, err := nginxprocess.List(ctx, nginxprocess.WithStatus(true))
require.NoError(b, err)
}
})
}