1
0
Fork 0

Add more tests for restoreCacheV2

pull/1876/head
Bassem Dghaidi 2024-11-25 03:53:03 -08:00 committed by GitHub
parent 27dfd2c41c
commit 4de30f744e
3 changed files with 216 additions and 179 deletions

View File

@ -3,11 +3,12 @@ import * as path from 'path'
import * as tar from '../src/internal/tar' import * as tar from '../src/internal/tar'
import * as config from '../src/internal/config' import * as config from '../src/internal/config'
import * as cacheUtils from '../src/internal/cacheUtils' import * as cacheUtils from '../src/internal/cacheUtils'
import * as cacheHttpClient from '../src/internal/cacheHttpClient' import * as downloadCacheModule from '../src/internal/blob/download-cache'
import {restoreCache} from '../src/cache' import {restoreCache} from '../src/cache'
import {CacheFilename, CompressionMethod} from '../src/internal/constants' import {CacheFilename, CompressionMethod} from '../src/internal/constants'
import { ArtifactCacheEntry } from '../src/internal/contracts'
import {CacheServiceClientJSON} from '../src/generated/results/api/v1/cache.twirp' import {CacheServiceClientJSON} from '../src/generated/results/api/v1/cache.twirp'
import {BlobDownloadResponseParsed} from '@azure/storage-blob'
// import {executePromisesSequentially} from '@azure/ms-rest-js'
jest.mock('../src/internal/cacheHttpClient') jest.mock('../src/internal/cacheHttpClient')
jest.mock('../src/internal/cacheUtils') jest.mock('../src/internal/cacheUtils')
@ -95,142 +96,177 @@ test('restore with server error should fail', async () => {
) )
}) })
// test('restore with restore keys and no cache found', async () => { test('restore with restore keys and no cache found', async () => {
// const paths = ['node_modules'] const paths = ['node_modules']
// const key = 'node-test' const key = 'node-test'
// const restoreKey = 'node-' const restoreKey = 'node-'
const logWarningMock = jest.spyOn(core, 'warning')
// jest jest
// .spyOn(CacheServiceClientJSON.prototype, 'GetCacheEntryDownloadURL') .spyOn(CacheServiceClientJSON.prototype, 'GetCacheEntryDownloadURL')
// .mockImplementation(() => { .mockReturnValue(Promise.resolve({ok: false, signedDownloadUrl: ''}))
// return Promise.resolve(null)
// })
// jest.spyOn(cacheHttpClient, 'getCacheEntry').mockImplementation(async () => {
// return Promise.resolve(null)
// })
// const cacheKey = await restoreCache(paths, key, [restoreKey]) const cacheKey = await restoreCache(paths, key, [restoreKey])
// expect(cacheKey).toBe(undefined) expect(cacheKey).toBe(undefined)
// }) expect(logWarningMock).toHaveBeenCalledWith(
`Cache not found for keys: ${[key, restoreKey].join(', ')}`
)
})
// test('restore with gzip compressed cache found', async () => { test('restore with gzip compressed cache found', async () => {
// const paths = ['node_modules'] const paths = ['node_modules']
// const key = 'node-test' const key = 'node-test'
const logInfoMock = jest.spyOn(core, 'info')
const compressionMethod = CompressionMethod.Gzip
const signedDownloadUrl = 'https://blob-storage.local?signed=true'
const cacheVersion =
'd90f107aaeb22920dba0c637a23c37b5bc497b4dfa3b07fe3f79bf88a273c11b'
// const cacheEntry: ArtifactCacheEntry = { const getCacheVersionMock = jest.spyOn(cacheUtils, 'getCacheVersion')
// cacheKey: key, getCacheVersionMock.mockReturnValue(cacheVersion)
// scope: 'refs/heads/main',
// archiveLocation: 'www.actionscache.test/download'
// }
// const getCacheMock = jest.spyOn(cacheHttpClient, 'getCacheEntry')
// getCacheMock.mockImplementation(async () => {
// return Promise.resolve(cacheEntry)
// })
// const tempPath = '/foo/bar' const compressionMethodMock = jest.spyOn(cacheUtils, 'getCompressionMethod')
compressionMethodMock.mockReturnValue(Promise.resolve(compressionMethod))
// const createTempDirectoryMock = jest.spyOn(cacheUtils, 'createTempDirectory') const getCacheDownloadURLMock = jest.spyOn(
// createTempDirectoryMock.mockImplementation(async () => { CacheServiceClientJSON.prototype,
// return Promise.resolve(tempPath) 'GetCacheEntryDownloadURL'
// }) )
getCacheDownloadURLMock.mockReturnValue(
Promise.resolve({ok: true, signedDownloadUrl})
)
// const archivePath = path.join(tempPath, CacheFilename.Gzip) const tempPath = '/foo/bar'
// const downloadCacheMock = jest.spyOn(cacheHttpClient, 'downloadCache')
// const fileSize = 142 const createTempDirectoryMock = jest.spyOn(cacheUtils, 'createTempDirectory')
// const getArchiveFileSizeInBytesMock = jest createTempDirectoryMock.mockImplementation(async () => {
// .spyOn(cacheUtils, 'getArchiveFileSizeInBytes') return Promise.resolve(tempPath)
// .mockReturnValue(fileSize) })
// const extractTarMock = jest.spyOn(tar, 'extractTar') const archivePath = path.join(tempPath, CacheFilename.Gzip)
// const unlinkFileMock = jest.spyOn(cacheUtils, 'unlinkFile') const downloadCacheFileMock = jest.spyOn(
downloadCacheModule,
'downloadCacheFile'
)
downloadCacheFileMock.mockReturnValue(
Promise.resolve({} as BlobDownloadResponseParsed)
)
// const compression = CompressionMethod.Gzip const fileSize = 142
// const getCompressionMock = jest const getArchiveFileSizeInBytesMock = jest
// .spyOn(cacheUtils, 'getCompressionMethod') .spyOn(cacheUtils, 'getArchiveFileSizeInBytes')
// .mockReturnValue(Promise.resolve(compression)) .mockReturnValue(fileSize)
// const cacheKey = await restoreCache(paths, key) const extractTarMock = jest.spyOn(tar, 'extractTar')
const unlinkFileMock = jest.spyOn(cacheUtils, 'unlinkFile')
// expect(cacheKey).toBe(key) const cacheKey = await restoreCache(paths, key)
// expect(getCacheMock).toHaveBeenCalledWith([key], paths, {
// compressionMethod: compression,
// enableCrossOsArchive: false
// })
// expect(createTempDirectoryMock).toHaveBeenCalledTimes(1)
// expect(downloadCacheMock).toHaveBeenCalledWith(
// cacheEntry.archiveLocation,
// archivePath,
// undefined
// )
// expect(getArchiveFileSizeInBytesMock).toHaveBeenCalledWith(archivePath)
// expect(extractTarMock).toHaveBeenCalledTimes(1) expect(cacheKey).toBe(key)
// expect(extractTarMock).toHaveBeenCalledWith(archivePath, compression) expect(getCacheVersionMock).toHaveBeenCalledWith(
paths,
compressionMethod,
false
)
expect(getCacheDownloadURLMock).toHaveBeenCalledWith({
key,
restoreKeys: [],
version: cacheVersion
})
expect(createTempDirectoryMock).toHaveBeenCalledTimes(1)
expect(downloadCacheFileMock).toHaveBeenCalledWith(
signedDownloadUrl,
archivePath
)
expect(getArchiveFileSizeInBytesMock).toHaveBeenCalledWith(archivePath)
expect(logInfoMock).toHaveBeenCalledWith(`Cache Size: ~0 MB (142 B)`)
// expect(unlinkFileMock).toHaveBeenCalledTimes(1) expect(extractTarMock).toHaveBeenCalledTimes(1)
// expect(unlinkFileMock).toHaveBeenCalledWith(archivePath) expect(extractTarMock).toHaveBeenCalledWith(archivePath, compressionMethod)
// expect(getCompressionMock).toHaveBeenCalledTimes(1) expect(unlinkFileMock).toHaveBeenCalledTimes(1)
// }) expect(unlinkFileMock).toHaveBeenCalledWith(archivePath)
// test('restore with zstd compressed cache found', async () => { expect(compressionMethodMock).toHaveBeenCalledTimes(1)
// const paths = ['node_modules'] })
// const key = 'node-test'
// const infoMock = jest.spyOn(core, 'info') test('restore with zstd compressed cache found', async () => {
const paths = ['node_modules']
const key = 'node-test'
const logInfoMock = jest.spyOn(core, 'info')
const compressionMethod = CompressionMethod.Zstd
const signedDownloadUrl = 'https://blob-storage.local?signed=true'
const cacheVersion =
'8e2e96a184cb0cd6b48285b176c06a418f3d7fce14c29d9886fd1bb4f05c513d'
// const cacheEntry: ArtifactCacheEntry = { const getCacheVersionMock = jest.spyOn(cacheUtils, 'getCacheVersion')
// cacheKey: key, getCacheVersionMock.mockReturnValue(cacheVersion)
// scope: 'refs/heads/main',
// archiveLocation: 'www.actionscache.test/download'
// }
// const getCacheMock = jest.spyOn(cacheHttpClient, 'getCacheEntry')
// getCacheMock.mockImplementation(async () => {
// return Promise.resolve(cacheEntry)
// })
// const tempPath = '/foo/bar'
// const createTempDirectoryMock = jest.spyOn(cacheUtils, 'createTempDirectory') const compressionMethodMock = jest.spyOn(cacheUtils, 'getCompressionMethod')
// createTempDirectoryMock.mockImplementation(async () => { compressionMethodMock.mockReturnValue(Promise.resolve(compressionMethod))
// return Promise.resolve(tempPath)
// })
// const archivePath = path.join(tempPath, CacheFilename.Zstd) const getCacheDownloadURLMock = jest.spyOn(
// const downloadCacheMock = jest.spyOn(cacheHttpClient, 'downloadCache') CacheServiceClientJSON.prototype,
'GetCacheEntryDownloadURL'
)
getCacheDownloadURLMock.mockReturnValue(
Promise.resolve({ok: true, signedDownloadUrl})
)
// const fileSize = 62915000 const tempPath = '/foo/bar'
// const getArchiveFileSizeInBytesMock = jest
// .spyOn(cacheUtils, 'getArchiveFileSizeInBytes')
// .mockReturnValue(fileSize)
// const extractTarMock = jest.spyOn(tar, 'extractTar') const createTempDirectoryMock = jest.spyOn(cacheUtils, 'createTempDirectory')
// const compression = CompressionMethod.Zstd createTempDirectoryMock.mockImplementation(async () => {
// const getCompressionMock = jest return Promise.resolve(tempPath)
// .spyOn(cacheUtils, 'getCompressionMethod') })
// .mockReturnValue(Promise.resolve(compression))
// const cacheKey = await restoreCache(paths, key) const archivePath = path.join(tempPath, CacheFilename.Zstd)
const downloadCacheFileMock = jest.spyOn(
downloadCacheModule,
'downloadCacheFile'
)
downloadCacheFileMock.mockReturnValue(
Promise.resolve({} as BlobDownloadResponseParsed)
)
// expect(cacheKey).toBe(key) const fileSize = 62915000
// expect(getCacheMock).toHaveBeenCalledWith([key], paths, { const getArchiveFileSizeInBytesMock = jest
// compressionMethod: compression, .spyOn(cacheUtils, 'getArchiveFileSizeInBytes')
// enableCrossOsArchive: false .mockReturnValue(fileSize)
// })
// expect(createTempDirectoryMock).toHaveBeenCalledTimes(1)
// expect(downloadCacheMock).toHaveBeenCalledWith(
// cacheEntry.archiveLocation,
// archivePath,
// undefined
// )
// expect(getArchiveFileSizeInBytesMock).toHaveBeenCalledWith(archivePath)
// expect(infoMock).toHaveBeenCalledWith(`Cache Size: ~60 MB (62915000 B)`)
// expect(extractTarMock).toHaveBeenCalledTimes(1) const extractTarMock = jest.spyOn(tar, 'extractTar')
// expect(extractTarMock).toHaveBeenCalledWith(archivePath, compression) const unlinkFileMock = jest.spyOn(cacheUtils, 'unlinkFile')
// expect(getCompressionMock).toHaveBeenCalledTimes(1)
// }) const cacheKey = await restoreCache(paths, key)
expect(cacheKey).toBe(key)
expect(getCacheVersionMock).toHaveBeenCalledWith(
paths,
compressionMethod,
false
)
expect(getCacheDownloadURLMock).toHaveBeenCalledWith({
key,
restoreKeys: [],
version: cacheVersion
})
expect(createTempDirectoryMock).toHaveBeenCalledTimes(1)
expect(downloadCacheFileMock).toHaveBeenCalledWith(
signedDownloadUrl,
archivePath
)
expect(getArchiveFileSizeInBytesMock).toHaveBeenCalledWith(archivePath)
expect(logInfoMock).toHaveBeenCalledWith(`Cache Size: ~60 MB (62915000 B)`)
expect(extractTarMock).toHaveBeenCalledTimes(1)
expect(extractTarMock).toHaveBeenCalledWith(archivePath, compressionMethod)
expect(unlinkFileMock).toHaveBeenCalledTimes(1)
expect(unlinkFileMock).toHaveBeenCalledWith(archivePath)
expect(compressionMethodMock).toHaveBeenCalledTimes(1)
})
// test('restore with cache found for restore key', async () => { // test('restore with cache found for restore key', async () => {
// const paths = ['node_modules'] // const paths = ['node_modules']

View File

@ -3,13 +3,14 @@ import * as core from '@actions/core'
import { import {
BlobClient, BlobClient,
BlockBlobClient, BlockBlobClient,
BlobDownloadOptions BlobDownloadOptions,
BlobDownloadResponseParsed
} from '@azure/storage-blob' } from '@azure/storage-blob'
export async function downloadCacheFile( export async function downloadCacheFile(
signedUploadURL: string, signedUploadURL: string,
archivePath: string archivePath: string
): Promise<{}> { ): Promise<BlobDownloadResponseParsed> {
const downloadOptions: BlobDownloadOptions = { const downloadOptions: BlobDownloadOptions = {
maxRetryRequests: 5 maxRetryRequests: 5
} }