Skip to content

Commit

Permalink
Fixing bash execution to make use of bash
Browse files Browse the repository at this point in the history
  • Loading branch information
deven96 committed Mar 16, 2022
1 parent 4ba9d86 commit e07550f
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 70 deletions.
14 changes: 7 additions & 7 deletions driver/local.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package driver

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"runtime"
"strings"

log "github.com/sirupsen/logrus"
)
Expand All @@ -26,21 +24,25 @@ func (d *Local) ReadFile(path string) (string, error) {
return string(content), nil
}

// RunCommand : For simple commands without shell variables, pipes, e.t.c
// They can be passed directly. For complex commands e.g
// `echo something | awk $var`, turn into a file to be saved
// under ./shell/
func (d *Local) RunCommand(command string) (string, error) {
// FIXME: If command contains a shell variable $ or glob
// type pattern, it would not be executed, see
// https://pkg.go.dev/os/exec for more information
cmdArgs := strings.Fields(command)
var cmd *exec.Cmd
log.Debugf("Running command `%s` ", command)
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
cmd = exec.Command("bash", "-c", command)
cmd.Env = os.Environ()
if len(d.Vars) != 0 {
for _, v := range d.Vars {
cmd.Env = append(cmd.Env, v)
}
}
_ = cmd.Wait()
out, err := cmd.Output()
fmt.Printf("%s", string(out))
if err != nil {
return ``, err
}
Expand All @@ -58,8 +60,6 @@ func (d *Local) GetDetails() SystemDetails {
details.IsLinux = true
case "darwin":
details.IsDarwin = true
default:
details.IsLinux = true
}
details.Extra = runtime.GOARCH
d.Info = details
Expand Down
2 changes: 1 addition & 1 deletion driver/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
GET Request = "GET"
)

// Web : Driver for handling ssh executions
// Web : Driver for handling web executions
type Web struct {
fields
// URL e.g https://google.com
Expand Down
6 changes: 4 additions & 2 deletions inspector/loadavg.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func (i LoadAvgDarwin) driverExec() driver.Command {
}

func (i *LoadAvgDarwin) Parse(output string) {
output = strings.TrimSuffix(output, "}")
output = strings.TrimPrefix(output, "{")
i.Values = loadavgParseOutput(output)
}

Expand Down Expand Up @@ -108,8 +110,8 @@ func NewLoadAvg(driver *driver.Driver) Inspector {
}
} else if details.IsDarwin {
loadavg = &LoadAvgDarwin{
// Command: `sysctl -n vm.loadavg | awk '/./ { printf "%.2f %.2f %.2f ", $2, $3, $4 }'`,
Command: `top -l 1 | grep "Load Avg\:" | awk '{print $3, $4, $5}'`,
// Command: `sysctl -n vm.loadavg | awk '{ printf "%.2f %.2f %.2f ", $2, $3, $4 }'`,
Command: `top -l 1 | grep "Load Avg:" | awk '{print $3, $4, $5}'`,
}
}
loadavg.SetDriver(driver)
Expand Down
12 changes: 12 additions & 0 deletions inspector/meminfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,16 @@ func TestMemInfoOnLocal(t *testing.T) {
driver := NewLocalForTest()
d := NewMemInfo(&driver)
d.Execute()
iConcreteLinux, ok := d.(*MemInfoLinux)
if ok {
if iConcreteLinux.Values == nil {
t.Error("Values did not get set for MemInfoLinux")
}
}
iConcreteDarwin, ok := d.(*MemInfoDarwin)
if ok {
if iConcreteDarwin.Values == nil {
t.Error("Values did not get set for MemInfoDarwin")
}
}
}
158 changes: 98 additions & 60 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,95 +9,133 @@ import (
"github.com/bisohns/saido/inspector"
)

func NewWebForTest() driver.Driver {
return &driver.Web{
URL: "https://duckduckgo.com",
Method: driver.GET,
}
}

func NewSSHForTest() driver.Driver {
return &driver.SSH{
User: "dev",
Host: "127.0.0.1",
Port: 2222,
KeyFile: "/home/diretnan/.ssh/id_rsa",
KeyPass: "",
CheckKnownHosts: false,
}
}

func TestDFonSSH(t *testing.T) {
d := driver.NewSSHForTest()
i := inspector.NewDF()
output, err := d.RunCommand(i.String())
if err != nil {
t.Error(err)
d := NewSSHForTest()
i := inspector.NewDF(&d)
i.Execute()
iConcrete, ok := i.(*inspector.DF)
if ok {
fmt.Printf(`%#v`, iConcrete.Values)
}
i.Parse(output)
fmt.Printf(`%#v`, i.Values)
}

func TestMemInfoonSSH(t *testing.T) {
d := driver.NewSSHForTest()
i := inspector.NewMemInfo()
output, err := d.ReadFile(i.String())
if err != nil {
t.Error(err)
d := NewSSHForTest()
i := inspector.NewMemInfo(&d)
i.Execute()
iConcreteLinux, ok := i.(*inspector.MemInfoLinux)
if ok {
if iConcreteLinux.Values.MemTotal == 0 {
t.Error("showing percent used as 0")
}
fmt.Printf(`%#v`, iConcreteLinux.Values)
}
i.Parse(output)
if i.Values.MemTotal == 0 {
t.Error("showing percent used as 0")
iConcreteDarwin, ok := i.(*inspector.MemInfoDarwin)
if ok {
if iConcreteDarwin.Values.MemTotal == 0 {
t.Error("showing percent used as 0")
}
fmt.Printf(`%#v`, iConcreteDarwin.Values)
}
fmt.Printf(`%#v`, i.Values)
}

func TestResponseTimeonWeb(t *testing.T) {
d := driver.NewWebForTest()
i := inspector.NewResponseTime()
output, err := d.RunCommand(i.String())
if err != nil {
t.Error(err)
d := NewWebForTest()
i := inspector.NewResponseTime(&d)
i.Execute()
iConcrete, ok := i.(*inspector.ResponseTime)
if ok {
if iConcrete.Values.Seconds == 0 {
t.Error("showing response time as 0")
}
fmt.Printf(`%#v`, iConcrete.Values)
}
i.Parse(output)
if i.Values.Seconds == 0 {
t.Error("showing response time as 0")
}
fmt.Printf(`%#v`, i.Values)
}

func TestProcessonSSH(t *testing.T) {
d := driver.NewSSHForTest()
i := inspector.NewProcess()
output, err := d.RunCommand(i.String())
if err != nil {
t.Error(err)
d := NewSSHForTest()
i := inspector.NewProcess(&d)
i.Execute()
iConcreteUnix, ok := i.(*inspector.Process)
if ok {
if len(iConcreteUnix.Values) <= 2 {
t.Error("Less than two processes running")
}
}
i.Parse(output)
if len(i.Values) <= 2 {
t.Error(err)
iConcreteWin, ok := i.(*inspector.ProcessWin)
if ok {
if len(iConcreteWin.Values) <= 2 {
t.Error("Less than two processes running")
}
}
}

func TestCustomonSSH(t *testing.T) {
d := driver.NewSSHForTest()
d := NewSSHForTest()
// set vars
d.Vars = []string{"MONKEY=true"}
i := inspector.NewCustom(`echo $MONKEY`)
output, err := d.RunCommand(i.String())
if err != nil {
t.Error(err)
}
i.Parse(output)
if strings.TrimSpace(i.Values.Output) != "true" {
t.Errorf("%s", i.Values.Output)
dfConcrete, _ := d.(*driver.SSH)
dfConcrete.Vars = []string{"MONKEY=true"}
d = dfConcrete
i := inspector.NewCustom(`echo $MONKEY`, &d)
i.Execute()
iConcrete, ok := i.(*inspector.Custom)
if ok {
if strings.TrimSpace(iConcrete.Values.Output) != "true" {
t.Errorf("%s", iConcrete.Values.Output)
}
}
}

func TestLoadAvgonSSH(t *testing.T) {
d := driver.NewSSHForTest()
i := inspector.NewLoadAvg()
output, err := d.ReadFile(i.String())
if err != nil {
t.Error(err)
d := NewSSHForTest()
i := inspector.NewLoadAvg(&d)
i.Execute()
iConcreteLinux, ok := i.(*inspector.LoadAvg)
if ok {
if iConcreteLinux.Values.Load1M == 0 {
t.Errorf("%f", iConcreteLinux.Values.Load1M)
}
}
i.Parse(output)
if i.Values.Load1M == 0 {
t.Errorf("%f", i.Values.Load1M)
iConcreteDarwin, ok := i.(*inspector.LoadAvgDarwin)
if ok {
if iConcreteDarwin.Values.Load1M == 0 {
t.Errorf("%f", iConcreteDarwin.Values.Load1M)
}
}
}

func TestUptimeonSSH(t *testing.T) {
d := driver.NewSSHForTest()
i := inspector.NewUptime()
output, err := d.ReadFile(i.String())
if err != nil {
t.Error(err)
d := NewSSHForTest()
i := inspector.NewUptime(&d)
i.Execute()
iConcreteLinux, ok := i.(*inspector.UptimeLinux)
if ok {
if iConcreteLinux.Values.Up == 0 {
t.Errorf("%f", iConcreteLinux.Values.Up)
}
}
i.Parse(output)
if i.Values.Up == 0 {
t.Errorf("%f", i.Values.Up)
iConcreteDarwin, ok := i.(*inspector.UptimeDarwin)
if ok {
if iConcreteDarwin.Values.Up == 0 {
t.Errorf("%f", iConcreteDarwin.Values.Up)
}
}
}

0 comments on commit e07550f

Please sign in to comment.