From 068fc70b52642978dfb0f37578a608ca37e2fa9d Mon Sep 17 00:00:00 2001 From: Barry Gordon Date: Mon, 28 Feb 2022 12:15:55 +0000 Subject: [PATCH] Add a unit test for the cleanup module --- __tests__/cleanup.test.ts | 15 +++++++++++++++ src/cleanup.ts | 11 +++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 __tests__/cleanup.test.ts diff --git a/__tests__/cleanup.test.ts b/__tests__/cleanup.test.ts new file mode 100644 index 0000000..3c24256 --- /dev/null +++ b/__tests__/cleanup.test.ts @@ -0,0 +1,15 @@ +import * as core from '@actions/core' + +import {run} from '../src/cleanup' + +describe('run', () => { + beforeEach(async () => { + jest.spyOn(core, 'error').mockImplementation(jest.fn()) + }) + + test('it does not log any errors interacting with Docker by default', async () => { + await run() + + expect(core.error).not.toHaveBeenCalled() + }) +}) diff --git a/src/cleanup.ts b/src/cleanup.ts index f9ea0b8..cecb2ae 100644 --- a/src/cleanup.ts +++ b/src/cleanup.ts @@ -1,11 +1,18 @@ import * as core from '@actions/core' import Docker from 'dockerode' -export async function run(): Promise { +// This method performs housekeeping checks to remove Docker artifacts +// which were left behind by old versions of the action or any jobs +// which may have crashed before deleting their own containers or networks +// +// cutoff - a Go duration string to pass to the Docker API's 'until' argument, default '24h' +export async function run(cutoff = '24h'): Promise { try { const docker = new Docker() - const untilFilter = JSON.stringify({until: '24h'}) + const untilFilter = JSON.stringify({until: cutoff}) + core.info(`Pruning networks older than ${cutoff}`) await docker.pruneNetworks({filters: untilFilter}) + core.info(`Pruning containers older than ${cutoff}`) await docker.pruneContainers({filters: untilFilter}) } catch (error) { core.error(`Error cleaning up: ${error.message}`)