fleet/orbit/pkg/table/crowdstrike/falconctl/table_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

88 lines
2.2 KiB
Go

// based on github.com/kolide/launcher/pkg/osquery/tables
package falconctl
import (
"bytes"
"context"
"strings"
"testing"
"github.com/fleetdm/fleet/v4/orbit/pkg/table/tablehelpers"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
)
// TestOptionRestrictions tests that the table only allows the options we expect.
func TestOptionRestrictions(t *testing.T) {
t.Parallel()
tests := []struct {
name string
options []string
expectedExecs int
expectedDisallows int
}{
{
name: "default",
expectedExecs: 1,
expectedDisallows: 0,
},
{
name: "allowed options as array",
options: []string{"--aid", "--aph"},
expectedExecs: 2,
expectedDisallows: 0,
},
{
name: "allowed options as string",
options: []string{"--aid --aph"},
expectedExecs: 1,
expectedDisallows: 0,
},
{
name: "disallowed option as array",
options: []string{"--not-allowed", "--definitely-not-allowed", "--aid", "--aph"},
expectedExecs: 2,
expectedDisallows: 2,
},
{
name: "disallowed option as string",
options: []string{"--aid --aph --not-allowed"},
expectedExecs: 0,
expectedDisallows: 1,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
var logBytes bytes.Buffer
testTable := &falconctlOptionsTable{
logger: zerolog.New(zerolog.ConsoleWriter{Out: &logBytes}),
execFunc: noopExec,
}
mockQC := tablehelpers.MockQueryContext(map[string][]string{
"options": tt.options,
})
_, err := testTable.generate(context.TODO(), mockQC)
require.NoError(t, err)
// test the number of times exec was called
require.Equal(t, tt.expectedExecs, strings.Count(logBytes.String(), "exec-in-test"))
// test the number of times we disallowed an option
require.Equal(t, tt.expectedDisallows, strings.Count(logBytes.String(), "requested option not allowed"))
})
}
}
func noopExec(_ context.Context, log zerolog.Logger, _ int, _ []string, args []string, _ bool) ([]byte, error) {
log.Info().Str("args", strings.Join(args, " ")).Msg("exec-in-test")
return []byte{}, nil
}