fleet/orbit/pkg/table/tablehelpers/exec_test.go
Jahziel Villasana-Espinoza 87f4a28419
fix: use zerolog for orbit osquery table logging (#20028)
> Related issue: #19886 

# Checklist for submitter

If some of the following don't apply, delete the relevant line.

<!-- Note that API documentation changes are now addressed by the
product design team. -->

- [x] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://fleetdm.com/docs/contributing/committing-changes#changes-files)
for more information.
- [x] Added/updated tests
- [x] Manual QA for all new/changed functionality
  - For Orbit and Fleet Desktop changes:
- [x] Manual QA must be performed in the three main OSs, macOS, Windows
and Linux.
- [x] Auto-update manual QA, from released version of component to new
version (see [tools/tuf/test](../tools/tuf/test/README.md)).
2024-06-27 13:26:20 -04:00

76 lines
1.4 KiB
Go

//go:build !windows
// +build !windows
// based on github.com/kolide/launcher/pkg/osquery/tables
package tablehelpers
import (
"context"
"testing"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
)
func TestExec(t *testing.T) {
t.Parallel()
tests := []struct {
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()
logger := zerolog.Nop()
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)
}
})
}
}