2020-05-06 15:10:18 +00:00
|
|
|
import * as core from '@actions/core'
|
|
|
|
import * as path from 'path'
|
|
|
|
import {restoreCache} from '../src/cache'
|
|
|
|
import * as cacheHttpClient from '../src/internal/cacheHttpClient'
|
|
|
|
import * as cacheUtils from '../src/internal/cacheUtils'
|
|
|
|
import {CacheFilename, CompressionMethod} from '../src/internal/constants'
|
|
|
|
import {ArtifactCacheEntry} from '../src/internal/contracts'
|
|
|
|
import * as tar from '../src/internal/tar'
|
|
|
|
|
|
|
|
jest.mock('../src/internal/cacheHttpClient')
|
|
|
|
jest.mock('../src/internal/cacheUtils')
|
|
|
|
jest.mock('../src/internal/tar')
|
|
|
|
|
|
|
|
beforeAll(() => {
|
2020-05-15 16:18:50 +00:00
|
|
|
jest.spyOn(console, 'log').mockImplementation(() => {})
|
|
|
|
jest.spyOn(core, 'debug').mockImplementation(() => {})
|
|
|
|
jest.spyOn(core, 'info').mockImplementation(() => {})
|
|
|
|
jest.spyOn(core, 'warning').mockImplementation(() => {})
|
|
|
|
jest.spyOn(core, 'error').mockImplementation(() => {})
|
|
|
|
|
2020-05-06 15:10:18 +00:00
|
|
|
jest.spyOn(cacheUtils, 'getCacheFileName').mockImplementation(cm => {
|
|
|
|
const actualUtils = jest.requireActual('../src/internal/cacheUtils')
|
|
|
|
return actualUtils.getCacheFileName(cm)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('restore with no path should fail', async () => {
|
2020-05-06 21:53:22 +00:00
|
|
|
const paths: string[] = []
|
2020-05-06 15:10:18 +00:00
|
|
|
const key = 'node-test'
|
2020-05-06 21:53:22 +00:00
|
|
|
await expect(restoreCache(paths, key)).rejects.toThrowError(
|
|
|
|
`Path Validation Error: At least one directory or file path is required`
|
2020-05-06 15:10:18 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('restore with too many keys should fail', async () => {
|
2020-05-06 21:53:22 +00:00
|
|
|
const paths = ['node_modules']
|
2020-05-06 15:10:18 +00:00
|
|
|
const key = 'node-test'
|
|
|
|
const restoreKeys = [...Array(20).keys()].map(x => x.toString())
|
2020-05-06 21:53:22 +00:00
|
|
|
await expect(restoreCache(paths, key, restoreKeys)).rejects.toThrowError(
|
2020-05-06 15:10:18 +00:00
|
|
|
`Key Validation Error: Keys are limited to a maximum of 10.`
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('restore with large key should fail', async () => {
|
2020-05-06 21:53:22 +00:00
|
|
|
const paths = ['node_modules']
|
2020-05-06 15:10:18 +00:00
|
|
|
const key = 'foo'.repeat(512) // Over the 512 character limit
|
2020-05-06 21:53:22 +00:00
|
|
|
await expect(restoreCache(paths, key)).rejects.toThrowError(
|
2020-05-06 15:10:18 +00:00
|
|
|
`Key Validation Error: ${key} cannot be larger than 512 characters.`
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('restore with invalid key should fail', async () => {
|
2020-05-06 21:53:22 +00:00
|
|
|
const paths = ['node_modules']
|
2020-05-06 15:10:18 +00:00
|
|
|
const key = 'comma,comma'
|
2020-05-06 21:53:22 +00:00
|
|
|
await expect(restoreCache(paths, key)).rejects.toThrowError(
|
2020-05-06 15:10:18 +00:00
|
|
|
`Key Validation Error: ${key} cannot contain commas.`
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('restore with no cache found', async () => {
|
2020-05-06 21:53:22 +00:00
|
|
|
const paths = ['node_modules']
|
2020-05-06 15:10:18 +00:00
|
|
|
const key = 'node-test'
|
|
|
|
|
2020-05-07 00:07:39 +00:00
|
|
|
jest.spyOn(cacheHttpClient, 'getCacheEntry').mockImplementation(async () => {
|
2020-05-06 15:10:18 +00:00
|
|
|
return Promise.resolve(null)
|
|
|
|
})
|
|
|
|
|
2020-05-06 21:53:22 +00:00
|
|
|
const cacheKey = await restoreCache(paths, key)
|
2020-05-06 15:10:18 +00:00
|
|
|
|
2020-05-06 21:53:22 +00:00
|
|
|
expect(cacheKey).toBe(undefined)
|
2020-05-06 15:10:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('restore with server error should fail', async () => {
|
2020-05-06 21:53:22 +00:00
|
|
|
const paths = ['node_modules']
|
2020-05-06 15:10:18 +00:00
|
|
|
const key = 'node-test'
|
2022-06-20 04:54:54 +00:00
|
|
|
const logWarningMock = jest.spyOn(core, 'warning')
|
2020-05-06 15:10:18 +00:00
|
|
|
|
2020-05-07 00:07:39 +00:00
|
|
|
jest.spyOn(cacheHttpClient, 'getCacheEntry').mockImplementation(() => {
|
2020-05-06 15:10:18 +00:00
|
|
|
throw new Error('HTTP Error Occurred')
|
|
|
|
})
|
|
|
|
|
2022-06-20 04:22:20 +00:00
|
|
|
const cacheKey = await restoreCache(paths, key)
|
|
|
|
expect(cacheKey).toBe(undefined)
|
|
|
|
expect(logWarningMock).toHaveBeenCalledTimes(1)
|
2022-06-20 04:54:54 +00:00
|
|
|
expect(logWarningMock).toHaveBeenCalledWith(
|
2022-06-20 08:48:14 +00:00
|
|
|
'Failed to restore: HTTP Error Occurred'
|
2022-06-20 04:54:54 +00:00
|
|
|
)
|
2020-05-06 15:10:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('restore with restore keys and no cache found', async () => {
|
2020-05-06 21:53:22 +00:00
|
|
|
const paths = ['node_modules']
|
2020-05-06 15:10:18 +00:00
|
|
|
const key = 'node-test'
|
|
|
|
const restoreKey = 'node-'
|
|
|
|
|
2020-05-07 00:07:39 +00:00
|
|
|
jest.spyOn(cacheHttpClient, 'getCacheEntry').mockImplementation(async () => {
|
2020-05-06 15:10:18 +00:00
|
|
|
return Promise.resolve(null)
|
|
|
|
})
|
|
|
|
|
2020-05-06 21:53:22 +00:00
|
|
|
const cacheKey = await restoreCache(paths, key, [restoreKey])
|
2020-05-06 15:10:18 +00:00
|
|
|
|
2020-05-06 21:53:22 +00:00
|
|
|
expect(cacheKey).toBe(undefined)
|
2020-05-06 15:10:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('restore with gzip compressed cache found', async () => {
|
2020-05-06 21:53:22 +00:00
|
|
|
const paths = ['node_modules']
|
2020-05-06 15:10:18 +00:00
|
|
|
const key = 'node-test'
|
|
|
|
|
|
|
|
const cacheEntry: ArtifactCacheEntry = {
|
|
|
|
cacheKey: key,
|
2020-07-21 15:33:05 +00:00
|
|
|
scope: 'refs/heads/main',
|
2020-05-06 15:10:18 +00:00
|
|
|
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')
|
|
|
|
createTempDirectoryMock.mockImplementation(async () => {
|
|
|
|
return Promise.resolve(tempPath)
|
|
|
|
})
|
|
|
|
|
|
|
|
const archivePath = path.join(tempPath, CacheFilename.Gzip)
|
|
|
|
const downloadCacheMock = jest.spyOn(cacheHttpClient, 'downloadCache')
|
|
|
|
|
|
|
|
const fileSize = 142
|
2021-05-03 15:09:44 +00:00
|
|
|
const getArchiveFileSizeInBytesMock = jest
|
|
|
|
.spyOn(cacheUtils, 'getArchiveFileSizeInBytes')
|
2020-05-06 15:10:18 +00:00
|
|
|
.mockReturnValue(fileSize)
|
|
|
|
|
|
|
|
const extractTarMock = jest.spyOn(tar, 'extractTar')
|
|
|
|
const unlinkFileMock = jest.spyOn(cacheUtils, 'unlinkFile')
|
|
|
|
|
|
|
|
const compression = CompressionMethod.Gzip
|
|
|
|
const getCompressionMock = jest
|
|
|
|
.spyOn(cacheUtils, 'getCompressionMethod')
|
|
|
|
.mockReturnValue(Promise.resolve(compression))
|
|
|
|
|
2020-05-06 21:53:22 +00:00
|
|
|
const cacheKey = await restoreCache(paths, key)
|
2020-05-06 15:10:18 +00:00
|
|
|
|
2020-05-06 21:53:22 +00:00
|
|
|
expect(cacheKey).toBe(key)
|
|
|
|
expect(getCacheMock).toHaveBeenCalledWith([key], paths, {
|
2020-05-06 15:10:18 +00:00
|
|
|
compressionMethod: compression
|
|
|
|
})
|
|
|
|
expect(createTempDirectoryMock).toHaveBeenCalledTimes(1)
|
|
|
|
expect(downloadCacheMock).toHaveBeenCalledWith(
|
|
|
|
cacheEntry.archiveLocation,
|
2020-07-10 15:09:32 +00:00
|
|
|
archivePath,
|
|
|
|
undefined
|
2020-05-06 15:10:18 +00:00
|
|
|
)
|
2021-05-03 15:09:44 +00:00
|
|
|
expect(getArchiveFileSizeInBytesMock).toHaveBeenCalledWith(archivePath)
|
2020-05-06 15:10:18 +00:00
|
|
|
|
|
|
|
expect(extractTarMock).toHaveBeenCalledTimes(1)
|
|
|
|
expect(extractTarMock).toHaveBeenCalledWith(archivePath, compression)
|
|
|
|
|
|
|
|
expect(unlinkFileMock).toHaveBeenCalledTimes(1)
|
|
|
|
expect(unlinkFileMock).toHaveBeenCalledWith(archivePath)
|
|
|
|
|
|
|
|
expect(getCompressionMock).toHaveBeenCalledTimes(1)
|
|
|
|
})
|
|
|
|
|
2020-05-15 16:18:50 +00:00
|
|
|
test('restore with zstd compressed cache found', async () => {
|
2020-05-06 21:53:22 +00:00
|
|
|
const paths = ['node_modules']
|
2020-05-06 15:10:18 +00:00
|
|
|
const key = 'node-test'
|
|
|
|
|
|
|
|
const infoMock = jest.spyOn(core, 'info')
|
|
|
|
|
|
|
|
const cacheEntry: ArtifactCacheEntry = {
|
|
|
|
cacheKey: key,
|
2020-07-21 15:33:05 +00:00
|
|
|
scope: 'refs/heads/main',
|
2020-05-06 15:10:18 +00:00
|
|
|
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')
|
|
|
|
createTempDirectoryMock.mockImplementation(async () => {
|
|
|
|
return Promise.resolve(tempPath)
|
|
|
|
})
|
|
|
|
|
|
|
|
const archivePath = path.join(tempPath, CacheFilename.Zstd)
|
|
|
|
const downloadCacheMock = jest.spyOn(cacheHttpClient, 'downloadCache')
|
|
|
|
|
|
|
|
const fileSize = 62915000
|
2021-05-03 15:09:44 +00:00
|
|
|
const getArchiveFileSizeInBytesMock = jest
|
|
|
|
.spyOn(cacheUtils, 'getArchiveFileSizeInBytes')
|
2020-05-06 15:10:18 +00:00
|
|
|
.mockReturnValue(fileSize)
|
|
|
|
|
|
|
|
const extractTarMock = jest.spyOn(tar, 'extractTar')
|
|
|
|
const compression = CompressionMethod.Zstd
|
|
|
|
const getCompressionMock = jest
|
|
|
|
.spyOn(cacheUtils, 'getCompressionMethod')
|
|
|
|
.mockReturnValue(Promise.resolve(compression))
|
|
|
|
|
2020-05-06 21:53:22 +00:00
|
|
|
const cacheKey = await restoreCache(paths, key)
|
2020-05-06 15:10:18 +00:00
|
|
|
|
2020-05-06 21:53:22 +00:00
|
|
|
expect(cacheKey).toBe(key)
|
|
|
|
expect(getCacheMock).toHaveBeenCalledWith([key], paths, {
|
2020-05-06 15:10:18 +00:00
|
|
|
compressionMethod: compression
|
|
|
|
})
|
|
|
|
expect(createTempDirectoryMock).toHaveBeenCalledTimes(1)
|
|
|
|
expect(downloadCacheMock).toHaveBeenCalledWith(
|
|
|
|
cacheEntry.archiveLocation,
|
2020-07-10 15:09:32 +00:00
|
|
|
archivePath,
|
|
|
|
undefined
|
2020-05-06 15:10:18 +00:00
|
|
|
)
|
2021-05-03 15:09:44 +00:00
|
|
|
expect(getArchiveFileSizeInBytesMock).toHaveBeenCalledWith(archivePath)
|
2020-05-06 15:10:18 +00:00
|
|
|
expect(infoMock).toHaveBeenCalledWith(`Cache Size: ~60 MB (62915000 B)`)
|
|
|
|
|
|
|
|
expect(extractTarMock).toHaveBeenCalledTimes(1)
|
|
|
|
expect(extractTarMock).toHaveBeenCalledWith(archivePath, compression)
|
|
|
|
expect(getCompressionMock).toHaveBeenCalledTimes(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('restore with cache found for restore key', async () => {
|
2020-05-06 21:53:22 +00:00
|
|
|
const paths = ['node_modules']
|
2020-05-06 15:10:18 +00:00
|
|
|
const key = 'node-test'
|
|
|
|
const restoreKey = 'node-'
|
|
|
|
|
|
|
|
const infoMock = jest.spyOn(core, 'info')
|
|
|
|
|
|
|
|
const cacheEntry: ArtifactCacheEntry = {
|
|
|
|
cacheKey: restoreKey,
|
2020-07-21 15:33:05 +00:00
|
|
|
scope: 'refs/heads/main',
|
2020-05-06 15:10:18 +00:00
|
|
|
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')
|
|
|
|
createTempDirectoryMock.mockImplementation(async () => {
|
|
|
|
return Promise.resolve(tempPath)
|
|
|
|
})
|
|
|
|
|
|
|
|
const archivePath = path.join(tempPath, CacheFilename.Zstd)
|
|
|
|
const downloadCacheMock = jest.spyOn(cacheHttpClient, 'downloadCache')
|
|
|
|
|
|
|
|
const fileSize = 142
|
2021-05-03 15:09:44 +00:00
|
|
|
const getArchiveFileSizeInBytesMock = jest
|
|
|
|
.spyOn(cacheUtils, 'getArchiveFileSizeInBytes')
|
2020-05-06 15:10:18 +00:00
|
|
|
.mockReturnValue(fileSize)
|
|
|
|
|
|
|
|
const extractTarMock = jest.spyOn(tar, 'extractTar')
|
|
|
|
const compression = CompressionMethod.Zstd
|
|
|
|
const getCompressionMock = jest
|
|
|
|
.spyOn(cacheUtils, 'getCompressionMethod')
|
|
|
|
.mockReturnValue(Promise.resolve(compression))
|
|
|
|
|
2020-05-06 21:53:22 +00:00
|
|
|
const cacheKey = await restoreCache(paths, key, [restoreKey])
|
2020-05-06 15:10:18 +00:00
|
|
|
|
2020-05-06 21:53:22 +00:00
|
|
|
expect(cacheKey).toBe(restoreKey)
|
|
|
|
expect(getCacheMock).toHaveBeenCalledWith([key, restoreKey], paths, {
|
2020-05-06 15:10:18 +00:00
|
|
|
compressionMethod: compression
|
|
|
|
})
|
|
|
|
expect(createTempDirectoryMock).toHaveBeenCalledTimes(1)
|
|
|
|
expect(downloadCacheMock).toHaveBeenCalledWith(
|
|
|
|
cacheEntry.archiveLocation,
|
2020-07-10 15:09:32 +00:00
|
|
|
archivePath,
|
|
|
|
undefined
|
2020-05-06 15:10:18 +00:00
|
|
|
)
|
2021-05-03 15:09:44 +00:00
|
|
|
expect(getArchiveFileSizeInBytesMock).toHaveBeenCalledWith(archivePath)
|
2020-05-06 15:10:18 +00:00
|
|
|
expect(infoMock).toHaveBeenCalledWith(`Cache Size: ~0 MB (142 B)`)
|
|
|
|
|
|
|
|
expect(extractTarMock).toHaveBeenCalledTimes(1)
|
|
|
|
expect(extractTarMock).toHaveBeenCalledWith(archivePath, compression)
|
|
|
|
expect(getCompressionMock).toHaveBeenCalledTimes(1)
|
|
|
|
})
|