Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wrapper to execute shell commands #261

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ require (

require (
github.com/bufbuild/buf v1.15.1
github.com/evilmartians/lefthook v1.3.5
github.com/evilmartians/lefthook v1.3.6
github.com/go-resty/resty/v2 v2.7.0
github.com/go-swagger/go-swagger v0.30.4
github.com/goreleaser/nfpm/v2 v2.26.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/envoyproxy/protoc-gen-validate v0.3.0-java h1:bV5JGEB1ouEzZa0hgVDFFiClrUEuGWRaAc/3mxR2QK0=
github.com/envoyproxy/protoc-gen-validate v0.3.0-java/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/evilmartians/lefthook v1.3.5 h1:K+wkFYtf1+oNe/gguNEGly1jXlUmfYIMYaZ7A43QZjQ=
github.com/evilmartians/lefthook v1.3.5/go.mod h1:X4o0AX2Jh9SVFyxyDMoJb6p8LmzzqGGfNkrcg1FiDt8=
github.com/evilmartians/lefthook v1.3.6 h1:xeSQszyJ4eJ/LPbg3zyH7jg6sK01sS0tJEnXT9OOi50=
github.com/evilmartians/lefthook v1.3.6/go.mod h1:X4o0AX2Jh9SVFyxyDMoJb6p8LmzzqGGfNkrcg1FiDt8=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.14.1 h1:qfhVLaG5s+nCROl1zJsZRxFeYrHLqWroPOQ8BWiNb4w=
github.com/fatih/color v1.14.1/go.mod h1:2oHN61fhTpgcxD3TSWCgKDiH1+x4OiDVVGH8WlgGZGg=
Expand Down
22 changes: 20 additions & 2 deletions src/core/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ var (
_ Environment = &EnvironmentType{}
)

var currentShellCommander = execShellCommander

func (env *EnvironmentType) NewHostInfo(agentVersion string, tags *[]string, configDirs string, clearCache bool) *proto.HostInfo {
// temp cache measure
if env.host == nil || clearCache {
Expand Down Expand Up @@ -654,15 +656,31 @@ func processorCache(item cpu.InfoStat) map[string]string {
return cache
}

type IExecShellCommander interface {
achawla2012 marked this conversation as resolved.
Show resolved Hide resolved
Output() ([]byte, error)
achawla2012 marked this conversation as resolved.
Show resolved Hide resolved
}

type execShellCommand struct {
*exec.Cmd
}

func execShellCommander(name string, arg ...string) IExecShellCommander {
execCmd := exec.Command(name, arg...)
return execShellCommand{Cmd: execCmd}
}

func getProcessorCacheInfo(cpuInfo cpuid.CPUInfo) map[string]string {
cache := getDefaultProcessorCacheInfo(cpuInfo)
return getCacheInfo(cache)
}

out, err := exec.Command("lscpu").Output()
func getCacheInfo(cache map[string]string) map[string]string {
cmd := currentShellCommander("lscpu")
out, err := cmd.Output()
if err != nil {
log.Warnf("Install lscpu on host to get processor info: %v", err)
return cache
}

return parseLscpu(string(out), cache)
}

Expand Down
62 changes: 51 additions & 11 deletions src/core/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package core

import (
"errors"
"io/ioutil"
"os"
"strings"
Expand Down Expand Up @@ -469,17 +470,55 @@ func TestProcessors(t *testing.T) {
assert.Equal(t, "arm64", processorInfo[0].GetArchitecture())
}

func TestParseLscpu(t *testing.T) {
type mockShellCommand struct {
OutputFunc func() ([]byte, error)
}

func (msc mockShellCommand) Output() ([]byte, error) {
return msc.OutputFunc()
}

type testExecShellCommander func(name string, arg ...string) IExecShellCommander

func mockExecShellCommander(output string, err error) testExecShellCommander {
return func(name string, arg ...string) IExecShellCommander {
outputFunc := func() ([]byte, error) {
return []byte(output), err
}
return mockShellCommand{
OutputFunc: outputFunc,
}
}
}

func TestGetCacheInfo(t *testing.T) {
tempShellCommander := currentShellCommander
defer func() { currentShellCommander = tempShellCommander }()
tests := []struct {
name string
defaultCacheInfo map[string]string
cpuInfoCache proto.CpuInfo
lscpuContent string
expect map[string]string
name string
mockExecShellCommander testExecShellCommander
defaultCacheInfo map[string]string
expect map[string]string
}{
{
name: "os-release present",
lscpuContent: lscpuInfo1,
name: "lscpu error",
mockExecShellCommander: mockExecShellCommander("", errors.New("Error executing lscpu")),
defaultCacheInfo: map[string]string{
"L1d": "64 KiB",
"L1i": "96 KiB",
"L2": "2 MiB",
"L3": "1 MiB",
},
expect: map[string]string{
"L1d": "64 KiB",
"L1i": "96 KiB",
"L2": "2 MiB",
"L3": "1 MiB",
},
},
{
name: "default cache info absent",
mockExecShellCommander: mockExecShellCommander(lscpuInfo1, nil),
defaultCacheInfo: map[string]string{
"L1d": "-1",
"L1i": "-1",
Expand All @@ -494,8 +533,8 @@ func TestParseLscpu(t *testing.T) {
},
},
{
name: "os-release present with quote",
lscpuContent: lscpuInfo2,
name: "os-release present with quote",
mockExecShellCommander: mockExecShellCommander(lscpuInfo2, nil),
defaultCacheInfo: map[string]string{
"L1d": "-1",
"L1i": "-1",
Expand All @@ -512,7 +551,8 @@ func TestParseLscpu(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := parseLscpu(tt.lscpuContent, tt.defaultCacheInfo)
currentShellCommander = tt.mockExecShellCommander
actual := getCacheInfo(tt.defaultCacheInfo)
assert.Equal(t, tt.expect, actual)
})
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ github.com/emirpasic/gods/utils
# github.com/envoyproxy/protoc-gen-validate v0.3.0-java
## explicit
github.com/envoyproxy/protoc-gen-validate/validate
# github.com/evilmartians/lefthook v1.3.5
# github.com/evilmartians/lefthook v1.3.6
## explicit; go 1.19
github.com/evilmartians/lefthook
github.com/evilmartians/lefthook/cmd
Expand Down