fleet/cmd/maintained-apps/validate/app_commander_test.go
Konstantin Sykulev b1a392d672
FMA test automation (#31210)
For #29183

# Checklist for submitter

- [x] Added/updated automated tests
- [x] Manual QA for all new/changed functionality


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

## Summary by CodeRabbit

* **New Features**
* Introduced automated validation workflows for maintained applications
on both macOS and Windows, ensuring apps can be installed, verified, and
uninstalled as expected.
* Added new command-line tool to validate maintained apps, providing
detailed reporting on validation results.
* Enhanced detection and handling of pre-installed applications during
validation.
* Improved post-installation steps for macOS, including quarantine
removal and system refresh.

* **Chores**
* Added new continuous integration workflows to automate application
validation on pull requests for relevant files.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-31 15:23:36 -05:00

117 lines
3.3 KiB
Go

//go:build darwin || windows
package main
import (
"errors"
"os"
"path/filepath"
"testing"
kitlog "github.com/go-kit/log"
"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(
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(
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(
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(
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: kitlog.NewNopLogger(),
installationSearchDirectory: installationSearchDirectory,
}
ac = AppCommander{cfg: cfg, appLogger: kitlog.NewNopLogger()}
tc.before()
tc.testFunc(t)
})
}
}