1
0
Fork 0

Refactor cache upload functionality and improve test cases

pull/1882/head
Bassem Dghaidi 2024-11-28 03:52:09 -08:00 committed by GitHub
parent c5a5de05f6
commit df166709a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 44 additions and 43 deletions

View File

@ -6,7 +6,7 @@ import {CacheFilename, CompressionMethod} from '../src/internal/constants'
import * as config from '../src/internal/config' import * as config from '../src/internal/config'
import * as tar from '../src/internal/tar' import * as tar from '../src/internal/tar'
import { CacheServiceClientJSON } from '../src/generated/results/api/v1/cache.twirp' import { CacheServiceClientJSON } from '../src/generated/results/api/v1/cache.twirp'
import * as uploadCacheModule from '../src/internal/blob/upload-cache' import * as uploadCacheModule from '../src/internal/uploadUtils'
import { BlobUploadCommonResponse } from '@azure/storage-blob' import { BlobUploadCommonResponse } from '@azure/storage-blob'
import { InvalidResponseError } from '../src/internal/shared/errors' import { InvalidResponseError } from '../src/internal/shared/errors'
@ -116,8 +116,8 @@ test('create cache entry failure', async () => {
.spyOn(cacheUtils, 'getArchiveFileSizeInBytes') .spyOn(cacheUtils, 'getArchiveFileSizeInBytes')
.mockReturnValueOnce(archiveFileSize) .mockReturnValueOnce(archiveFileSize)
const cacheVersion = cacheUtils.getCacheVersion(paths, compression) const cacheVersion = cacheUtils.getCacheVersion(paths, compression)
const uploadCacheFileMock = jest const uploadCacheArchiveSDKMock = jest
.spyOn(uploadCacheModule, 'uploadCacheFile') .spyOn(uploadCacheModule, 'uploadCacheArchiveSDK')
.mockReturnValueOnce( .mockReturnValueOnce(
Promise.resolve({ Promise.resolve({
_response: { _response: {
@ -139,7 +139,7 @@ test('create cache entry failure', async () => {
expect(createTarMock).toHaveBeenCalledTimes(1) expect(createTarMock).toHaveBeenCalledTimes(1)
expect(getCompressionMock).toHaveBeenCalledTimes(1) expect(getCompressionMock).toHaveBeenCalledTimes(1)
expect(finalizeCacheEntryMock).toHaveBeenCalledTimes(0) expect(finalizeCacheEntryMock).toHaveBeenCalledTimes(0)
expect(uploadCacheFileMock).toHaveBeenCalledTimes(0) expect(uploadCacheArchiveSDKMock).toHaveBeenCalledTimes(0)
}) })
test('finalize save cache failure', async () => { test('finalize save cache failure', async () => {
@ -157,7 +157,7 @@ test('finalize save cache failure', async () => {
const createTarMock = jest.spyOn(tar, 'createTar') const createTarMock = jest.spyOn(tar, 'createTar')
const uploadCacheMock = jest.spyOn(uploadCacheModule, 'uploadCacheFile') const uploadCacheMock = jest.spyOn(uploadCacheModule, 'uploadCacheArchiveSDK')
uploadCacheMock.mockReturnValueOnce( uploadCacheMock.mockReturnValueOnce(
Promise.resolve({ Promise.resolve({
_response: { _response: {
@ -226,7 +226,7 @@ test('save with uploadCache Server error will fail', async () => {
.spyOn(cacheUtils, 'getArchiveFileSizeInBytes') .spyOn(cacheUtils, 'getArchiveFileSizeInBytes')
.mockReturnValueOnce(archiveFileSize) .mockReturnValueOnce(archiveFileSize)
jest jest
.spyOn(uploadCacheModule, 'uploadCacheFile') .spyOn(uploadCacheModule, 'uploadCacheArchiveSDK')
.mockRejectedValueOnce(new InvalidResponseError('boom')) .mockRejectedValueOnce(new InvalidResponseError('boom'))
const cacheId = await saveCache([paths], key) const cacheId = await saveCache([paths], key)
@ -248,7 +248,7 @@ test('uploadFile returns 500', async () => {
jest jest
.spyOn(cacheUtils, 'getArchiveFileSizeInBytes') .spyOn(cacheUtils, 'getArchiveFileSizeInBytes')
.mockReturnValueOnce(archiveFileSize) .mockReturnValueOnce(archiveFileSize)
jest.spyOn(uploadCacheModule, 'uploadCacheFile').mockRestore() jest.spyOn(uploadCacheModule, 'uploadCacheArchiveSDK').mockRestore()
uploadFileMock = jest.fn().mockResolvedValueOnce({ uploadFileMock = jest.fn().mockResolvedValueOnce({
_response: { _response: {
@ -283,7 +283,7 @@ test('save with valid inputs uploads a cache', async () => {
) )
const uploadCacheMock = jest const uploadCacheMock = jest
.spyOn(uploadCacheModule, 'uploadCacheFile') .spyOn(uploadCacheModule, 'uploadCacheArchiveSDK')
.mockReturnValueOnce( .mockReturnValueOnce(
Promise.resolve({ Promise.resolve({
_response: { _response: {

View File

@ -14,7 +14,7 @@ import {
GetCacheEntryDownloadURLRequest GetCacheEntryDownloadURLRequest
} from './generated/results/api/v1/cache' } from './generated/results/api/v1/cache'
import { CacheFileSizeLimit } from './internal/constants' import { CacheFileSizeLimit } from './internal/constants'
import {uploadCacheFile} from './internal/blob/upload-cache' import { uploadCacheArchiveSDK } from './internal/uploadUtils'
export class ValidationError extends Error { export class ValidationError extends Error {
constructor(message: string) { constructor(message: string) {
super(message) super(message)
@ -521,7 +521,7 @@ async function saveCacheV2(
} }
core.debug(`Attempting to upload cache located at: ${archivePath}`) core.debug(`Attempting to upload cache located at: ${archivePath}`)
const uploadResponse = await uploadCacheFile( const uploadResponse = await uploadCacheArchiveSDK(
response.signedUploadUrl, response.signedUploadUrl,
archivePath archivePath
) )

View File

@ -5,9 +5,10 @@ import {
BlockBlobClient, BlockBlobClient,
BlockBlobParallelUploadOptions BlockBlobParallelUploadOptions
} from '@azure/storage-blob' } from '@azure/storage-blob'
import {InvalidResponseError} from '../shared/errors' import { InvalidResponseError } from './shared/errors'
export async function uploadCacheFile( export async function uploadCacheArchiveSDK
(
signedUploadURL: string, signedUploadURL: string,
archivePath: string archivePath: string
): Promise<BlobUploadCommonResponse> { ): Promise<BlobUploadCommonResponse> {