Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache: Add support for explicitly choosing compression, and zero compression #1877

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions .github/workflows/cache-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
strategy:
matrix:
runs-on: [ubuntu-latest, windows-latest, macOS-latest]
compression: [auto, gzip, zstd, none]
fail-fast: false

runs-on: ${{ matrix.runs-on }}
Expand Down Expand Up @@ -55,8 +56,14 @@ jobs:

# We're using node -e to call the functions directly available in the @actions/cache package
- name: Save cache using saveCache()
run: |
node -e "Promise.resolve(require('./packages/cache/lib/cache').saveCache(['test-cache','~/test-cache'],'test-${{ runner.os }}-${{ github.run_id }}'))"
run: >
node -e "Promise.resolve(require('./packages/cache/lib/cache').saveCache(
['test-cache','~/test-cache'],
'test-${{ runner.os }}-${{ github.run_id }}',
undefined,
false,
'${{ matrix.compression }}'
))"

- name: Delete cache folders before restoring
shell: bash
Expand All @@ -65,8 +72,15 @@ jobs:
rm -rf ~/test-cache

- name: Restore cache using restoreCache() with http-client
run: |
node -e "Promise.resolve(require('./packages/cache/lib/cache').restoreCache(['test-cache','~/test-cache'],'test-${{ runner.os }}-${{ github.run_id }}',[],{useAzureSdk: false}))"
run: >
node -e "Promise.resolve(require('./packages/cache/lib/cache').restoreCache(
['test-cache','~/test-cache'],
'test-${{ runner.os }}-${{ github.run_id }}',
[],
{useAzureSdk: false},
false,
'${{ matrix.compression }}'
))"

- name: Verify cache restored with http-client
shell: bash
Expand Down
65 changes: 65 additions & 0 deletions packages/cache/__tests__/restoreCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,71 @@ test('restore with zstd compressed cache found', async () => {
expect(getCompressionMock).toHaveBeenCalledTimes(1)
})

test('restore with uncompressed cache found', async () => {
const paths = ['node_modules']
const key = 'node-test'

const infoMock = jest.spyOn(core, 'info')

const cacheEntry: ArtifactCacheEntry = {
cacheKey: key,
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')
createTempDirectoryMock.mockImplementation(async () => {
return Promise.resolve(tempPath)
})

const archivePath = path.join(tempPath, CacheFilename.None)
const downloadCacheMock = jest.spyOn(cacheHttpClient, 'downloadCache')

const fileSize = 62915000
const getArchiveFileSizeInBytesMock = jest
.spyOn(cacheUtils, 'getArchiveFileSizeInBytes')
.mockReturnValue(fileSize)

const extractTarMock = jest.spyOn(tar, 'extractTar')
const compression = CompressionMethod.None
const getCompressionMock = jest
.spyOn(cacheUtils, 'getCompressionMethod')
.mockReturnValue(Promise.resolve(compression))

const cacheKey = await restoreCache(
paths,
key,
undefined,
undefined,
false,
CompressionMethod.None
)

expect(cacheKey).toBe(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(infoMock).toHaveBeenCalledWith(`Cache Size: ~60 MB (62915000 B)`)

expect(extractTarMock).toHaveBeenCalledTimes(1)
expect(extractTarMock).toHaveBeenCalledWith(archivePath, compression)
// We can only use no compression by specifying it explicitly
expect(getCompressionMock).toHaveBeenCalledTimes(0)
})

test('restore with cache found for restore key', async () => {
const paths = ['node_modules']
const key = 'node-test'
Expand Down
52 changes: 50 additions & 2 deletions packages/cache/__tests__/saveCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {CacheFilename, CompressionMethod} from '../src/internal/constants'
import * as tar from '../src/internal/tar'
import {TypedResponse} from '@actions/http-client/lib/interfaces'
import {
ReserveCacheResponse,
ITypedResponseWithError
ITypedResponseWithError,
ReserveCacheResponse
} from '../src/internal/contracts'
import {HttpClientError} from '@actions/http-client'

Expand Down Expand Up @@ -329,6 +329,54 @@ test('save with valid inputs uploads a cache', async () => {
expect(getCompressionMock).toHaveBeenCalledTimes(1)
})

test('upload a cache without compression', async () => {
const filePath = 'node_modules'
const primaryKey = 'Linux-node-bb828da54c148048dd17899ba9fda624811cfb43'
const cachePaths = [path.resolve(filePath)]

const cacheId = 4
const reserveCacheMock = jest
.spyOn(cacheHttpClient, 'reserveCache')
.mockImplementation(async () => {
const response: TypedResponse<ReserveCacheResponse> = {
statusCode: 500,
result: {cacheId},
headers: {}
}
return response
})
const createTarMock = jest.spyOn(tar, 'createTar')

const saveCacheMock = jest.spyOn(cacheHttpClient, 'saveCache')
const getCompressionMock = jest.spyOn(cacheUtils, 'getCompressionMethod')

await saveCache(
[filePath],
primaryKey,
undefined,
false,
CompressionMethod.None
)

expect(reserveCacheMock).toHaveBeenCalledTimes(1)
expect(reserveCacheMock).toHaveBeenCalledWith(primaryKey, [filePath], {
cacheSize: undefined,
compressionMethod: CompressionMethod.None,
enableCrossOsArchive: false
})
const archiveFolder = '/foo/bar'
const archiveFile = path.join(archiveFolder, CacheFilename.None)
expect(createTarMock).toHaveBeenCalledTimes(1)
expect(createTarMock).toHaveBeenCalledWith(
archiveFolder,
cachePaths,
CompressionMethod.None
)
expect(saveCacheMock).toHaveBeenCalledTimes(1)
expect(saveCacheMock).toHaveBeenCalledWith(cacheId, archiveFile, undefined)
expect(getCompressionMock).toHaveBeenCalledTimes(0)
})

test('save with non existing path should not save cache', async () => {
const path = 'node_modules'
const primaryKey = 'Linux-node-bb828da54c148048dd17899ba9fda624811cfb43'
Expand Down
102 changes: 102 additions & 0 deletions packages/cache/__tests__/tar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,40 @@ afterAll(async () => {
await jest.requireActual('@actions/io').rmRF(getTempDir())
})

test('extract tar', async () => {
const mkdirMock = jest.spyOn(io, 'mkdirP')
const execMock = jest.spyOn(exec, 'exec')

const archivePath = IS_WINDOWS
? `${process.env['windir']}\\fakepath\\cache.tar`
: 'cache.tar'
const workspace = process.env['GITHUB_WORKSPACE']
const tarPath = IS_WINDOWS ? GnuTarPathOnWindows : defaultTarPath

await tar.extractTar(archivePath, CompressionMethod.None)

expect(mkdirMock).toHaveBeenCalledWith(workspace)
expect(execMock).toHaveBeenCalledTimes(1)
expect(execMock).toHaveBeenCalledWith(
[
`"${tarPath}"`,
'-xf',
IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath,
'-P',
'-C',
IS_WINDOWS ? workspace?.replace(/\\/g, '/') : workspace
]
.concat(IS_WINDOWS ? ['--force-local'] : [])
.concat(IS_MAC ? ['--delay-directory-restore'] : [])
.join(' '),
undefined,
{
cwd: undefined,
env: expect.objectContaining(defaultEnv)
}
)
})

test('zstd extract tar', async () => {
const mkdirMock = jest.spyOn(io, 'mkdirP')
const execMock = jest.spyOn(exec, 'exec')
Expand Down Expand Up @@ -201,6 +235,45 @@ test('gzip extract GNU tar on windows with GNUtar in path', async () => {
}
})

test('create tar', async () => {
const execMock = jest.spyOn(exec, 'exec')

const archiveFolder = getTempDir()
const workspace = process.env['GITHUB_WORKSPACE']
const sourceDirectories = ['~/.npm/cache', `${workspace}/dist`]

await fs.promises.mkdir(archiveFolder, {recursive: true})

await tar.createTar(archiveFolder, sourceDirectories, CompressionMethod.None)

const tarPath = IS_WINDOWS ? GnuTarPathOnWindows : defaultTarPath

expect(execMock).toHaveBeenCalledTimes(1)
expect(execMock).toHaveBeenCalledWith(
[
`"${tarPath}"`,
'--posix',
'-cf',
IS_WINDOWS ? CacheFilename.None.replace(/\\/g, '/') : CacheFilename.None,
'--exclude',
IS_WINDOWS ? CacheFilename.None.replace(/\\/g, '/') : CacheFilename.None,
'-P',
'-C',
IS_WINDOWS ? workspace?.replace(/\\/g, '/') : workspace,
'--files-from',
ManifestFilename
]
.concat(IS_WINDOWS ? ['--force-local'] : [])
.concat(IS_MAC ? ['--delay-directory-restore'] : [])
.join(' '),
undefined, // args
{
cwd: archiveFolder,
env: expect.objectContaining(defaultEnv)
}
)
})

test('zstd create tar', async () => {
const execMock = jest.spyOn(exec, 'exec')

Expand Down Expand Up @@ -345,6 +418,35 @@ test('gzip create tar', async () => {
)
})

test('list tar', async () => {
const execMock = jest.spyOn(exec, 'exec')

const archivePath = IS_WINDOWS
? `${process.env['windir']}\\fakepath\\cache.tar`
: 'cache.tar'

await tar.listTar(archivePath, CompressionMethod.None)

const tarPath = IS_WINDOWS ? GnuTarPathOnWindows : defaultTarPath
expect(execMock).toHaveBeenCalledTimes(1)
expect(execMock).toHaveBeenCalledWith(
[
`"${tarPath}"`,
'-tf',
IS_WINDOWS ? archivePath.replace(/\\/g, '/') : archivePath,
'-P'
]
.concat(IS_WINDOWS ? ['--force-local'] : [])
.concat(IS_MAC ? ['--delay-directory-restore'] : [])
.join(' '),
undefined,
{
cwd: undefined,
env: expect.objectContaining(defaultEnv)
}
)
})

test('zstd list tar', async () => {
const execMock = jest.spyOn(exec, 'exec')

Expand Down
Loading