Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Link- authored Dec 2, 2024
1 parent a762876 commit 87171e2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 46 deletions.
39 changes: 21 additions & 18 deletions packages/cache/__tests__/saveCacheV2.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as core from '@actions/core'
import * as path from 'path'
import {saveCache} from '../src/cache'
import { saveCache } from '../src/cache'
import * as cacheUtils from '../src/internal/cacheUtils'
import {CacheFilename, CompressionMethod} from '../src/internal/constants'
import { CacheFilename, CompressionMethod } from '../src/internal/constants'
import * as config from '../src/internal/config'
import * as tar from '../src/internal/tar'
import {CacheServiceClientJSON} from '../src/generated/results/api/v1/cache.twirp'
import { CacheServiceClientJSON } from '../src/generated/results/api/v1/cache.twirp'
import * as cacheHttpClient from '../src/internal/cacheHttpClient'
import {UploadOptions} from '../src/options'
import { UploadOptions } from '../src/options'

let logDebugMock: jest.SpyInstance

Expand All @@ -27,11 +27,11 @@ jest.mock('@azure/storage-blob', () => ({

beforeAll(() => {
process.env['ACTIONS_RUNTIME_TOKEN'] = 'token'
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(() => {})
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(() => { })
jest.spyOn(cacheUtils, 'resolvePaths').mockImplementation(async filePaths => {
return filePaths.map(x => path.resolve(x))
})
Expand Down Expand Up @@ -99,7 +99,7 @@ test('create cache entry failure', async () => {

const createCacheEntryMock = jest
.spyOn(CacheServiceClientJSON.prototype, 'CreateCacheEntry')
.mockReturnValue(Promise.resolve({ok: false, signedUploadUrl: ''}))
.mockReturnValue(Promise.resolve({ ok: false, signedUploadUrl: '' }))

const createTarMock = jest.spyOn(tar, 'createTar')
const finalizeCacheEntryMock = jest.spyOn(
Expand Down Expand Up @@ -138,7 +138,9 @@ test('save cache fails if a signedUploadURL was not passed', async () => {
const key = 'Linux-node-bb828da54c148048dd17899ba9fda624811cfb43'
const cachePaths = [path.resolve(paths)]
const signedUploadURL = ''
const archiveFileSize = 1024
const options: UploadOptions = {
archiveSizeBytes: archiveFileSize, // These should always match
useAzureSdk: true,
uploadChunkSize: 64 * 1024 * 1024,
uploadConcurrency: 8
Expand All @@ -147,7 +149,7 @@ test('save cache fails if a signedUploadURL was not passed', async () => {
const createCacheEntryMock = jest
.spyOn(CacheServiceClientJSON.prototype, 'CreateCacheEntry')
.mockReturnValue(
Promise.resolve({ok: true, signedUploadUrl: signedUploadURL})
Promise.resolve({ ok: true, signedUploadUrl: signedUploadURL })
)

const createTarMock = jest.spyOn(tar, 'createTar')
Expand All @@ -159,7 +161,6 @@ test('save cache fails if a signedUploadURL was not passed', async () => {
.mockReturnValueOnce(Promise.resolve(compression))

const cacheVersion = cacheUtils.getCacheVersion([paths], compression)
const archiveFileSize = 1024
jest
.spyOn(cacheUtils, 'getArchiveFileSizeInBytes')
.mockReturnValueOnce(archiveFileSize)
Expand Down Expand Up @@ -195,7 +196,9 @@ test('finalize save cache failure', async () => {
const cachePaths = [path.resolve(paths)]
const logWarningMock = jest.spyOn(core, 'warning')
const signedUploadURL = 'https://blob-storage.local?signed=true'
const archiveFileSize = 1024
const options: UploadOptions = {
archiveSizeBytes: archiveFileSize, // These should always match
useAzureSdk: true,
uploadChunkSize: 64 * 1024 * 1024,
uploadConcurrency: 8
Expand All @@ -204,7 +207,7 @@ test('finalize save cache failure', async () => {
const createCacheEntryMock = jest
.spyOn(CacheServiceClientJSON.prototype, 'CreateCacheEntry')
.mockReturnValue(
Promise.resolve({ok: true, signedUploadUrl: signedUploadURL})
Promise.resolve({ ok: true, signedUploadUrl: signedUploadURL })
)

const createTarMock = jest.spyOn(tar, 'createTar')
Expand All @@ -218,14 +221,13 @@ test('finalize save cache failure', async () => {
.mockReturnValueOnce(Promise.resolve(compression))

const cacheVersion = cacheUtils.getCacheVersion([paths], compression)
const archiveFileSize = 1024
jest
.spyOn(cacheUtils, 'getArchiveFileSizeInBytes')
.mockReturnValueOnce(archiveFileSize)

const finalizeCacheEntryMock = jest
.spyOn(CacheServiceClientJSON.prototype, 'FinalizeCacheEntryUpload')
.mockReturnValue(Promise.resolve({ok: false, entryId: ''}))
.mockReturnValue(Promise.resolve({ ok: false, entryId: '' }))

const cacheId = await saveCache([paths], key, options)

Expand Down Expand Up @@ -268,13 +270,14 @@ test('save with valid inputs uploads a cache', async () => {
const cachePaths = [path.resolve(paths)]
const signedUploadURL = 'https://blob-storage.local?signed=true'
const createTarMock = jest.spyOn(tar, 'createTar')
const archiveFileSize = 1024
const options: UploadOptions = {
archiveSizeBytes: archiveFileSize, // These should always match
useAzureSdk: true,
uploadChunkSize: 64 * 1024 * 1024,
uploadConcurrency: 8
}

const archiveFileSize = 1024
jest
.spyOn(cacheUtils, 'getArchiveFileSizeInBytes')
.mockReturnValueOnce(archiveFileSize)
Expand All @@ -283,7 +286,7 @@ test('save with valid inputs uploads a cache', async () => {
jest
.spyOn(CacheServiceClientJSON.prototype, 'CreateCacheEntry')
.mockReturnValue(
Promise.resolve({ok: true, signedUploadUrl: signedUploadURL})
Promise.resolve({ ok: true, signedUploadUrl: signedUploadURL })
)

const saveCacheMock = jest.spyOn(cacheHttpClient, 'saveCache')
Expand All @@ -296,7 +299,7 @@ test('save with valid inputs uploads a cache', async () => {

const finalizeCacheEntryMock = jest
.spyOn(CacheServiceClientJSON.prototype, 'FinalizeCacheEntryUpload')
.mockReturnValue(Promise.resolve({ok: true, entryId: cacheId.toString()}))
.mockReturnValue(Promise.resolve({ ok: true, entryId: cacheId.toString() }))

const expectedCacheId = await saveCache([paths], key)

Expand Down
34 changes: 6 additions & 28 deletions packages/cache/__tests__/uploadUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as uploadUtils from '../src/internal/uploadUtils'
import {TransferProgressEvent} from '@azure/ms-rest-js'
import { TransferProgressEvent } from '@azure/ms-rest-js'

test('upload progress tracked correctly', () => {
const progress = new uploadUtils.UploadProgress(1000)
Expand All @@ -11,7 +11,7 @@ test('upload progress tracked correctly', () => {
expect(progress.getTransferredBytes()).toBe(0)
expect(progress.isDone()).toBe(false)

progress.onProgress()({loadedBytes: 0} as TransferProgressEvent)
progress.onProgress()({ loadedBytes: 0 } as TransferProgressEvent)

expect(progress.contentLength).toBe(1000)
expect(progress.sentBytes).toBe(0)
Expand All @@ -20,7 +20,7 @@ test('upload progress tracked correctly', () => {
expect(progress.getTransferredBytes()).toBe(0)
expect(progress.isDone()).toBe(false)

progress.onProgress()({loadedBytes: 250} as TransferProgressEvent)
progress.onProgress()({ loadedBytes: 250 } as TransferProgressEvent)

expect(progress.contentLength).toBe(1000)
expect(progress.sentBytes).toBe(250)
Expand All @@ -29,7 +29,7 @@ test('upload progress tracked correctly', () => {
expect(progress.getTransferredBytes()).toBe(250)
expect(progress.isDone()).toBe(false)

progress.onProgress()({loadedBytes: 500} as TransferProgressEvent)
progress.onProgress()({ loadedBytes: 500 } as TransferProgressEvent)

expect(progress.contentLength).toBe(1000)
expect(progress.sentBytes).toBe(500)
Expand All @@ -38,7 +38,7 @@ test('upload progress tracked correctly', () => {
expect(progress.getTransferredBytes()).toBe(500)
expect(progress.isDone()).toBe(false)

progress.onProgress()({loadedBytes: 750} as TransferProgressEvent)
progress.onProgress()({ loadedBytes: 750 } as TransferProgressEvent)

expect(progress.contentLength).toBe(1000)
expect(progress.sentBytes).toBe(750)
Expand All @@ -47,7 +47,7 @@ test('upload progress tracked correctly', () => {
expect(progress.getTransferredBytes()).toBe(750)
expect(progress.isDone()).toBe(false)

progress.onProgress()({loadedBytes: 1000} as TransferProgressEvent)
progress.onProgress()({ loadedBytes: 1000 } as TransferProgressEvent)

expect(progress.contentLength).toBe(1000)
expect(progress.sentBytes).toBe(1000)
Expand All @@ -56,25 +56,3 @@ test('upload progress tracked correctly', () => {
expect(progress.getTransferredBytes()).toBe(1000)
expect(progress.isDone()).toBe(true)
})

// test('upload to azure blob storage is successful', () => {
// const archivePath = 'path/to/archive.tzst'
// const signedUploadURL = 'https://storage10.blob.core.windows.net/cache-container/3fe-60?se=2024-12-002T11%3A08%3A58Z&sv=2024-11-04'
// const options: UploadOptions = {
// useAzureSdk: true,
// uploadChunkSize: 64 * 1024 * 1024,
// uploadConcurrency: 8
// }

// jest.spyOn(uploadUtils.UploadProgress.prototype, 'onProgress').mockImplementation(() => (progress: TransferProgressEvent) => {
// return progress.loadedBytes
// })

// jest.spyOn(uploadUtils.UploadProgress.prototype, 'onProgress').mockImplementation(() => (progress: TransferProgressEvent) => {
// return progress.loadedBytes
// })

// const response = uploadUtils.uploadCacheArchiveSDK(signedUploadURL, archivePath, options)

// expect(response).toBeInstanceOf(Promise)
// })

0 comments on commit 87171e2

Please sign in to comment.