1
0
Fork 0

Add cache restore dryRun option

pull/1286/head
Marc Mueller 2022-12-23 12:44:35 +01:00
parent 0db3029fcf
commit eb06c21794
4 changed files with 65 additions and 4 deletions

View File

@ -9,6 +9,7 @@ const useAzureSdk = true
const downloadConcurrency = 8 const downloadConcurrency = 8
const timeoutInMs = 30000 const timeoutInMs = 30000
const segmentTimeoutInMs = 3600000 const segmentTimeoutInMs = 3600000
const dryRun = false
const uploadConcurrency = 4 const uploadConcurrency = 4
const uploadChunkSize = 32 * 1024 * 1024 const uploadChunkSize = 32 * 1024 * 1024
@ -19,7 +20,8 @@ test('getDownloadOptions sets defaults', async () => {
useAzureSdk, useAzureSdk,
downloadConcurrency, downloadConcurrency,
timeoutInMs, timeoutInMs,
segmentTimeoutInMs segmentTimeoutInMs,
dryRun
}) })
}) })
@ -28,7 +30,8 @@ test('getDownloadOptions overrides all settings', async () => {
useAzureSdk: false, useAzureSdk: false,
downloadConcurrency: 14, downloadConcurrency: 14,
timeoutInMs: 20000, timeoutInMs: 20000,
segmentTimeoutInMs: 3600000 segmentTimeoutInMs: 3600000,
dryRun: true
} }
const actualOptions = getDownloadOptions(expectedOptions) const actualOptions = getDownloadOptions(expectedOptions)
@ -61,7 +64,8 @@ test('getDownloadOptions overrides download timeout minutes', async () => {
useAzureSdk: false, useAzureSdk: false,
downloadConcurrency: 14, downloadConcurrency: 14,
timeoutInMs: 20000, timeoutInMs: 20000,
segmentTimeoutInMs: 3600000 segmentTimeoutInMs: 3600000,
dryRun: true
} }
process.env.SEGMENT_DOWNLOAD_TIMEOUT_MINS = '10' process.env.SEGMENT_DOWNLOAD_TIMEOUT_MINS = '10'
const actualOptions = getDownloadOptions(expectedOptions) const actualOptions = getDownloadOptions(expectedOptions)
@ -72,4 +76,5 @@ test('getDownloadOptions overrides download timeout minutes', async () => {
) )
expect(actualOptions.timeoutInMs).toEqual(expectedOptions.timeoutInMs) expect(actualOptions.timeoutInMs).toEqual(expectedOptions.timeoutInMs)
expect(actualOptions.segmentTimeoutInMs).toEqual(600000) expect(actualOptions.segmentTimeoutInMs).toEqual(600000)
expect(actualOptions.dryRun).toEqual(expectedOptions.dryRun)
}) })

View File

@ -276,3 +276,39 @@ test('restore with cache found for restore key', async () => {
expect(extractTarMock).toHaveBeenCalledWith(archivePath, compression) expect(extractTarMock).toHaveBeenCalledWith(archivePath, compression)
expect(getCompressionMock).toHaveBeenCalledTimes(1) expect(getCompressionMock).toHaveBeenCalledTimes(1)
}) })
test('restore with dry run', async () => {
const paths = ['node_modules']
const key = 'node-test'
const options = {dryRun: true}
const cacheEntry: ArtifactCacheEntry = {
cacheKey: key,
scope: 'refs/heads/main',
archiveLocation: 'www.actionscache.test/download'
}
const getCacheMock = jest.spyOn(cacheHttpClient, 'getCacheEntry')
getCacheMock.mockImplementation(async () => {
return Promise.resolve(cacheEntry)
})
const createTempDirectoryMock = jest.spyOn(cacheUtils, 'createTempDirectory')
const downloadCacheMock = jest.spyOn(cacheHttpClient, 'downloadCache')
const compression = CompressionMethod.Gzip
const getCompressionMock = jest
.spyOn(cacheUtils, 'getCompressionMethod')
.mockReturnValue(Promise.resolve(compression))
const cacheKey = await restoreCache(paths, key, undefined, options)
expect(cacheKey).toBe(key)
expect(getCompressionMock).toHaveBeenCalledTimes(1)
expect(getCacheMock).toHaveBeenCalledWith([key], paths, {
compressionMethod: compression,
enableCrossOsArchive: false
})
// creating a tempDir and downloading the cache are skipped
expect(createTempDirectoryMock).toHaveBeenCalledTimes(0)
expect(downloadCacheMock).toHaveBeenCalledTimes(0)
})

View File

@ -100,6 +100,11 @@ export async function restoreCache(
return undefined return undefined
} }
if (options?.dryRun) {
core.info('Dry run - skipping download')
return cacheEntry.cacheKey
}
archivePath = path.join( archivePath = path.join(
await utils.createTempDirectory(), await utils.createTempDirectory(),
utils.getCacheFileName(compressionMethod) utils.getCacheFileName(compressionMethod)

View File

@ -53,6 +53,15 @@ export interface DownloadOptions {
* @default 3600000 * @default 3600000
*/ */
segmentTimeoutInMs?: number segmentTimeoutInMs?: number
/**
* Weather to skip downloading the cache entry.
* If dryRun is set to true, the restore function will only check if
* a matching cache entry exists and return the cache key if it does.
*
* @default false
*/
dryRun?: boolean
} }
/** /**
@ -92,7 +101,8 @@ export function getDownloadOptions(copy?: DownloadOptions): DownloadOptions {
useAzureSdk: true, useAzureSdk: true,
downloadConcurrency: 8, downloadConcurrency: 8,
timeoutInMs: 30000, timeoutInMs: 30000,
segmentTimeoutInMs: 3600000 segmentTimeoutInMs: 3600000,
dryRun: false
} }
if (copy) { if (copy) {
@ -111,6 +121,10 @@ export function getDownloadOptions(copy?: DownloadOptions): DownloadOptions {
if (typeof copy.segmentTimeoutInMs === 'number') { if (typeof copy.segmentTimeoutInMs === 'number') {
result.segmentTimeoutInMs = copy.segmentTimeoutInMs result.segmentTimeoutInMs = copy.segmentTimeoutInMs
} }
if (typeof copy.dryRun === 'boolean') {
result.dryRun = copy.dryRun
}
} }
const segmentDownloadTimeoutMins = const segmentDownloadTimeoutMins =
process.env['SEGMENT_DOWNLOAD_TIMEOUT_MINS'] process.env['SEGMENT_DOWNLOAD_TIMEOUT_MINS']
@ -129,6 +143,7 @@ export function getDownloadOptions(copy?: DownloadOptions): DownloadOptions {
`Cache segment download timeout mins env var: ${process.env['SEGMENT_DOWNLOAD_TIMEOUT_MINS']}` `Cache segment download timeout mins env var: ${process.env['SEGMENT_DOWNLOAD_TIMEOUT_MINS']}`
) )
core.debug(`Segment download timeout (ms): ${result.segmentTimeoutInMs}`) core.debug(`Segment download timeout (ms): ${result.segmentTimeoutInMs}`)
core.debug(`Dry run: ${result.dryRun}`)
return result return result
} }