Fix flaky TestEnqueueMDMCommand test (#24697)

FYI this was diagnosed and fixed using the
[RandoKiller](https://github.com/fleetdm/fleet/pull/24696).

---

This PR fixes the TestEnqueueMDMCommand, which has been failing
intermittently
[here](https://github.com/fleetdm/fleet/blob/main/server/service/integration_mdm_test.go#L2922).
Most of the time the `/api/latest/fleet/mdm/apple/commands` API is
returning one result as expected, but occasionally it returns 2, for
example:

```
[
  {
    "device_id": "B11F1FC1-F176-48CF-88A4-CB7A3DFEF987",
    "command_uuid": "63bb4313-ccbf-4647-ac07-7d15df5f92d7",
    "updated_at": "2024-12-12T02:41:36Z",
    "request_type": "ProfileList",
    "status": "Acknowledged",
    "hostname": "test-host"
  },
  {
    "device_id": "B11F1FC1-F176-48CF-88A4-CB7A3DFEF987",
    "command_uuid": "7de9d712-7524-4443-a20a-7127e6064f6e",
    "updated_at": "2024-12-12T02:41:36.141498Z",
    "request_type": "InstallEnterpriseApplication",
    "status": "Pending",
    "hostname": "test-host"
  }
]
```

It seems that the second command is related to trying to install a
bootstrap package (uploaded by a previous test) to the newly-enrolled
host.

The fix in this PR is to filter the API response to only the command
we're verifying the presence of. It's a decent solve, but leaves open
the edge case of a bug that causes multiple commands to be sent
unexpectedly. The ideal solution would be to remove the interaction
between the two tests, perhaps by deleting any created bootstraps before
those tests complete, or by re-initializing the state in some other way.
I don't currently have enough context to easily implement a solution
like that (i.e. I know there's a "delete bootstrap" API, but not sure if
that's enough to solve this issue).
This commit is contained in:
Scott Gress 2024-12-12 12:30:42 -06:00 committed by GitHub
parent 5db90645a4
commit cdae1749bf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2919,16 +2919,30 @@ func (s *integrationMDMTestSuite) TestEnqueueMDMCommand() {
// list commands returns that command
s.DoJSON("GET", "/api/latest/fleet/mdm/apple/commands", nil, http.StatusOK, &listCmdResp)
require.Len(t, listCmdResp.Results, 1)
require.NotZero(t, listCmdResp.Results[0].UpdatedAt)
listCmdResp.Results[0].UpdatedAt = time.Time{}
results, err := json.Marshal(listCmdResp.Results)
require.NoError(t, err)
t.Logf("GET /api/latest/fleet/mdm/apple/commands response:\n%s", results)
// filter to the expected command.
// there may be other commands due to the bootstrap packages uploaded in prior tests.
// TODO: decouple these tests so we don't have to do this -- perhaps delete bootstrap packages?
var profileListCommands []*fleet.MDMAppleCommand
for _, result := range listCmdResp.Results {
if result.RequestType == "ProfileList" {
profileListCommands = append(profileListCommands, result)
}
}
require.Len(t, profileListCommands, 1)
require.NotZero(t, profileListCommands[0].UpdatedAt)
profileListCommands[0].UpdatedAt = time.Time{}
require.Equal(t, &fleet.MDMAppleCommand{
DeviceID: mdmDevice.UUID,
CommandUUID: uuid2,
Status: "Acknowledged",
RequestType: "ProfileList",
Hostname: "test-host",
}, listCmdResp.Results[0])
}, profileListCommands[0])
}
func (s *integrationMDMTestSuite) TestMDMWindowsCommandResults() {