1
0
Fork 0
pull/1036/head
Deepak Dahiya 2022-03-31 10:41:54 +00:00 committed by GitHub
parent bda035c74d
commit daa24d7958
4 changed files with 51 additions and 25 deletions

View File

@ -6,7 +6,10 @@ import * as cacheUtils from '../src/internal/cacheUtils'
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 {ITypedResponse} from '@actions/http-client/interfaces' import {ITypedResponse} from '@actions/http-client/interfaces'
import {ReserveCacheResponse, ITypedResponseWithErrorMessage} from '../src/internal/contracts' import {
ReserveCacheResponse,
ITypedResponseWithErrorMessage
} from '../src/internal/contracts'
jest.mock('../src/internal/cacheHttpClient') jest.mock('../src/internal/cacheHttpClient')
jest.mock('../src/internal/cacheUtils') jest.mock('../src/internal/cacheUtils')
@ -60,8 +63,13 @@ test('save with large cache outputs should fail', async () => {
const reserveCacheMock = jest const reserveCacheMock = jest
.spyOn(cacheHttpClient, 'reserveCache') .spyOn(cacheHttpClient, 'reserveCache')
.mockImplementation(async () => { .mockImplementation(async () => {
let response: ITypedResponseWithErrorMessage<ReserveCacheResponse> = {statusCode:400, result: null, headers:{}, typeKey:"InvalidReserveCacheRequestException"} const response: ITypedResponseWithErrorMessage<ReserveCacheResponse> = {
return response statusCode: 400,
result: null,
headers: {},
typeKey: 'InvalidReserveCacheRequestException'
}
return response
}) })
await expect(saveCache([filePath], primaryKey)).rejects.toThrowError( await expect(saveCache([filePath], primaryKey)).rejects.toThrowError(
@ -86,8 +94,12 @@ test('save with reserve cache failure should fail', async () => {
const reserveCacheMock = jest const reserveCacheMock = jest
.spyOn(cacheHttpClient, 'reserveCache') .spyOn(cacheHttpClient, 'reserveCache')
.mockImplementation(async () => { .mockImplementation(async () => {
let response: ITypedResponseWithErrorMessage<ReserveCacheResponse> = {statusCode:500, result: null, headers:{}} const response: ITypedResponseWithErrorMessage<ReserveCacheResponse> = {
return response statusCode: 500,
result: null,
headers: {}
}
return response
}) })
const createTarMock = jest.spyOn(tar, 'createTar') const createTarMock = jest.spyOn(tar, 'createTar')
@ -118,8 +130,12 @@ test('save with server error should fail', async () => {
const reserveCacheMock = jest const reserveCacheMock = jest
.spyOn(cacheHttpClient, 'reserveCache') .spyOn(cacheHttpClient, 'reserveCache')
.mockImplementation(async () => { .mockImplementation(async () => {
let response: ITypedResponse<ReserveCacheResponse> = {statusCode:500, result: {cacheId}, headers:{}} const response: ITypedResponse<ReserveCacheResponse> = {
return response statusCode: 500,
result: {cacheId},
headers: {}
}
return response
}) })
const createTarMock = jest.spyOn(tar, 'createTar') const createTarMock = jest.spyOn(tar, 'createTar')
@ -166,8 +182,12 @@ test('save with valid inputs uploads a cache', async () => {
const reserveCacheMock = jest const reserveCacheMock = jest
.spyOn(cacheHttpClient, 'reserveCache') .spyOn(cacheHttpClient, 'reserveCache')
.mockImplementation(async () => { .mockImplementation(async () => {
let response: ITypedResponse<ReserveCacheResponse> = {statusCode:500, result: {cacheId}, headers:{}} const response: ITypedResponse<ReserveCacheResponse> = {
return response statusCode: 500,
result: {cacheId},
headers: {}
}
return response
}) })
const createTarMock = jest.spyOn(tar, 'createTar') const createTarMock = jest.spyOn(tar, 'createTar')

View File

@ -177,21 +177,30 @@ export async function saveCache(
const cacheSize = archiveFileSize const cacheSize = archiveFileSize
core.debug('Reserving Cache') core.debug('Reserving Cache')
let reserveCacheResponse = await cacheHttpClient.reserveCache(key, paths, { const reserveCacheResponse = await cacheHttpClient.reserveCache(
compressionMethod, key,
cacheSize paths,
}) {
compressionMethod,
cacheSize
}
)
if(reserveCacheResponse?.statusCode === 400 && reserveCacheResponse?.typeKey === "InvalidReserveCacheRequestException"){ if (
reserveCacheResponse?.statusCode === 400 &&
reserveCacheResponse?.typeKey === 'InvalidReserveCacheRequestException'
) {
throw new ReserveCacheError( throw new ReserveCacheError(
reserveCacheResponse?.message ?? reserveCacheResponse?.message ??
`Cache size of ~${Math.round(archiveFileSize / (1024 * 1024))} MB (${archiveFileSize} B) is over the data cap limit, not saving cache.` `Cache size of ~${Math.round(
archiveFileSize / (1024 * 1024)
)} MB (${archiveFileSize} B) is over the data cap limit, not saving cache.`
) )
} }
if(reserveCacheResponse?.result?.cacheId){ if (reserveCacheResponse?.result?.cacheId) {
cacheId = reserveCacheResponse?.result?.cacheId cacheId = reserveCacheResponse?.result?.cacheId
}else{ } else {
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.`
) )

View File

@ -1,7 +1,5 @@
import {CompressionMethod} from './constants' import {CompressionMethod} from './constants'
import { import {ITypedResponse} from '@actions/http-client/interfaces'
ITypedResponse
} from '@actions/http-client/interfaces'
export interface ITypedResponseWithErrorMessage<T> extends ITypedResponse<T> { export interface ITypedResponseWithErrorMessage<T> extends ITypedResponse<T> {
message?: string message?: string

View File

@ -1,8 +1,7 @@
import * as core from '@actions/core' import * as core from '@actions/core'
import {HttpCodes, HttpClientError} from '@actions/http-client' import {HttpCodes, HttpClientError} from '@actions/http-client'
import { import {
IHttpClientResponse, IHttpClientResponse
ITypedResponse
} from '@actions/http-client/interfaces' } from '@actions/http-client/interfaces'
import {DefaultRetryDelay, DefaultRetryAttempts} from './constants' import {DefaultRetryDelay, DefaultRetryAttempts} from './constants'
import {ITypedResponseWithErrorMessage} from './contracts' import {ITypedResponseWithErrorMessage} from './contracts'
@ -113,8 +112,8 @@ export async function retryTypedResponse<T>(
statusCode: error.statusCode, statusCode: error.statusCode,
result: null, result: null,
headers: {}, headers: {},
message:error.message, message: error.message,
typeKey:error.result.typeKey typeKey: error.result.typeKey
} }
} else { } else {
return undefined return undefined