fleet/cmd/maintained-apps/validate/app_commander_test.go
Victor Lyuboslavsky 44c6aee5c7
Converted osquery_utils to slog (#39883)
<!-- Add the related story/sub-task/bug number, like Resolves #123, or
remove if NA -->
**Related issue:** Resolves #38889 

Plan was to convert `osquery_utils` package to slog. Picked up some
additional code that was related.

# Checklist for submitter

- [ ] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
  - Already have changes

## Testing

- [x] Added/updated automated tests
- [x] QA'd all new/changed functionality manually


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

# Release Notes

## Refactor
* Updated internal logging infrastructure to use improved system-level
logging utilities

## Tests
* Updated test suite to align with internal logging changes

---

**Note:** This release contains internal infrastructure improvements
with no user-facing changes or new features.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-02-16 15:43:59 -06:00

117 lines
3.4 KiB
Go

//go:build darwin || windows
package main
import (
"errors"
"log/slog"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
func TestExpectToChangeFileSystem(t *testing.T) {
var ac AppCommander
testCases := []struct {
name string
before func()
testFunc func(*testing.T)
}{
{
name: "no changes",
before: func() {},
testFunc: func(t *testing.T) {
appPath, changerError, listError := ac.expectToChangeFileSystem(t.Context(),
func() error {
return nil
},
)
require.NoError(t, changerError)
require.NoError(t, listError)
require.Empty(t, appPath, "Expected no")
},
},
{
name: "item added",
before: func() {},
testFunc: func(t *testing.T) {
appPath, changerError, listError := ac.expectToChangeFileSystem(t.Context(),
func() error {
err := os.Mkdir(filepath.Join(ac.cfg.installationSearchDirectory, "app1"), 0o755)
if err != nil {
t.Fatalf("Failed to create directory: %v, test cannot properly run", err)
}
return nil
},
)
require.NoError(t, changerError)
require.NoError(t, listError)
expectedPath := filepath.Join(ac.cfg.installationSearchDirectory, "app1")
require.Equal(t, expectedPath, appPath, "Expected appPath to return path to new item")
},
},
{
name: "item removed",
before: func() {
err := os.Mkdir(filepath.Join(ac.cfg.installationSearchDirectory, "app1"), 0o755)
if err != nil {
t.Fatalf("Failed to create directory: %v, test cannot properly run", err)
}
err = os.Mkdir(filepath.Join(ac.cfg.installationSearchDirectory, "app2"), 0o755)
if err != nil {
t.Fatalf("Failed to create directory: %v, test cannot properly run", err)
}
},
testFunc: func(t *testing.T) {
appPath, changerError, listError := ac.expectToChangeFileSystem(t.Context(),
func() error {
err := os.Remove(filepath.Join(ac.cfg.installationSearchDirectory, "app2"))
if err != nil {
t.Fatalf("Failed to remove directory: %v, test cannot properly run", err)
}
return nil
},
)
require.NoError(t, changerError)
require.NoError(t, listError)
expectedPath := filepath.Join(ac.cfg.installationSearchDirectory, "app2")
require.Equal(t, expectedPath, appPath, "Expected appPath to return path to removed item")
},
},
{
name: "error inside change function",
before: func() {},
testFunc: func(t *testing.T) {
appPath, changerError, listError := ac.expectToChangeFileSystem(t.Context(),
func() error {
return errors.New("simulated error in change function")
},
)
require.Error(t, changerError, "Expected an error from the change function")
require.NoError(t, listError)
require.Empty(t, appPath, "Expected no appPath due to error in change function")
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// installationSearchDirectory is the variable that expectToChangeFileSystem uses
installationSearchDirectory, err := os.MkdirTemp("", "TestExpectToChangeFileSystem-")
require.NoError(t, err)
defer os.RemoveAll(installationSearchDirectory)
cfg := &Config{
logger: slog.New(slog.DiscardHandler),
installationSearchDirectory: installationSearchDirectory,
}
ac = AppCommander{cfg: cfg, appLogger: slog.New(slog.DiscardHandler)}
tc.before()
tc.testFunc(t)
})
}
}