mirror of
https://code.forgejo.org/actions/cache.git
synced 2025-02-13 11:40:21 -05:00
Update tests for deleteCacheByKey.
And rebuild. Signed-off-by: Gregorio Litenstein <g.litenstein@gmail.com>
This commit is contained in:
parent
debc4e75e7
commit
4043ab8d77
8 changed files with 7163 additions and 4838 deletions
|
@ -1,6 +1,5 @@
|
|||
import * as cache from "@actions/cache";
|
||||
import * as core from "@actions/core";
|
||||
import { RequestError } from "@octokit/request-error";
|
||||
import nock from "nock";
|
||||
|
||||
import { Events, RefKey } from "../src/constants";
|
||||
|
@ -214,7 +213,7 @@ test("getInputAsBool throws if required and value missing", () => {
|
|||
).toThrowError();
|
||||
});
|
||||
|
||||
test("deleteCacheByKey returns 'HttpError: 404' when cache is not found.", async () => {
|
||||
test("deleteCacheByKey produces 'HttpError: 404' when cache is not found.", async () => {
|
||||
const event = Events.Push;
|
||||
|
||||
process.env["GITHUB_REPOSITORY"] = "owner/repo";
|
||||
|
@ -232,14 +231,73 @@ test("deleteCacheByKey returns 'HttpError: 404' when cache is not found.", async
|
|||
expect(logWarningMock).toHaveBeenCalledWith(
|
||||
expect.stringMatching(/404: Not Found/i)
|
||||
);
|
||||
expect(response).toBeInstanceOf(RequestError);
|
||||
expect(response).toMatchObject({
|
||||
name: "HttpError",
|
||||
status: 404
|
||||
});
|
||||
expect(response).toBe(undefined);
|
||||
});
|
||||
|
||||
test("deleteCacheByKey returns 'HttpError: 401' on an invalid non-mocked request.", async () => {
|
||||
test("deleteCacheByKey does not delete anything if it finds more than one entry for the given key.", async () => {
|
||||
const event = Events.Push;
|
||||
|
||||
process.env["GITHUB_REPOSITORY"] = "owner/repo";
|
||||
process.env["GITHUB_TOKEN"] =
|
||||
"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";
|
||||
process.env["GITHUB_ACTION"] = "__owner___run-repo";
|
||||
process.env[Events.Key] = event;
|
||||
process.env[RefKey] = "";
|
||||
const logWarningMock = jest.spyOn(actionUtils, "logWarning");
|
||||
const response = await actionUtils.deleteCacheByKey(
|
||||
testUtils.failureCacheKey,
|
||||
"owner",
|
||||
"repo"
|
||||
);
|
||||
expect(logWarningMock).toHaveBeenCalledWith(
|
||||
`More than one cache entry found for key ${testUtils.failureCacheKey}`
|
||||
);
|
||||
expect(response).toBe(undefined);
|
||||
});
|
||||
|
||||
test("deleteCacheByKey does not delete anything if the key matches a cache belonging to another ref.", async () => {
|
||||
const event = Events.Push;
|
||||
|
||||
process.env["GITHUB_REPOSITORY"] = "owner/repo";
|
||||
process.env["GITHUB_TOKEN"] =
|
||||
"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";
|
||||
process.env["GITHUB_ACTION"] = "__owner___run-repo";
|
||||
process.env[Events.Key] = event;
|
||||
process.env[RefKey] = "ref/heads/feature";
|
||||
const logWarningMock = jest.spyOn(actionUtils, "logWarning");
|
||||
const response = await actionUtils.deleteCacheByKey(
|
||||
testUtils.wrongRefCacheKey,
|
||||
"owner",
|
||||
"repo"
|
||||
);
|
||||
expect(logWarningMock).toHaveBeenCalledWith(
|
||||
`No cache entries for key ${testUtils.wrongRefCacheKey} belong to gitref ${process.env[RefKey]}.`
|
||||
);
|
||||
expect(response).toBe(undefined);
|
||||
});
|
||||
|
||||
test("deleteCacheByKey produces 'HttpError: 404' when cache is not found.", async () => {
|
||||
const event = Events.Push;
|
||||
|
||||
process.env["GITHUB_REPOSITORY"] = "owner/repo";
|
||||
process.env["GITHUB_TOKEN"] =
|
||||
"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";
|
||||
process.env["GITHUB_ACTION"] = "__owner___run-repo";
|
||||
process.env[Events.Key] = event;
|
||||
process.env[RefKey] = "ref/heads/feature";
|
||||
const logWarningMock = jest.spyOn(actionUtils, "logWarning");
|
||||
const response = await actionUtils.deleteCacheByKey(
|
||||
testUtils.failureCacheKey,
|
||||
"owner",
|
||||
"repo"
|
||||
);
|
||||
expect(logWarningMock).toHaveBeenCalledWith(
|
||||
expect.stringMatching(/404: Not Found/i)
|
||||
);
|
||||
expect(response).toBe(undefined);
|
||||
});
|
||||
|
||||
test("deleteCacheByKey produces 'HttpError: 401' on an invalid non-mocked request.", async () => {
|
||||
const event = Events.Push;
|
||||
|
||||
process.env["GITHUB_REPOSITORY"] = "owner/repo";
|
||||
|
@ -258,15 +316,11 @@ test("deleteCacheByKey returns 'HttpError: 401' on an invalid non-mocked request
|
|||
expect(logWarningMock).toHaveBeenCalledWith(
|
||||
expect.stringMatching(/401: Bad Credentials/i)
|
||||
);
|
||||
expect(response).toBeInstanceOf(RequestError);
|
||||
expect(response).toMatchObject({
|
||||
name: "HttpError",
|
||||
status: 401
|
||||
});
|
||||
expect(response).toBe(undefined);
|
||||
nock.disableNetConnect();
|
||||
});
|
||||
|
||||
test("deleteCacheByKey returns matched cache data when successful.", async () => {
|
||||
test("deleteCacheByKey returns 204 / No Content when successful.", async () => {
|
||||
const event = Events.Push;
|
||||
|
||||
process.env["GITHUB_REPOSITORY"] = "owner/repo";
|
||||
|
@ -276,29 +330,13 @@ test("deleteCacheByKey returns matched cache data when successful.", async () =>
|
|||
process.env[Events.Key] = event;
|
||||
process.env[RefKey] = "ref/heads/feature";
|
||||
|
||||
const expectedResponse = {
|
||||
id: expect.any(Number),
|
||||
ref: expect.any(String),
|
||||
key: expect.any(String),
|
||||
version: expect.any(String),
|
||||
last_accessed_at: expect.any(String),
|
||||
created_at: expect.any(String),
|
||||
size_in_bytes: expect.any(Number)
|
||||
};
|
||||
const logWarningMock = jest.spyOn(actionUtils, "logWarning");
|
||||
const response = await actionUtils.deleteCacheByKey(
|
||||
testUtils.successCacheKey,
|
||||
"owner",
|
||||
"repo"
|
||||
);
|
||||
expect(response).toMatchObject({
|
||||
data: expect.objectContaining({
|
||||
total_count: expect.any(Number),
|
||||
actions_caches: expect.arrayContaining([
|
||||
expect.objectContaining(expectedResponse)
|
||||
])
|
||||
})
|
||||
});
|
||||
expect(response).toBe(204);
|
||||
expect(logWarningMock).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
|
||||
|
|
|
@ -492,7 +492,11 @@ test("save with cache hit and refresh-cache will try to delete and re-create ent
|
|||
);
|
||||
expect(infoMock).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
`Succesfully deleted cache with key: ${primaryKey}`
|
||||
expect.stringMatching(
|
||||
new RegExp(
|
||||
`Succesfully deleted cache with key: ${primaryKey}, id: \\d+`
|
||||
)
|
||||
)
|
||||
);
|
||||
expect(infoMock).toHaveBeenNthCalledWith(
|
||||
3,
|
||||
|
@ -565,7 +569,11 @@ test("Granular save will use lookup to determine if cache needs to be updated or
|
|||
);
|
||||
expect(infoMock).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
`Succesfully deleted cache with key: ${primaryKey}`
|
||||
expect.stringMatching(
|
||||
new RegExp(
|
||||
`Succesfully deleted cache with key: ${primaryKey}, id: \\d+`
|
||||
)
|
||||
)
|
||||
);
|
||||
expect(infoMock).toHaveBeenNthCalledWith(
|
||||
3,
|
||||
|
|
2589
dist/restore-only/index.js
vendored
2589
dist/restore-only/index.js
vendored
File diff suppressed because it is too large
Load diff
2589
dist/restore/index.js
vendored
2589
dist/restore/index.js
vendored
File diff suppressed because it is too large
Load diff
2590
dist/save-only/index.js
vendored
2590
dist/save-only/index.js
vendored
File diff suppressed because it is too large
Load diff
2590
dist/save/index.js
vendored
2590
dist/save/index.js
vendored
File diff suppressed because it is too large
Load diff
1424
package-lock.json
generated
1424
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -1,12 +1,16 @@
|
|||
/* istanbul ignore file */
|
||||
|
||||
import { Inputs } from "../constants";
|
||||
import { rest } from "msw";
|
||||
import { setupServer } from "msw/node";
|
||||
import nock from "nock";
|
||||
|
||||
export const successCacheKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43";
|
||||
export const wrongRefCacheKey = "Linux-latest-node-bb828da54c148048dd17899ba9fda624811cfb43";
|
||||
export const failureCacheKey = "Windows-node-bb828da54c148048dd17899ba9fda624811cfb43";
|
||||
export const passThroughCacheKey = "macOS-node-bb828da54c148048dd17899ba9fda624811cfb43";
|
||||
|
||||
const successCacheId = 1337;
|
||||
const failureCacheId = 69;
|
||||
|
||||
// See: https://github.com/actions/toolkit/blob/master/packages/core/src/core.ts#L67
|
||||
function getInputName(name: string): string {
|
||||
|
@ -56,31 +60,80 @@ export function clearInputs(): void {
|
|||
delete process.env[getInputName(Inputs.RefreshCache)];
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
export const mockServer = setupServer(rest.delete('https://api.github.com/repos/owner/repo/actions/caches', (req, res, ctx) => {
|
||||
if (req.url?.searchParams?.get('key') === failureCacheKey) {
|
||||
return res(ctx.status(404),
|
||||
export const mockServer = setupServer(
|
||||
rest.delete('https://api.github.com/repos/owner/repo/actions/caches/', (req, res, ctx) => {
|
||||
return res(ctx.status(422),
|
||||
ctx.json({
|
||||
message: "Not Found",
|
||||
documentation_url: "https://docs.github.com/rest/actions/cache#delete-github-actions-caches-for-a-repository-using-a-cache-key"
|
||||
}));
|
||||
}
|
||||
else if (req.url?.searchParams?.get('key') === successCacheKey) {
|
||||
return res(ctx.status(200),
|
||||
ctx.json({
|
||||
total_count: 1,
|
||||
actions_caches: [{
|
||||
id: 15,
|
||||
ref: "refs/heads/main",
|
||||
key: successCacheKey,
|
||||
version: "93a0f912fdb70083e929c1bf564bca2050be1c4e0932f7f9e78465ddcfbcc8f6",
|
||||
last_accessed_at: "2022-12-29T22:06:42.683333300Z",
|
||||
created_at: "2022-12-29T22:06:42.683333300Z",
|
||||
size_in_bytes: 6057793
|
||||
}]
|
||||
}));
|
||||
}
|
||||
else if (req.url?.searchParams?.get('key') === passThroughCacheKey) {
|
||||
message: "Invalid request.\n\nMissing required query parameter key",
|
||||
documentation_url: "https://docs.github.com/rest/actions/cache#delete-github-actions-caches-for-a-repository-using-a-cache-key",
|
||||
})
|
||||
)
|
||||
}),
|
||||
rest.delete('https://api.github.com/repos/owner/repo/actions/caches/:id', (req, res, ctx) => {
|
||||
const { id } = req.params;
|
||||
if (parseInt(id as string) === failureCacheId) {
|
||||
return res(ctx.status(404),
|
||||
ctx.json({
|
||||
message: "Not Found",
|
||||
documentation_url: "https://docs.github.com/rest/actions/cache#delete-a-github-actions-cache-for-a-repository-using-a-cache-id"
|
||||
}));
|
||||
}
|
||||
return res(ctx.status(204));
|
||||
}),
|
||||
// This endpoint always returns 200/OK, what we're checking here is whether we can get a unique cache ID, to avoid deleting the wrong entry.
|
||||
rest.get('https://api.github.com/repos/owner/repo/actions/caches', (req, res, ctx) => {
|
||||
let key : string = req.url?.searchParams?.get('key') || '';
|
||||
let ref : string = req.url?.searchParams?.get('ref') || '';
|
||||
if (key === '' || ref === '') {
|
||||
return res(ctx.status(200),
|
||||
ctx.json({
|
||||
total_count: 2,
|
||||
actions_caches: [{
|
||||
id: 15,
|
||||
ref: "refs/heads/main",
|
||||
key: failureCacheKey,
|
||||
version: "73885106f58cc52a7df9ec4d4a5622a5614813162cb516c759a30af6bf56e6f0",
|
||||
last_accessed_at: "2022-12-29T22:06:42.683333300Z",
|
||||
created_at: "2022-12-29T22:06:42.683333300Z",
|
||||
size_in_bytes: 6057793
|
||||
},
|
||||
{
|
||||
id: 16,
|
||||
ref: "refs/heads/another-feature-branch",
|
||||
key: failureCacheKey,
|
||||
version: "73885106f58cc52a7df9ec4d4a5622a5614813162cb516c759a30af6bf56e6f0",
|
||||
last_accessed_at: "2022-12-29T22:06:42.683333300Z",
|
||||
created_at: "2022-12-29T22:06:42.683333300Z",
|
||||
size_in_bytes: 6057793
|
||||
}]
|
||||
})
|
||||
);
|
||||
}
|
||||
// This is the behavior seen when search doesn't find anything, but it is seen both when no key matches, as well as when the key matches but the entry belongs to another (likely the base) branch.
|
||||
else if (key === wrongRefCacheKey) {
|
||||
return res(ctx.status(200),
|
||||
ctx.json({
|
||||
total_count: 0,
|
||||
actions_caches: []
|
||||
})
|
||||
);
|
||||
}
|
||||
else if (key === successCacheKey || key === failureCacheKey) {
|
||||
return res(ctx.status(200),
|
||||
ctx.json({
|
||||
total_count: 1,
|
||||
actions_caches: [{
|
||||
id: (key === successCacheKey ? successCacheId : failureCacheId),
|
||||
ref: ref,
|
||||
key: key,
|
||||
version: "93a0f912fdb70083e929c1bf564bca2050be1c4e0932f7f9e78465ddcfbcc8f6",
|
||||
last_accessed_at: "2022-12-29T22:06:42.683333300Z",
|
||||
created_at: "2022-12-29T22:06:42.683333300Z",
|
||||
size_in_bytes: 6057793
|
||||
}]
|
||||
})
|
||||
);
|
||||
}
|
||||
return req.passthrough();
|
||||
}
|
||||
}));
|
||||
})
|
||||
);
|
Loading…
Add table
Reference in a new issue