From 29304ebf733a1c0200033a7e6990aa6343da78a6 Mon Sep 17 00:00:00 2001 From: Lomanic Date: Thu, 5 Nov 2020 19:32:02 +0100 Subject: [PATCH 1/8] [ci] Implement automatic tests on GH Actions on linux, windows and darwin Based off https://github.com/mvdan/github-actions-golang/blob/3710bfcc3e71fc692118d0d5a42df57481095db7/.github/workflows/test.yml --- .github/workflows/test.yml | 91 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..48c05e2 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,91 @@ +on: [push, pull_request] +name: Test +jobs: + test_v3_module: + strategy: + matrix: + go-version: [1.12.x, 1.14.x, 1.15.x] + os: [ubuntu-18.04, ubuntu-16.04, windows-2019, windows-2016, macOS-10.14, macos-11.0] + runs-on: ${{ matrix.os }} + steps: + - name: Install Go + uses: actions/setup-go@v2 + with: + go-version: ${{ matrix.go-version }} + - name: Checkout code + uses: actions/checkout@v2 + - name: Test + run: | + cd ./v3/ + go test ./... + + test_v3_gopath: + env: + GOPATH: ${{ github.workspace }} + GO111MODULE: off + strategy: + matrix: + go-version: [1.13.x, 1.14.x, 1.15.x] + os: [ubuntu-18.04, ubuntu-16.04, windows-2019, windows-2016, macOS-10.14, macos-11.0] + runs-on: ${{ matrix.os }} + steps: + - name: Install Go + uses: actions/setup-go@v2 + with: + go-version: ${{ matrix.go-version }} + - name: Checkout code + uses: actions/checkout@v2 + with: + path: ${{ github.workspace }}/src/github.com/shirou/gopsutil + - name: Get dependencies + run: | + go get -t github.com/shirou/gopsutil/v3/... + - name: Test + run: | + go test github.com/shirou/gopsutil/v3/... + + test_v2_gopath: + env: + GOPATH: ${{ github.workspace }} + GO111MODULE: off + strategy: + matrix: + go-version: [1.12.x, 1.14.x, 1.15.x] + os: [ubuntu-18.04, ubuntu-16.04, windows-2019, windows-2016, macOS-10.14, macos-11.0] + runs-on: ${{ matrix.os }} + steps: + - name: Install Go + uses: actions/setup-go@v2 + with: + go-version: ${{ matrix.go-version }} + - name: Checkout code + uses: actions/checkout@v2 + with: + path: ${{ github.workspace }}/src/github.com/shirou/gopsutil + - name: Get dependencies + if: runner.os == 'Windows' + run: | + go get -d -u github.com/golang/dep + cd $Env:GOPATH/src/github.com/golang/dep + git checkout v0.5.4 + go install -ldflags="-X main.version=v0.5.4" ./cmd/dep + echo "$Env:GOPATH/bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append + cd $Env:GOPATH/src/github.com/shirou/gopsutil + dep ensure + # exclude v3 from being run with ./... + try { rm -ErrorAction:Stop -Recurse -Force $Env:GOPATH/src/github.com/shirou/gopsutil/v3 } catch [System.Management.Automation.ItemNotFoundException] {} + - name: Get dependencies + if: runner.os != 'Windows' + run: | + if ! command -v dep &>/dev/null; then + mkdir -p $GOPATH/bin + curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh + echo "PATH=$GOPATH/bin:$PATH" >> $GITHUB_PATH + fi + cd $GOPATH/src/github.com/shirou/gopsutil + dep ensure + # exclude v3 from being run with ./... + rm -rf $GOPATH/src/github.com/shirou/gopsutil/v3 + - name: Test + run: | + go test github.com/shirou/gopsutil/... From 907e475a45fb2df85a0cd85d1d7b80fcf46280f7 Mon Sep 17 00:00:00 2001 From: Lomanic Date: Sat, 7 Nov 2020 00:54:04 +0100 Subject: [PATCH 2/8] [v3][process] Fix Test_Process_Status test with new Status() API --- v3/process/process_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/v3/process/process_test.go b/v3/process/process_test.go index a34f0f0..a894164 100644 --- a/v3/process/process_test.go +++ b/v3/process/process_test.go @@ -198,8 +198,8 @@ func Test_Process_Status(t *testing.T) { if len(v) == 0 { t.Errorf("could not get state") } - if v[0] != "R" && v[0] != "S" { - t.Errorf("get wrong state, %v", v) + if v[0] != Running && v[0] != Sleep { + t.Errorf("got wrong state, %v", v) } } From 7f0ba433986d439eb276786b36aa738ee570c5aa Mon Sep 17 00:00:00 2001 From: Lomanic Date: Sat, 7 Nov 2020 23:38:56 +0100 Subject: [PATCH 3/8] [net][darwin][freebsd] Fix tests with func not returning common.ErrNotImplementedError --- net/net_darwin.go | 4 ++-- net/net_freebsd.go | 7 +++---- v3/net/net_darwin.go | 4 ++-- v3/net/net_freebsd.go | 7 +++---- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/net/net_darwin.go b/net/net_darwin.go index 1daed86..8e6e7f9 100644 --- a/net/net_darwin.go +++ b/net/net_darwin.go @@ -269,7 +269,7 @@ func FilterCounters() ([]FilterStat, error) { } func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) { - return nil, errors.New("NetFilterCounters not implemented for darwin") + return nil, common.ErrNotImplementedError } func ConntrackStats(percpu bool) ([]ConntrackStat, error) { @@ -289,5 +289,5 @@ func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) { } func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) { - return nil, errors.New("NetProtoCounters not implemented for darwin") + return nil, common.ErrNotImplementedError } diff --git a/net/net_freebsd.go b/net/net_freebsd.go index 2284d98..1204f59 100644 --- a/net/net_freebsd.go +++ b/net/net_freebsd.go @@ -4,7 +4,6 @@ package net import ( "context" - "errors" "os/exec" "strconv" "strings" @@ -109,7 +108,7 @@ func FilterCounters() ([]FilterStat, error) { } func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) { - return nil, errors.New("NetFilterCounters not implemented for freebsd") + return nil, common.ErrNotImplementedError } func ConntrackStats(percpu bool) ([]ConntrackStat, error) { @@ -117,7 +116,7 @@ func ConntrackStats(percpu bool) ([]ConntrackStat, error) { } func ConntrackStatsWithContext(ctx context.Context, percpu bool) ([]ConntrackStat, error) { - return nil, errors.New("ConntrackStats not implemented for freebsd") + return nil, common.ErrNotImplementedError } // NetProtoCounters returns network statistics for the entire system @@ -129,5 +128,5 @@ func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) { } func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) { - return nil, errors.New("NetProtoCounters not implemented for freebsd") + return nil, common.ErrNotImplementedError } diff --git a/v3/net/net_darwin.go b/v3/net/net_darwin.go index 109f11e..2327031 100644 --- a/v3/net/net_darwin.go +++ b/v3/net/net_darwin.go @@ -269,7 +269,7 @@ func FilterCounters() ([]FilterStat, error) { } func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) { - return nil, errors.New("NetFilterCounters not implemented for darwin") + return nil, common.ErrNotImplementedError } func ConntrackStats(percpu bool) ([]ConntrackStat, error) { @@ -289,5 +289,5 @@ func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) { } func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) { - return nil, errors.New("NetProtoCounters not implemented for darwin") + return nil, common.ErrNotImplementedError } diff --git a/v3/net/net_freebsd.go b/v3/net/net_freebsd.go index 0883590..739f8cc 100644 --- a/v3/net/net_freebsd.go +++ b/v3/net/net_freebsd.go @@ -4,7 +4,6 @@ package net import ( "context" - "errors" "os/exec" "strconv" "strings" @@ -109,7 +108,7 @@ func FilterCounters() ([]FilterStat, error) { } func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) { - return nil, errors.New("NetFilterCounters not implemented for freebsd") + return nil, common.ErrNotImplementedError } func ConntrackStats(percpu bool) ([]ConntrackStat, error) { @@ -117,7 +116,7 @@ func ConntrackStats(percpu bool) ([]ConntrackStat, error) { } func ConntrackStatsWithContext(ctx context.Context, percpu bool) ([]ConntrackStat, error) { - return nil, errors.New("ConntrackStats not implemented for freebsd") + return nil, common.ErrNotImplementedError } // NetProtoCounters returns network statistics for the entire system @@ -129,5 +128,5 @@ func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) { } func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) { - return nil, errors.New("NetProtoCounters not implemented for freebsd") + return nil, common.ErrNotImplementedError } From 64e16cc8c397e2825f01baf37ac5348d9312eb62 Mon Sep 17 00:00:00 2001 From: Lomanic Date: Sun, 8 Nov 2020 00:09:48 +0100 Subject: [PATCH 4/8] [net][darwin] Fix tests with mismatched types --- net/net_darwin_test.go | 12 ++++++------ v3/net/net_darwin_test.go | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/net/net_darwin_test.go b/net/net_darwin_test.go index f5e5a47..0680d08 100644 --- a/net/net_darwin_test.go +++ b/net/net_darwin_test.go @@ -40,12 +40,12 @@ func TestParseNetstatLineHeader(t *testing.T) { func assertLoopbackStat(t *testing.T, err error, stat *IOCountersStat) { assert.NoError(t, err) - assert.Equal(t, 869107, stat.PacketsRecv) - assert.Equal(t, 0, stat.Errin) - assert.Equal(t, 169411755, stat.BytesRecv) - assert.Equal(t, 869108, stat.PacketsSent) - assert.Equal(t, 1, stat.Errout) - assert.Equal(t, 169411756, stat.BytesSent) + assert.Equal(t, uint64(869107), stat.PacketsRecv) + assert.Equal(t, uint64(0), stat.Errin) + assert.Equal(t, uint64(169411755), stat.BytesRecv) + assert.Equal(t, uint64(869108), stat.PacketsSent) + assert.Equal(t, uint64(1), stat.Errout) + assert.Equal(t, uint64(169411756), stat.BytesSent) } func TestParseNetstatLineLink(t *testing.T) { diff --git a/v3/net/net_darwin_test.go b/v3/net/net_darwin_test.go index f5e5a47..0680d08 100644 --- a/v3/net/net_darwin_test.go +++ b/v3/net/net_darwin_test.go @@ -40,12 +40,12 @@ func TestParseNetstatLineHeader(t *testing.T) { func assertLoopbackStat(t *testing.T, err error, stat *IOCountersStat) { assert.NoError(t, err) - assert.Equal(t, 869107, stat.PacketsRecv) - assert.Equal(t, 0, stat.Errin) - assert.Equal(t, 169411755, stat.BytesRecv) - assert.Equal(t, 869108, stat.PacketsSent) - assert.Equal(t, 1, stat.Errout) - assert.Equal(t, 169411756, stat.BytesSent) + assert.Equal(t, uint64(869107), stat.PacketsRecv) + assert.Equal(t, uint64(0), stat.Errin) + assert.Equal(t, uint64(169411755), stat.BytesRecv) + assert.Equal(t, uint64(869108), stat.PacketsSent) + assert.Equal(t, uint64(1), stat.Errout) + assert.Equal(t, uint64(169411756), stat.BytesSent) } func TestParseNetstatLineLink(t *testing.T) { From 148a662b060a2e2aa2626ae7f896a8855c3786e5 Mon Sep 17 00:00:00 2001 From: Lomanic Date: Sun, 8 Nov 2020 01:29:50 +0100 Subject: [PATCH 5/8] [process][darwin] Remove Groups() as it's non-functional --- process/process_darwin.go | 19 ++++++++++--------- v3/process/process_darwin.go | 19 ++++++++++--------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/process/process_darwin.go b/process/process_darwin.go index 324801c..5d64840 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -212,17 +212,18 @@ func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) { } func (p *Process) GroupsWithContext(ctx context.Context) ([]int32, error) { - k, err := p.getKProc() - if err != nil { - return nil, err - } + return nil, common.ErrNotImplementedError + // k, err := p.getKProc() + // if err != nil { + // return nil, err + // } - groups := make([]int32, k.Eproc.Ucred.Ngroups) - for i := int16(0); i < k.Eproc.Ucred.Ngroups; i++ { - groups[i] = int32(k.Eproc.Ucred.Groups[i]) - } + // groups := make([]int32, k.Eproc.Ucred.Ngroups) + // for i := int16(0); i < k.Eproc.Ucred.Ngroups; i++ { + // groups[i] = int32(k.Eproc.Ucred.Groups[i]) + // } - return groups, nil + // return groups, nil } func (p *Process) TerminalWithContext(ctx context.Context) (string, error) { diff --git a/v3/process/process_darwin.go b/v3/process/process_darwin.go index 644d5bc..3f93fb3 100644 --- a/v3/process/process_darwin.go +++ b/v3/process/process_darwin.go @@ -212,17 +212,18 @@ func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) { } func (p *Process) GroupsWithContext(ctx context.Context) ([]int32, error) { - k, err := p.getKProc() - if err != nil { - return nil, err - } + return nil, common.ErrNotImplementedError + // k, err := p.getKProc() + // if err != nil { + // return nil, err + // } - groups := make([]int32, k.Eproc.Ucred.Ngroups) - for i := int16(0); i < k.Eproc.Ucred.Ngroups; i++ { - groups[i] = int32(k.Eproc.Ucred.Groups[i]) - } + // groups := make([]int32, k.Eproc.Ucred.Ngroups) + // for i := int16(0); i < k.Eproc.Ucred.Ngroups; i++ { + // groups[i] = int32(k.Eproc.Ucred.Groups[i]) + // } - return groups, nil + // return groups, nil } func (p *Process) TerminalWithContext(ctx context.Context) (string, error) { From 5641beec4cb82330e4de765b7198a39d1e5cb629 Mon Sep 17 00:00:00 2001 From: Lomanic Date: Sun, 8 Nov 2020 01:58:48 +0100 Subject: [PATCH 6/8] [process][darwin] Fix Parent() when lsof returns warnings Same as #867, the error being: error strconv.Atoi: parsing " Output information may be incomplete.": invalid syntax --- process/process_darwin.go | 18 ++++++++---------- v3/process/process_darwin.go | 18 ++++++++---------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/process/process_darwin.go b/process/process_darwin.go index 5d64840..8b8b58a 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -146,20 +146,18 @@ func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) { } func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - rr, err := common.CallLsofWithContext(ctx, invoke, p.Pid, "-FR") + out, err := common.CallLsofWithContext(ctx, invoke, p.Pid, "-FR") if err != nil { return nil, err } - for _, r := range rr { - if strings.HasPrefix(r, "p") { // skip if process - continue - } - l := string(r) - v, err := strconv.Atoi(strings.Replace(l, "R", "", 1)) - if err != nil { - return nil, err + for _, line := range out { + if len(line) >= 1 && line[0] == 'R' { + v, err := strconv.Atoi(line[1:]) + if err != nil { + return nil, err + } + return NewProcessWithContext(ctx, int32(v)) } - return NewProcessWithContext(ctx, int32(v)) } return nil, fmt.Errorf("could not find parent line") } diff --git a/v3/process/process_darwin.go b/v3/process/process_darwin.go index 3f93fb3..a7c4115 100644 --- a/v3/process/process_darwin.go +++ b/v3/process/process_darwin.go @@ -146,20 +146,18 @@ func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) { } func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - rr, err := common.CallLsofWithContext(ctx, invoke, p.Pid, "-FR") + out, err := common.CallLsofWithContext(ctx, invoke, p.Pid, "-FR") if err != nil { return nil, err } - for _, r := range rr { - if strings.HasPrefix(r, "p") { // skip if process - continue - } - l := string(r) - v, err := strconv.Atoi(strings.Replace(l, "R", "", 1)) - if err != nil { - return nil, err + for _, line := range out { + if len(line) >= 1 && line[0] == 'R' { + v, err := strconv.Atoi(line[1:]) + if err != nil { + return nil, err + } + return NewProcessWithContext(ctx, int32(v)) } - return NewProcessWithContext(ctx, int32(v)) } return nil, fmt.Errorf("could not find parent line") } From 995610e10907a2fe266e2f6a052d13faddfd286c Mon Sep 17 00:00:00 2001 From: Lomanic Date: Sun, 8 Nov 2020 02:14:30 +0100 Subject: [PATCH 7/8] [process][windows] Fix Nice() test expecting Unix values --- process/process_test.go | 2 +- v3/process/process_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/process/process_test.go b/process/process_test.go index 28518b8..97827c0 100644 --- a/process/process_test.go +++ b/process/process_test.go @@ -244,7 +244,7 @@ func Test_Process_Nice(t *testing.T) { if err != nil { t.Errorf("getting nice error %v", err) } - if n != 0 && n != 20 && n != 8 { + if runtime.GOOS != "windows" && n != 0 && n != 20 && n != 8 { t.Errorf("invalid nice: %d", n) } } diff --git a/v3/process/process_test.go b/v3/process/process_test.go index a894164..47045af 100644 --- a/v3/process/process_test.go +++ b/v3/process/process_test.go @@ -247,7 +247,7 @@ func Test_Process_Nice(t *testing.T) { if err != nil { t.Errorf("getting nice error %v", err) } - if n != 0 && n != 20 && n != 8 { + if runtime.GOOS != "windows" && n != 0 && n != 20 && n != 8 { t.Errorf("invalid nice: %d", n) } } From 318614d7ed39f29816423e057763169d2fabf21f Mon Sep 17 00:00:00 2001 From: Lomanic Date: Sun, 8 Nov 2020 17:26:07 +0100 Subject: [PATCH 8/8] [v3][process][darwin] Fix test Test_Pids_Fail missing testdata folder --- v3/process/testdata/darwin/ps-ax-opid_fail | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 v3/process/testdata/darwin/ps-ax-opid_fail diff --git a/v3/process/testdata/darwin/ps-ax-opid_fail b/v3/process/testdata/darwin/ps-ax-opid_fail new file mode 100644 index 0000000..fce59ef --- /dev/null +++ b/v3/process/testdata/darwin/ps-ax-opid_fail @@ -0,0 +1,10 @@ + PID + 245 + 247 + 248 + 249 + 254 + 262 + 264 + 265 + 267