1
0
Fork 0

Merge pull request #1966 from actions/robherley/wrap-create-cache-err

cache: wrap create failures in ReserveCacheError
pull/1968/head
Rob Herley 2025-02-25 15:00:25 -05:00 committed by GitHub
commit 1b9063ee0e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 47 additions and 6 deletions

View File

@ -92,14 +92,14 @@ test('save with large cache outputs should fail using', async () => {
expect(getCompressionMock).toHaveBeenCalledTimes(1) expect(getCompressionMock).toHaveBeenCalledTimes(1)
}) })
test('create cache entry failure', async () => { test('create cache entry failure on non-ok response', async () => {
const paths = ['node_modules'] const paths = ['node_modules']
const key = 'Linux-node-bb828da54c148048dd17899ba9fda624811cfb43' const key = 'Linux-node-bb828da54c148048dd17899ba9fda624811cfb43'
const infoLogMock = jest.spyOn(core, 'info') const infoLogMock = jest.spyOn(core, 'info')
const createCacheEntryMock = jest const createCacheEntryMock = jest
.spyOn(CacheServiceClientJSON.prototype, 'CreateCacheEntry') .spyOn(CacheServiceClientJSON.prototype, 'CreateCacheEntry')
.mockReturnValue(Promise.resolve({ok: false, signedUploadUrl: ''})) .mockResolvedValue({ok: false, signedUploadUrl: ''})
const createTarMock = jest.spyOn(tar, 'createTar') const createTarMock = jest.spyOn(tar, 'createTar')
const finalizeCacheEntryMock = jest.spyOn( const finalizeCacheEntryMock = jest.spyOn(
@ -109,7 +109,7 @@ test('create cache entry failure', async () => {
const compression = CompressionMethod.Zstd const compression = CompressionMethod.Zstd
const getCompressionMock = jest const getCompressionMock = jest
.spyOn(cacheUtils, 'getCompressionMethod') .spyOn(cacheUtils, 'getCompressionMethod')
.mockReturnValueOnce(Promise.resolve(compression)) .mockResolvedValueOnce(compression)
const archiveFileSize = 1024 const archiveFileSize = 1024
jest jest
.spyOn(cacheUtils, 'getArchiveFileSizeInBytes') .spyOn(cacheUtils, 'getArchiveFileSizeInBytes')
@ -133,6 +133,39 @@ test('create cache entry failure', async () => {
expect(saveCacheMock).toHaveBeenCalledTimes(0) expect(saveCacheMock).toHaveBeenCalledTimes(0)
}) })
test('create cache entry fails on rejected promise', async () => {
const paths = ['node_modules']
const key = 'Linux-node-bb828da54c148048dd17899ba9fda624811cfb43'
const infoLogMock = jest.spyOn(core, 'info')
const createCacheEntryMock = jest
.spyOn(CacheServiceClientJSON.prototype, 'CreateCacheEntry')
.mockRejectedValue(new Error('Failed to create cache entry'))
const createTarMock = jest.spyOn(tar, 'createTar')
const compression = CompressionMethod.Zstd
const getCompressionMock = jest
.spyOn(cacheUtils, 'getCompressionMethod')
.mockResolvedValueOnce(compression)
const archiveFileSize = 1024
jest
.spyOn(cacheUtils, 'getArchiveFileSizeInBytes')
.mockReturnValueOnce(archiveFileSize)
const cacheId = await saveCache(paths, key)
expect(cacheId).toBe(-1)
expect(infoLogMock).toHaveBeenCalledWith(
`Failed to save: Unable to reserve cache with key ${key}, another job may be creating this cache.`
)
expect(createCacheEntryMock).toHaveBeenCalledWith({
key,
version: cacheUtils.getCacheVersion(paths, compression)
})
expect(createTarMock).toHaveBeenCalledTimes(1)
expect(getCompressionMock).toHaveBeenCalledTimes(1)
})
test('save cache fails if a signedUploadURL was not passed', async () => { test('save cache fails if a signedUploadURL was not passed', async () => {
const paths = 'node_modules' const paths = 'node_modules'
const key = 'Linux-node-bb828da54c148048dd17899ba9fda624811cfb43' const key = 'Linux-node-bb828da54c148048dd17899ba9fda624811cfb43'

View File

@ -525,8 +525,16 @@ async function saveCacheV2(
version version
} }
const response = await twirpClient.CreateCacheEntry(request) let signedUploadUrl
if (!response.ok) {
try {
const response = await twirpClient.CreateCacheEntry(request)
if (!response.ok) {
throw new Error('Response was not ok')
}
signedUploadUrl = response.signedUploadUrl
} catch (error) {
core.debug(`Failed to reserve cache: ${error}`)
throw new ReserveCacheError( throw new ReserveCacheError(
`Unable to reserve cache with key ${key}, another job may be creating this cache.` `Unable to reserve cache with key ${key}, another job may be creating this cache.`
) )
@ -536,7 +544,7 @@ async function saveCacheV2(
await cacheHttpClient.saveCache( await cacheHttpClient.saveCache(
cacheId, cacheId,
archivePath, archivePath,
response.signedUploadUrl, signedUploadUrl,
options options
) )