forked from jirs5/gosigar-freebsd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support process group ID (pgid) for Darwin and Linux (#26)
* Added Darwin pgid * Added Linux pgid * Skip the task_info test if not running as root
- Loading branch information
1 parent
fe371dd
commit d752b70
Showing
5 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package gosigar_test | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"os/exec" | ||
"os/user" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
|
||
sigar "github.com/elastic/gosigar" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var procinfo map[string]string | ||
|
||
func setUp(t testing.TB) { | ||
out, err := exec.Command("/bin/ps", "-p1", "-c", "-opid,comm,stat,ppid,pgid,tty,pri,ni").Output() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
rdr := bufio.NewReader(bytes.NewReader(out)) | ||
_, err = rdr.ReadString('\n') // skip header | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
data, err := rdr.ReadString('\n') | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
procinfo = make(map[string]string, 8) | ||
fields := strings.Fields(data) | ||
procinfo["pid"] = fields[0] | ||
procinfo["name"] = fields[1] | ||
procinfo["stat"] = fields[2] | ||
procinfo["ppid"] = fields[3] | ||
procinfo["pgid"] = fields[4] | ||
procinfo["tty"] = fields[5] | ||
procinfo["prio"] = fields[6] | ||
procinfo["nice"] = fields[7] | ||
|
||
} | ||
|
||
func tearDown(t testing.TB) { | ||
} | ||
|
||
/* ProcState.Get() call task_info, which on Mac OS X requires root | ||
or a signed executable. Skip the test if not running as root | ||
to accommodate automated tests, but let users test locally using | ||
`sudo -E go test` | ||
*/ | ||
|
||
func TestDarwinProcState(t *testing.T) { | ||
setUp(t) | ||
defer tearDown(t) | ||
|
||
state := sigar.ProcState{} | ||
usr, err := user.Current() | ||
if err == nil && usr.Username == "root" { | ||
if assert.NoError(t, state.Get(1)) { | ||
|
||
ppid, _ := strconv.Atoi(procinfo["ppid"]) | ||
pgid, _ := strconv.Atoi(procinfo["pgid"]) | ||
|
||
assert.Equal(t, procinfo["name"], state.Name) | ||
assert.Equal(t, ppid, state.Ppid) | ||
assert.Equal(t, pgid, state.Pgid) | ||
assert.Equal(t, 1, state.Pgid) | ||
assert.Equal(t, 0, state.Ppid) | ||
} | ||
} else { | ||
fmt.Println("Skipping ProcState test; run as root to test") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters