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
|
package proc
import (
"fmt"
"os"
"os/exec"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
type (
// procIDInfos implements procs using a slice of already
// populated ProcIdInfo. Used for testing.
procIDInfos []IDInfo
)
func (p procIDInfos) get(i int) Proc {
return &p[i]
}
func (p procIDInfos) length() int {
return len(p)
}
func procInfoIter(ps ...IDInfo) *procIterator {
return &procIterator{procs: procIDInfos(ps), idx: -1}
}
func allprocs(procpath string) Iter {
fs, err := NewFS(procpath, false)
if err != nil {
cwd, _ := os.Getwd()
panic("can't read " + procpath + ", cwd=" + cwd + ", err=" + fmt.Sprintf("%v", err))
}
return fs.AllProcs()
}
func TestReadFixture(t *testing.T) {
procs := allprocs("../fixtures")
var pii IDInfo
count := 0
for procs.Next() {
count++
var err error
pii, err = procinfo(procs)
noerr(t, err)
}
err := procs.Close()
noerr(t, err)
if count != 1 {
t.Fatalf("got %d procs, want 1", count)
}
wantprocid := ID{Pid: 14804, StartTimeRel: 0x4f27b}
if diff := cmp.Diff(pii.ID, wantprocid); diff != "" {
t.Errorf("procid differs: (-got +want)\n%s", diff)
}
stime, _ := time.Parse(time.RFC3339Nano, "2017-10-19T22:52:51.19Z")
wantstatic := Static{
Name: "process-exporte",
Cmdline: []string{"./process-exporter", "-procnames", "bash"},
Cgroups: []string{"/system.slice/docker-8dde0b0d6e919baef8d635cd9399b22639ed1e400eaec1b1cb94ff3b216cf3c3.scope"},
ParentPid: 10884,
StartTime: stime,
EffectiveUID: 1000,
}
if diff := cmp.Diff(pii.Static, wantstatic); diff != "" {
t.Errorf("static differs: (-got +want)\n%s", diff)
}
wantmetrics := Metrics{
Counts: Counts{
CPUUserTime: 0.1,
CPUSystemTime: 0.04,
ReadBytes: 1814455,
WriteBytes: 0,
MajorPageFaults: 0x2ff,
MinorPageFaults: 0x643,
CtxSwitchVoluntary: 72,
CtxSwitchNonvoluntary: 6,
},
Memory: Memory{
ResidentBytes: uint64(0x7b1 * os.Getpagesize()),
VirtualBytes: 0x1061000,
VmSwapBytes: 0x2800,
},
Filedesc: Filedesc{
Open: 5,
Limit: 0x400,
},
NumThreads: 7,
States: States{Sleeping: 1},
}
if diff := cmp.Diff(pii.Metrics, wantmetrics); diff != "" {
t.Errorf("metrics differs: (-got +want)\n%s", diff)
}
}
func noerr(t *testing.T, err error) {
if err != nil {
t.Fatalf("error: %v", err)
}
}
// Basic test of proc reading: does AllProcs return at least two procs, one of which is us.
func TestAllProcs(t *testing.T) {
procs := allprocs("/proc")
count := 0
for procs.Next() {
count++
if procs.GetPid() != os.Getpid() {
continue
}
procid, err := procs.GetProcID()
noerr(t, err)
if procid.Pid != os.Getpid() {
t.Errorf("got %d, want %d", procid.Pid, os.Getpid())
}
static, err := procs.GetStatic()
noerr(t, err)
if static.ParentPid != os.Getppid() {
t.Errorf("got %d, want %d", static.ParentPid, os.Getppid())
}
metrics, _, err := procs.GetMetrics()
noerr(t, err)
if metrics.ResidentBytes == 0 {
t.Errorf("got 0 bytes resident, want nonzero")
}
// All Go programs have multiple threads.
if metrics.NumThreads < 2 {
t.Errorf("got %d threads, want >1", metrics.NumThreads)
}
var zstates States
if metrics.States == zstates {
t.Errorf("got empty states")
}
threads, err := procs.GetThreads()
if len(threads) < 2 {
t.Errorf("got %d thread details, want >1", len(threads))
}
}
err := procs.Close()
noerr(t, err)
if count == 0 {
t.Errorf("got %d, want 0", count)
}
}
// 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 TestAllProcsSpawn(t *testing.T) {
childprocs := func() []IDInfo {
found := []IDInfo{}
procs := allprocs("/proc")
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, IDInfo{procid, static, Metrics{}, nil})
}
}
err := procs.Close()
if err != nil {
t.Fatalf("error closing procs iterator: %v", err)
}
return found
}
foundcat := func(procs []IDInfo) bool {
for _, proc := range procs {
if proc.Name == "cat" {
return true
}
}
return false
}
if foundcat(childprocs()) {
t.Errorf("found cat before spawning it")
}
cmd := exec.Command("/bin/cat")
wc, err := cmd.StdinPipe()
noerr(t, err)
err = cmd.Start()
noerr(t, err)
if !foundcat(childprocs()) {
t.Errorf("didn't find cat after spawning it")
}
err = wc.Close()
noerr(t, err)
err = cmd.Wait()
noerr(t, err)
if foundcat(childprocs()) {
t.Errorf("found cat after exit")
}
}
func TestIterator(t *testing.T) {
p1 := newProc(1, "p1", Metrics{})
p2 := newProc(2, "p2", Metrics{})
want := []IDInfo{p1, p2}
pis := procInfoIter(want...)
got, err := consumeIter(pis)
noerr(t, err)
if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("procs differs: (-got +want)\n%s", diff)
}
}
|