|
| 1 | +import { randomUUID } from 'node:crypto'; |
| 2 | +import fs, { WriteStream } from 'node:fs'; |
| 3 | +import os from 'node:os'; |
| 4 | +import path from 'node:path'; |
| 5 | + |
| 6 | +import axios from 'axios'; |
| 7 | +import axiosRetry from 'axios-retry'; |
| 8 | +import { mocked } from 'jest-mock'; |
| 9 | + |
| 10 | +import { expectThrownError } from '../test/util'; |
| 11 | +import { Artifact, ArtifactDownloadError } from './artifact'; |
| 12 | +import { computeIntegrityHash } from './integrity-hash'; |
| 13 | + |
| 14 | +jest.mock('node:fs'); |
| 15 | +jest.mock('node:os'); |
| 16 | +jest.mock('axios'); |
| 17 | +jest.mock('axios-retry'); |
| 18 | +jest.mock('./integrity-hash'); |
| 19 | + |
| 20 | +const ARTIFACT_URL = 'https://foo.bar/artifact.baz'; |
| 21 | +const TEMP_DIR = '/tmp'; |
| 22 | +const TEMP_FOLDER = 'artifact-1234'; |
| 23 | + |
| 24 | +beforeEach(() => { |
| 25 | + mocked(axios.get).mockReturnValue( |
| 26 | + Promise.resolve({ |
| 27 | + data: { |
| 28 | + pipe: jest.fn(), |
| 29 | + }, |
| 30 | + status: 200, |
| 31 | + }) |
| 32 | + ); |
| 33 | + |
| 34 | + mocked(fs.createWriteStream).mockReturnValue({ |
| 35 | + on: jest.fn((event: string, func: (...args: any[]) => unknown) => { |
| 36 | + if (event === 'finish') { |
| 37 | + func(); |
| 38 | + } |
| 39 | + }), |
| 40 | + } as any); |
| 41 | + |
| 42 | + mocked(os.tmpdir).mockReturnValue(TEMP_DIR); |
| 43 | + mocked(fs.mkdtempSync).mockReturnValue(path.join(TEMP_DIR, TEMP_FOLDER)); |
| 44 | + mocked(computeIntegrityHash).mockReturnValue(`sha256-${randomUUID()}`); |
| 45 | +}); |
| 46 | + |
| 47 | +describe('Artifact', () => { |
| 48 | + describe('download', () => { |
| 49 | + test('downloads the artifact', async () => { |
| 50 | + const artifact = new Artifact(ARTIFACT_URL); |
| 51 | + await artifact.download(); |
| 52 | + |
| 53 | + expect(axios.get).toHaveBeenCalledWith(ARTIFACT_URL, { |
| 54 | + responseType: 'stream', |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + test('retries the request if it fails', async () => { |
| 59 | + const artifact = new Artifact(ARTIFACT_URL); |
| 60 | + |
| 61 | + // Restore the original behavior of exponentialDelay. |
| 62 | + mocked(axiosRetry.exponentialDelay).mockImplementation( |
| 63 | + jest.requireActual('axios-retry').exponentialDelay |
| 64 | + ); |
| 65 | + |
| 66 | + await artifact.download(); |
| 67 | + |
| 68 | + expect(axiosRetry).toHaveBeenCalledWith(axios, { |
| 69 | + retries: 3, |
| 70 | + |
| 71 | + retryCondition: expect.matchesPredicate( |
| 72 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type |
| 73 | + (retryConditionFn: Function) => { |
| 74 | + // Make sure HTTP 404 errors are retried. |
| 75 | + const notFoundError = { response: { status: 404 } }; |
| 76 | + return retryConditionFn.call(this, notFoundError); |
| 77 | + } |
| 78 | + ), |
| 79 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type |
| 80 | + retryDelay: expect.matchesPredicate((retryDelayFn: Function) => { |
| 81 | + // Make sure the retry delays follow exponential backoff |
| 82 | + // and the final retry happens after at least 1 minute total |
| 83 | + // (in this case, at least 70 seconds). |
| 84 | + // Axios randomly adds an extra 0-20% of jitter to each delay. |
| 85 | + // Test upper bounds as well to ensure the workflow completes reasonably quickly |
| 86 | + // (in this case, no more than 84 seconds total). |
| 87 | + const firstRetryDelay = retryDelayFn.call(this, 0); |
| 88 | + const secondRetryDelay = retryDelayFn.call(this, 1); |
| 89 | + const thirdRetryDelay = retryDelayFn.call(this, 2); |
| 90 | + return ( |
| 91 | + 10000 <= firstRetryDelay && |
| 92 | + firstRetryDelay <= 12000 && |
| 93 | + 20000 <= secondRetryDelay && |
| 94 | + secondRetryDelay <= 24000 && |
| 95 | + 40000 <= thirdRetryDelay && |
| 96 | + thirdRetryDelay <= 48000 |
| 97 | + ); |
| 98 | + }), |
| 99 | + shouldResetTimeout: true, |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + test('saves the artifact to disk', async () => { |
| 104 | + const artifact = new Artifact(ARTIFACT_URL); |
| 105 | + |
| 106 | + await artifact.download(); |
| 107 | + |
| 108 | + const expectedPath = path.join(TEMP_DIR, TEMP_FOLDER, 'artifact.baz'); |
| 109 | + expect(fs.createWriteStream).toHaveBeenCalledWith(expectedPath, { |
| 110 | + flags: 'w', |
| 111 | + }); |
| 112 | + |
| 113 | + const mockedAxiosResponse = await (mocked(axios.get).mock.results[0] |
| 114 | + .value as Promise<{ data: { pipe: Function } }>); // eslint-disable-line @typescript-eslint/no-unsafe-function-type |
| 115 | + const mockedWriteStream = mocked(fs.createWriteStream).mock.results[0] |
| 116 | + .value as WriteStream; |
| 117 | + |
| 118 | + expect(mockedAxiosResponse.data.pipe).toHaveBeenCalledWith( |
| 119 | + mockedWriteStream |
| 120 | + ); |
| 121 | + }); |
| 122 | + |
| 123 | + test('sets the diskPath', async () => { |
| 124 | + const artifact = new Artifact(ARTIFACT_URL); |
| 125 | + |
| 126 | + await artifact.download(); |
| 127 | + |
| 128 | + const expectedPath = path.join(TEMP_DIR, TEMP_FOLDER, 'artifact.baz'); |
| 129 | + expect(artifact.diskPath).toEqual(expectedPath); |
| 130 | + }); |
| 131 | + |
| 132 | + test('throws on a non 200 status', async () => { |
| 133 | + const artifact = new Artifact(ARTIFACT_URL); |
| 134 | + |
| 135 | + mocked(axios.get).mockRejectedValue({ |
| 136 | + response: { |
| 137 | + status: 401, |
| 138 | + }, |
| 139 | + }); |
| 140 | + |
| 141 | + const thrownError = await expectThrownError( |
| 142 | + () => artifact.download(), |
| 143 | + ArtifactDownloadError |
| 144 | + ); |
| 145 | + |
| 146 | + expect(thrownError.message.includes(ARTIFACT_URL)).toEqual(true); |
| 147 | + expect(thrownError.message.includes('401')).toEqual(true); |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + describe('computeIntegrityHash', () => { |
| 152 | + test('throws when artifact has not yet been downloaded', () => { |
| 153 | + const artifact = new Artifact(ARTIFACT_URL); |
| 154 | + |
| 155 | + expect(() => artifact.computeIntegrityHash()).toThrowWithMessage( |
| 156 | + Error, |
| 157 | + `The artifact ${ARTIFACT_URL} must be downloaded before an integrity hash can be calculated` |
| 158 | + ); |
| 159 | + }); |
| 160 | + |
| 161 | + test('computes the integrity of the file', async () => { |
| 162 | + const artifact = new Artifact(ARTIFACT_URL); |
| 163 | + await artifact.download(); |
| 164 | + |
| 165 | + const expected = `sha256-${randomUUID()}`; |
| 166 | + mocked(computeIntegrityHash).mockReturnValue(expected); |
| 167 | + |
| 168 | + const actual = await artifact.computeIntegrityHash(); |
| 169 | + |
| 170 | + expect(expected).toEqual(actual); |
| 171 | + expect(computeIntegrityHash).toHaveBeenCalledWith(artifact.diskPath); |
| 172 | + }); |
| 173 | + }); |
| 174 | + |
| 175 | + describe('cleanup', () => { |
| 176 | + test('removed the stored file', async () => { |
| 177 | + const artifact = new Artifact(ARTIFACT_URL); |
| 178 | + await artifact.download(); |
| 179 | + const diskPath = artifact.diskPath; |
| 180 | + artifact.cleanup(); |
| 181 | + |
| 182 | + expect(fs.rmSync).toHaveBeenCalledWith(diskPath, { force: true }); |
| 183 | + }); |
| 184 | + |
| 185 | + test('removes the diskPath', async () => { |
| 186 | + const artifact = new Artifact(ARTIFACT_URL); |
| 187 | + await artifact.download(); |
| 188 | + artifact.cleanup(); |
| 189 | + |
| 190 | + expect(() => artifact.diskPath).toThrowWithMessage( |
| 191 | + Error, |
| 192 | + `The artifact ${ARTIFACT_URL} has not been downloaded yet` |
| 193 | + ); |
| 194 | + }); |
| 195 | + }); |
| 196 | +}); |
0 commit comments