1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-05-08 19:29:25 +08:00

Merge pull request #332 from shirou/test_refactoring

refactoring
This commit is contained in:
shirou 2017-03-15 23:36:14 +09:00 committed by GitHub
commit 98d5de7ce9
18 changed files with 48 additions and 52 deletions

View File

@ -171,7 +171,7 @@ func percentUsedFromLastCall(percpu bool) ([]float64, error) {
} }
if lastTimes == nil { if lastTimes == nil {
return nil, fmt.Errorf("Error getting times for cpu percent. LastTimes was nil") return nil, fmt.Errorf("error getting times for cpu percent. lastTimes was nil")
} }
return calculateAllBusy(lastTimes, cpuTimes) return calculateAllBusy(lastTimes, cpuTimes)
} }

View File

@ -118,7 +118,7 @@ func Info() ([]InfoStat, error) {
return nil, err return nil, err
} }
if c.Mhz, err = strconv.ParseFloat(vals[0], 64); err != nil { if c.Mhz, err = strconv.ParseFloat(vals[0], 64); err != nil {
return nil, fmt.Errorf("Unable to parse FreeBSD CPU clock rate: %v", err) return nil, fmt.Errorf("unable to parse FreeBSD CPU clock rate: %v", err)
} }
if vals, err = common.DoSysctrl("hw.ncpu"); err != nil { if vals, err = common.DoSysctrl("hw.ncpu"); err != nil {
@ -126,7 +126,7 @@ func Info() ([]InfoStat, error) {
} }
var i64 int64 var i64 int64
if i64, err = strconv.ParseInt(vals[0], 10, 32); err != nil { if i64, err = strconv.ParseInt(vals[0], 10, 32); err != nil {
return nil, fmt.Errorf("Unable to parse FreeBSD cores: %v", err) return nil, fmt.Errorf("unable to parse FreeBSD cores: %v", err)
} }
c.Cores = int32(i64) c.Cores = int32(i64)
@ -156,7 +156,7 @@ func parseDmesgBoot(fileName string) (InfoStat, int, error) {
c.Model = matches[4] c.Model = matches[4]
t, err := strconv.ParseInt(matches[5], 10, 32) t, err := strconv.ParseInt(matches[5], 10, 32)
if err != nil { if err != nil {
return c, 0, fmt.Errorf("Unable to parse FreeBSD CPU stepping information from %q: %v", line, err) return c, 0, fmt.Errorf("unable to parse FreeBSD CPU stepping information from %q: %v", line, err)
} }
c.Stepping = int32(t) c.Stepping = int32(t)
} else if matches := featuresMatch.FindStringSubmatch(line); matches != nil { } else if matches := featuresMatch.FindStringSubmatch(line); matches != nil {
@ -170,12 +170,12 @@ func parseDmesgBoot(fileName string) (InfoStat, int, error) {
} else if matches := cpuCores.FindStringSubmatch(line); matches != nil { } else if matches := cpuCores.FindStringSubmatch(line); matches != nil {
t, err := strconv.ParseInt(matches[1], 10, 32) t, err := strconv.ParseInt(matches[1], 10, 32)
if err != nil { if err != nil {
return c, 0, fmt.Errorf("Unable to parse FreeBSD CPU Nums from %q: %v", line, err) return c, 0, fmt.Errorf("unable to parse FreeBSD CPU Nums from %q: %v", line, err)
} }
cpuNum = int(t) cpuNum = int(t)
t2, err := strconv.ParseInt(matches[2], 10, 32) t2, err := strconv.ParseInt(matches[2], 10, 32)
if err != nil { if err != nil {
return c, 0, fmt.Errorf("Unable to parse FreeBSD CPU cores from %q: %v", line, err) return c, 0, fmt.Errorf("unable to parse FreeBSD CPU cores from %q: %v", line, err)
} }
c.Cores = int32(t2) c.Cores = int32(t2)
} }

View File

@ -23,7 +23,7 @@ func TestParseDmesgBoot(t *testing.T) {
{"2cpu_4core.txt", 2, 4}, {"2cpu_4core.txt", 2, 4},
} }
for _, tt := range cpuTests { for _, tt := range cpuTests {
v, num, err := parseDmesgBoot(filepath.Join("expected/freebsd/", tt.file)) v, num, err := parseDmesgBoot(filepath.Join("testdata", "freebsd", tt.file))
if err != nil { if err != nil {
t.Errorf("parseDmesgBoot failed(%s), %v", tt.file, err) t.Errorf("parseDmesgBoot failed(%s), %v", tt.file, err)
} }

View File

@ -36,30 +36,30 @@ func Times(percpu bool) ([]TimesStat, error) {
func Info() ([]InfoStat, error) { func Info() ([]InfoStat, error) {
psrInfo, err := exec.LookPath("/usr/sbin/psrinfo") psrInfo, err := exec.LookPath("/usr/sbin/psrinfo")
if err != nil { if err != nil {
return nil, fmt.Errorf("Cannot find psrinfo: %s", err) return nil, fmt.Errorf("cannot find psrinfo: %s", err)
} }
psrInfoOut, err := invoke.Command(psrInfo, "-p", "-v") psrInfoOut, err := invoke.Command(psrInfo, "-p", "-v")
if err != nil { if err != nil {
return nil, fmt.Errorf("Cannot execute psrinfo: %s", err) return nil, fmt.Errorf("cannot execute psrinfo: %s", err)
} }
isaInfo, err := exec.LookPath("/usr/bin/isainfo") isaInfo, err := exec.LookPath("/usr/bin/isainfo")
if err != nil { if err != nil {
return nil, fmt.Errorf("Cannot find isainfo: %s", err) return nil, fmt.Errorf("cannot find isainfo: %s", err)
} }
isaInfoOut, err := invoke.Command(isaInfo, "-b", "-v") isaInfoOut, err := invoke.Command(isaInfo, "-b", "-v")
if err != nil { if err != nil {
return nil, fmt.Errorf("Cannot execute isainfo: %s", err) return nil, fmt.Errorf("cannot execute isainfo: %s", err)
} }
procs, err := parseProcessorInfo(string(psrInfoOut)) procs, err := parseProcessorInfo(string(psrInfoOut))
if err != nil { if err != nil {
return nil, fmt.Errorf("Error parsing psrinfo output: %s", err) return nil, fmt.Errorf("error parsing psrinfo output: %s", err)
} }
flags, err := parseISAInfo(string(isaInfoOut)) flags, err := parseISAInfo(string(isaInfoOut))
if err != nil { if err != nil {
return nil, fmt.Errorf("Error parsing isainfo output: %s", err) return nil, fmt.Errorf("error parsing isainfo output: %s", err)
} }
result := make([]InfoStat, 0, len(flags)) result := make([]InfoStat, 0, len(flags))
@ -79,7 +79,7 @@ func parseISAInfo(cmdOutput string) ([]string, error) {
// Sanity check the output // Sanity check the output
if len(words) < 4 || words[1] != "bit" || words[3] != "applications" { if len(words) < 4 || words[1] != "bit" || words[3] != "applications" {
return nil, errors.New("Attempted to parse invalid isainfo output") return nil, errors.New("attempted to parse invalid isainfo output")
} }
flags := make([]string, len(words)-4) flags := make([]string, len(words)-4)
@ -117,7 +117,7 @@ func parseProcessorInfo(cmdOutput string) ([]InfoStat, error) {
if physicalCPU[psrStepOffset] != "" { if physicalCPU[psrStepOffset] != "" {
stepParsed, err := strconv.ParseInt(physicalCPU[psrStepOffset], 10, 32) stepParsed, err := strconv.ParseInt(physicalCPU[psrStepOffset], 10, 32)
if err != nil { if err != nil {
return nil, fmt.Errorf("Cannot parse value %q for step as 32-bit integer: %s", physicalCPU[9], err) return nil, fmt.Errorf("cannot parse value %q for step as 32-bit integer: %s", physicalCPU[9], err)
} }
step = int32(stepParsed) step = int32(stepParsed)
} }
@ -125,7 +125,7 @@ func parseProcessorInfo(cmdOutput string) ([]InfoStat, error) {
if physicalCPU[psrClockOffset] != "" { if physicalCPU[psrClockOffset] != "" {
clockParsed, err := strconv.ParseInt(physicalCPU[psrClockOffset], 10, 64) clockParsed, err := strconv.ParseInt(physicalCPU[psrClockOffset], 10, 64)
if err != nil { if err != nil {
return nil, fmt.Errorf("Cannot parse value %q for clock as 32-bit integer: %s", physicalCPU[10], err) return nil, fmt.Errorf("cannot parse value %q for clock as 32-bit integer: %s", physicalCPU[10], err)
} }
clock = float64(clockParsed) clock = float64(clockParsed)
} }
@ -137,7 +137,7 @@ func parseProcessorInfo(cmdOutput string) ([]InfoStat, error) {
case physicalCPU[psrNumCoresOffset] != "": case physicalCPU[psrNumCoresOffset] != "":
numCores, err = strconv.ParseInt(physicalCPU[psrNumCoresOffset], 10, 32) numCores, err = strconv.ParseInt(physicalCPU[psrNumCoresOffset], 10, 32)
if err != nil { if err != nil {
return nil, fmt.Errorf("Cannot parse value %q for core count as 32-bit integer: %s", physicalCPU[1], err) return nil, fmt.Errorf("cannot parse value %q for core count as 32-bit integer: %s", physicalCPU[1], err)
} }
for i := 0; i < int(numCores); i++ { for i := 0; i < int(numCores); i++ {
@ -158,12 +158,12 @@ func parseProcessorInfo(cmdOutput string) ([]InfoStat, error) {
case physicalCPU[psrNumCoresHTOffset] != "": case physicalCPU[psrNumCoresHTOffset] != "":
numCores, err = strconv.ParseInt(physicalCPU[psrNumCoresHTOffset], 10, 32) numCores, err = strconv.ParseInt(physicalCPU[psrNumCoresHTOffset], 10, 32)
if err != nil { if err != nil {
return nil, fmt.Errorf("Cannot parse value %q for core count as 32-bit integer: %s", physicalCPU[3], err) return nil, fmt.Errorf("cannot parse value %q for core count as 32-bit integer: %s", physicalCPU[3], err)
} }
numHT, err = strconv.ParseInt(physicalCPU[psrNumHTOffset], 10, 32) numHT, err = strconv.ParseInt(physicalCPU[psrNumHTOffset], 10, 32)
if err != nil { if err != nil {
return nil, fmt.Errorf("Cannot parse value %q for hyperthread count as 32-bit integer: %s", physicalCPU[4], err) return nil, fmt.Errorf("cannot parse value %q for hyperthread count as 32-bit integer: %s", physicalCPU[4], err)
} }
for i := 0; i < int(numCores); i++ { for i := 0; i < int(numCores); i++ {
@ -182,7 +182,7 @@ func parseProcessorInfo(cmdOutput string) ([]InfoStat, error) {
infoStatCount++ infoStatCount++
} }
default: default:
return nil, errors.New("Values for cores with and without hyperthreading are both set") return nil, errors.New("values for cores with and without hyperthreading are both set")
} }
} }
return result, nil return result, nil

View File

@ -36,7 +36,7 @@ func TestParseISAInfo(t *testing.T) {
} }
for _, tc := range cases { for _, tc := range cases {
content, err := ioutil.ReadFile(filepath.Join("expected", "solaris", tc.filename)) content, err := ioutil.ReadFile(filepath.Join("testdata", "solaris", tc.filename))
if err != nil { if err != nil {
t.Errorf("cannot read test case: %s", err) t.Errorf("cannot read test case: %s", err)
} }
@ -96,7 +96,7 @@ func TestParseProcessorInfo(t *testing.T) {
} }
for _, tc := range cases { for _, tc := range cases {
content, err := ioutil.ReadFile(filepath.Join("expected", "solaris", tc.filename)) content, err := ioutil.ReadFile(filepath.Join("testdata", "solaris", tc.filename))
if err != nil { if err != nil {
t.Errorf("cannot read test case: %s", err) t.Errorf("cannot read test case: %s", err)
} }

View File

@ -109,7 +109,7 @@ func BootTime() (uint64, error) {
kstats := kstatMatch.FindAllStringSubmatch(string(out), -1) kstats := kstatMatch.FindAllStringSubmatch(string(out), -1)
if len(kstats) != 1 { if len(kstats) != 1 {
return 0, fmt.Errorf("Expected 1 kstat, found %d", len(kstats)) return 0, fmt.Errorf("expected 1 kstat, found %d", len(kstats))
} }
return strconv.ParseUint(kstats[0][2], 10, 64) return strconv.ParseUint(kstats[0][2], 10, 64)

View File

@ -10,6 +10,7 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"errors" "errors"
"fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"net/url" "net/url"
@ -41,7 +42,6 @@ func (i Invoke) Command(name string, arg ...string) ([]byte, error) {
} }
type FakeInvoke struct { type FakeInvoke struct {
CommandExpectedDir string // CommandExpectedDir specifies dir which includes expected outputs.
Suffix string // Suffix species expected file name suffix such as "fail" Suffix string // Suffix species expected file name suffix such as "fail"
Error error // If Error specfied, return the error. Error error // If Error specfied, return the error.
} }
@ -54,22 +54,18 @@ func (i FakeInvoke) Command(name string, arg ...string) ([]byte, error) {
arch := runtime.GOOS arch := runtime.GOOS
fname := strings.Join(append([]string{name}, arg...), "") commandName := filepath.Base(name)
fname := strings.Join(append([]string{commandName}, arg...), "")
fname = url.QueryEscape(fname) fname = url.QueryEscape(fname)
var dir string fpath := path.Join("testdata", arch, fname)
if i.CommandExpectedDir == "" {
dir = "expected"
} else {
dir = i.CommandExpectedDir
}
fpath := path.Join(dir, arch, fname)
if i.Suffix != "" { if i.Suffix != "" {
fpath += "_" + i.Suffix fpath += "_" + i.Suffix
} }
if PathExists(fpath) { if PathExists(fpath) {
return ioutil.ReadFile(fpath) return ioutil.ReadFile(fpath)
} }
return exec.Command(name, arg...).Output() return []byte{}, fmt.Errorf("could not find testdata: %s", fpath)
} }
var ErrNotImplementedError = errors.New("not implemented yet") var ErrNotImplementedError = errors.New("not implemented yet")

View File

@ -56,7 +56,7 @@ func zoneName() (string, error) {
return strings.TrimSpace(string(out)), nil return strings.TrimSpace(string(out)), nil
} }
var globalZoneMemoryCapacityMatch = regexp.MustCompile(`Memory size: ([\d]+) Megabytes`) var globalZoneMemoryCapacityMatch = regexp.MustCompile(`memory size: ([\d]+) Megabytes`)
func globalZoneMemoryCapacity() (uint64, error) { func globalZoneMemoryCapacity() (uint64, error) {
prtconf, err := exec.LookPath("/usr/sbin/prtconf") prtconf, err := exec.LookPath("/usr/sbin/prtconf")
@ -71,7 +71,7 @@ func globalZoneMemoryCapacity() (uint64, error) {
match := globalZoneMemoryCapacityMatch.FindAllStringSubmatch(string(out), -1) match := globalZoneMemoryCapacityMatch.FindAllStringSubmatch(string(out), -1)
if len(match) != 1 { if len(match) != 1 {
return 0, errors.New("Memory size not contained in output of /usr/sbin/prtconf") return 0, errors.New("memory size not contained in output of /usr/sbin/prtconf")
} }
totalMB, err := strconv.ParseUint(match[0][1], 10, 64) totalMB, err := strconv.ParseUint(match[0][1], 10, 64)
@ -97,7 +97,7 @@ func nonGlobalZoneMemoryCapacity() (uint64, error) {
kstats := kstatMatch.FindAllStringSubmatch(string(out), -1) kstats := kstatMatch.FindAllStringSubmatch(string(out), -1)
if len(kstats) != 1 { if len(kstats) != 1 {
return 0, fmt.Errorf("Expected 1 kstat, found %d", len(kstats)) return 0, fmt.Errorf("expected 1 kstat, found %d", len(kstats))
} }
memSizeBytes, err := strconv.ParseUint(kstats[0][2], 10, 64) memSizeBytes, err := strconv.ParseUint(kstats[0][2], 10, 64)