Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,12 @@ export const createAndEnrollEndpointHostCI = async ({
log.warning(
`There is no agent installer for ${agentFileName} present on disk, trying to download it now.`
);
const { url: agentUrl } = await getAgentDownloadUrl(agentVersion, useClosestVersionMatch, log);
agentDownload = await downloadAndStoreAgent(agentUrl, agentFileName);
const { url: agentUrl, shaUrl } = await getAgentDownloadUrl(
agentVersion,
useClosestVersionMatch,
log
);
agentDownload = await downloadAndStoreAgent(agentUrl, agentFileName, shaUrl);
}

const hostVm = await createVm({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ describe('agentDownloaderRunner', () => {
const version = '8.15.0';
let closestMatch = false;
const url = 'http://example.com/agent.tar.gz';
const shaUrl = 'http://example.com/agent.tar.gz.sha512';
const fileName = 'elastic-agent-8.15.0.tar.gz';

it('downloads and stores the specified version', async () => {
(getAgentDownloadUrl as jest.Mock).mockResolvedValue({ url });
(getAgentDownloadUrl as jest.Mock).mockResolvedValue({ url, shaUrl });
(getAgentFileName as jest.Mock).mockReturnValue('elastic-agent-8.15.0');
(downloadAndStoreAgent as jest.Mock).mockResolvedValue(undefined);

Expand All @@ -43,12 +44,12 @@ describe('agentDownloaderRunner', () => {

expect(getAgentDownloadUrl).toHaveBeenCalledWith(version, closestMatch, log);
expect(getAgentFileName).toHaveBeenCalledWith(version);
expect(downloadAndStoreAgent).toHaveBeenCalledWith(url, fileName);
expect(downloadAndStoreAgent).toHaveBeenCalledWith(url, fileName, shaUrl);
expect(log.info).toHaveBeenCalledWith('Successfully downloaded and stored version 8.15.0');
});

it('logs an error if the download fails', async () => {
(getAgentDownloadUrl as jest.Mock).mockResolvedValue({ url });
(getAgentDownloadUrl as jest.Mock).mockResolvedValue({ url, shaUrl });
(getAgentFileName as jest.Mock).mockReturnValue('elastic-agent-8.15.0');
(downloadAndStoreAgent as jest.Mock).mockRejectedValue(new Error('Download failed'));

Expand All @@ -59,7 +60,7 @@ describe('agentDownloaderRunner', () => {

expect(getAgentDownloadUrl).toHaveBeenCalledWith(version, closestMatch, log);
expect(getAgentFileName).toHaveBeenCalledWith(version);
expect(downloadAndStoreAgent).toHaveBeenCalledWith(url, fileName);
expect(downloadAndStoreAgent).toHaveBeenCalledWith(url, fileName, shaUrl);
expect(log.error).toHaveBeenCalledWith(
'Failed to download or store version 8.15.0: Download failed'
);
Expand All @@ -70,8 +71,8 @@ describe('agentDownloaderRunner', () => {
const fallbackFileName = 'elastic-agent-8.15.0.tar.gz';

(getAgentDownloadUrl as jest.Mock)
.mockResolvedValueOnce({ url })
.mockResolvedValueOnce({ url });
.mockResolvedValueOnce({ url, shaUrl })
.mockResolvedValueOnce({ url, shaUrl });
(getAgentFileName as jest.Mock)
.mockReturnValueOnce('elastic-agent-8.15.1')
.mockReturnValueOnce('elastic-agent-8.15.0');
Expand All @@ -88,16 +89,16 @@ describe('agentDownloaderRunner', () => {
expect(getAgentDownloadUrl).toHaveBeenCalledWith(fallbackVersion, closestMatch, log);
expect(getAgentFileName).toHaveBeenCalledWith('8.15.1');
expect(getAgentFileName).toHaveBeenCalledWith(fallbackVersion);
expect(downloadAndStoreAgent).toHaveBeenCalledWith(url, 'elastic-agent-8.15.1.tar.gz');
expect(downloadAndStoreAgent).toHaveBeenCalledWith(url, fallbackFileName);
expect(downloadAndStoreAgent).toHaveBeenCalledWith(url, 'elastic-agent-8.15.1.tar.gz', shaUrl);
expect(downloadAndStoreAgent).toHaveBeenCalledWith(url, fallbackFileName, shaUrl);
expect(log.error).toHaveBeenCalledWith(
'Failed to download or store version 8.15.1: Download failed'
);
expect(log.info).toHaveBeenCalledWith('Successfully downloaded and stored version 8.15.0');
});

it('logs an error if all downloads fail', async () => {
(getAgentDownloadUrl as jest.Mock).mockResolvedValue({ url });
(getAgentDownloadUrl as jest.Mock).mockResolvedValue({ url, shaUrl });
(getAgentFileName as jest.Mock)
.mockReturnValueOnce('elastic-agent-8.15.1')
.mockReturnValueOnce('elastic-agent-8.15.0');
Expand All @@ -114,8 +115,8 @@ describe('agentDownloaderRunner', () => {
expect(getAgentDownloadUrl).toHaveBeenCalledWith('8.15.0', closestMatch, log);
expect(getAgentFileName).toHaveBeenCalledWith('8.15.1');
expect(getAgentFileName).toHaveBeenCalledWith('8.15.0');
expect(downloadAndStoreAgent).toHaveBeenCalledWith(url, 'elastic-agent-8.15.1.tar.gz');
expect(downloadAndStoreAgent).toHaveBeenCalledWith(url, 'elastic-agent-8.15.0.tar.gz');
expect(downloadAndStoreAgent).toHaveBeenCalledWith(url, 'elastic-agent-8.15.1.tar.gz', shaUrl);
expect(downloadAndStoreAgent).toHaveBeenCalledWith(url, 'elastic-agent-8.15.0.tar.gz', shaUrl);
expect(log.error).toHaveBeenCalledWith(
'Failed to download or store version 8.15.1: Download failed'
);
Expand All @@ -125,7 +126,7 @@ describe('agentDownloaderRunner', () => {
});

it('does not attempt fallback when patch version is 0', async () => {
(getAgentDownloadUrl as jest.Mock).mockResolvedValue({ url });
(getAgentDownloadUrl as jest.Mock).mockResolvedValue({ url, shaUrl });
(getAgentFileName as jest.Mock).mockReturnValue('elastic-agent-8.15.0');
(downloadAndStoreAgent as jest.Mock).mockResolvedValue(undefined);

Expand All @@ -136,7 +137,7 @@ describe('agentDownloaderRunner', () => {

expect(getAgentDownloadUrl).toHaveBeenCalledTimes(1); // Only one call for 8.15.0
expect(getAgentFileName).toHaveBeenCalledTimes(1);
expect(downloadAndStoreAgent).toHaveBeenCalledWith(url, fileName);
expect(downloadAndStoreAgent).toHaveBeenCalledWith(url, fileName, shaUrl);
expect(log.info).toHaveBeenCalledWith('Successfully downloaded and stored version 8.15.0');
});

Expand All @@ -154,7 +155,7 @@ describe('agentDownloaderRunner', () => {
it('passes the closestMatch flag correctly', async () => {
closestMatch = true;

(getAgentDownloadUrl as jest.Mock).mockResolvedValue({ url });
(getAgentDownloadUrl as jest.Mock).mockResolvedValue({ url, shaUrl });
(getAgentFileName as jest.Mock).mockReturnValue('elastic-agent-8.15.0');
(downloadAndStoreAgent as jest.Mock).mockResolvedValue(undefined);

Expand All @@ -179,8 +180,8 @@ describe('agentDownloaderRunner', () => {
const primaryVersion = '8.15.1';

(getAgentDownloadUrl as jest.Mock)
.mockResolvedValueOnce({ url })
.mockResolvedValueOnce({ url });
.mockResolvedValueOnce({ url, shaUrl })
.mockResolvedValueOnce({ url, shaUrl });

(getAgentFileName as jest.Mock)
.mockReturnValueOnce('elastic-agent-8.15.1')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ const downloadAndStoreElasticAgent = async (
// Download all the versions in the list
for (const versionToDownload of versionsToDownload) {
try {
const { url } = await getAgentDownloadUrl(versionToDownload, closestMatch, log);
const { url, shaUrl } = await getAgentDownloadUrl(versionToDownload, closestMatch, log);
const fileName = `${getAgentFileName(versionToDownload)}.tar.gz`;

await downloadAndStoreAgent(url, fileName);
await downloadAndStoreAgent(url, fileName, shaUrl);
log.info(`Successfully downloaded and stored version ${versionToDownload}`);
} catch (error) {
log.error(`Failed to download or store version ${versionToDownload}: ${error.message}`);
Expand Down
Loading
Loading