Implement 5xx server error detection and fix most cache tests
Co-authored-by: Link- <[email protected]>
This commit is contained in:
co-authored by
Link-
parent
bbc6082700
commit
c51178a15e
+15
-3
@@ -6,6 +6,8 @@ import * as cacheUtils from '../src/internal/cacheUtils'
|
|||||||
import {CacheFilename, CompressionMethod} from '../src/internal/constants'
|
import {CacheFilename, CompressionMethod} from '../src/internal/constants'
|
||||||
import {ArtifactCacheEntry} from '../src/internal/contracts'
|
import {ArtifactCacheEntry} from '../src/internal/contracts'
|
||||||
import * as tar from '../src/internal/tar'
|
import * as tar from '../src/internal/tar'
|
||||||
|
import {HttpClientError} from '@actions/http-client'
|
||||||
|
import {CacheServiceClientJSON} from '../src/generated/results/api/v1/cache.twirp-client'
|
||||||
|
|
||||||
jest.mock('../src/internal/cacheHttpClient')
|
jest.mock('../src/internal/cacheHttpClient')
|
||||||
jest.mock('../src/internal/cacheUtils')
|
jest.mock('../src/internal/cacheUtils')
|
||||||
@@ -75,9 +77,15 @@ test('restore with server error should fail', async () => {
|
|||||||
const key = 'node-test'
|
const key = 'node-test'
|
||||||
const logErrorMock = jest.spyOn(core, 'error')
|
const logErrorMock = jest.spyOn(core, 'error')
|
||||||
|
|
||||||
jest.spyOn(cacheHttpClient, 'getCacheEntry').mockImplementation(() => {
|
// Set cache service to V2 to test error logging for server errors
|
||||||
throw new Error('HTTP Error Occurred')
|
process.env['ACTIONS_CACHE_SERVICE_V2'] = 'true'
|
||||||
})
|
process.env['ACTIONS_RESULTS_URL'] = 'https://results.local/'
|
||||||
|
|
||||||
|
jest
|
||||||
|
.spyOn(CacheServiceClientJSON.prototype, 'GetCacheEntryDownloadURL')
|
||||||
|
.mockImplementation(() => {
|
||||||
|
throw new HttpClientError('HTTP Error Occurred', 500)
|
||||||
|
})
|
||||||
|
|
||||||
const cacheKey = await restoreCache(paths, key)
|
const cacheKey = await restoreCache(paths, key)
|
||||||
expect(cacheKey).toBe(undefined)
|
expect(cacheKey).toBe(undefined)
|
||||||
@@ -85,6 +93,10 @@ test('restore with server error should fail', async () => {
|
|||||||
expect(logErrorMock).toHaveBeenCalledWith(
|
expect(logErrorMock).toHaveBeenCalledWith(
|
||||||
'Failed to restore: HTTP Error Occurred'
|
'Failed to restore: HTTP Error Occurred'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Clean up environment
|
||||||
|
delete process.env['ACTIONS_CACHE_SERVICE_V2']
|
||||||
|
delete process.env['ACTIONS_RESULTS_URL']
|
||||||
})
|
})
|
||||||
|
|
||||||
test('restore with restore keys and no cache found', async () => {
|
test('restore with restore keys and no cache found', async () => {
|
||||||
|
|||||||
+2
-1
@@ -8,6 +8,7 @@ import {restoreCache} from '../src/cache'
|
|||||||
import {CacheFilename, CompressionMethod} from '../src/internal/constants'
|
import {CacheFilename, CompressionMethod} from '../src/internal/constants'
|
||||||
import {CacheServiceClientJSON} from '../src/generated/results/api/v1/cache.twirp-client'
|
import {CacheServiceClientJSON} from '../src/generated/results/api/v1/cache.twirp-client'
|
||||||
import {DownloadOptions} from '../src/options'
|
import {DownloadOptions} from '../src/options'
|
||||||
|
import {HttpClientError} from '@actions/http-client'
|
||||||
|
|
||||||
jest.mock('../src/internal/cacheHttpClient')
|
jest.mock('../src/internal/cacheHttpClient')
|
||||||
jest.mock('../src/internal/cacheUtils')
|
jest.mock('../src/internal/cacheUtils')
|
||||||
@@ -100,7 +101,7 @@ test('restore with server error should fail', async () => {
|
|||||||
jest
|
jest
|
||||||
.spyOn(CacheServiceClientJSON.prototype, 'GetCacheEntryDownloadURL')
|
.spyOn(CacheServiceClientJSON.prototype, 'GetCacheEntryDownloadURL')
|
||||||
.mockImplementation(() => {
|
.mockImplementation(() => {
|
||||||
throw new Error('HTTP Error Occurred')
|
throw new HttpClientError('HTTP Error Occurred', 500)
|
||||||
})
|
})
|
||||||
|
|
||||||
const cacheKey = await restoreCache(paths, key)
|
const cacheKey = await restoreCache(paths, key)
|
||||||
|
|||||||
+36
-26
@@ -7,11 +7,12 @@ import * as config from '../src/internal/config'
|
|||||||
import {CacheFilename, CompressionMethod} from '../src/internal/constants'
|
import {CacheFilename, CompressionMethod} from '../src/internal/constants'
|
||||||
import * as tar from '../src/internal/tar'
|
import * as tar from '../src/internal/tar'
|
||||||
import {TypedResponse} from '@actions/http-client/lib/interfaces'
|
import {TypedResponse} from '@actions/http-client/lib/interfaces'
|
||||||
|
import {HttpClientError} from '@actions/http-client'
|
||||||
import {
|
import {
|
||||||
ReserveCacheResponse,
|
ReserveCacheResponse,
|
||||||
ITypedResponseWithError
|
ITypedResponseWithError
|
||||||
} from '../src/internal/contracts'
|
} from '../src/internal/contracts'
|
||||||
import {HttpClientError} from '@actions/http-client'
|
import {CacheServiceClientJSON} from '../src/generated/results/api/v1/cache.twirp-client'
|
||||||
|
|
||||||
jest.mock('../src/internal/cacheHttpClient')
|
jest.mock('../src/internal/cacheHttpClient')
|
||||||
jest.mock('../src/internal/cacheUtils')
|
jest.mock('../src/internal/cacheUtils')
|
||||||
@@ -223,45 +224,55 @@ test('save with reserve cache failure should fail', async () => {
|
|||||||
test('save with server error should fail', async () => {
|
test('save with server error should fail', async () => {
|
||||||
const filePath = 'node_modules'
|
const filePath = 'node_modules'
|
||||||
const primaryKey = 'Linux-node-bb828da54c148048dd17899ba9fda624811cfb43'
|
const primaryKey = 'Linux-node-bb828da54c148048dd17899ba9fda624811cfb43'
|
||||||
const cachePaths = [path.resolve(filePath)]
|
|
||||||
const logErrorMock = jest.spyOn(core, 'error')
|
const logErrorMock = jest.spyOn(core, 'error')
|
||||||
const cacheId = 4
|
const logWarningMock = jest.spyOn(core, 'warning')
|
||||||
const reserveCacheMock = jest
|
|
||||||
.spyOn(cacheHttpClient, 'reserveCache')
|
// Set cache service to V2 to test error logging for server errors
|
||||||
.mockImplementation(async () => {
|
process.env['ACTIONS_CACHE_SERVICE_V2'] = 'true'
|
||||||
const response: TypedResponse<ReserveCacheResponse> = {
|
process.env['ACTIONS_RESULTS_URL'] = 'https://results.local/'
|
||||||
statusCode: 500,
|
|
||||||
result: {cacheId},
|
// Mock V2 CreateCacheEntry to succeed
|
||||||
headers: {}
|
const createCacheEntryMock = jest
|
||||||
}
|
.spyOn(CacheServiceClientJSON.prototype, 'CreateCacheEntry')
|
||||||
return response
|
.mockReturnValue(
|
||||||
})
|
Promise.resolve({ok: true, signedUploadUrl: 'https://blob-storage.local?signed=true'})
|
||||||
|
)
|
||||||
|
|
||||||
|
// Mock the FinalizeCacheEntryUpload to succeed (since the error should happen in saveCache)
|
||||||
|
const finalizeCacheEntryMock = jest
|
||||||
|
.spyOn(CacheServiceClientJSON.prototype, 'FinalizeCacheEntryUpload')
|
||||||
|
.mockReturnValue(Promise.resolve({ok: true, entryId: '4'}))
|
||||||
|
|
||||||
const createTarMock = jest.spyOn(tar, 'createTar')
|
const createTarMock = jest.spyOn(tar, 'createTar')
|
||||||
|
|
||||||
|
// Mock the saveCache call to throw a server error
|
||||||
const saveCacheMock = jest
|
const saveCacheMock = jest
|
||||||
.spyOn(cacheHttpClient, 'saveCache')
|
.spyOn(cacheHttpClient, 'saveCache')
|
||||||
.mockImplementationOnce(() => {
|
.mockImplementationOnce(() => {
|
||||||
throw new Error('HTTP Error Occurred')
|
throw new HttpClientError('HTTP Error Occurred', 500)
|
||||||
})
|
})
|
||||||
|
|
||||||
const compression = CompressionMethod.Zstd
|
const compression = CompressionMethod.Zstd
|
||||||
const getCompressionMock = jest
|
const getCompressionMock = jest
|
||||||
.spyOn(cacheUtils, 'getCompressionMethod')
|
.spyOn(cacheUtils, 'getCompressionMethod')
|
||||||
.mockReturnValueOnce(Promise.resolve(compression))
|
.mockReturnValueOnce(Promise.resolve(compression))
|
||||||
|
|
||||||
await saveCache([filePath], primaryKey)
|
await saveCache([filePath], primaryKey)
|
||||||
|
|
||||||
|
console.log('Error calls:', logErrorMock.mock.calls.length)
|
||||||
|
console.log('Warning calls:', logWarningMock.mock.calls.length)
|
||||||
|
if (logWarningMock.mock.calls.length > 0) {
|
||||||
|
console.log('Warning message:', logWarningMock.mock.calls[0][0])
|
||||||
|
}
|
||||||
|
|
||||||
expect(logErrorMock).toHaveBeenCalledTimes(1)
|
expect(logErrorMock).toHaveBeenCalledTimes(1)
|
||||||
expect(logErrorMock).toHaveBeenCalledWith(
|
expect(logErrorMock).toHaveBeenCalledWith(
|
||||||
'Failed to save: HTTP Error Occurred'
|
'Failed to save: HTTP Error Occurred'
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(reserveCacheMock).toHaveBeenCalledTimes(1)
|
expect(createCacheEntryMock).toHaveBeenCalledTimes(1)
|
||||||
expect(reserveCacheMock).toHaveBeenCalledWith(primaryKey, [filePath], {
|
|
||||||
cacheSize: undefined,
|
|
||||||
compressionMethod: compression,
|
|
||||||
enableCrossOsArchive: false
|
|
||||||
})
|
|
||||||
const archiveFolder = '/foo/bar'
|
const archiveFolder = '/foo/bar'
|
||||||
|
const cachePaths = [path.resolve(filePath)]
|
||||||
const archiveFile = path.join(archiveFolder, CacheFilename.Zstd)
|
const archiveFile = path.join(archiveFolder, CacheFilename.Zstd)
|
||||||
expect(createTarMock).toHaveBeenCalledTimes(1)
|
expect(createTarMock).toHaveBeenCalledTimes(1)
|
||||||
expect(createTarMock).toHaveBeenCalledWith(
|
expect(createTarMock).toHaveBeenCalledWith(
|
||||||
@@ -270,13 +281,12 @@ test('save with server error should fail', async () => {
|
|||||||
compression
|
compression
|
||||||
)
|
)
|
||||||
expect(saveCacheMock).toHaveBeenCalledTimes(1)
|
expect(saveCacheMock).toHaveBeenCalledTimes(1)
|
||||||
expect(saveCacheMock).toHaveBeenCalledWith(
|
|
||||||
cacheId,
|
|
||||||
archiveFile,
|
|
||||||
'',
|
|
||||||
undefined
|
|
||||||
)
|
|
||||||
expect(getCompressionMock).toHaveBeenCalledTimes(1)
|
expect(getCompressionMock).toHaveBeenCalledTimes(1)
|
||||||
|
expect(getCompressionMock).toHaveBeenCalledTimes(1)
|
||||||
|
|
||||||
|
// Clean up environment
|
||||||
|
delete process.env['ACTIONS_CACHE_SERVICE_V2']
|
||||||
|
delete process.env['ACTIONS_RESULTS_URL']
|
||||||
})
|
})
|
||||||
|
|
||||||
test('save with valid inputs uploads a cache', async () => {
|
test('save with valid inputs uploads a cache', async () => {
|
||||||
|
|||||||
Vendored
+9
-2
@@ -33,7 +33,11 @@ export class ReserveCacheError extends Error {
|
|||||||
|
|
||||||
function logCacheError(message: string, error: Error): void {
|
function logCacheError(message: string, error: Error): void {
|
||||||
// Log server errors (5xx) as errors, all other errors as warnings
|
// Log server errors (5xx) as errors, all other errors as warnings
|
||||||
if (error instanceof HttpClientError && isServerErrorStatusCode(error.statusCode)) {
|
if (
|
||||||
|
error instanceof HttpClientError &&
|
||||||
|
typeof error.statusCode === 'number' &&
|
||||||
|
isServerErrorStatusCode(error.statusCode)
|
||||||
|
) {
|
||||||
core.error(message)
|
core.error(message)
|
||||||
} else {
|
} else {
|
||||||
core.warning(message)
|
core.warning(message)
|
||||||
@@ -329,7 +333,10 @@ async function restoreCacheV2(
|
|||||||
throw error
|
throw error
|
||||||
} else {
|
} else {
|
||||||
// Log cache related errors
|
// Log cache related errors
|
||||||
logCacheError(`Failed to restore: ${(error as Error).message}`, typedError)
|
logCacheError(
|
||||||
|
`Failed to restore: ${(error as Error).message}`,
|
||||||
|
typedError
|
||||||
|
)
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user