From df5639170ce5832993e7b4a6e249b74af8c9c709 Mon Sep 17 00:00:00 2001 From: boxofyellow <54955040+boxofyellow@users.noreply.github.com> Date: Thu, 25 Jan 2024 09:00:06 -0800 Subject: [PATCH] add test for **_some_** of the layer to already be there --- __tests__/ghcr-client.test.ts | 64 ++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/__tests__/ghcr-client.test.ts b/__tests__/ghcr-client.test.ts index 4a0e431..d039fe8 100644 --- a/__tests__/ghcr-client.test.ts +++ b/__tests__/ghcr-client.test.ts @@ -137,7 +137,7 @@ describe('publishOCIArtifact', () => { // TODO: Check that the base64 encoded token is sent in the Authorization header }) - it('skips uploading layer blobs that already exist', async () => { + it('skips uploading all layer blobs when they all already exist', async () => { // Simulate all blobs already existing axiosHeadMock.mockImplementation(async (url, config) => { validateRequestConfig(200, url, config) @@ -187,6 +187,68 @@ describe('publishOCIArtifact', () => { expect(axiosPutMock).toHaveBeenCalledTimes(1) }) + it('skips uploading layer blobs that already exist', async () => { + // Simulate some blobs already existing + + var count = 0 + axiosHeadMock.mockImplementation(async (url, config) => { + count++ + if (count === 1) { + // report the first blob as being there + validateRequestConfig(200, url, config) + return { + status: 200 + } + } else { + // report all others are missing + validateRequestConfig(404, url, config) + return { + status: 404 + } + } + }) + + // Simulate successful initiation of uploads for all blobs & return location + axiosPostMock.mockImplementation(async (url, data, config) => { + validateRequestConfig(202, url, config) + return { + status: 202, + headers: { + location: 'https://ghcr.io/v2/test/test/blobs/uploads/1234567890' + } + } + }) + + // Simulate successful reading of all the files + fsReadFileSyncMock.mockImplementation(() => { + return Buffer.from('test') + }) + + // Simulate successful upload of all blobs & then the manifest + axiosPutMock.mockImplementation(async (url, data, config) => { + validateRequestConfig(201, url, config) + return { + status: 201 + } + }) + + await publishOCIArtifact( + token, + registry, + repository, + releaseId, + semver, + zipFile, + tarFile, + testManifest + ) + + // We should only head all the blobs and then upload the missing blobs and manifest + expect(axiosHeadMock).toHaveBeenCalledTimes(3) + expect(axiosPostMock).toHaveBeenCalledTimes(2) + expect(axiosPutMock).toHaveBeenCalledTimes(3) + }) + it('throws an error if checking for existing blobs fails', async () => { // Simulate failed response code axiosHeadMock.mockImplementation(async (url, config) => {