package process import ( "os" "runtime" "strings" "testing" "time" "github.com/influxdb/telegraf/plugins/system/ps/common" ) func testGetProcess() Process { checkPid := os.Getpid() // process.test ret, _ := NewProcess(int32(checkPid)) return *ret } func Test_Pids(t *testing.T) { ret, err := Pids() if err != nil { t.Errorf("error %v", err) } if len(ret) == 0 { t.Errorf("could not get pids %v", ret) } } func Test_Pid_exists(t *testing.T) { checkPid := os.Getpid() ret, err := PidExists(int32(checkPid)) if err != nil { t.Errorf("error %v", err) } if ret == false { t.Errorf("could not get process exists: %v", ret) } } func Test_NewProcess(t *testing.T) { checkPid := os.Getpid() ret, err := NewProcess(int32(checkPid)) if err != nil { t.Errorf("error %v", err) } empty := &Process{} if runtime.GOOS != "windows" { // Windows pid is 0 if empty == ret { t.Errorf("error %v", ret) } } } func Test_Process_memory_maps(t *testing.T) { checkPid := os.Getpid() ret, err := NewProcess(int32(checkPid)) mmaps, err := ret.MemoryMaps(false) if err != nil { if err == common.NotImplementedError { t.SkipNow() } t.Errorf("memory map get error %v", err) } empty := MemoryMapsStat{} for _, m := range *mmaps { if m == empty { t.Errorf("memory map get error %v", m) } } } func Test_Process_MemoryInfo(t *testing.T) { p := testGetProcess() v, err := p.MemoryInfo() if err != nil { t.Errorf("geting ppid error %v", err) } empty := MemoryInfoStat{} if v == nil || *v == empty { t.Errorf("could not get memory info %v", v) } } func Test_Process_CmdLine(t *testing.T) { p := testGetProcess() v, err := p.Cmdline() if err != nil { t.Errorf("geting ppid error %v", err) } if !strings.Contains(v, "process.test") { t.Errorf("invalid cmd line %v", v) } } func Test_Process_Ppid(t *testing.T) { p := testGetProcess() v, err := p.Ppid() if err != nil { t.Errorf("geting ppid error %v", err) } if v == 0 { t.Errorf("return value is 0 %v", v) } } func Test_Process_Status(t *testing.T) { p := testGetProcess() v, err := p.Status() if err != nil { t.Errorf("geting ppid error %v", err) } if !strings.HasPrefix(v, "S") && v != "running" && v != "sleeping" { t.Errorf("could not get state %v", v) } } func Test_Process_Terminal(t *testing.T) { p := testGetProcess() _, err := p.Terminal() if err != nil { if err == common.NotImplementedError { t.SkipNow() } t.Errorf("geting terminal error %v", err) } /* if v == "" { t.Errorf("could not get terminal %v", v) } */ } func Test_Process_IOCounters(t *testing.T) { p := testGetProcess() v, err := p.IOCounters() if err != nil { if err == common.NotImplementedError { t.SkipNow() } t.Errorf("geting iocounter error %v", err) return } empty := &IOCountersStat{} if v == empty { t.Errorf("error %v", v) } } func Test_Process_NumCtx(t *testing.T) { p := testGetProcess() _, err := p.NumCtxSwitches() if err != nil { if err == common.NotImplementedError { t.SkipNow() } t.Errorf("geting numctx error %v", err) return } } func Test_Process_Nice(t *testing.T) { p := testGetProcess() n, err := p.Nice() if err != nil { t.Errorf("geting nice error %v", err) } if n != 0 && n != 20 && n != 8 { t.Errorf("invalid nice: %d", n) } } func Test_Process_NumThread(t *testing.T) { p := testGetProcess() n, err := p.NumThreads() if err != nil { if err == common.NotImplementedError { t.SkipNow() } t.Errorf("geting NumThread error %v", err) } if n < 0 { t.Errorf("invalid NumThread: %d", n) } } func Test_Process_Name(t *testing.T) { p := testGetProcess() n, err := p.Name() if err != nil { t.Errorf("geting name error %v", err) } if !strings.Contains(n, "process.test") { t.Errorf("invalid Exe %s", n) } } func Test_Process_Exe(t *testing.T) { p := testGetProcess() n, err := p.Exe() if err != nil { if err == common.NotImplementedError { t.SkipNow() } t.Errorf("geting Exe error %v", err) } if !strings.Contains(n, "process.test") { t.Errorf("invalid Exe %s", n) } } func Test_Process_CpuPercent(t *testing.T) { p := testGetProcess() percent, err := p.CPUPercent(0) if err != nil { if err == common.NotImplementedError { t.SkipNow() } t.Errorf("error %v", err) } duration := time.Duration(1000) * time.Microsecond time.Sleep(duration) percent, err = p.CPUPercent(0) if err != nil { t.Errorf("error %v", err) } numcpu := runtime.NumCPU() // if percent < 0.0 || percent > 100.0*float64(numcpu) { // TODO if percent < 0.0 { t.Fatalf("CPUPercent value is invalid: %f, %d", percent, numcpu) } } func Test_Process_CpuPercentLoop(t *testing.T) { p := testGetProcess() numcpu := runtime.NumCPU() for i := 0; i < 2; i++ { duration := time.Duration(100) * time.Microsecond percent, err := p.CPUPercent(duration) if err != nil { if err == common.NotImplementedError { t.SkipNow() } t.Errorf("error %v", err) } // if percent < 0.0 || percent > 100.0*float64(numcpu) { // TODO if percent < 0.0 { t.Fatalf("CPUPercent value is invalid: %f, %d", percent, numcpu) } } }