2023-11-02 02:11:35 +00:00
|
|
|
//go:build !windows
|
|
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
|
|
// based on github.com/kolide/launcher/pkg/osquery/tables
|
|
|
|
|
package tablehelpers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2024-06-27 17:26:20 +00:00
|
|
|
"github.com/rs/zerolog"
|
2023-11-02 02:11:35 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestExec(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
2024-06-27 17:26:20 +00:00
|
|
|
tests := []struct {
|
2023-11-02 02:11:35 +00:00
|
|
|
name string
|
|
|
|
|
timeout int
|
|
|
|
|
bins []string
|
|
|
|
|
args []string
|
|
|
|
|
err bool
|
|
|
|
|
output string
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "timeout",
|
|
|
|
|
timeout: 1,
|
|
|
|
|
bins: []string{"/bin/sleep", "/usr/bin/sleep"},
|
|
|
|
|
args: []string{"30"},
|
|
|
|
|
err: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "no binaries",
|
|
|
|
|
bins: []string{"/hello/world", "/hello/friends"},
|
|
|
|
|
err: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "false",
|
|
|
|
|
bins: []string{"/bin/false", "/usr/bin/false"},
|
|
|
|
|
err: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "eventually finds binary",
|
|
|
|
|
bins: []string{"/hello/world", "/bin/true", "/usr/bin/true"},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "output",
|
|
|
|
|
bins: []string{"/bin/echo"},
|
|
|
|
|
args: []string{"hello"},
|
|
|
|
|
output: "hello\n",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx := context.Background()
|
2024-06-27 17:26:20 +00:00
|
|
|
logger := zerolog.Nop()
|
2023-11-02 02:11:35 +00:00
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
tt := tt
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
if tt.timeout == 0 {
|
|
|
|
|
tt.timeout = 30
|
|
|
|
|
}
|
|
|
|
|
output, err := Exec(ctx, logger, tt.timeout, tt.bins, tt.args, false)
|
|
|
|
|
if tt.err {
|
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
assert.Empty(t, output)
|
|
|
|
|
} else {
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.Equal(t, []byte(tt.output), output)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|