fleet/server/mdm/maintainedapps/testing_utils.go
Ian Littman 2f25580c3a
Only allow FLEET_DEV_* env vars when --dev is passed, allow overriding configs one at a time in dev (#38652)
Resolves #38484. This includes a CI job change to make sure we don't
introduce any more env vars that don't get proxied (and thus turned off
outside `--dev`).

# Checklist for submitter

If some of the following don't apply, delete the relevant line.

- [x] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files)
for more information.

- [x] Input data is properly validated, `SELECT *` is avoided, SQL
injection is prevented (using placeholders for values in statements)

## Testing

- [x] Added/updated automated tests

Manual QA touched hot paths, but did _not_ manually test every
FLEET_DEV_* environment variable change.

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

* **New Features**
* Centralized dev-mode environment management for consistent FLEET_DEV_*
handling and test-friendly overrides.
* Dev-mode allows targeted overrides for certain dev-only configuration
when running with --dev.

* **Chores**
* Migrated environment access to the centralized dev-mode helper across
the codebase.
  * Added CI checks to enforce proper usage of FLEET_DEV_* variables.

* **Documentation**
  * Added guidance on dev-mode environment variable rules and overrides.

<sub>✏️ Tip: You can customize this high-level summary in your review
settings.</sub>
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Victor Lyuboslavsky <2685025+getvictor@users.noreply.github.com>
2026-01-27 14:32:56 -06:00

137 lines
4.4 KiB
Go

package maintained_apps
import (
"context"
"encoding/json"
"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/go-kit/log"
"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, log.NewNopLogger())
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, log.NewNopLogger())
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, log.NewNopLogger())
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, log.NewNopLogger())
require.NoError(t, err)
modifiedApps, _, err = ds.ListAvailableFleetMaintainedApps(context.Background(), nil, fleet.ListOptions{})
require.ErrorIs(t, err, &fleet.NoMaintainedAppsInDatabaseError{})
require.Empty(t, modifiedApps)
}