Files
Lucas Manuel Rodriguez 393007ede1 First move of server/service to server/service/svctest to prevent testing code in Fleet's production binary (#45221)
**Related issue:** Resolves #45220 (one of many small PRs)

## Testing

- [x] QA'd all new/changed functionality manually. (Ran basic smoke
testing.)

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

* **Tests**
* Added a consolidated test utilities package to boot full test servers,
seed users, retrieve auth tokens, and provide common mocks for
integration/service tests.
* Updated integration and service tests to use the new helpers for more
consistent, reliable test setup and wiring.

* **Chores**
* Centralized and reorganized test configuration types and options, and
standardized test wiring for clearer, maintainable test setup.

[![Review Change
Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/fleetdm/fleet/pull/45221)
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-05-12 17:43:37 -03:00

88 lines
2.6 KiB
Go

//go:build !windows
// Windows is disabled because the TPM simulator requires CGO, which causes lint failures on Windows.
package hostidentity
import (
"log/slog"
"os"
"testing"
"github.com/fleetdm/fleet/v4/server/config"
"github.com/fleetdm/fleet/v4/server/datastore/redis/redistest"
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/fleetdm/fleet/v4/server/service"
"github.com/fleetdm/fleet/v4/server/service/integrationtest"
"github.com/fleetdm/fleet/v4/server/service/svctest"
"github.com/stretchr/testify/require"
)
// enrollOrbitResponse is the response structure for orbit enrollment
type enrollOrbitResponse struct {
OrbitNodeKey string `json:"orbit_node_key,omitempty"`
Err error `json:"error,omitempty"`
}
// orbitConfigRequest is used for orbit config endpoint requests
type orbitConfigRequest struct {
OrbitNodeKey string `json:"orbit_node_key"`
}
// osqueryConfigRequest is used for osquery config endpoint requests
type osqueryConfigRequest struct {
NodeKey string `json:"node_key"`
}
type Suite struct {
integrationtest.BaseSuite
}
func SetUpSuite(t *testing.T, uniqueTestName string, requireSignature bool) *Suite {
return SetUpSuiteWithConfig(t, uniqueTestName, requireSignature, nil)
}
func SetUpSuiteWithConfig(t *testing.T, uniqueTestName string, requireSignature bool, configModifier func(cfg *config.FleetConfig)) *Suite {
// Note: t.Parallel() is called when MySQL datastore options are processed
license := &fleet.LicenseInfo{
Tier: fleet.TierPremium,
}
ds, fleetCfg, fleetSvc, ctx := integrationtest.SetUpMySQLAndService(t, uniqueTestName, &service.TestServerOpts{
License: license,
Pool: redistest.SetupRedis(t, t.Name(), false, false, false),
})
// Apply config modifications
if configModifier != nil {
configModifier(&fleetCfg)
}
slogLogger := slog.New(slog.NewTextHandler(os.Stdout, nil))
hostIdentitySCEPDepot, err := ds.NewHostIdentitySCEPDepot(slogLogger.With("component", "host-id-scep-depot"), &fleetCfg)
require.NoError(t, err)
users, server := svctest.RunServerForTestsWithServiceWithDS(t, ctx, ds, fleetSvc, &service.TestServerOpts{
License: license,
FleetConfig: &fleetCfg,
Logger: slogLogger,
HostIdentity: &service.HostIdentity{
SCEPStorage: hostIdentitySCEPDepot,
RequireHTTPMessageSignature: requireSignature,
},
})
s := &Suite{
BaseSuite: integrationtest.BaseSuite{
Logger: slogLogger,
DS: ds,
FleetCfg: fleetCfg,
Users: users,
Server: server,
},
}
integrationtest.SetUpServerURL(t, ds, server)
s.BaseSuite.Token = s.BaseSuite.GetTestAdminToken(t)
return s
}