fleet/cmd/fleetctl/integrationtest/gitops/starter_library_integration_test.go
Scott Gress c4aa6f5529
Use fleetctl new templates for new instances (#42768)
<!-- Add the related story/sub-task/bug number, like Resolves #123, or
remove if NA -->
**Related issue:** Resolves #41409 

# Details

This PR updates the `ApplyStarterLibrary` method and functionality to
rely on the same templates and mechanisms as `fleetctl new`. The end
result is that running `fleetctl new` and `fleetctl gitops` on a new
instance should be a no-op; no changes should be made. Similarly,
changing the templates in a Fleet release will automatically affect
`fleetctl new` and `ApplyStarterLibrary` in the same exact way for that
release.

> Note that this moves the template files out of `fleetctl` and into
their own shared package. This move comprises the majority of the file
changes in the PR.

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

## Testing

- [X] Added/updated automated tests
Note that 

<img width="668" height="44" alt="image"
src="https://github.com/user-attachments/assets/066cd566-f91d-4661-84fc-2aabbfce2ef9"
/>

will fail until the 4.83 Fleet docker image is published, since it's
trying to push 4.83 config (including `exceptions`) to a 4.82 server.

- [X] QA'd all new/changed functionality manually
- [X] Created a new instance and validated that the fleets, policies and
labels created matched the ones created by `fleetctl new`
- [X] Ran `fleetctl new` and verified that it created the expected
folders and files
- [X] Ran `fleetctl gitops` with the files created by `fleetctl new` and
verified that the instance was unchanged.
- [X] Ran `fleetctl preview` successfully using a dev build of the Fleet
server image (since it won't work against the latest published build,
which doesn't support `exceptions`). Verified it shows the expected
teams, policies and labels
2026-04-03 09:58:03 -05:00

108 lines
3.4 KiB
Go

package gitops
import (
"context"
"log/slog"
"testing"
"github.com/fleetdm/fleet/v4/cmd/fleetctl/fleetctl"
"github.com/fleetdm/fleet/v4/cmd/fleetctl/integrationtest"
"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/ptr"
"github.com/fleetdm/fleet/v4/server/service"
"github.com/fleetdm/fleet/v4/server/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
func TestIntegrationsStarterLibrary(t *testing.T) {
testingSuite := new(starterLibraryIntegrationTestSuite)
testingSuite.WithServer.Suite = &testingSuite.Suite
suite.Run(t, testingSuite)
}
type starterLibraryIntegrationTestSuite struct {
suite.Suite
integrationtest.WithServer
}
func (s *starterLibraryIntegrationTestSuite) SetupSuite() {
s.WithDS.SetupSuite("starterLibraryIntegrationTestSuite")
appConf, err := s.DS.AppConfig(context.Background())
s.Require().NoError(err)
err = s.DS.SaveAppConfig(context.Background(), appConf)
s.Require().NoError(err)
fleetCfg := config.TestConfig()
fleetCfg.Osquery.EnrollCooldown = 0
redisPool := redistest.SetupRedis(s.T(), "starter_library", false, false, false)
serverConfig := service.TestServerOpts{
FleetConfig: &fleetCfg,
Pool: redisPool,
}
users, server := service.RunServerForTestsWithDS(s.T(), s.DS, &serverConfig)
s.T().Setenv("FLEET_SERVER_ADDRESS", server.URL)
s.Server = server
s.Users = users
appConf, err = s.DS.AppConfig(context.Background())
s.Require().NoError(err)
appConf.ServerSettings.ServerURL = server.URL
appConf.OrgInfo.OrgName = "Test Org"
appConf.GitOpsConfig.Exceptions = fleet.GitOpsExceptions{}
err = s.DS.SaveAppConfig(context.Background(), appConf)
s.Require().NoError(err)
}
// TestApplyStarterLibraryFree verifies that ApplyStarterLibrary applies only
// the global config (no teams) when using a free license.
func (s *starterLibraryIntegrationTestSuite) TestApplyStarterLibraryFree() {
t := s.T()
ctx := context.Background()
token := s.GetTestToken("admin1@example.com", test.GoodPassword)
logger := slog.New(slog.DiscardHandler)
err := service.ApplyStarterLibrary(
ctx,
s.Server.URL,
token,
logger,
func(args []string) error {
_, err := fleetctl.RunAppNoChecks(args)
return err
},
)
require.NoError(t, err)
// Verify the org name was applied.
appConfig, err := s.DS.AppConfig(ctx)
require.NoError(t, err)
assert.Equal(t, "Test Org", appConfig.OrgInfo.OrgName)
// Verify that no teams were created for a free license.
teams, err := s.DS.ListTeams(ctx, fleet.TeamFilter{User: &fleet.User{GlobalRole: ptr.String("admin")}}, fleet.ListOptions{})
require.NoError(t, err)
assert.Empty(t, teams)
// Verify labels were created (global labels, team_id=0).
labelSpecs, err := s.DS.GetLabelSpecs(ctx, fleet.TeamFilter{User: &fleet.User{GlobalRole: ptr.String("admin")}})
require.NoError(t, err)
var customLabelNames []string
for _, l := range labelSpecs {
if l.LabelType != fleet.LabelTypeBuiltIn {
customLabelNames = append(customLabelNames, l.Name)
}
}
assert.Contains(t, customLabelNames, "Apple Silicon macOS hosts")
assert.Contains(t, customLabelNames, "ARM-based Windows hosts")
assert.Contains(t, customLabelNames, "Debian-based Linux hosts")
assert.Contains(t, customLabelNames, "x86-based Windows hosts")
}