Add tests for procstat systemd & cgroup matching (#3469)

This commit is contained in:
Patrick Hemmer 2017-11-13 17:45:31 -05:00 committed by Daniel Nelson
parent 181a56018f
commit cbd346117a
2 changed files with 78 additions and 2 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os/exec" "os/exec"
"path/filepath"
"strconv" "strconv"
"time" "time"
@ -297,9 +298,12 @@ func (p *Procstat) findPids() ([]PID, map[string]string, error) {
return pids, tags, err return pids, tags, err
} }
// execCommand is so tests can mock out exec.Command usage.
var execCommand = exec.Command
func (p *Procstat) systemdUnitPIDs() ([]PID, error) { func (p *Procstat) systemdUnitPIDs() ([]PID, error) {
var pids []PID var pids []PID
cmd := exec.Command("systemctl", "show", p.SystemdUnit) cmd := execCommand("systemctl", "show", p.SystemdUnit)
out, err := cmd.Output() out, err := cmd.Output()
if err != nil { if err != nil {
return nil, err return nil, err
@ -331,7 +335,7 @@ func (p *Procstat) cgroupPIDs() ([]PID, error) {
if procsPath[0] != '/' { if procsPath[0] != '/' {
procsPath = "/sys/fs/cgroup/" + procsPath procsPath = "/sys/fs/cgroup/" + procsPath
} }
procsPath = procsPath + "/cgroup.procs" procsPath = filepath.Join(procsPath, "cgroup.procs")
out, err := ioutil.ReadFile(procsPath) out, err := ioutil.ReadFile(procsPath)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -2,7 +2,11 @@ package procstat
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/exec"
"path/filepath"
"strings"
"testing" "testing"
"time" "time"
@ -13,6 +17,46 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func init() {
execCommand = mockExecCommand
}
func mockExecCommand(arg0 string, args ...string) *exec.Cmd {
args = append([]string{"-test.run=TestMockExecCommand", "--", arg0}, args...)
cmd := exec.Command(os.Args[0], args...)
cmd.Stderr = os.Stderr
return cmd
}
func TestMockExecCommand(t *testing.T) {
var cmd []string
for _, arg := range os.Args {
if string(arg) == "--" {
cmd = []string{}
continue
}
if cmd == nil {
continue
}
cmd = append(cmd, string(arg))
}
if cmd == nil {
return
}
cmdline := strings.Join(cmd, " ")
if cmdline == "systemctl show TestGather_systemdUnitPIDs" {
fmt.Printf(`PIDFile=
GuessMainPID=yes
MainPID=11408
ControlPID=0
ExecMainPID=11408
`)
os.Exit(0)
}
fmt.Printf("command not found\n")
os.Exit(1)
}
type testPgrep struct { type testPgrep struct {
pids []PID pids []PID
err error err error
@ -292,3 +336,31 @@ func TestGather_PercentSecondPass(t *testing.T) {
assert.True(t, acc.HasFloatField("procstat", "cpu_time_user")) assert.True(t, acc.HasFloatField("procstat", "cpu_time_user"))
assert.True(t, acc.HasFloatField("procstat", "cpu_usage")) assert.True(t, acc.HasFloatField("procstat", "cpu_usage"))
} }
func TestGather_systemdUnitPIDs(t *testing.T) {
p := Procstat{
createPIDFinder: pidFinder([]PID{}, nil),
SystemdUnit: "TestGather_systemdUnitPIDs",
}
pids, tags, err := p.findPids()
require.NoError(t, err)
assert.Equal(t, []PID{11408}, pids)
assert.Equal(t, "TestGather_systemdUnitPIDs", tags["systemd_unit"])
}
func TestGather_cgroupPIDs(t *testing.T) {
td, err := ioutil.TempDir("", "")
require.NoError(t, err)
defer os.RemoveAll(td)
err = ioutil.WriteFile(filepath.Join(td, "cgroup.procs"), []byte("1234\n5678\n"), 0644)
require.NoError(t, err)
p := Procstat{
createPIDFinder: pidFinder([]PID{}, nil),
CGroup: td,
}
pids, tags, err := p.findPids()
require.NoError(t, err)
assert.Equal(t, []PID{1234, 5678}, pids)
assert.Equal(t, td, tags["cgroup"])
}