Add cache service version debug message

This commit is contained in:
Bassem Dghaidi 2024-11-25 04:08:47 -08:00 committed by GitHub
parent 4de30f744e
commit 54ac2dd012
2 changed files with 321 additions and 279 deletions

View File

@ -15,6 +15,9 @@ jest.mock('../src/internal/cacheUtils')
jest.mock('../src/internal/config') jest.mock('../src/internal/config')
jest.mock('../src/internal/tar') jest.mock('../src/internal/tar')
let logDebugMock: jest.SpyInstance
let logInfoMock: jest.SpyInstance
beforeAll(() => { beforeAll(() => {
jest.spyOn(console, 'log').mockImplementation(() => { }) jest.spyOn(console, 'log').mockImplementation(() => { })
jest.spyOn(core, 'debug').mockImplementation(() => { }) jest.spyOn(core, 'debug').mockImplementation(() => { })
@ -29,6 +32,13 @@ beforeAll(() => {
// Ensure that we're using v2 for these tests // Ensure that we're using v2 for these tests
jest.spyOn(config, 'getCacheServiceVersion').mockReturnValue('v2') jest.spyOn(config, 'getCacheServiceVersion').mockReturnValue('v2')
logDebugMock = jest.spyOn(core, 'debug')
logInfoMock = jest.spyOn(core, 'info')
})
afterEach(() => {
expect(logDebugMock).toHaveBeenCalledWith('Cache service version: v2')
}) })
test('restore with no path should fail', async () => { test('restore with no path should fail', async () => {
@ -117,7 +127,6 @@ test('restore with restore keys and no cache found', async () => {
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 compressionMethod = CompressionMethod.Gzip
const signedDownloadUrl = 'https://blob-storage.local?signed=true' const signedDownloadUrl = 'https://blob-storage.local?signed=true'
const cacheVersion = const cacheVersion =
@ -194,7 +203,6 @@ test('restore with gzip compressed cache found', async () => {
test('restore with zstd compressed cache found', async () => { test('restore with zstd 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.Zstd const compressionMethod = CompressionMethod.Zstd
const signedDownloadUrl = 'https://blob-storage.local?signed=true' const signedDownloadUrl = 'https://blob-storage.local?signed=true'
const cacheVersion = const cacheVersion =
@ -268,96 +276,128 @@ test('restore with zstd compressed cache found', async () => {
expect(compressionMethodMock).toHaveBeenCalledTimes(1) 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']
// const key = 'node-test' const key = 'node-test'
// const restoreKey = 'node-' const restoreKey = 'node-'
const compressionMethod = CompressionMethod.Gzip
const signedDownloadUrl = 'https://blob-storage.local?signed=true'
const cacheVersion =
'b8b58e9bd7b1e8f83d9f05c7e06ea865ba44a0330e07a14db74ac74386677bed'
// const infoMock = jest.spyOn(core, 'info') const getCacheVersionMock = jest.spyOn(cacheUtils, 'getCacheVersion')
getCacheVersionMock.mockReturnValue(cacheVersion)
// const cacheEntry: ArtifactCacheEntry = { const compressionMethodMock = jest.spyOn(cacheUtils, 'getCompressionMethod')
// cacheKey: restoreKey, compressionMethodMock.mockReturnValue(Promise.resolve(compressionMethod))
// 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 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.Zstd) 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 compression = CompressionMethod.Zstd const downloadCacheFileMock = jest.spyOn(
// const getCompressionMock = jest downloadCacheModule,
// .spyOn(cacheUtils, 'getCompressionMethod') 'downloadCacheFile'
// .mockReturnValue(Promise.resolve(compression)) )
downloadCacheFileMock.mockReturnValue(
Promise.resolve({} as BlobDownloadResponseParsed)
)
// const cacheKey = await restoreCache(paths, key, [restoreKey]) const fileSize = 142
const getArchiveFileSizeInBytesMock = jest
.spyOn(cacheUtils, 'getArchiveFileSizeInBytes')
.mockReturnValue(fileSize)
// expect(cacheKey).toBe(restoreKey) const extractTarMock = jest.spyOn(tar, 'extractTar')
// expect(getCacheMock).toHaveBeenCalledWith([key, restoreKey], paths, { const unlinkFileMock = jest.spyOn(cacheUtils, 'unlinkFile')
// compressionMethod: compression,
// enableCrossOsArchive: false
// })
// expect(createTempDirectoryMock).toHaveBeenCalledTimes(1)
// expect(downloadCacheMock).toHaveBeenCalledWith(
// cacheEntry.archiveLocation,
// archivePath,
// undefined
// )
// expect(getArchiveFileSizeInBytesMock).toHaveBeenCalledWith(archivePath)
// expect(infoMock).toHaveBeenCalledWith(`Cache Size: ~0 MB (142 B)`)
// expect(extractTarMock).toHaveBeenCalledTimes(1) const cacheKey = await restoreCache(paths, key, [restoreKey])
// expect(extractTarMock).toHaveBeenCalledWith(archivePath, compression)
// expect(getCompressionMock).toHaveBeenCalledTimes(1)
// })
// test('restore with dry run', async () => { expect(cacheKey).toBe(restoreKey)
// const paths = ['node_modules'] expect(getCacheVersionMock).toHaveBeenCalledWith(
// const key = 'node-test' paths,
// const options = { lookupOnly: true } compressionMethod,
false
)
expect(getCacheDownloadURLMock).toHaveBeenCalledWith({
key,
restoreKeys: restoreKey,
version: cacheVersion
})
expect(createTempDirectoryMock).toHaveBeenCalledTimes(1)
expect(downloadCacheFileMock).toHaveBeenCalledWith(
signedDownloadUrl,
archivePath
)
expect(getArchiveFileSizeInBytesMock).toHaveBeenCalledWith(archivePath)
expect(logInfoMock).toHaveBeenCalledWith(`Cache Size: ~0 MB (142 B)`)
// const cacheEntry: ArtifactCacheEntry = { expect(extractTarMock).toHaveBeenCalledTimes(1)
// cacheKey: key, expect(extractTarMock).toHaveBeenCalledWith(archivePath, compressionMethod)
// scope: 'refs/heads/main',
// archiveLocation: 'www.actionscache.test/download'
// }
// const getCacheMock = jest.spyOn(cacheHttpClient, 'getCacheEntry')
// getCacheMock.mockImplementation(async () => {
// return Promise.resolve(cacheEntry)
// })
// const createTempDirectoryMock = jest.spyOn(cacheUtils, 'createTempDirectory') expect(unlinkFileMock).toHaveBeenCalledTimes(1)
// const downloadCacheMock = jest.spyOn(cacheHttpClient, 'downloadCache') expect(unlinkFileMock).toHaveBeenCalledWith(archivePath)
// const compression = CompressionMethod.Gzip expect(compressionMethodMock).toHaveBeenCalledTimes(1)
// const getCompressionMock = jest })
// .spyOn(cacheUtils, 'getCompressionMethod')
// .mockReturnValue(Promise.resolve(compression))
// const cacheKey = await restoreCache(paths, key, undefined, options) test('restore with dry run', async () => {
const paths = ['node_modules']
const key = 'node-test'
const options = { lookupOnly: true }
const compressionMethod = CompressionMethod.Gzip
const signedDownloadUrl = 'https://blob-storage.local?signed=true'
const cacheVersion =
'd90f107aaeb22920dba0c637a23c37b5bc497b4dfa3b07fe3f79bf88a273c11b'
// expect(cacheKey).toBe(key) const getCacheVersionMock = jest.spyOn(cacheUtils, 'getCacheVersion')
// expect(getCompressionMock).toHaveBeenCalledTimes(1) getCacheVersionMock.mockReturnValue(cacheVersion)
// expect(getCacheMock).toHaveBeenCalledWith([key], paths, {
// compressionMethod: compression, const compressionMethodMock = jest.spyOn(cacheUtils, 'getCompressionMethod')
// enableCrossOsArchive: false compressionMethodMock.mockReturnValue(Promise.resolve(compressionMethod))
// })
// // creating a tempDir and downloading the cache are skipped const getCacheDownloadURLMock = jest.spyOn(
// expect(createTempDirectoryMock).toHaveBeenCalledTimes(0) CacheServiceClientJSON.prototype,
// expect(downloadCacheMock).toHaveBeenCalledTimes(0) 'GetCacheEntryDownloadURL'
// }) )
getCacheDownloadURLMock.mockReturnValue(
Promise.resolve({ ok: true, signedDownloadUrl })
)
const createTempDirectoryMock = jest.spyOn(cacheUtils, 'createTempDirectory')
const downloadCacheFileMock = jest.spyOn(
downloadCacheModule,
'downloadCacheFile'
)
const cacheKey = await restoreCache(paths, key, undefined, options)
expect(cacheKey).toBe(key)
expect(getCacheVersionMock).toHaveBeenCalledWith(
paths,
compressionMethod,
false
)
expect(getCacheDownloadURLMock).toHaveBeenCalledWith({
key,
restoreKeys: [],
version: cacheVersion
})
expect(logInfoMock).toHaveBeenCalledWith('Lookup only - skipping download')
// creating a tempDir and downloading the cache are skipped
expect(createTempDirectoryMock).toHaveBeenCalledTimes(0)
expect(downloadCacheFileMock).toHaveBeenCalledTimes(0)
})

View File

@ -79,9 +79,11 @@ export async function restoreCache(
options?: DownloadOptions, options?: DownloadOptions,
enableCrossOsArchive = false enableCrossOsArchive = false
): Promise<string | undefined> { ): Promise<string | undefined> {
const cacheServiceVersion: string = getCacheServiceVersion()
core.debug(`Cache service version: ${cacheServiceVersion}`)
checkPaths(paths) checkPaths(paths)
const cacheServiceVersion: string = getCacheServiceVersion()
switch (cacheServiceVersion) { switch (cacheServiceVersion) {
case 'v2': case 'v2':
return await restoreCacheV2( return await restoreCacheV2(