fleet/server/mdm/maintainedapps/testing_utils.go
Victor Lyuboslavsky 4236363522
Next set of slog migration changes for MDM (#39981)
<!-- Add the related story/sub-task/bug number, like Resolves #123, or
remove if NA -->
**Related issue:** Resolves #38889

Incremental set of slog migration changes for MDM packages.

# Checklist for submitter
- [ ] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
  - already added in a previous PR

## 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

* **Refactor**
* Standardized logging to Go's structured slog across MDM (Apple,
Windows), DEP/ABM flows, maintained app sync, and related
tests—improving log consistency and contextual diagnostics without
changing user-facing behavior.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-02-19 11:40:52 -06:00

137 lines
4.4 KiB
Go

package maintained_apps
import (
"context"
"encoding/json"
"log/slog"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/fleetdm/fleet/v4/server/dev_mode"
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/stretchr/testify/require"
)
// SyncApps ingests the maintained apps from the apps list manifest
// to fill the library of maintained apps with valid data for tests.
// It returns the results of the ingestion as a slice of
// fleet.MaintainedApps.
func SyncApps(t *testing.T, ds fleet.Datastore) []fleet.MaintainedApp {
_, filename, _, _ := runtime.Caller(0)
base := filepath.Dir(filepath.Dir(filepath.Dir(filepath.Dir(filename))))
outputsDir := filepath.Join(base, "ee/maintained-apps/outputs")
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := os.ReadFile(filepath.Join(outputsDir, r.URL.Path))
if err != nil {
if os.IsNotExist(err) {
w.WriteHeader(http.StatusNotFound)
return
}
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
return
}
_, _ = w.Write(b)
}))
defer srv.Close()
// not using t.Setenv because we want the env var to be unset on return of
// this call
dev_mode.SetOverride("FLEET_DEV_MAINTAINED_APPS_BASE_URL", srv.URL)
defer dev_mode.ClearOverride("FLEET_DEV_MAINTAINED_APPS_BASE_URL")
err := Refresh(context.Background(), ds, slog.New(slog.DiscardHandler))
require.NoError(t, err)
apps, _, err := ds.ListAvailableFleetMaintainedApps(context.Background(), nil, fleet.ListOptions{
OrderKey: "slug",
})
require.NoError(t, err)
return apps
}
// ExpectedAppSlugs returns the list of app slugs (unique identifier) that are
// expected to be in the maintained apps library after ingestion. The slugs are
// taken from the apps.json list.
func ExpectedAppSlugs(t *testing.T) []string {
_, filename, _, _ := runtime.Caller(0)
base := filepath.Dir(filepath.Dir(filepath.Dir(filepath.Dir(filename))))
outputsDir := filepath.Join(base, "ee/maintained-apps/outputs")
b, err := os.ReadFile(filepath.Join(outputsDir, "apps.json"))
require.NoError(t, err)
var appsList AppsList
err = json.Unmarshal(b, &appsList)
require.NoError(t, err)
slugs := make([]string, len(appsList.Apps))
for i, app := range appsList.Apps {
slugs[i] = app.Slug
}
return slugs
}
func SyncAndRemoveApps(t *testing.T, ds fleet.Datastore) {
_, filename, _, _ := runtime.Caller(0)
base := filepath.Dir(filepath.Dir(filepath.Dir(filepath.Dir(filename))))
outputsDir := filepath.Join(base, "ee/maintained-apps/outputs")
b, err := os.ReadFile(filepath.Join(outputsDir, "apps.json"))
require.NoError(t, err)
var appsFile AppsList
require.NoError(t, json.Unmarshal(b, &appsFile))
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
data, err := json.Marshal(&appsFile)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
return
}
_, _ = w.Write(data)
}))
defer srv.Close()
// not using t.Setenv because we want the env var to be unset on return of
// this call
dev_mode.SetOverride("FLEET_DEV_MAINTAINED_APPS_BASE_URL", srv.URL)
defer dev_mode.ClearOverride("FLEET_DEV_MAINTAINED_APPS_BASE_URL")
err = Refresh(context.Background(), ds, slog.New(slog.DiscardHandler))
require.NoError(t, err)
originalApps, _, err := ds.ListAvailableFleetMaintainedApps(context.Background(), nil, fleet.ListOptions{})
require.NoError(t, err)
require.Equal(t, len(appsFile.Apps), len(originalApps))
// Modify the apps list to simulate removing an app from upstream
removedApp := appsFile.Apps[0]
appsFile.Apps = appsFile.Apps[1:]
err = Refresh(context.Background(), ds, slog.New(slog.DiscardHandler))
require.NoError(t, err)
modifiedApps, _, err := ds.ListAvailableFleetMaintainedApps(context.Background(), nil, fleet.ListOptions{})
require.NoError(t, err)
require.Equal(t, len(appsFile.Apps), len(modifiedApps))
require.Equal(t, len(originalApps)-1, len(modifiedApps))
for _, a := range modifiedApps {
require.NotEqual(t, removedApp.Slug, a.Slug)
}
// remove all apps from upstream.
appsFile.Apps = []appListing{}
err = Refresh(context.Background(), ds, slog.New(slog.DiscardHandler))
require.NoError(t, err)
modifiedApps, _, err = ds.ListAvailableFleetMaintainedApps(context.Background(), nil, fleet.ListOptions{})
require.ErrorIs(t, err, &fleet.NoMaintainedAppsInDatabaseError{})
require.Empty(t, modifiedApps)
}