fleet/server/mdm/apple/profile_verifier_test.go
Victor Lyuboslavsky e9fe5eb489
Increased Apple retry from 1 to 3. (#42331)
<!-- Add the related story/sub-task/bug number, like Resolves #123, or
remove if NA -->
**Related issue:** Resolves #42327 

We're not doing Windows because we're missing the failed activity for
Windows profiles, which we do have for Apple.

The actual code change is small. This PR is mostly test changes.

## Demo video and docs

https://www.youtube.com/watch?v=YKNguaQQs_E
https://github.com/fleetdm/fleet/pull/42332/changes

# Checklist for submitter

- [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
- [x] QA'd all new/changed functionality manually

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

## Summary by CodeRabbit

* **Improvements**
* Apple device configuration profiles (macOS, iOS, iPadOS) now
automatically retry failed deliveries up to 3 times instead of once.
* Windows configuration profiles maintain their existing single retry
limit.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-03-26 11:29:20 -05:00

126 lines
4.2 KiB
Go

package apple_mdm
import (
"context"
"errors"
"testing"
"time"
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/fleetdm/fleet/v4/server/mdm"
"github.com/fleetdm/fleet/v4/server/mock"
"github.com/stretchr/testify/require"
)
func TestVerifyHostMDMProfiles(t *testing.T) {
ctx := context.Background()
host := &fleet.Host{
DetailUpdatedAt: time.Now(),
}
ds := new(mock.Store)
tests := []struct {
name string
expectedProfiles map[string]*fleet.ExpectedMDMProfile
retryCounts []fleet.HostMDMProfileRetryCount
installed map[string]*fleet.HostMacOSProfile
toVerify []string
toFail []string
toRetry []string
expectErr bool
}{
{
name: "error on getting expected profiles",
expectErr: true,
},
{
name: "all profiles verified",
expectedProfiles: map[string]*fleet.ExpectedMDMProfile{"profile1": {}},
installed: map[string]*fleet.HostMacOSProfile{"profile1": {}},
toVerify: []string{"profile1"},
},
{
name: "profiles missing, not within grace period, no retries yet",
expectedProfiles: map[string]*fleet.ExpectedMDMProfile{
"profile1": {},
"profile2": {EarliestInstallDate: host.DetailUpdatedAt.Add(-24 * time.Hour)},
},
installed: map[string]*fleet.HostMacOSProfile{"profile1": {}},
toVerify: []string{"profile1"},
toRetry: []string{"profile2"},
},
{
name: "profiles missing, with and without retries, not within grace period",
expectedProfiles: map[string]*fleet.ExpectedMDMProfile{"profile1": {}, "profile2": {}},
retryCounts: []fleet.HostMDMProfileRetryCount{
{ProfileIdentifier: "profile1", Retries: 0},
{ProfileIdentifier: "profile2", Retries: mdm.MaxAppleProfileRetries},
},
installed: map[string]*fleet.HostMacOSProfile{},
toRetry: []string{"profile1"},
toFail: []string{"profile2"},
},
{
name: "host profile installed prior to uploading profile to Fleet",
expectedProfiles: map[string]*fleet.ExpectedMDMProfile{
"profile1": {EarliestInstallDate: time.Now().Add(-2 * time.Hour)},
},
installed: map[string]*fleet.HostMacOSProfile{
"profile1": {InstallDate: time.Now().Add(-24 * time.Hour)},
},
toRetry: []string{"profile1"},
},
{
name: "host profile installed prior to uploading profile to Fleet with max retries",
expectedProfiles: map[string]*fleet.ExpectedMDMProfile{
"profile1": {EarliestInstallDate: time.Now().Add(-2 * time.Hour)},
},
installed: map[string]*fleet.HostMacOSProfile{
"profile1": {InstallDate: time.Now().Add(-24 * time.Hour)},
},
retryCounts: []fleet.HostMDMProfileRetryCount{
{ProfileIdentifier: "profile1", Retries: mdm.MaxAppleProfileRetries},
},
toFail: []string{"profile1"},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// setup mocks
ds.GetHostMDMProfilesExpectedForVerificationFunc = func(ctx context.Context, host *fleet.Host) (map[string]*fleet.ExpectedMDMProfile, error) {
if tc.expectErr {
return nil, errors.New("error")
}
return tc.expectedProfiles, nil
}
ds.GetHostMDMProfilesRetryCountsFunc = func(ctx context.Context, host *fleet.Host) ([]fleet.HostMDMProfileRetryCount, error) {
return tc.retryCounts, nil
}
ds.UpdateHostMDMProfilesVerificationFunc = func(ctx context.Context, host *fleet.Host, verified, toFail, toRetry []string) error {
require.ElementsMatch(t, tc.toVerify, verified, "verified profiles do not match")
require.ElementsMatch(t, tc.toFail, toFail, "failed profiles do not match")
require.ElementsMatch(t, tc.toRetry, toRetry, "retried profiles do not match")
return nil
}
// run the test
err := VerifyHostMDMProfiles(ctx, ds, host, tc.installed)
if tc.expectErr {
require.Error(t, err)
require.False(
t,
ds.UpdateHostMDMProfilesVerificationFuncInvoked,
"UpdateHostMDMProfilesVerificationFunc should not have been called",
)
} else {
require.NoError(t, err)
require.True(t, ds.UpdateHostMDMProfilesVerificationFuncInvoked, "UpdateHostMDMProfilesVerificationFunc should have been called")
}
ds.UpdateHostMDMProfilesVerificationFuncInvoked = false
})
}
}