mirror of
https://github.com/github/codeql-action.git
synced 2026-10-30 01:27:22 +00:00
Add unit test for cleanup
This commit is contained in:
@@ -6,20 +6,25 @@ import test from "ava";
|
||||
import * as sinon from "sinon";
|
||||
|
||||
import * as actionsUtil from "./actions-util";
|
||||
import * as apiClient from "./api-client";
|
||||
import {
|
||||
setCodeQL,
|
||||
getTrapCachingExtractorConfigArgs,
|
||||
getTrapCachingExtractorConfigArgsForLang,
|
||||
} from "./codeql";
|
||||
import * as configUtils from "./config-utils";
|
||||
import { Feature } from "./feature-flags";
|
||||
import { Language } from "./languages";
|
||||
import { getRunnerLogger } from "./logging";
|
||||
import {
|
||||
createFeatures,
|
||||
createTestConfig,
|
||||
getRecordingLogger,
|
||||
makeVersionInfo,
|
||||
setupTests,
|
||||
} from "./testing-utils";
|
||||
import {
|
||||
cleanupTrapCaches,
|
||||
downloadTrapCaches,
|
||||
getLanguagesSupportingCaching,
|
||||
uploadTrapCaches,
|
||||
@@ -189,3 +194,75 @@ test("download cache looks for the right key and creates dir", async (t) => {
|
||||
t.assert(fs.existsSync(path.resolve(tmpDir, "trapCaches", "javascript")));
|
||||
});
|
||||
});
|
||||
|
||||
test("cleanup removes only old CodeQL TRAP caches", async (t) => {
|
||||
await util.withTmpDir(async (tmpDir) => {
|
||||
const config = getTestConfigWithTempDir(tmpDir);
|
||||
|
||||
sinon.stub(actionsUtil, "getRef").resolves("refs/heads/main");
|
||||
sinon.stub(actionsUtil, "isAnalyzingDefaultBranch").resolves(true);
|
||||
const listStub = sinon.stub(apiClient, "listActionsCaches").resolves([
|
||||
{
|
||||
id: 1,
|
||||
key: "some-other-key",
|
||||
created_at: "2024-05-23T14:25:00Z",
|
||||
size_in_bytes: 100 * 1024 * 1024,
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
key: "codeql-trap-1-2.0.0-javascript-newest",
|
||||
created_at: "2024-04-23T14:25:00Z",
|
||||
size_in_bytes: 50 * 1024 * 1024,
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
key: "codeql-trap-1-2.0.0-javascript-older",
|
||||
created_at: "2024-03-22T14:25:00Z",
|
||||
size_in_bytes: 200 * 1024 * 1024,
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
key: "codeql-trap-1-2.0.0-javascript-oldest",
|
||||
created_at: "2024-02-21T14:25:00Z",
|
||||
size_in_bytes: 300 * 1024 * 1024,
|
||||
},
|
||||
{
|
||||
id: 5,
|
||||
key: "codeql-trap-1-2.0.0-ruby-newest",
|
||||
created_at: "2024-02-20T14:25:00Z",
|
||||
size_in_bytes: 300 * 1024 * 1024,
|
||||
},
|
||||
{
|
||||
id: 6,
|
||||
key: "codeql-trap-1-2.0.0-swift-newest",
|
||||
created_at: "2024-02-22T14:25:00Z",
|
||||
size_in_bytes: 300 * 1024 * 1024,
|
||||
},
|
||||
{
|
||||
id: 7,
|
||||
key: "codeql-trap-1-2.0.0-swift-older",
|
||||
created_at: "2024-02-21T14:25:00Z",
|
||||
size_in_bytes: 300 * 1024 * 1024,
|
||||
},
|
||||
]);
|
||||
|
||||
const deleteStub = sinon.stub(apiClient, "deleteActionsCache").resolves();
|
||||
|
||||
const statusReport = await cleanupTrapCaches(
|
||||
config,
|
||||
createFeatures([Feature.CleanupTrapCaches]),
|
||||
getRunnerLogger(true),
|
||||
);
|
||||
|
||||
t.is(listStub.callCount, 1);
|
||||
t.assert(listStub.calledWithExactly("codeql-trap", "refs/heads/main"));
|
||||
|
||||
t.deepEqual(statusReport, {
|
||||
trap_cache_cleanup_size_bytes: 500 * 1024 * 1024,
|
||||
});
|
||||
|
||||
t.is(deleteStub.callCount, 2);
|
||||
t.assert(deleteStub.calledWithExactly(3));
|
||||
t.assert(deleteStub.calledWithExactly(4));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -186,9 +186,19 @@ export async function cleanupTrapCaches(
|
||||
|
||||
try {
|
||||
let totalBytesCleanedUp = 0;
|
||||
|
||||
const allCaches = await apiClient.listActionsCaches(
|
||||
CODEQL_TRAP_CACHE_PREFIX,
|
||||
await actionsUtil.getRef(),
|
||||
);
|
||||
|
||||
for (const language of config.languages) {
|
||||
if (config.trapCaches[language]) {
|
||||
const cachesToRemove = await getTrapCachesForLanguage(language, logger);
|
||||
const cachesToRemove = await getTrapCachesForLanguage(
|
||||
allCaches,
|
||||
language,
|
||||
logger,
|
||||
);
|
||||
// Dates returned by the API are in ISO 8601 format, so we can sort them lexicographically
|
||||
cachesToRemove.sort((a, b) => a.created_at.localeCompare(b.created_at));
|
||||
// Keep the most recent cache
|
||||
@@ -227,14 +237,11 @@ export async function cleanupTrapCaches(
|
||||
}
|
||||
|
||||
async function getTrapCachesForLanguage(
|
||||
allCaches: apiClient.ActionsCacheItem[],
|
||||
language: Language,
|
||||
logger: Logger,
|
||||
): Promise<Array<Required<apiClient.ActionsCacheItem>>> {
|
||||
logger.debug(`Listing TRAP caches for ${language}`);
|
||||
const allCaches = await apiClient.listActionsCaches(
|
||||
CODEQL_TRAP_CACHE_PREFIX,
|
||||
await actionsUtil.getRef(),
|
||||
);
|
||||
|
||||
for (const cache of allCaches) {
|
||||
if (!cache.created_at || !cache.id || !cache.key || !cache.size_in_bytes) {
|
||||
|
||||
Reference in New Issue
Block a user