This commit is contained in:
ddivad195
2024-02-06 18:27:04 +00:00
parent 6d082c4eab
commit 4fb632b14a
+77 -35
View File
@@ -22,14 +22,14 @@ const tarFile: fsHelper.FileMetadata = {
sha256: genericSha sha256: genericSha
} }
const headMockNoExistingBlobs = (url: string, options: any) => { const headMockNoExistingBlobs = (): object => {
// Simulate none of the blobs existing currently // Simulate none of the blobs existing currently
return { return {
status: 404 status: 404
} }
} }
const headMockAllExistingBlobs = (url: string, options: any) => { const headMockAllExistingBlobs = (): object => {
// Simulate all of the blobs existing currently // Simulate all of the blobs existing currently
return { return {
status: 200 status: 200
@@ -37,7 +37,7 @@ const headMockAllExistingBlobs = (url: string, options: any) => {
} }
let count = 0 let count = 0
const headMockSomeExistingBlobs = (url: string, options: any) => { const headMockSomeExistingBlobs = (): object => {
count++ count++
// report one as existing // report one as existing
if (count === 1) { if (count === 1) {
@@ -52,13 +52,13 @@ const headMockSomeExistingBlobs = (url: string, options: any) => {
} }
} }
const headMockFailure = (url: string, options: any) => { const headMockFailure = (): object => {
return { return {
status: 503 status: 503
} }
} }
const postMockSuccessfulIniationForAllBlobs = (url: string, options: any) => { const postMockSuccessfulIniationForAllBlobs = (): object => {
// Simulate successful initiation of uploads for all blobs & return location // Simulate successful initiation of uploads for all blobs & return location
return { return {
status: 202, status: 202,
@@ -72,14 +72,14 @@ const postMockSuccessfulIniationForAllBlobs = (url: string, options: any) => {
} }
} }
const postMockFailure = (url: string, options: any) => { const postMockFailure = (): object => {
// Simulate failed initiation of uploads // Simulate failed initiation of uploads
return { return {
status: 503 status: 503
} }
} }
const postMockNoLocationHeader = (url: string, options: any) => { const postMockNoLocationHeader = (): object => {
return { return {
status: 202, status: 202,
headers: { headers: {
@@ -88,9 +88,9 @@ const postMockNoLocationHeader = (url: string, options: any) => {
} }
} }
const putMockSuccessfulBlobUpload = (url: string, options: any) => { const putMockSuccessfulBlobUpload = (url: string): object => {
// Simulate successful upload of all blobs & then the manifest // Simulate successful upload of all blobs & then the manifest
if ((url as string).includes('manifest')) { if (url.includes('manifest')) {
return { return {
status: 201, status: 201,
headers: { headers: {
@@ -107,39 +107,51 @@ const putMockSuccessfulBlobUpload = (url: string, options: any) => {
} }
} }
const putMockFailure = (url: string, options: any) => { const putMockFailure = (): object => {
// Simulate fails upload of all blobs & manifest // Simulate fails upload of all blobs & manifest
return { return {
status: 500 status: 500
} }
} }
const putMockFailureManifestUpload = (url: string, options: any) => { const putMockFailureManifestUpload = (url: string): object => {
// Simulate unsuccessful upload of all blobs & then the manifest // Simulate unsuccessful upload of all blobs & then the manifest
if (url.includes('manifest')) { if (url.includes('manifest')) {
return { return {
status: 500 status: 500
}; }
} }
return { return {
status: 201 status: 201
}; }
} }
function configureFetchMock(fetchMock: jest.SpyInstance, methodHandlers: any) { type MethodHandlers = {
fetchMock.mockImplementation(async (url: string, options: any) => { getMock?: (url: string, options: { method: string }) => object
validateRequestConfig(url, options) headMock?: (url: string, options: { method: string }) => object
switch (options.method) { postMock?: (url: string, options: { method: string }) => object
case 'GET': putMock?: (url: string, options: { method: string }) => object
return methodHandlers.getMock(url, options) }
case 'HEAD':
return methodHandlers.headMock(url, options) function configureFetchMock(
case 'POST': fetchMockInstance: jest.SpyInstance,
return methodHandlers.postMock(url, options) methodHandlers: MethodHandlers
case 'PUT': ): void {
return methodHandlers.putMock(url, options) fetchMockInstance.mockImplementation(
async (url: string, options: { method: string }) => {
validateRequestConfig(url, options)
switch (options.method) {
case 'GET':
return methodHandlers.getMock?.(url, options)
case 'HEAD':
return methodHandlers.headMock?.(url, options)
case 'POST':
return methodHandlers.postMock?.(url, options)
case 'PUT':
return methodHandlers.putMock?.(url, options)
}
} }
}); )
} }
const testManifest: ociContainer.Manifest = { const testManifest: ociContainer.Manifest = {
@@ -202,7 +214,11 @@ describe('publishOCIArtifact', () => {
}) })
it('publishes layer blobs & then a manifest to the provided registry', async () => { it('publishes layer blobs & then a manifest to the provided registry', async () => {
configureFetchMock(fetchMock, { headMock: headMockNoExistingBlobs, postMock: postMockSuccessfulIniationForAllBlobs, putMock: putMockSuccessfulBlobUpload }) configureFetchMock(fetchMock, {
headMock: headMockNoExistingBlobs,
postMock: postMockSuccessfulIniationForAllBlobs,
putMock: putMockSuccessfulBlobUpload
})
// Simulate successful reading of all the files // Simulate successful reading of all the files
fsReadFileSyncMock.mockImplementation(() => { fsReadFileSyncMock.mockImplementation(() => {
@@ -232,7 +248,11 @@ describe('publishOCIArtifact', () => {
}) })
it('skips uploading all layer blobs when they all already exist', async () => { it('skips uploading all layer blobs when they all already exist', async () => {
configureFetchMock(fetchMock, { headMock: headMockAllExistingBlobs, postMock: postMockSuccessfulIniationForAllBlobs, putMock: putMockSuccessfulBlobUpload }) configureFetchMock(fetchMock, {
headMock: headMockAllExistingBlobs,
postMock: postMockSuccessfulIniationForAllBlobs,
putMock: putMockSuccessfulBlobUpload
})
// Simulate successful reading of all the files // Simulate successful reading of all the files
fsReadFileSyncMock.mockImplementation(() => { fsReadFileSyncMock.mockImplementation(() => {
@@ -263,8 +283,12 @@ describe('publishOCIArtifact', () => {
}) })
it('skips uploading layer blobs that already exist', async () => { it('skips uploading layer blobs that already exist', async () => {
configureFetchMock(fetchMock, { headMock: headMockSomeExistingBlobs, postMock: postMockSuccessfulIniationForAllBlobs, putMock: putMockSuccessfulBlobUpload }) configureFetchMock(fetchMock, {
count = 0; headMock: headMockSomeExistingBlobs,
postMock: postMockSuccessfulIniationForAllBlobs,
putMock: putMockSuccessfulBlobUpload
})
count = 0
// Simulate successful reading of all the files // Simulate successful reading of all the files
fsReadFileSyncMock.mockImplementation(() => { fsReadFileSyncMock.mockImplementation(() => {
@@ -311,7 +335,10 @@ describe('publishOCIArtifact', () => {
}) })
it('throws an error if initiating layer upload fails', async () => { it('throws an error if initiating layer upload fails', async () => {
configureFetchMock(fetchMock, { headMock: headMockNoExistingBlobs, postMock: postMockFailure }) configureFetchMock(fetchMock, {
headMock: headMockNoExistingBlobs,
postMock: postMockFailure
})
await expect( await expect(
publishOCIArtifact( publishOCIArtifact(
@@ -327,7 +354,10 @@ describe('publishOCIArtifact', () => {
}) })
it('throws an error if the upload endpoint does not return a location', async () => { it('throws an error if the upload endpoint does not return a location', async () => {
configureFetchMock(fetchMock, { headMock: headMockNoExistingBlobs, postMock: postMockNoLocationHeader }) configureFetchMock(fetchMock, {
headMock: headMockNoExistingBlobs,
postMock: postMockNoLocationHeader
})
await expect( await expect(
publishOCIArtifact( publishOCIArtifact(
@@ -343,7 +373,11 @@ describe('publishOCIArtifact', () => {
}) })
it('throws an error if a layer upload fails', async () => { it('throws an error if a layer upload fails', async () => {
configureFetchMock(fetchMock, { headMock: headMockNoExistingBlobs, postMock: postMockSuccessfulIniationForAllBlobs, putMock: putMockFailure }) configureFetchMock(fetchMock, {
headMock: headMockNoExistingBlobs,
postMock: postMockSuccessfulIniationForAllBlobs,
putMock: putMockFailure
})
// Simulate successful reading of all the files // Simulate successful reading of all the files
fsReadFileSyncMock.mockImplementation(() => { fsReadFileSyncMock.mockImplementation(() => {
@@ -364,7 +398,11 @@ describe('publishOCIArtifact', () => {
}) })
it('throws an error if a manifest upload fails', async () => { it('throws an error if a manifest upload fails', async () => {
configureFetchMock(fetchMock, { headMock: headMockNoExistingBlobs, postMock: postMockSuccessfulIniationForAllBlobs, putMock: putMockFailureManifestUpload }) configureFetchMock(fetchMock, {
headMock: headMockNoExistingBlobs,
postMock: postMockSuccessfulIniationForAllBlobs,
putMock: putMockFailureManifestUpload
})
// Simulate successful reading of all the files // Simulate successful reading of all the files
fsReadFileSyncMock.mockImplementation(() => { fsReadFileSyncMock.mockImplementation(() => {
@@ -385,7 +423,11 @@ describe('publishOCIArtifact', () => {
}) })
it('throws an error if reading one of the files fails', async () => { it('throws an error if reading one of the files fails', async () => {
configureFetchMock(fetchMock, { headMock: headMockNoExistingBlobs, postMock: postMockSuccessfulIniationForAllBlobs, putMock: putMockSuccessfulBlobUpload }) configureFetchMock(fetchMock, {
headMock: headMockNoExistingBlobs,
postMock: postMockSuccessfulIniationForAllBlobs,
putMock: putMockSuccessfulBlobUpload
})
// Simulate successful reading of all the files // Simulate successful reading of all the files
fsReadFileSyncMock.mockImplementation(() => { fsReadFileSyncMock.mockImplementation(() => {