fleet/orbit/pkg/table/firefox_preferences/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

80 lines
2.1 KiB
Go

// based on github.com/kolide/launcher/pkg/osquery/tables
package firefox_preferences
import (
"context"
"encoding/json"
"os"
"path"
"testing"
"github.com/fleetdm/fleet/v4/orbit/pkg/table/tablehelpers"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_generate(t *testing.T) {
t.Parallel()
tests := []struct {
name string
filePaths []string
expectedResultsFilePath string
query string
}{
{
name: "no path",
},
{
name: "single path",
filePaths: []string{path.Join("testdata", "prefs.js")},
expectedResultsFilePath: "testdata/output.single_path.json",
},
{
name: "single path with query",
filePaths: []string{path.Join("testdata", "prefs.js")},
expectedResultsFilePath: "testdata/output.single_path_with_query.json",
query: "app.normandy.first_run",
},
{
name: "multiple paths",
filePaths: []string{path.Join("testdata", "prefs.js"), path.Join("testdata", "prefs2.js")},
expectedResultsFilePath: "testdata/output.multiple_paths.json",
},
{
name: "file with bad data",
filePaths: []string{path.Join("testdata", "prefs3.js")},
expectedResultsFilePath: "testdata/output.file_with_bad_data.json",
},
}
table := Table{logger: zerolog.Nop()}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
constraints := make(map[string][]string)
constraints["path"] = tt.filePaths
if tt.query != "" {
constraints["query"] = append(constraints["query"], tt.query)
}
got, _ := table.generate(context.Background(), tablehelpers.MockQueryContext(constraints))
var want []map[string]string
if tt.expectedResultsFilePath != "" {
wantBytes, err := os.ReadFile(tt.expectedResultsFilePath)
require.NoError(t, err)
err = json.Unmarshal(wantBytes, &want)
require.NoError(t, err)
}
assert.ElementsMatch(t, want, got)
})
}
}