Update go dependencies

This commit is contained in:
Manuel de Brito Fontes 2018-05-26 11:27:53 -04:00 committed by Manuel Alejandro de Brito Fontes
parent 15ffb51394
commit bb4d483837
No known key found for this signature in database
GPG key ID: 786136016A8BA02A
1621 changed files with 86368 additions and 284392 deletions

View file

@ -1,26 +0,0 @@
package proc
import (
. "gopkg.in/check.v1"
"testing"
)
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }
type MySuite struct{}
var _ = Suite(&MySuite{})
// read everything in the iterator
func consumeIter(pi ProcIter) ([]ProcIdInfo, error) {
infos := []ProcIdInfo{}
for pi.Next() {
info, err := Info(pi)
if err != nil {
return nil, err
}
infos = append(infos, info)
}
return infos, nil
}

View file

@ -1,268 +0,0 @@
package proc
import (
"github.com/kylelemons/godebug/pretty"
common "github.com/ncabatoff/process-exporter"
. "gopkg.in/check.v1"
"time"
)
type namer map[string]struct{}
func newNamer(names ...string) namer {
nr := make(namer, len(names))
for _, name := range names {
nr[name] = struct{}{}
}
return nr
}
func (n namer) MatchAndName(nacl common.NameAndCmdline) (bool, string) {
if _, ok := n[nacl.Name]; ok {
return true, nacl.Name
}
return false, ""
}
// Test core group() functionality, i.e things not related to namers or parents
// or processes that have exited.
func (s MySuite) TestGrouperBasic(c *C) {
newProc := func(pid int, name string, m ProcMetrics) ProcIdInfo {
pis := newProcIdStatic(pid, 0, 0, name, nil)
return ProcIdInfo{
ProcId: pis.ProcId,
ProcStatic: pis.ProcStatic,
ProcMetrics: m,
}
}
gr := NewGrouper(false, newNamer("g1", "g2"))
p1 := newProc(1, "g1", ProcMetrics{1, 2, 3, 4, 5, 4, 400})
p2 := newProc(2, "g2", ProcMetrics{2, 3, 4, 5, 6, 40, 400})
p3 := newProc(3, "g3", ProcMetrics{})
_, err := gr.Update(procInfoIter(p1, p2, p3))
c.Assert(err, IsNil)
got1 := gr.groups()
want1 := GroupCountMap{
"g1": GroupCounts{Counts{0, 0, 0}, 1, 4, 5, time.Time{}, 4, 0.01},
"g2": GroupCounts{Counts{0, 0, 0}, 1, 5, 6, time.Time{}, 40, 0.1},
}
c.Check(got1, DeepEquals, want1, Commentf("diff %s", pretty.Compare(got1, want1)))
// Now increment counts and memory and make sure group counts updated.
p1.ProcMetrics = ProcMetrics{2, 3, 4, 5, 6, 4, 400}
p2.ProcMetrics = ProcMetrics{4, 5, 6, 7, 8, 40, 400}
_, err = gr.Update(procInfoIter(p1, p2, p3))
c.Assert(err, IsNil)
got2 := gr.groups()
want2 := GroupCountMap{
"g1": GroupCounts{Counts{1, 1, 1}, 1, 5, 6, time.Time{}, 4, 0.01},
"g2": GroupCounts{Counts{2, 2, 2}, 1, 7, 8, time.Time{}, 40, 0.1},
}
c.Check(got2, DeepEquals, want2, Commentf("diff %s", pretty.Compare(got2, want2)))
// Now add a new proc and update p2's metrics. The
// counts for p4 won't be factored into the total yet
// because we only add to counts starting with the
// second time we see a proc. Memory and FDs are affected
// though.
p4 := newProc(4, "g2", ProcMetrics{1, 1, 1, 1, 1, 80, 400})
p2.ProcMetrics = ProcMetrics{5, 6, 7, 8, 9, 40, 400}
_, err = gr.Update(procInfoIter(p1, p2, p3, p4))
c.Assert(err, IsNil)
got3 := gr.groups()
want3 := GroupCountMap{
"g1": GroupCounts{Counts{0, 0, 0}, 1, 5, 6, time.Time{}, 4, 0.01},
"g2": GroupCounts{Counts{1, 1, 1}, 2, 9, 10, time.Time{}, 120, 0.2},
}
c.Check(got3, DeepEquals, want3, Commentf("diff %s", pretty.Compare(got3, want3)))
p4.ProcMetrics = ProcMetrics{2, 2, 2, 2, 2, 100, 400}
p2.ProcMetrics = ProcMetrics{6, 7, 8, 8, 9, 40, 400}
_, err = gr.Update(procInfoIter(p1, p2, p3, p4))
c.Assert(err, IsNil)
got4 := gr.groups()
want4 := GroupCountMap{
"g1": GroupCounts{Counts{0, 0, 0}, 1, 5, 6, time.Time{}, 4, 0.01},
"g2": GroupCounts{Counts{2, 2, 2}, 2, 10, 11, time.Time{}, 140, 0.25},
}
c.Check(got4, DeepEquals, want4, Commentf("diff %s", pretty.Compare(got4, want4)))
}
// Test that if a proc is tracked, we track its descendants, and if not as
// before it gets ignored. We won't bother testing metric accumulation since
// that should be covered by TestGrouperBasic.
func (s MySuite) TestGrouperParents(c *C) {
newProc := func(pid, ppid int, name string) ProcIdInfo {
pis := newProcIdStatic(pid, ppid, 0, name, nil)
return ProcIdInfo{
ProcId: pis.ProcId,
ProcStatic: pis.ProcStatic,
ProcMetrics: ProcMetrics{},
}
}
gr := NewGrouper(true, newNamer("g1", "g2"))
p1 := newProc(1, 0, "g1")
p2 := newProc(2, 0, "g2")
p3 := newProc(3, 0, "g3")
_, err := gr.Update(procInfoIter(p1, p2, p3))
c.Assert(err, IsNil)
got1 := gr.groups()
want1 := GroupCountMap{
"g1": GroupCounts{Counts{}, 1, 0, 0, time.Time{}, 0, 0},
"g2": GroupCounts{Counts{}, 1, 0, 0, time.Time{}, 0, 0},
}
c.Check(got1, DeepEquals, want1, Commentf("diff %s", pretty.Compare(got1, want1)))
// Now we'll give each of the procs a child and test that the count of procs
// in each group is incremented.
p4 := newProc(4, p1.Pid, "")
p5 := newProc(5, p2.Pid, "")
p6 := newProc(6, p3.Pid, "")
_, err = gr.Update(procInfoIter(p1, p2, p3, p4, p5, p6))
c.Assert(err, IsNil)
got2 := gr.groups()
want2 := GroupCountMap{
"g1": GroupCounts{Counts{}, 2, 0, 0, time.Time{}, 0, 0},
"g2": GroupCounts{Counts{}, 2, 0, 0, time.Time{}, 0, 0},
}
c.Check(got2, DeepEquals, want2, Commentf("diff %s", pretty.Compare(got2, want2)))
// Now we'll let p4 die, and give p5 a child and grandchild and great-grandchild.
p7 := newProc(7, p5.Pid, "")
p8 := newProc(8, p7.Pid, "")
p9 := newProc(9, p8.Pid, "")
_, err = gr.Update(procInfoIter(p1, p2, p3, p5, p6, p7, p8, p9))
c.Assert(err, IsNil)
got3 := gr.groups()
want3 := GroupCountMap{
"g1": GroupCounts{Counts{}, 1, 0, 0, time.Time{}, 0, 0},
"g2": GroupCounts{Counts{}, 5, 0, 0, time.Time{}, 0, 0},
}
c.Check(got3, DeepEquals, want3, Commentf("diff %s", pretty.Compare(got3, want3)))
}
// Test that Groups() reports on new CPU/IO activity, even if some processes in the
// group have gone away.
func (s MySuite) TestGrouperGroup(c *C) {
newProc := func(pid int, name string, m ProcMetrics) ProcIdInfo {
pis := newProcIdStatic(pid, 0, 0, name, nil)
return ProcIdInfo{
ProcId: pis.ProcId,
ProcStatic: pis.ProcStatic,
ProcMetrics: m,
}
}
gr := NewGrouper(false, newNamer("g1"))
// First call should return zero CPU/IO.
p1 := newProc(1, "g1", ProcMetrics{1, 2, 3, 4, 5, 8, 400})
_, err := gr.Update(procInfoIter(p1))
c.Assert(err, IsNil)
got1 := gr.Groups()
want1 := GroupCountMap{
"g1": GroupCounts{Counts{0, 0, 0}, 1, 4, 5, time.Time{}, 8, 0.02},
}
c.Check(got1, DeepEquals, want1)
// Second call should return the delta CPU/IO from first observance,
// as well as latest memory/proccount.
p1.ProcMetrics = ProcMetrics{2, 3, 4, 5, 6, 12, 400}
_, err = gr.Update(procInfoIter(p1))
c.Assert(err, IsNil)
got2 := gr.Groups()
want2 := GroupCountMap{
"g1": GroupCounts{Counts{1, 1, 1}, 1, 5, 6, time.Time{}, 12, 0.03},
}
c.Check(got2, DeepEquals, want2)
// Third call: process hasn't changed, nor should our group stats.
_, err = gr.Update(procInfoIter(p1))
c.Assert(err, IsNil)
got3 := gr.Groups()
want3 := GroupCountMap{
"g1": GroupCounts{Counts{1, 1, 1}, 1, 5, 6, time.Time{}, 12, 0.03},
}
c.Check(got3, DeepEquals, want3, Commentf("diff %s", pretty.Compare(got3, want3)))
}
// Test that Groups() reports on new CPU/IO activity, even if some processes in the
// group have gone away.
func (s MySuite) TestGrouperNonDecreasing(c *C) {
newProc := func(pid int, name string, m ProcMetrics) ProcIdInfo {
pis := newProcIdStatic(pid, 0, 0, name, nil)
return ProcIdInfo{
ProcId: pis.ProcId,
ProcStatic: pis.ProcStatic,
ProcMetrics: m,
}
}
gr := NewGrouper(false, newNamer("g1", "g2"))
p1 := newProc(1, "g1", ProcMetrics{1, 2, 3, 4, 5, 4, 400})
p2 := newProc(2, "g2", ProcMetrics{2, 3, 4, 5, 6, 40, 400})
_, err := gr.Update(procInfoIter(p1, p2))
c.Assert(err, IsNil)
got1 := gr.Groups()
want1 := GroupCountMap{
"g1": GroupCounts{Counts{0, 0, 0}, 1, 4, 5, time.Time{}, 4, 0.01},
"g2": GroupCounts{Counts{0, 0, 0}, 1, 5, 6, time.Time{}, 40, 0.1},
}
c.Check(got1, DeepEquals, want1, Commentf("diff %s", pretty.Compare(got1, want1)))
// Now add a new proc p3 to g2, and increment p1/p2's metrics.
p1.ProcMetrics = ProcMetrics{2, 3, 4, 5, 6, 8, 400}
p2.ProcMetrics = ProcMetrics{4, 5, 6, 7, 8, 80, 400}
p3 := newProc(3, "g2", ProcMetrics{1, 1, 1, 1, 1, 8, 400})
_, err = gr.Update(procInfoIter(p1, p2, p3))
c.Assert(err, IsNil)
got2 := gr.Groups()
want2 := GroupCountMap{
"g1": GroupCounts{Counts{1, 1, 1}, 1, 5, 6, time.Time{}, 8, 0.02},
"g2": GroupCounts{Counts{2, 2, 2}, 2, 8, 9, time.Time{}, 88, 0.2},
}
c.Check(got2, DeepEquals, want2, Commentf("diff %s", pretty.Compare(got2, want2)))
// Now update p3's metrics and kill p2.
p3.ProcMetrics = ProcMetrics{2, 3, 4, 5, 6, 8, 400}
_, err = gr.Update(procInfoIter(p1, p3))
c.Assert(err, IsNil)
got3 := gr.Groups()
want3 := GroupCountMap{
"g1": GroupCounts{Counts{1, 1, 1}, 1, 5, 6, time.Time{}, 8, 0.02},
"g2": GroupCounts{Counts{3, 4, 5}, 1, 5, 6, time.Time{}, 8, 0.02},
}
c.Check(got3, DeepEquals, want3, Commentf("diff %s", pretty.Compare(got3, want3)))
// Now update p3's metrics and kill p1.
p3.ProcMetrics = ProcMetrics{4, 4, 4, 2, 1, 4, 400}
_, err = gr.Update(procInfoIter(p3))
c.Assert(err, IsNil)
got4 := gr.Groups()
want4 := GroupCountMap{
"g1": GroupCounts{Counts{1, 1, 1}, 0, 0, 0, time.Time{}, 0, 0},
"g2": GroupCounts{Counts{5, 5, 5}, 1, 2, 1, time.Time{}, 4, 0.01},
}
c.Check(got4, DeepEquals, want4, Commentf("diff %s\n%s", pretty.Compare(got4, want4), pretty.Sprint(gr)))
}

View file

@ -1,120 +0,0 @@
package proc
import (
. "gopkg.in/check.v1"
"os"
"os/exec"
)
var fs *FS
func init() {
fs, _ = NewFS("/proc")
}
// Basic test of proc reading: does AllProcs return at least two procs, one of which is us.
func (s MySuite) TestAllProcs(c *C) {
procs := fs.AllProcs()
count := 0
for procs.Next() {
count++
if procs.GetPid() != os.Getpid() {
continue
}
procid, err := procs.GetProcId()
c.Assert(err, IsNil)
c.Check(procid.Pid, Equals, os.Getpid())
static, err := procs.GetStatic()
c.Assert(err, IsNil)
c.Check(static.ParentPid, Equals, os.Getppid())
}
err := procs.Close()
c.Assert(err, IsNil)
c.Check(count, Not(Equals), 0)
}
// Test that we can observe the absence of a child process before it spawns and after it exits,
// and its presence during its lifetime.
func (s MySuite) TestAllProcsSpawn(c *C) {
childprocs := func() ([]ProcIdStatic, error) {
found := []ProcIdStatic{}
procs := fs.AllProcs()
mypid := os.Getpid()
for procs.Next() {
procid, err := procs.GetProcId()
if err != nil {
continue
}
static, err := procs.GetStatic()
if err != nil {
continue
}
if static.ParentPid == mypid {
found = append(found, ProcIdStatic{procid, static})
}
}
err := procs.Close()
if err != nil {
return nil, err
}
return found, nil
}
children1, err := childprocs()
c.Assert(err, IsNil)
cmd := exec.Command("/bin/cat")
wc, err := cmd.StdinPipe()
c.Assert(err, IsNil)
err = cmd.Start()
c.Assert(err, IsNil)
children2, err := childprocs()
c.Assert(err, IsNil)
err = wc.Close()
c.Assert(err, IsNil)
err = cmd.Wait()
c.Assert(err, IsNil)
children3, err := childprocs()
c.Assert(err, IsNil)
foundcat := func(procs []ProcIdStatic) bool {
for _, proc := range procs {
if proc.Name == "cat" {
return true
}
}
return false
}
c.Check(foundcat(children1), Equals, false)
c.Check(foundcat(children2), Equals, true)
c.Check(foundcat(children3), Equals, false)
}
func (s MySuite) TestIterator(c *C) {
// create a new proc with zero metrics, cmdline, starttime, ppid
newProc := func(pid int, name string) ProcIdInfo {
pis := newProcIdStatic(pid, 0, 0, name, nil)
return ProcIdInfo{
ProcId: pis.ProcId,
ProcStatic: pis.ProcStatic,
ProcMetrics: ProcMetrics{},
}
}
p1 := newProc(1, "p1")
want1 := []ProcIdInfo{p1}
pi1 := procInfoIter(want1...)
got, err := consumeIter(pi1)
c.Assert(err, IsNil)
c.Check(got, DeepEquals, want1)
p2 := newProc(2, "p2")
want2 := []ProcIdInfo{p1, p2}
pi2 := procInfoIter(want2...)
got2, err := consumeIter(pi2)
c.Assert(err, IsNil)
c.Check(got2, DeepEquals, want2)
}

View file

@ -1,94 +0,0 @@
package proc
import (
. "gopkg.in/check.v1"
)
// Verify that the tracker accurately reports new procs that aren't ignored or tracked.
func (s MySuite) TestTrackerBasic(c *C) {
// create a new proc with zero metrics, cmdline, starttime, ppid
newProc := func(pid int, startTime uint64, name string) ProcIdInfo {
pis := newProcIdStatic(pid, 0, 0, name, nil)
return ProcIdInfo{
ProcId: pis.ProcId,
ProcStatic: pis.ProcStatic,
ProcMetrics: ProcMetrics{},
}
}
tr := NewTracker()
// Test that p1 is seen as new
p1 := newProc(1, 1, "p1")
want1 := []ProcIdInfo{p1}
got1, _, err := tr.Update(procInfoIter(want1...))
c.Assert(err, IsNil)
c.Check(got1, DeepEquals, want1)
// Test that p1 is no longer seen as new once tracked
tr.Track("g1", p1)
got2, _, err := tr.Update(procInfoIter(want1...))
c.Assert(err, IsNil)
c.Check(got2, DeepEquals, []ProcIdInfo(nil))
// Test that p2 is new now, but p1 is still not
p2 := newProc(2, 2, "p2")
want2 := []ProcIdInfo{p2}
got3, _, err := tr.Update(procInfoIter(p1, p2))
c.Assert(err, IsNil)
c.Check(got3, DeepEquals, want2)
// Test that p2 stops being new once ignored
tr.Ignore(p2.ProcId)
got4, _, err := tr.Update(procInfoIter(p1, p2))
c.Assert(err, IsNil)
c.Check(got4, DeepEquals, []ProcIdInfo(nil))
// TODO test that starttime is taken into account, i.e. pid recycling is handled.
}
// Verify that the tracker accurately reports metric changes.
func (s MySuite) TestTrackerCounts(c *C) {
// create a new proc with cmdline, starttime, ppid
newProc := func(pid int, startTime uint64, name string, m ProcMetrics) ProcIdInfo {
pis := newProcIdStatic(pid, 0, 0, name, nil)
return ProcIdInfo{
ProcId: pis.ProcId,
ProcStatic: pis.ProcStatic,
ProcMetrics: m,
}
}
tr := NewTracker()
// Test that p1 is seen as new
p1 := newProc(1, 1, "p1", ProcMetrics{1, 2, 3, 4, 5, 6, 4096})
want1 := []ProcIdInfo{p1}
got1, _, err := tr.Update(procInfoIter(p1))
c.Assert(err, IsNil)
c.Check(got1, DeepEquals, want1)
// Test that p1 is no longer seen as new once tracked
tr.Track("g1", p1)
got2, _, err := tr.Update(procInfoIter(p1))
c.Assert(err, IsNil)
c.Check(got2, DeepEquals, []ProcIdInfo(nil))
// Now update p1's metrics
p1.ProcMetrics = ProcMetrics{2, 3, 4, 5, 6, 7, 4096}
got3, _, err := tr.Update(procInfoIter(p1))
c.Assert(err, IsNil)
c.Check(got3, DeepEquals, []ProcIdInfo(nil))
// Test that counts are correct
c.Check(tr.Tracked[p1.ProcId].accum, Equals, Counts{1, 1, 1})
c.Check(tr.Tracked[p1.ProcId].info, DeepEquals, ProcInfo{p1.ProcStatic, p1.ProcMetrics})
// Now update p1's metrics again
p1.ProcMetrics = ProcMetrics{4, 6, 8, 9, 10, 11, 4096}
got4, _, err := tr.Update(procInfoIter(p1))
c.Assert(err, IsNil)
c.Check(got4, DeepEquals, []ProcIdInfo(nil))
// Test that counts are correct
c.Check(tr.Tracked[p1.ProcId].accum, Equals, Counts{3, 4, 5})
c.Check(tr.Tracked[p1.ProcId].info, DeepEquals, ProcInfo{p1.ProcStatic, p1.ProcMetrics})
}