Skip to content

Commit

Permalink
Add on e last test
Browse files Browse the repository at this point in the history
  • Loading branch information
seanrathier committed Oct 16, 2024
1 parent e43a014 commit 38efcb4
Showing 1 changed file with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,66 @@ describe('Agentless Agent service', () => {
).rejects.toThrowError(new AgentlessAgentCreateError('missing Fleet enrollment token'));
});

it('should throw an error and log and error when the Agentless API returns a status not handled and not in the 2xx series', async () => {
const soClient = getAgentPolicyCreateMock();
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
jest.spyOn(appContextService, 'getConfig').mockReturnValue({
agentless: {
enabled: true,
api: {
url: 'http://api.agentless.com',
tls: {
certificate: '/path/to/cert',
key: '/path/to/key',
ca: '/path/to/ca',
},
},
},
} as any);
jest.spyOn(appContextService, 'getCloud').mockReturnValue({ isCloudEnabled: true } as any);
mockedListFleetServerHosts.mockResolvedValue({
items: [
{
id: 'mocked-fleet-server-id',
host: 'http://fleetserver:8220',
active: true,
is_default: true,
host_urls: ['http://fleetserver:8220'],
},
],
} as any);
mockedListEnrollmentApiKeys.mockResolvedValue({
items: [
{
id: 'mocked-fleet-enrollment-token-id',
policy_id: 'mocked-policy-id',
api_key: 'mocked-api-key',
},
],
} as any);
// Force axios to throw an AxiosError to simulate an error response
(axios as jest.MockedFunction<typeof axios>).mockRejectedValueOnce({
response: {
status: 999,
data: {
message: 'This is a fake error status that is never to be handled handled',
},
},
} as AxiosError);

await expect(
agentlessAgentService.createAgentlessAgent(esClient, soClient, {
id: 'mocked-agentless-agent-policy-id',
name: 'agentless agent policy',
namespace: 'default',
supports_agentless: true,
} as AgentPolicy)
).rejects.toThrowError();

// Assert that the error is logged
expect(mockedLogger.error).toHaveBeenCalledTimes(1);
});

it('should throw an error and log and error when the Agentless API returns status 500', async () => {
const soClient = getAgentPolicyCreateMock();
const esClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
Expand Down

0 comments on commit 38efcb4

Please sign in to comment.