Clean up Gitops tests and add deprecation tests#43039
Conversation
There was a problem hiding this comment.
Added to test bootstrap_package and macos_bootstrap_package
There was a problem hiding this comment.
updated all keys in here and copied the original to global_config_no_paths_deprecated_keys.yml
There was a problem hiding this comment.
updated keys and copied original to team_config_no_paths_deprecated_keys.yml
| case strings.HasSuffix(r.URL.Path, ".pkg"): | ||
| pkgDir := getPathRelative("../testdata/gitops/lib/") | ||
| http.ServeFile(w, r, filepath.Join(pkgDir, filepath.Base(r.URL.Path))) |
There was a problem hiding this comment.
to mock serve the setup experience script to test macos_setup.script / setup_experience.script
| ds.InsertMDMAppleBootstrapPackageFunc = func(ctx context.Context, bp *fleet.MDMAppleBootstrapPackage, pkgStore fleet.MDMBootstrapPackageStore) error { | ||
| return nil | ||
| } |
There was a problem hiding this comment.
new mock for us to spy on to test that macos_setup.boostrap_package / setup_experience.macos_bootstrap_package work
There was a problem hiding this comment.
We were maintaining (lazily) the entire set of renamed keys in here to guard against missing any, but we now have a better set of deprecated keys maintained in pkg/spec/gitops_deprecations.go, so i'm just having generate_gitops derive its renaming rules from that list.
I spot-checked this and found one missing (android config profiles), then I told Claude to run through all calls to validateRawKeys or validateYAMLKeys which should provide a pretty comprehensive list of GitOps structs, then find keys in those structs with renames. The only missing ones were things we don't actually use in GitOps (like labels.team_id).
If we did miss something in this list, the result would be missing a deprecation warning and not having old/new key conflict detection. It's not a risk of an old or new key not working. So I think it's worth the cleanup to keep these from drifting.
| assert.False(t, savedTeam.Config.MDM.MacOSSetup.EnableReleaseDeviceManually.Value) | ||
| assert.False(t, savedTeam.Config.MDM.MacOSSetup.ManualAgentInstall.Value) | ||
| assert.True(t, ds.SetSetupExperienceScriptFuncInvoked) | ||
| assert.True(t, ds.InsertMDMAppleBootstrapPackageFuncInvoked) |
There was a problem hiding this comment.
This validates that the bootstrap_package / macos_bootstrap_package key works
| assert.False(t, savedTeam.Config.MDM.MacOSSetup.EnableReleaseDeviceManually.Value) | ||
| assert.False(t, savedTeam.Config.MDM.MacOSSetup.ManualAgentInstall.Value) | ||
| assert.True(t, ds.SetSetupExperienceScriptFuncInvoked) |
There was a problem hiding this comment.
This validates that the other setup experience keys are checked
| assert.Equal(t, []uint{policy.ID}, IDs) | ||
| return []uint{policy.ID}, nil |
There was a problem hiding this comment.
moved this assertion to the main test block
| ds.ListQueriesFunc = func(ctx context.Context, opts fleet.ListQueryOptions) ([]*fleet.Query, int, int, *fleet.PaginationMetadata, error) { | ||
| return []*fleet.Query{&query}, 1, 0, nil, nil | ||
| } | ||
| ds.DeleteQueriesFunc = func(ctx context.Context, ids []uint) (uint, error) { | ||
| queryDeleted = true | ||
| assert.Equal(t, []uint{query.ID}, ids) |
There was a problem hiding this comment.
moved this assertion to the main test block
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #43039 +/- ##
==========================================
- Coverage 66.86% 66.86% -0.01%
==========================================
Files 2578 2578
Lines 206869 207053 +184
Branches 9168 9168
==========================================
+ Hits 138328 138443 +115
- Misses 55978 56014 +36
- Partials 12563 12596 +33
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
The majority of changes here are just removing mocks in favor of calling setupEmptyGitOpsMocks(ds). In a couple of places, assertions in the mocks have been moved to the tests.
The other changes are the wrapping of some test code in loops to test with both new and deprecated keys, which also involves hoisting some testing vars up.
Lastly, we add a couple of new assertions to test things like setup experience keys. These have been commented separately.
| // or return specific values. | ||
| // | ||
| //nolint:unused // used in tests | ||
| func setupEmptyGitOpsMocks(ds *mock.Store) { |
There was a problem hiding this comment.
There may be some overlap here with mocks set up in RunServerWithMockedDS(). Just moving the rest of these into that method would be an option, but it's used in other places so this felt like the lightest touch.
There was a problem hiding this comment.
Claude Code Review
This repository is configured for manual code reviews. Comment @claude review to trigger a review and subscribe this PR to future pushes, or @claude review once for a one-time review.
Tip: disable this comment in your organization's Code Review settings.
There was a problem hiding this comment.
Pull request overview
This PR cleans up fleetctl gitops test scaffolding and expands coverage for recently renamed/deprecated GitOps YAML keys by centralizing deprecated→new key mappings and adding fixtures/tests that exercise both canonical and deprecated forms.
Changes:
- Centralized deprecated GitOps key mappings in
pkg/specand derivedgenerate-gitopsalias rules from that single source of truth. - Refactored
fleetctl gitopstests to use a sharedsetupEmptyGitOpsMockshelper and added new fixtures for deprecated-key “kitchen sink” runs. - Updated GitOps YAML fixtures to use canonical keys (
settings,reports,setup_experience,apple_settings, server_settings report fields) and added a.pkgtest fixture served by the installer test server.
Reviewed changes
Copilot reviewed 10 out of 12 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/spec/testdata/org-settings.yml | Updates org settings fixture to canonical report-related server_settings keys. |
| pkg/spec/gitops_test.go | Updates assertions for canonical org settings key names/types. |
| pkg/spec/gitops_deprecations.go | Adds/centralizes deprecated→canonical GitOps key mappings and warning behavior. |
| cmd/fleetctl/fleetctl/generate_gitops.go | Builds output alias rules from the centralized mappings. |
| cmd/fleetctl/fleetctl/testing_utils/testing_utils.go | Extends test installer server to serve .pkg and adds missing datastore mock. |
| cmd/fleetctl/fleetctl/testing_utils.go | Introduces setupEmptyGitOpsMocks to reduce duplicated gitops datastore mocking. |
| cmd/fleetctl/fleetctl/gitops_test.go | Refactors tests to use shared mocks and adds runs for deprecated vs canonical keys. |
| cmd/fleetctl/fleetctl/testdata/gitops/*.yml | Updates canonical fixtures and adds deprecated-key variants for coverage. |
| cmd/fleetctl/fleetctl/testdata/gitops/lib/signed.pkg | Adds a .pkg fixture used by setup experience/bootstrap package tests. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
cmd/fleetctl/fleetctl/gitops_test.go
Outdated
| t.Setenv("FLEET_SERVER_URL", fleetServerURL) | ||
| t.Setenv("ORG_NAME", orgName) | ||
| t.Setenv("SOFTWARE_INSTALLER_URL", fleetServerURL) | ||
| t.Setenv("FLEET_ENABLE_LOG_TOPICS", "deprecated-field-names") | ||
|
|
||
| // Dry run w/ top-level labels key | ||
| logs := RunAppForTest(t, []string{"gitops", "-f", path, "--dry-run"}) | ||
| fmt.Printf("%s", logs) |
There was a problem hiding this comment.
The new subtests enable the deprecated-field-names log topic but never assert on the output. To make these true deprecation tests, assert that the warning text is present when useDeprecatedKeys==true (and absent when false) so the test fails if warnings stop being emitted.
| // | ||
| //nolint:unused // used in tests |
There was a problem hiding this comment.
setupEmptyGitOpsMocks is referenced from tests in this package, so the //nolint:unused suppression is redundant and can mask future dead code. Consider removing the nolint directive so the linter will catch it if the helper stops being used.
| // | |
| //nolint:unused // used in tests |
There was a problem hiding this comment.
Since we added new entries to ApplyDeprecatedKeyMappings, conflicts for those keys are caught earlier (during file parsing rather than during the control: section processing), but they use the full paths rather than just the key names, hence the test updates
1683fe7 to
bc950ad
Compare
iansltx
left a comment
There was a problem hiding this comment.
Got to L1722 in the new version of gitops_test.go. Checkpointing so I don't lose my place. One piece of feedback so far; some of these refactors made the mocks that weren't swalloped up in the default mocks more consistent, which is nice.
| // Take the last segment of the old and new paths as the leaf key names. | ||
| // Remove any array indicators (e.g. "[]") in case an array key is renamed. | ||
| oldLeaf := m.OldPath | ||
| if i := strings.LastIndex(oldLeaf, "."); i >= 0 { |
There was a problem hiding this comment.
Does this work properly with query?
There was a problem hiding this comment.
You mean paths without .? It should, the i >= 0 will fail (not found is -1, just like JS), so oldLeaf will remain unchanged.
There was a problem hiding this comment.
Yeah, thinking in terms of false-positive replacing old -> new when we shouldn't, given that this strips initial path components when they exist.
There was a problem hiding this comment.
Ah yeah for query specifically we just don't alias that, as it's not applicable anywhere in gitops (i.e. there's no query field in gitops config that doesn't refer to an actual SQL query).
iansltx
left a comment
There was a problem hiding this comment.
Given that my concern about the query case is misplaced, this looks fine. Had to trust that tests were equivalent when thrown in the loop because diffs were inaccurate, but given that test length was comparable and we didn't have code changes for that part of the code that seems like a valid assumption.
<!-- Add the related story/sub-task/bug number, like Resolves #123, or remove if NA --> **Related issue:** For #40015 * Moves repeated empty mocks into a new `setupEmptyGitOpsMocks` method * Adds new "deprecation" tests: * In TestGitOpsFullGlobal, TestGitOpsFullTeam and TestGitOpsFullGlobalAndTeam tests "kitchen sink" with both new and deprecated keys * Added keys and checks to verify `setup_experience`, `apple_business_manager` and `volume_purchasing_program` configs * Consolidated map of deprecated -> new GitOps keys in one place
<!-- Add the related story/sub-task/bug number, like Resolves #123, or remove if NA --> **Related issue:** For #40015 * Moves repeated empty mocks into a new `setupEmptyGitOpsMocks` method * Adds new "deprecation" tests: * In TestGitOpsFullGlobal, TestGitOpsFullTeam and TestGitOpsFullGlobalAndTeam tests "kitchen sink" with both new and deprecated keys * Added keys and checks to verify `setup_experience`, `apple_business_manager` and `volume_purchasing_program` configs * Consolidated map of deprecated -> new GitOps keys in one place
Related issue: For #40015
setupEmptyGitOpsMocksmethodsetup_experience,apple_business_managerandvolume_purchasing_programconfigs