mirror of https://github.com/actions/toolkit
Add cache service version debug message
parent
4de30f744e
commit
54ac2dd012
|
@ -4,10 +4,10 @@ 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 downloadCacheModule from '../src/internal/blob/download-cache'
|
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 {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 { BlobDownloadResponseParsed } from '@azure/storage-blob'
|
||||||
// import {executePromisesSequentially} from '@azure/ms-rest-js'
|
// import {executePromisesSequentially} from '@azure/ms-rest-js'
|
||||||
|
|
||||||
jest.mock('../src/internal/cacheHttpClient')
|
jest.mock('../src/internal/cacheHttpClient')
|
||||||
|
@ -15,349 +15,389 @@ 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(() => { })
|
||||||
jest.spyOn(core, 'info').mockImplementation(() => {})
|
jest.spyOn(core, 'info').mockImplementation(() => { })
|
||||||
jest.spyOn(core, 'warning').mockImplementation(() => {})
|
jest.spyOn(core, 'warning').mockImplementation(() => { })
|
||||||
jest.spyOn(core, 'error').mockImplementation(() => {})
|
jest.spyOn(core, 'error').mockImplementation(() => { })
|
||||||
|
|
||||||
jest.spyOn(cacheUtils, 'getCacheFileName').mockImplementation(cm => {
|
jest.spyOn(cacheUtils, 'getCacheFileName').mockImplementation(cm => {
|
||||||
const actualUtils = jest.requireActual('../src/internal/cacheUtils')
|
const actualUtils = jest.requireActual('../src/internal/cacheUtils')
|
||||||
return actualUtils.getCacheFileName(cm)
|
return actualUtils.getCacheFileName(cm)
|
||||||
})
|
})
|
||||||
|
|
||||||
// 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 () => {
|
||||||
const paths: string[] = []
|
const paths: string[] = []
|
||||||
const key = 'node-test'
|
const key = 'node-test'
|
||||||
await expect(restoreCache(paths, key)).rejects.toThrowError(
|
await expect(restoreCache(paths, key)).rejects.toThrowError(
|
||||||
`Path Validation Error: At least one directory or file path is required`
|
`Path Validation Error: At least one directory or file path is required`
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('restore with too many keys should fail', async () => {
|
test('restore with too many keys should fail', async () => {
|
||||||
const paths = ['node_modules']
|
const paths = ['node_modules']
|
||||||
const key = 'node-test'
|
const key = 'node-test'
|
||||||
const restoreKeys = [...Array(20).keys()].map(x => x.toString())
|
const restoreKeys = [...Array(20).keys()].map(x => x.toString())
|
||||||
await expect(restoreCache(paths, key, restoreKeys)).rejects.toThrowError(
|
await expect(restoreCache(paths, key, restoreKeys)).rejects.toThrowError(
|
||||||
`Key Validation Error: Keys are limited to a maximum of 10.`
|
`Key Validation Error: Keys are limited to a maximum of 10.`
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('restore with large key should fail', async () => {
|
test('restore with large key should fail', async () => {
|
||||||
const paths = ['node_modules']
|
const paths = ['node_modules']
|
||||||
const key = 'foo'.repeat(512) // Over the 512 character limit
|
const key = 'foo'.repeat(512) // Over the 512 character limit
|
||||||
await expect(restoreCache(paths, key)).rejects.toThrowError(
|
await expect(restoreCache(paths, key)).rejects.toThrowError(
|
||||||
`Key Validation Error: ${key} cannot be larger than 512 characters.`
|
`Key Validation Error: ${key} cannot be larger than 512 characters.`
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('restore with invalid key should fail', async () => {
|
test('restore with invalid key should fail', async () => {
|
||||||
const paths = ['node_modules']
|
const paths = ['node_modules']
|
||||||
const key = 'comma,comma'
|
const key = 'comma,comma'
|
||||||
await expect(restoreCache(paths, key)).rejects.toThrowError(
|
await expect(restoreCache(paths, key)).rejects.toThrowError(
|
||||||
`Key Validation Error: ${key} cannot contain commas.`
|
`Key Validation Error: ${key} cannot contain commas.`
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('restore with no cache found', async () => {
|
test('restore with no cache found', async () => {
|
||||||
const paths = ['node_modules']
|
const paths = ['node_modules']
|
||||||
const key = 'node-test'
|
const key = 'node-test'
|
||||||
|
|
||||||
jest
|
jest
|
||||||
.spyOn(CacheServiceClientJSON.prototype, 'GetCacheEntryDownloadURL')
|
.spyOn(CacheServiceClientJSON.prototype, 'GetCacheEntryDownloadURL')
|
||||||
.mockReturnValue(Promise.resolve({ok: false, signedDownloadUrl: ''}))
|
.mockReturnValue(Promise.resolve({ ok: false, signedDownloadUrl: '' }))
|
||||||
|
|
||||||
const cacheKey = await restoreCache(paths, key)
|
const cacheKey = await restoreCache(paths, key)
|
||||||
|
|
||||||
expect(cacheKey).toBe(undefined)
|
expect(cacheKey).toBe(undefined)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('restore with server error should fail', async () => {
|
test('restore with server error should fail', async () => {
|
||||||
const paths = ['node_modules']
|
const paths = ['node_modules']
|
||||||
const key = 'node-test'
|
const key = 'node-test'
|
||||||
const logWarningMock = jest.spyOn(core, 'warning')
|
const logWarningMock = jest.spyOn(core, 'warning')
|
||||||
|
|
||||||
jest
|
jest
|
||||||
.spyOn(CacheServiceClientJSON.prototype, 'GetCacheEntryDownloadURL')
|
.spyOn(CacheServiceClientJSON.prototype, 'GetCacheEntryDownloadURL')
|
||||||
.mockImplementation(() => {
|
.mockImplementation(() => {
|
||||||
throw new Error('HTTP Error Occurred')
|
throw new Error('HTTP Error Occurred')
|
||||||
})
|
})
|
||||||
|
|
||||||
const cacheKey = await restoreCache(paths, key)
|
const cacheKey = await restoreCache(paths, key)
|
||||||
expect(cacheKey).toBe(undefined)
|
expect(cacheKey).toBe(undefined)
|
||||||
expect(logWarningMock).toHaveBeenCalledTimes(1)
|
expect(logWarningMock).toHaveBeenCalledTimes(1)
|
||||||
expect(logWarningMock).toHaveBeenCalledWith(
|
expect(logWarningMock).toHaveBeenCalledWith(
|
||||||
'Failed to restore: HTTP Error Occurred'
|
'Failed to restore: HTTP Error Occurred'
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
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')
|
const logWarningMock = jest.spyOn(core, 'warning')
|
||||||
|
|
||||||
jest
|
jest
|
||||||
.spyOn(CacheServiceClientJSON.prototype, 'GetCacheEntryDownloadURL')
|
.spyOn(CacheServiceClientJSON.prototype, 'GetCacheEntryDownloadURL')
|
||||||
.mockReturnValue(Promise.resolve({ok: false, signedDownloadUrl: ''}))
|
.mockReturnValue(Promise.resolve({ ok: false, signedDownloadUrl: '' }))
|
||||||
|
|
||||||
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(
|
expect(logWarningMock).toHaveBeenCalledWith(
|
||||||
`Cache not found for keys: ${[key, restoreKey].join(', ')}`
|
`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 compressionMethod = CompressionMethod.Gzip
|
const signedDownloadUrl = 'https://blob-storage.local?signed=true'
|
||||||
const signedDownloadUrl = 'https://blob-storage.local?signed=true'
|
const cacheVersion =
|
||||||
const cacheVersion =
|
'd90f107aaeb22920dba0c637a23c37b5bc497b4dfa3b07fe3f79bf88a273c11b'
|
||||||
'd90f107aaeb22920dba0c637a23c37b5bc497b4dfa3b07fe3f79bf88a273c11b'
|
|
||||||
|
|
||||||
const getCacheVersionMock = jest.spyOn(cacheUtils, 'getCacheVersion')
|
const getCacheVersionMock = jest.spyOn(cacheUtils, 'getCacheVersion')
|
||||||
getCacheVersionMock.mockReturnValue(cacheVersion)
|
getCacheVersionMock.mockReturnValue(cacheVersion)
|
||||||
|
|
||||||
const compressionMethodMock = jest.spyOn(cacheUtils, 'getCompressionMethod')
|
const compressionMethodMock = jest.spyOn(cacheUtils, 'getCompressionMethod')
|
||||||
compressionMethodMock.mockReturnValue(Promise.resolve(compressionMethod))
|
compressionMethodMock.mockReturnValue(Promise.resolve(compressionMethod))
|
||||||
|
|
||||||
const getCacheDownloadURLMock = jest.spyOn(
|
const getCacheDownloadURLMock = jest.spyOn(
|
||||||
CacheServiceClientJSON.prototype,
|
CacheServiceClientJSON.prototype,
|
||||||
'GetCacheEntryDownloadURL'
|
'GetCacheEntryDownloadURL'
|
||||||
)
|
)
|
||||||
getCacheDownloadURLMock.mockReturnValue(
|
getCacheDownloadURLMock.mockReturnValue(
|
||||||
Promise.resolve({ok: true, signedDownloadUrl})
|
Promise.resolve({ ok: true, signedDownloadUrl })
|
||||||
)
|
)
|
||||||
|
|
||||||
const tempPath = '/foo/bar'
|
const tempPath = '/foo/bar'
|
||||||
|
|
||||||
const createTempDirectoryMock = jest.spyOn(cacheUtils, 'createTempDirectory')
|
const createTempDirectoryMock = jest.spyOn(cacheUtils, 'createTempDirectory')
|
||||||
createTempDirectoryMock.mockImplementation(async () => {
|
createTempDirectoryMock.mockImplementation(async () => {
|
||||||
return Promise.resolve(tempPath)
|
return Promise.resolve(tempPath)
|
||||||
})
|
})
|
||||||
|
|
||||||
const archivePath = path.join(tempPath, CacheFilename.Gzip)
|
const archivePath = path.join(tempPath, CacheFilename.Gzip)
|
||||||
const downloadCacheFileMock = jest.spyOn(
|
const downloadCacheFileMock = jest.spyOn(
|
||||||
downloadCacheModule,
|
downloadCacheModule,
|
||||||
'downloadCacheFile'
|
'downloadCacheFile'
|
||||||
)
|
)
|
||||||
downloadCacheFileMock.mockReturnValue(
|
downloadCacheFileMock.mockReturnValue(
|
||||||
Promise.resolve({} as BlobDownloadResponseParsed)
|
Promise.resolve({} as BlobDownloadResponseParsed)
|
||||||
)
|
)
|
||||||
|
|
||||||
const fileSize = 142
|
const fileSize = 142
|
||||||
const getArchiveFileSizeInBytesMock = jest
|
const getArchiveFileSizeInBytesMock = jest
|
||||||
.spyOn(cacheUtils, 'getArchiveFileSizeInBytes')
|
.spyOn(cacheUtils, 'getArchiveFileSizeInBytes')
|
||||||
.mockReturnValue(fileSize)
|
.mockReturnValue(fileSize)
|
||||||
|
|
||||||
const extractTarMock = jest.spyOn(tar, 'extractTar')
|
const extractTarMock = jest.spyOn(tar, 'extractTar')
|
||||||
const unlinkFileMock = jest.spyOn(cacheUtils, 'unlinkFile')
|
const unlinkFileMock = jest.spyOn(cacheUtils, 'unlinkFile')
|
||||||
|
|
||||||
const cacheKey = await restoreCache(paths, key)
|
const cacheKey = await restoreCache(paths, key)
|
||||||
|
|
||||||
expect(cacheKey).toBe(key)
|
expect(cacheKey).toBe(key)
|
||||||
expect(getCacheVersionMock).toHaveBeenCalledWith(
|
expect(getCacheVersionMock).toHaveBeenCalledWith(
|
||||||
paths,
|
paths,
|
||||||
compressionMethod,
|
compressionMethod,
|
||||||
false
|
false
|
||||||
)
|
)
|
||||||
expect(getCacheDownloadURLMock).toHaveBeenCalledWith({
|
expect(getCacheDownloadURLMock).toHaveBeenCalledWith({
|
||||||
key,
|
key,
|
||||||
restoreKeys: [],
|
restoreKeys: [],
|
||||||
version: cacheVersion
|
version: cacheVersion
|
||||||
})
|
})
|
||||||
expect(createTempDirectoryMock).toHaveBeenCalledTimes(1)
|
expect(createTempDirectoryMock).toHaveBeenCalledTimes(1)
|
||||||
expect(downloadCacheFileMock).toHaveBeenCalledWith(
|
expect(downloadCacheFileMock).toHaveBeenCalledWith(
|
||||||
signedDownloadUrl,
|
signedDownloadUrl,
|
||||||
archivePath
|
archivePath
|
||||||
)
|
)
|
||||||
expect(getArchiveFileSizeInBytesMock).toHaveBeenCalledWith(archivePath)
|
expect(getArchiveFileSizeInBytesMock).toHaveBeenCalledWith(archivePath)
|
||||||
expect(logInfoMock).toHaveBeenCalledWith(`Cache Size: ~0 MB (142 B)`)
|
expect(logInfoMock).toHaveBeenCalledWith(`Cache Size: ~0 MB (142 B)`)
|
||||||
|
|
||||||
expect(extractTarMock).toHaveBeenCalledTimes(1)
|
expect(extractTarMock).toHaveBeenCalledTimes(1)
|
||||||
expect(extractTarMock).toHaveBeenCalledWith(archivePath, compressionMethod)
|
expect(extractTarMock).toHaveBeenCalledWith(archivePath, compressionMethod)
|
||||||
|
|
||||||
expect(unlinkFileMock).toHaveBeenCalledTimes(1)
|
expect(unlinkFileMock).toHaveBeenCalledTimes(1)
|
||||||
expect(unlinkFileMock).toHaveBeenCalledWith(archivePath)
|
expect(unlinkFileMock).toHaveBeenCalledWith(archivePath)
|
||||||
|
|
||||||
expect(compressionMethodMock).toHaveBeenCalledTimes(1)
|
expect(compressionMethodMock).toHaveBeenCalledTimes(1)
|
||||||
})
|
})
|
||||||
|
|
||||||
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 =
|
'8e2e96a184cb0cd6b48285b176c06a418f3d7fce14c29d9886fd1bb4f05c513d'
|
||||||
'8e2e96a184cb0cd6b48285b176c06a418f3d7fce14c29d9886fd1bb4f05c513d'
|
|
||||||
|
|
||||||
const getCacheVersionMock = jest.spyOn(cacheUtils, 'getCacheVersion')
|
const getCacheVersionMock = jest.spyOn(cacheUtils, 'getCacheVersion')
|
||||||
getCacheVersionMock.mockReturnValue(cacheVersion)
|
getCacheVersionMock.mockReturnValue(cacheVersion)
|
||||||
|
|
||||||
const compressionMethodMock = jest.spyOn(cacheUtils, 'getCompressionMethod')
|
const compressionMethodMock = jest.spyOn(cacheUtils, 'getCompressionMethod')
|
||||||
compressionMethodMock.mockReturnValue(Promise.resolve(compressionMethod))
|
compressionMethodMock.mockReturnValue(Promise.resolve(compressionMethod))
|
||||||
|
|
||||||
const getCacheDownloadURLMock = jest.spyOn(
|
const getCacheDownloadURLMock = jest.spyOn(
|
||||||
CacheServiceClientJSON.prototype,
|
CacheServiceClientJSON.prototype,
|
||||||
'GetCacheEntryDownloadURL'
|
'GetCacheEntryDownloadURL'
|
||||||
)
|
)
|
||||||
getCacheDownloadURLMock.mockReturnValue(
|
getCacheDownloadURLMock.mockReturnValue(
|
||||||
Promise.resolve({ok: true, signedDownloadUrl})
|
Promise.resolve({ ok: true, signedDownloadUrl })
|
||||||
)
|
)
|
||||||
|
|
||||||
const tempPath = '/foo/bar'
|
const tempPath = '/foo/bar'
|
||||||
|
|
||||||
const createTempDirectoryMock = jest.spyOn(cacheUtils, 'createTempDirectory')
|
const createTempDirectoryMock = jest.spyOn(cacheUtils, 'createTempDirectory')
|
||||||
createTempDirectoryMock.mockImplementation(async () => {
|
createTempDirectoryMock.mockImplementation(async () => {
|
||||||
return Promise.resolve(tempPath)
|
return Promise.resolve(tempPath)
|
||||||
})
|
})
|
||||||
|
|
||||||
const archivePath = path.join(tempPath, CacheFilename.Zstd)
|
const archivePath = path.join(tempPath, CacheFilename.Zstd)
|
||||||
const downloadCacheFileMock = jest.spyOn(
|
const downloadCacheFileMock = jest.spyOn(
|
||||||
downloadCacheModule,
|
downloadCacheModule,
|
||||||
'downloadCacheFile'
|
'downloadCacheFile'
|
||||||
)
|
)
|
||||||
downloadCacheFileMock.mockReturnValue(
|
downloadCacheFileMock.mockReturnValue(
|
||||||
Promise.resolve({} as BlobDownloadResponseParsed)
|
Promise.resolve({} as BlobDownloadResponseParsed)
|
||||||
)
|
)
|
||||||
|
|
||||||
const fileSize = 62915000
|
const fileSize = 62915000
|
||||||
const getArchiveFileSizeInBytesMock = jest
|
const getArchiveFileSizeInBytesMock = jest
|
||||||
.spyOn(cacheUtils, 'getArchiveFileSizeInBytes')
|
.spyOn(cacheUtils, 'getArchiveFileSizeInBytes')
|
||||||
.mockReturnValue(fileSize)
|
.mockReturnValue(fileSize)
|
||||||
|
|
||||||
const extractTarMock = jest.spyOn(tar, 'extractTar')
|
const extractTarMock = jest.spyOn(tar, 'extractTar')
|
||||||
const unlinkFileMock = jest.spyOn(cacheUtils, 'unlinkFile')
|
const unlinkFileMock = jest.spyOn(cacheUtils, 'unlinkFile')
|
||||||
|
|
||||||
const cacheKey = await restoreCache(paths, key)
|
const cacheKey = await restoreCache(paths, key)
|
||||||
|
|
||||||
expect(cacheKey).toBe(key)
|
expect(cacheKey).toBe(key)
|
||||||
expect(getCacheVersionMock).toHaveBeenCalledWith(
|
expect(getCacheVersionMock).toHaveBeenCalledWith(
|
||||||
paths,
|
paths,
|
||||||
compressionMethod,
|
compressionMethod,
|
||||||
false
|
false
|
||||||
)
|
)
|
||||||
expect(getCacheDownloadURLMock).toHaveBeenCalledWith({
|
expect(getCacheDownloadURLMock).toHaveBeenCalledWith({
|
||||||
key,
|
key,
|
||||||
restoreKeys: [],
|
restoreKeys: [],
|
||||||
version: cacheVersion
|
version: cacheVersion
|
||||||
})
|
})
|
||||||
expect(createTempDirectoryMock).toHaveBeenCalledTimes(1)
|
expect(createTempDirectoryMock).toHaveBeenCalledTimes(1)
|
||||||
expect(downloadCacheFileMock).toHaveBeenCalledWith(
|
expect(downloadCacheFileMock).toHaveBeenCalledWith(
|
||||||
signedDownloadUrl,
|
signedDownloadUrl,
|
||||||
archivePath
|
archivePath
|
||||||
)
|
)
|
||||||
expect(getArchiveFileSizeInBytesMock).toHaveBeenCalledWith(archivePath)
|
expect(getArchiveFileSizeInBytesMock).toHaveBeenCalledWith(archivePath)
|
||||||
expect(logInfoMock).toHaveBeenCalledWith(`Cache Size: ~60 MB (62915000 B)`)
|
expect(logInfoMock).toHaveBeenCalledWith(`Cache Size: ~60 MB (62915000 B)`)
|
||||||
|
|
||||||
expect(extractTarMock).toHaveBeenCalledTimes(1)
|
expect(extractTarMock).toHaveBeenCalledTimes(1)
|
||||||
expect(extractTarMock).toHaveBeenCalledWith(archivePath, compressionMethod)
|
expect(extractTarMock).toHaveBeenCalledWith(archivePath, compressionMethod)
|
||||||
|
|
||||||
expect(unlinkFileMock).toHaveBeenCalledTimes(1)
|
expect(unlinkFileMock).toHaveBeenCalledTimes(1)
|
||||||
expect(unlinkFileMock).toHaveBeenCalledWith(archivePath)
|
expect(unlinkFileMock).toHaveBeenCalledWith(archivePath)
|
||||||
|
|
||||||
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)
|
||||||
|
})
|
||||||
|
|
|
@ -3,18 +3,18 @@ import * as path from 'path'
|
||||||
import * as utils from './internal/cacheUtils'
|
import * as utils from './internal/cacheUtils'
|
||||||
import * as cacheHttpClient from './internal/cacheHttpClient'
|
import * as cacheHttpClient from './internal/cacheHttpClient'
|
||||||
import * as cacheTwirpClient from './internal/shared/cacheTwirpClient'
|
import * as cacheTwirpClient from './internal/shared/cacheTwirpClient'
|
||||||
import {getCacheServiceVersion, isGhes} from './internal/config'
|
import { getCacheServiceVersion, isGhes } from './internal/config'
|
||||||
import {DownloadOptions, UploadOptions} from './options'
|
import { DownloadOptions, UploadOptions } from './options'
|
||||||
import {createTar, extractTar, listTar} from './internal/tar'
|
import { createTar, extractTar, listTar } from './internal/tar'
|
||||||
import {
|
import {
|
||||||
CreateCacheEntryRequest,
|
CreateCacheEntryRequest,
|
||||||
FinalizeCacheEntryUploadRequest,
|
FinalizeCacheEntryUploadRequest,
|
||||||
FinalizeCacheEntryUploadResponse,
|
FinalizeCacheEntryUploadResponse,
|
||||||
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 { uploadCacheFile } from './internal/blob/upload-cache'
|
||||||
import {downloadCacheFile} from './internal/blob/download-cache'
|
import { downloadCacheFile } from './internal/blob/download-cache'
|
||||||
export class ValidationError extends Error {
|
export class ValidationError extends Error {
|
||||||
constructor(message: string) {
|
constructor(message: string) {
|
||||||
super(message)
|
super(message)
|
||||||
|
@ -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(
|
||||||
|
@ -405,9 +407,9 @@ async function saveCacheV1(
|
||||||
} else if (reserveCacheResponse?.statusCode === 400) {
|
} else if (reserveCacheResponse?.statusCode === 400) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
reserveCacheResponse?.error?.message ??
|
reserveCacheResponse?.error?.message ??
|
||||||
`Cache size of ~${Math.round(
|
`Cache size of ~${Math.round(
|
||||||
archiveFileSize / (1024 * 1024)
|
archiveFileSize / (1024 * 1024)
|
||||||
)} MB (${archiveFileSize} B) is over the data cap limit, not saving cache.`
|
)} MB (${archiveFileSize} B) is over the data cap limit, not saving cache.`
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
throw new ReserveCacheError(
|
throw new ReserveCacheError(
|
||||||
|
|
Loading…
Reference in New Issue