@@ -16,10 +16,15 @@ import (
16
16
"runtime"
17
17
"strconv"
18
18
"strings"
19
+ gotesting "testing"
19
20
"time"
20
21
22
+ "github.com/stretchr/testify/assert"
21
23
"github.com/stretchr/testify/require"
22
24
25
+ "github.com/elastic/elastic-agent-libs/mapstr"
26
+ "github.com/elastic/elastic-agent-libs/opt"
27
+ agentsystemprocess "github.com/elastic/elastic-agent-system-metrics/metric/system/process"
23
28
"github.com/elastic/elastic-agent/internal/pkg/agent/application/paths"
24
29
"github.com/elastic/elastic-agent/pkg/control/v2/client"
25
30
"github.com/elastic/elastic-agent/pkg/core/process"
@@ -123,6 +128,10 @@ func NewBool(value bool) *bool {
123
128
// - an error if any.
124
129
func (f * Fixture ) Install (ctx context.Context , installOpts * InstallOpts , opts ... process.CmdOption ) ([]byte , error ) {
125
130
f .t .Logf ("[test %s] Inside fixture install function" , f .t .Name ())
131
+
132
+ // check for running agents before installing
133
+ assert .Empty (f .t , getElasticAgentProcesses (f .t ), "there should be no running agent at beginning of Install()" )
134
+
126
135
installArgs := []string {"install" }
127
136
if installOpts == nil {
128
137
// default options when not provided
@@ -161,8 +170,14 @@ func (f *Fixture) Install(ctx context.Context, installOpts *InstallOpts, opts ..
161
170
c := client .New (client .WithAddress (socketPath ))
162
171
f .setClient (c )
163
172
173
+ f .t .Cleanup (func () {
174
+ // check for running agents after uninstall had a chance to run
175
+ assert .Empty (f .t , getElasticAgentProcesses (f .t ), "there should be no running agent at the end of the test" )
176
+ })
177
+
164
178
f .t .Cleanup (func () {
165
179
f .t .Logf ("[test %s] Inside fixture cleanup function" , f .t .Name ())
180
+
166
181
if ! f .installed {
167
182
f .t .Logf ("skipping uninstall; agent not installed (fixture.installed is false)" )
168
183
// not installed; no need to clean up or collect diagnostics
@@ -218,6 +233,75 @@ func (f *Fixture) Install(ctx context.Context, installOpts *InstallOpts, opts ..
218
233
return out , nil
219
234
}
220
235
236
+ type runningProcess struct {
237
+ // Basic Process data
238
+ Name string
239
+ State agentsystemprocess.PidState
240
+ Username string
241
+ Pid opt.Int
242
+ Ppid opt.Int
243
+ Pgid opt.Int
244
+
245
+ // Extended Process Data
246
+ Args []string
247
+ Cmdline string
248
+ Cwd string
249
+ Exe string
250
+ Env mapstr.M
251
+ }
252
+
253
+ func (p runningProcess ) String () string {
254
+ return fmt .Sprintf ("{PID:%v, PPID: %v, Cwd: %s, Exe: %s, Cmdline: %s, Args: %v}" ,
255
+ p .Pid , p .Ppid , p .Cwd , p .Exe , p .Cmdline , p .Args )
256
+ }
257
+
258
+ func mapProcess (p agentsystemprocess.ProcState ) runningProcess {
259
+ mappedProcess := runningProcess {
260
+ Name : p .Name ,
261
+ State : p .State ,
262
+ Username : p .Username ,
263
+ Pid : p .Pid ,
264
+ Ppid : p .Ppid ,
265
+ Pgid : p .Pgid ,
266
+ Cmdline : p .Cmdline ,
267
+ Cwd : p .Cwd ,
268
+ Exe : p .Exe ,
269
+ Args : make ([]string , len (p .Args )),
270
+ Env : make (mapstr.M ),
271
+ }
272
+ copy (mappedProcess .Args , p .Args )
273
+ for k , v := range p .Env {
274
+ mappedProcess .Env [k ] = v
275
+ }
276
+ return mappedProcess
277
+ }
278
+
279
+ func getElasticAgentProcesses (t * gotesting.T ) []runningProcess {
280
+ procStats := agentsystemprocess.Stats {
281
+ Procs : []string {`.*elastic\-agent.*` },
282
+ }
283
+
284
+ err := procStats .Init ()
285
+ if ! assert .NoError (t , err , "error initializing process.Stats" ) {
286
+ // we failed
287
+ return nil
288
+ }
289
+
290
+ _ , pids , err := procStats .FetchPids ()
291
+ if ! assert .NoError (t , err , "error fetching process information" ) {
292
+ // we failed a bit further
293
+ return nil
294
+ }
295
+
296
+ processes := make ([]runningProcess , 0 , len (pids ))
297
+
298
+ for _ , p := range pids {
299
+ processes = append (processes , mapProcess (p ))
300
+ }
301
+
302
+ return processes
303
+ }
304
+
221
305
type UninstallOpts struct {
222
306
Force bool // --force
223
307
UninstallToken string
0 commit comments