github.com/hernad/nomad@v1.6.112/drivers/shared/executor/pid_collector_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package executor 5 6 import ( 7 "testing" 8 9 "github.com/hernad/nomad/ci" 10 "github.com/mitchellh/go-ps" 11 ) 12 13 func TestScanPids(t *testing.T) { 14 ci.Parallel(t) 15 p1 := NewFakeProcess(2, 5) 16 p2 := NewFakeProcess(10, 2) 17 p3 := NewFakeProcess(15, 6) 18 p4 := NewFakeProcess(3, 10) 19 p5 := NewFakeProcess(20, 18) 20 21 nomadPids, err := scanPids(5, []ps.Process{p1, p2, p3, p4, p5}) 22 if err != nil { 23 t.Fatalf("error: %v", err) 24 } 25 if len(nomadPids) != 4 { 26 t.Fatalf("expected: 4, actual: %v", len(nomadPids)) 27 } 28 } 29 30 type FakeProcess struct { 31 pid int 32 ppid int 33 } 34 35 func (f FakeProcess) Pid() int { 36 return f.pid 37 } 38 39 func (f FakeProcess) PPid() int { 40 return f.ppid 41 } 42 43 func (f FakeProcess) Executable() string { 44 return "fake" 45 } 46 47 func NewFakeProcess(pid int, ppid int) ps.Process { 48 return FakeProcess{pid: pid, ppid: ppid} 49 }