Skip to content

Commit

Permalink
Add a unit test for the cleanup module
Browse files Browse the repository at this point in the history
  • Loading branch information
Barry Gordon committed Feb 28, 2022
1 parent f738f6b commit 068fc70
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
15 changes: 15 additions & 0 deletions __tests__/cleanup.test.ts
Original file line number Diff line number Diff line change
@@ -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()
})
})
11 changes: 9 additions & 2 deletions src/cleanup.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import * as core from '@actions/core'
import Docker from 'dockerode'

export async function run(): Promise<void> {
// 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<void> {
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}`)
Expand Down

0 comments on commit 068fc70

Please sign in to comment.